|
1 | | -import { decodeJWT } from '../../../../src/singleton/Auth/utils'; |
| 1 | +import { |
| 2 | + assertIdentityPoolIdConfig, |
| 3 | + assertOAuthConfig, |
| 4 | + assertTokenProviderConfig, |
| 5 | + decodeJWT, |
| 6 | +} from '../../../../src/singleton/Auth/utils'; |
2 | 7 |
|
3 | 8 | const testSamples = [ |
4 | 9 | { |
@@ -35,4 +40,111 @@ describe('decodeJWT', () => { |
35 | 40 | expect(result.toString()).toEqual(token); |
36 | 41 | }, |
37 | 42 | ); |
| 43 | + |
| 44 | + it('throws error for invalid token format', () => { |
| 45 | + expect(() => decodeJWT('invalid')).toThrow('Invalid token'); |
| 46 | + }); |
| 47 | + |
| 48 | + it('throws error for malformed payload', () => { |
| 49 | + expect(() => decodeJWT('header.invalid-payload.signature')).toThrow( |
| 50 | + 'Invalid token payload', |
| 51 | + ); |
| 52 | + }); |
| 53 | +}); |
| 54 | + |
| 55 | +describe('assertTokenProviderConfig', () => { |
| 56 | + it('passes with valid user pool config', () => { |
| 57 | + expect(() => { |
| 58 | + assertTokenProviderConfig({ |
| 59 | + userPoolId: 'us-east-1_test', |
| 60 | + userPoolClientId: 'client123', |
| 61 | + }); |
| 62 | + }).not.toThrow(); |
| 63 | + }); |
| 64 | + |
| 65 | + it('throws when config is undefined', () => { |
| 66 | + expect(() => { |
| 67 | + assertTokenProviderConfig(undefined); |
| 68 | + }).toThrow(); |
| 69 | + }); |
| 70 | + |
| 71 | + it('throws when userPoolId is missing', () => { |
| 72 | + expect(() => { |
| 73 | + assertTokenProviderConfig({ |
| 74 | + userPoolClientId: 'client123', |
| 75 | + } as any); |
| 76 | + }).toThrow(); |
| 77 | + }); |
| 78 | + |
| 79 | + it('throws when userPoolClientId is missing', () => { |
| 80 | + expect(() => { |
| 81 | + assertTokenProviderConfig({ |
| 82 | + userPoolId: 'us-east-1_test', |
| 83 | + } as any); |
| 84 | + }).toThrow(); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +describe('assertOAuthConfig', () => { |
| 89 | + it('passes with valid oauth config', () => { |
| 90 | + expect(() => { |
| 91 | + assertOAuthConfig({ |
| 92 | + userPoolId: 'us-east-1_test', |
| 93 | + userPoolClientId: 'client123', |
| 94 | + loginWith: { |
| 95 | + oauth: { |
| 96 | + domain: 'example.auth.us-east-1.amazoncognito.com', |
| 97 | + redirectSignIn: ['http://localhost:3000/'], |
| 98 | + redirectSignOut: ['http://localhost:3000/'], |
| 99 | + responseType: 'code', |
| 100 | + scopes: ['openid'], |
| 101 | + }, |
| 102 | + }, |
| 103 | + }); |
| 104 | + }).not.toThrow(); |
| 105 | + }); |
| 106 | + |
| 107 | + it('throws when oauth config is missing', () => { |
| 108 | + expect(() => { |
| 109 | + assertOAuthConfig(undefined); |
| 110 | + }).toThrow(); |
| 111 | + }); |
| 112 | + |
| 113 | + it('throws when domain is missing', () => { |
| 114 | + expect(() => { |
| 115 | + assertOAuthConfig({ |
| 116 | + userPoolId: 'us-east-1_test', |
| 117 | + userPoolClientId: 'client123', |
| 118 | + loginWith: { |
| 119 | + oauth: { |
| 120 | + redirectSignIn: ['http://localhost:3000/'], |
| 121 | + redirectSignOut: ['http://localhost:3000/'], |
| 122 | + responseType: 'code', |
| 123 | + } as any, |
| 124 | + }, |
| 125 | + }); |
| 126 | + }).toThrow(); |
| 127 | + }); |
| 128 | +}); |
| 129 | + |
| 130 | +describe('assertIdentityPoolIdConfig', () => { |
| 131 | + it('passes with valid identity pool config', () => { |
| 132 | + expect(() => { |
| 133 | + assertIdentityPoolIdConfig({ |
| 134 | + identityPoolId: 'us-east-1:test-id', |
| 135 | + }); |
| 136 | + }).not.toThrow(); |
| 137 | + }); |
| 138 | + |
| 139 | + it('throws when identityPoolId is missing', () => { |
| 140 | + expect(() => { |
| 141 | + assertIdentityPoolIdConfig(undefined); |
| 142 | + }).toThrow(); |
| 143 | + }); |
| 144 | + |
| 145 | + it('throws when identityPoolId is empty', () => { |
| 146 | + expect(() => { |
| 147 | + assertIdentityPoolIdConfig({} as any); |
| 148 | + }).toThrow(); |
| 149 | + }); |
38 | 150 | }); |
0 commit comments