|
1 | 1 | //@ts-nocheck |
2 | 2 | import { expect } from 'chai'; |
3 | 3 | import { assert, stub, createSandbox } from 'sinon'; |
4 | | -import { cliux } from '@contentstack/cli-utilities'; |
| 4 | +import cliux from '../../src/cli-ux'; |
5 | 5 | import authHandler from '../../src/auth-handler'; |
6 | 6 | import configHandler from '../../src/config-handler'; |
7 | 7 | import { HttpClient } from '../../src/http-client'; |
@@ -32,15 +32,18 @@ describe('Auth Handler', () => { |
32 | 32 | describe('oauth', () => { |
33 | 33 | let createHTTPServerStub; |
34 | 34 | let openOAuthURLStub; |
| 35 | + let initSDKStub; |
35 | 36 |
|
36 | 37 | beforeEach(() => { |
| 38 | + initSDKStub = stub(authHandler, 'initSDK').resolves(); |
37 | 39 | createHTTPServerStub = stub(authHandler, 'createHTTPServer'); |
38 | 40 | openOAuthURLStub = stub(authHandler, 'openOAuthURL'); |
39 | 41 | }); |
40 | 42 |
|
41 | 43 | afterEach(() => { |
42 | 44 | createHTTPServerStub.restore(); |
43 | 45 | openOAuthURLStub.restore(); |
| 46 | + initSDKStub.restore(); |
44 | 47 | }); |
45 | 48 |
|
46 | 49 | it('should reject with an error when createHTTPServer fails', async () => { |
@@ -167,16 +170,21 @@ describe('Auth Handler', () => { |
167 | 170 | }; |
168 | 171 |
|
169 | 172 | const exchangeStub = sandbox.stub().resolves(userData); |
170 | | - sandbox.stub(authHandler, 'oauthHandler').value({ |
| 173 | + const prevOAuthHandler = authHandler.oauthHandler; |
| 174 | + authHandler.oauthHandler = { |
171 | 175 | exchangeCodeForToken: exchangeStub, |
172 | | - }); |
| 176 | + }; |
173 | 177 | const getUserDetailsStub = sandbox.stub(authHandler, 'getUserDetails').resolves(userData); |
174 | 178 | const setConfigDataStub = sandbox.stub(authHandler, 'setConfigData').resolves(); |
175 | | - await authHandler.getAccessToken(code); |
176 | | - // Verify the actual calls made: |
177 | | - assert.calledWith(exchangeStub, code); // exchangeCodeForToken called with code |
178 | | - assert.calledWith(getUserDetailsStub, userData); // getUserDetails called with result from exchange |
179 | | - assert.calledWith(setConfigDataStub, 'oauth', userData); // setConfigData called with 'oauth' and userData |
| 179 | + try { |
| 180 | + await authHandler.getAccessToken(code); |
| 181 | + // Verify the actual calls made: |
| 182 | + assert.calledWith(exchangeStub, code); // exchangeCodeForToken called with code |
| 183 | + assert.calledWith(getUserDetailsStub, userData); // getUserDetails called with result from exchange |
| 184 | + assert.calledWith(setConfigDataStub, 'oauth', userData); // setConfigData called with 'oauth' and userData |
| 185 | + } finally { |
| 186 | + authHandler.oauthHandler = prevOAuthHandler; |
| 187 | + } |
180 | 188 | }); |
181 | 189 | }); |
182 | 190 |
|
@@ -296,51 +304,59 @@ describe('Auth Handler', () => { |
296 | 304 | }; |
297 | 305 | // Stub oauthHandler with refreshAccessToken method |
298 | 306 | const refreshAccessTokenStub = sandbox.stub().resolves(expectedData); |
299 | | - sandbox.stub(authHandler, 'oauthHandler').value({ |
| 307 | + const prevOAuthHandler = authHandler.oauthHandler; |
| 308 | + authHandler.oauthHandler = { |
300 | 309 | refreshAccessToken: refreshAccessTokenStub, |
301 | | - }); |
302 | | - // Stub configHandler.get to return proper values |
303 | | - sandbox |
304 | | - .stub(configHandler, 'get') |
305 | | - .withArgs(authHandler.oauthRefreshTokenKeyName) |
306 | | - .returns(configOauthRefreshToken) |
307 | | - .withArgs(authHandler.authorisationTypeKeyName) |
308 | | - .returns(configAuthorisationType); |
309 | | - // Stub setConfigData |
310 | | - sandbox.stub(authHandler, 'setConfigData').resolves(expectedData); |
311 | | - const result = await authHandler.refreshToken(); |
312 | | - // Verify calls |
313 | | - assert.calledWith(refreshAccessTokenStub, configOauthRefreshToken); |
314 | | - assert.calledWith(authHandler.setConfigData, 'refreshToken', expectedData); |
315 | | - expect(result).to.deep.equal(expectedData); |
| 310 | + }; |
| 311 | + try { |
| 312 | + // Stub configHandler.get to return proper values |
| 313 | + sandbox |
| 314 | + .stub(configHandler, 'get') |
| 315 | + .withArgs(authHandler.oauthRefreshTokenKeyName) |
| 316 | + .returns(configOauthRefreshToken) |
| 317 | + .withArgs(authHandler.authorisationTypeKeyName) |
| 318 | + .returns(configAuthorisationType); |
| 319 | + // Stub setConfigData |
| 320 | + sandbox.stub(authHandler, 'setConfigData').resolves(expectedData); |
| 321 | + const result = await authHandler.refreshToken(); |
| 322 | + // Verify calls |
| 323 | + assert.calledWith(refreshAccessTokenStub, configOauthRefreshToken); |
| 324 | + assert.calledWith(authHandler.setConfigData, 'refreshToken', expectedData); |
| 325 | + expect(result).to.deep.equal(expectedData); |
| 326 | + } finally { |
| 327 | + authHandler.oauthHandler = prevOAuthHandler; |
| 328 | + } |
316 | 329 | }); |
317 | 330 | }); |
318 | 331 |
|
319 | 332 | describe('getUserDetails', () => { |
320 | 333 | let sandbox; |
321 | | - let managementAPIClientStub; |
322 | 334 |
|
323 | 335 | beforeEach(() => { |
324 | 336 | sandbox = createSandbox(); |
325 | | - managementAPIClientStub = sandbox.stub(); |
326 | 337 | }); |
327 | 338 |
|
328 | 339 | afterEach(() => { |
329 | 340 | sandbox.restore(); |
| 341 | + authHandler.managementAPIClient = undefined; |
330 | 342 | }); |
331 | 343 |
|
332 | | - it('should reject with error when access token is invalid/empty', async () => { |
| 344 | + it('should reject when Management SDK getUser fails', async () => { |
333 | 345 | const data = { |
334 | 346 | access_token: config.invalid_access_token, |
335 | 347 | }; |
336 | 348 | const expectedError = new Error('The provided access token is invalid or expired or revoked'); |
337 | 349 |
|
338 | 350 | const getUserStub = sandbox.stub().rejects(expectedError); |
339 | | - managementAPIClientStub.returns({ getUser: getUserStub }); |
| 351 | + authHandler.managementAPIClient = { getUser: getUserStub }; |
340 | 352 |
|
341 | | - authHandler.contentstackManagementSDKClient = managementAPIClientStub; |
342 | | - |
343 | | - authHandler.getUserDetails(data); |
| 353 | + try { |
| 354 | + await authHandler.getUserDetails(data); |
| 355 | + expect.fail('Expected getUserDetails to reject'); |
| 356 | + } catch (error) { |
| 357 | + expect(error).to.equal(expectedError); |
| 358 | + } |
| 359 | + assert.calledOnce(getUserStub); |
344 | 360 | }); |
345 | 361 |
|
346 | 362 | it('should reject with error when access token is invalid/empty', async () => { |
|
0 commit comments