|
| 1 | +jest.mock('./strategyCrypto', () => { |
| 2 | + return { |
| 3 | + createVerifier: jest.fn(() => 'verifier'), |
| 4 | + base64URLEncode: jest.fn(() => 'base64URLEncode'), |
| 5 | + sha256: jest.fn(() => 'sha256'), |
| 6 | + basicAuth: jest.fn(() => 'basicAuth'), |
| 7 | + }; |
| 8 | +}); |
| 9 | + |
| 10 | +import { OAuthProvider } from './authInfo'; |
| 11 | +import { strategyForProvider } from './strategy'; |
| 12 | + |
| 13 | +// it.each is not found without this: |
| 14 | +import { it } from '@jest/globals'; |
| 15 | + |
| 16 | +const expectedData = { |
| 17 | + bbcloud: { |
| 18 | + provider: 'bbcloud', |
| 19 | + authorizeUrl: |
| 20 | + 'https://bitbucket.org/site/oauth2/authorize?client_id=3hasX42a7Ugka2FJja&response_type=code&state=state', |
| 21 | + accessibleResourcesUrl: '', |
| 22 | + tokenAuthorizationData: 'grant_type=authorization_code&code=code', |
| 23 | + tokenUrl: 'https://bitbucket.org/site/oauth2/access_token', |
| 24 | + apiUrl: 'https://bitbucket.org', |
| 25 | + refreshHeaders: { |
| 26 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 27 | + Authorization: 'basicAuth', |
| 28 | + }, |
| 29 | + tokenRefreshData: 'grant_type=refresh_token&refresh_token=refreshToken', |
| 30 | + profileUrl: 'https://api.bitbucket.org/2.0/user', |
| 31 | + emailsUrl: 'https://api.bitbucket.org/2.0/user/emails', |
| 32 | + }, |
| 33 | + bbcloudstaging: { |
| 34 | + provider: 'bbcloudstaging', |
| 35 | + authorizeUrl: |
| 36 | + 'https://staging.bb-inf.net/site/oauth2/authorize?client_id=7jspxC7fgemuUbnWQL&response_type=code&state=state', |
| 37 | + accessibleResourcesUrl: '', |
| 38 | + tokenAuthorizationData: 'grant_type=authorization_code&code=code', |
| 39 | + tokenUrl: 'https://staging.bb-inf.net/site/oauth2/access_token', |
| 40 | + apiUrl: 'https://staging.bb-inf.net', |
| 41 | + refreshHeaders: { |
| 42 | + 'Content-Type': 'application/x-www-form-urlencoded', |
| 43 | + Authorization: 'basicAuth', |
| 44 | + }, |
| 45 | + tokenRefreshData: 'grant_type=refresh_token&refresh_token=refreshToken', |
| 46 | + profileUrl: 'https://api-staging.bb-inf.net/2.0/user', |
| 47 | + emailsUrl: 'https://api-staging.bb-inf.net/2.0/user/emails', |
| 48 | + }, |
| 49 | + jiracloud: { |
| 50 | + provider: 'jiracloud', |
| 51 | + authorizeUrl: |
| 52 | + 'https://auth.atlassian.com/authorize?client_id=bJChVgBQd0aNUPuFZ8YzYBVZz3X4QTe2&redirect_uri=http%3A%2F%2F127.0.0.1%3A31415%2Fjiracloud&response_type=code&scope=read%3Ajira-user+read%3Ajira-work+write%3Ajira-work+offline_access+manage%3Ajira-project&audience=api.atlassian.com&prompt=consent&state=state&code_challenge=base64URLEncode&code_challenge_method=S256', |
| 53 | + accessibleResourcesUrl: 'https://api.atlassian.com/oauth/token/accessible-resources', |
| 54 | + tokenAuthorizationData: |
| 55 | + '{"grant_type":"authorization_code","code":"code","redirect_uri":"http://127.0.0.1:31415/jiracloud","client_id":"bJChVgBQd0aNUPuFZ8YzYBVZz3X4QTe2","code_verifier":"verifier"}', |
| 56 | + tokenUrl: 'https://auth.atlassian.com/oauth/token', |
| 57 | + apiUrl: 'api.atlassian.com', |
| 58 | + refreshHeaders: { |
| 59 | + 'Content-Type': 'application/json', |
| 60 | + }, |
| 61 | + tokenRefreshData: |
| 62 | + '{"grant_type":"refresh_token","client_id":"bJChVgBQd0aNUPuFZ8YzYBVZz3X4QTe2","refresh_token":"refreshToken"}', |
| 63 | + profileUrl: '', |
| 64 | + emailsUrl: '', |
| 65 | + }, |
| 66 | + jiracloudstaging: { |
| 67 | + provider: 'jiracloudstaging', |
| 68 | + authorizeUrl: |
| 69 | + 'https://auth.stg.atlassian.com/authorize?client_id=pmzXmUav3Rr5XEL0Sie7Biec0WGU8BKg&redirect_uri=http%3A%2F%2F127.0.0.1%3A31415%2Fjiracloudstaging&response_type=code&scope=read%3Ajira-user+read%3Ajira-work+write%3Ajira-work+offline_access+manage%3Ajira-project&audience=api.stg.atlassian.com&prompt=consent&state=state&code_challenge=base64URLEncode&code_challenge_method=S256', |
| 70 | + accessibleResourcesUrl: 'https://api.stg.atlassian.com/oauth/token/accessible-resources', |
| 71 | + tokenAuthorizationData: |
| 72 | + '{"grant_type":"authorization_code","code":"code","redirect_uri":"http://127.0.0.1:31415/jiracloudstaging","client_id":"pmzXmUav3Rr5XEL0Sie7Biec0WGU8BKg","code_verifier":"verifier"}', |
| 73 | + tokenUrl: 'https://auth.stg.atlassian.com/oauth/token', |
| 74 | + apiUrl: 'api.stg.atlassian.com', |
| 75 | + refreshHeaders: { |
| 76 | + 'Content-Type': 'application/json', |
| 77 | + }, |
| 78 | + tokenRefreshData: |
| 79 | + '{"grant_type":"refresh_token","client_id":"pmzXmUav3Rr5XEL0Sie7Biec0WGU8BKg","refresh_token":"refreshToken"}', |
| 80 | + profileUrl: '', |
| 81 | + emailsUrl: '', |
| 82 | + }, |
| 83 | +}; |
| 84 | + |
| 85 | +describe('Authentication strategies', () => { |
| 86 | + it.each([ |
| 87 | + [OAuthProvider.BitbucketCloud], |
| 88 | + [OAuthProvider.BitbucketCloudStaging], |
| 89 | + [OAuthProvider.JiraCloud], |
| 90 | + [OAuthProvider.JiraCloudStaging], |
| 91 | + ])('Strategy for provider %s yields expected results', (provider: OAuthProvider) => { |
| 92 | + const expected = expectedData[provider] as any; |
| 93 | + const strategy = strategyForProvider(provider); |
| 94 | + expect(strategy.provider()).toBe(expected.provider); |
| 95 | + expect(strategy.authorizeUrl('state')).toBe(expected.authorizeUrl); |
| 96 | + expect(strategy.accessibleResourcesUrl()).toBe(expected.accessibleResourcesUrl); |
| 97 | + expect(strategy.tokenAuthorizationData('code')).toBe(expected.tokenAuthorizationData); |
| 98 | + expect(strategy.tokenUrl()).toBe(expected.tokenUrl); |
| 99 | + expect(strategy.apiUrl()).toBe(expected.apiUrl); |
| 100 | + expect(strategy.refreshHeaders()).toStrictEqual(expected.refreshHeaders); |
| 101 | + expect(strategy.tokenRefreshData('refreshToken')).toBe(expected.tokenRefreshData); |
| 102 | + expect(strategy.profileUrl()).toBe(expected.profileUrl); |
| 103 | + expect(strategy.emailsUrl()).toBe(expected.emailsUrl); |
| 104 | + }); |
| 105 | +}); |
0 commit comments