|
| 1 | +import { $TSContext } from '@aws-amplify/amplify-cli-core'; |
| 2 | +import { createIdentityPoolService } from '../../aws-utils/IdentityPoolService'; |
| 3 | +import { loadConfiguration } from '../../configuration-manager'; |
| 4 | + |
| 5 | +let mockCognitoIdentityRoles = { |
| 6 | + authenticated: 'arn:aws:iam::123456789012:role/service-role/my-auth-role', |
| 7 | + unauthenticated: 'arn:aws:iam::123456789012:role/service-role/my-unauth-role', |
| 8 | +}; |
| 9 | + |
| 10 | +jest.mock('aws-sdk', () => { |
| 11 | + return { |
| 12 | + CognitoIdentity: jest.fn(() => { |
| 13 | + return { |
| 14 | + config: {}, |
| 15 | + getIdentityPoolRoles: jest.fn().mockImplementation(() => ({ |
| 16 | + promise: async () => { |
| 17 | + return { |
| 18 | + Roles: mockCognitoIdentityRoles, |
| 19 | + }; |
| 20 | + }, |
| 21 | + })), |
| 22 | + }; |
| 23 | + }), |
| 24 | + }; |
| 25 | +}); |
| 26 | + |
| 27 | +jest.mock('../../configuration-manager', () => { |
| 28 | + return { |
| 29 | + loadConfiguration: jest.fn().mockReturnValue({}) as jest.MockedFunction<typeof loadConfiguration>, |
| 30 | + }; |
| 31 | +}); |
| 32 | + |
| 33 | +describe('IdentityPoolService', () => { |
| 34 | + it('should correctly parse arn if it contains multiple forward slashes', async () => { |
| 35 | + const idpService = await createIdentityPoolService({} as unknown as $TSContext, {}); |
| 36 | + expect(await idpService.getIdentityPoolRoles('mockIdpId')).toEqual({ |
| 37 | + authRoleArn: 'arn:aws:iam::123456789012:role/service-role/my-auth-role', |
| 38 | + authRoleName: 'service-role/my-auth-role', |
| 39 | + unauthRoleArn: 'arn:aws:iam::123456789012:role/service-role/my-unauth-role', |
| 40 | + unauthRoleName: 'service-role/my-unauth-role', |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + it('should correctly parse arn if it contains a single forward slash', async () => { |
| 45 | + const idpService = await createIdentityPoolService({} as unknown as $TSContext, {}); |
| 46 | + mockCognitoIdentityRoles = { |
| 47 | + authenticated: 'arn:aws:iam::123456789012:role/my-auth-role', |
| 48 | + unauthenticated: 'arn:aws:iam::123456789012:role/my-unauth-role', |
| 49 | + }; |
| 50 | + |
| 51 | + expect(await idpService.getIdentityPoolRoles('mockIdpId')).toEqual({ |
| 52 | + authRoleArn: 'arn:aws:iam::123456789012:role/my-auth-role', |
| 53 | + authRoleName: 'my-auth-role', |
| 54 | + unauthRoleArn: 'arn:aws:iam::123456789012:role/my-unauth-role', |
| 55 | + unauthRoleName: 'my-unauth-role', |
| 56 | + }); |
| 57 | + }); |
| 58 | + |
| 59 | + it('should fail to parse arn if it contains no forward slash', async () => { |
| 60 | + const idpService = await createIdentityPoolService({} as unknown as $TSContext, {}); |
| 61 | + mockCognitoIdentityRoles = { |
| 62 | + authenticated: 'arn:aws:iam::123456789012:my-auth-role', |
| 63 | + unauthenticated: 'arn:aws:iam::123456789012:my-unauth-role', |
| 64 | + }; |
| 65 | + |
| 66 | + await expect(idpService.getIdentityPoolRoles('mockIdpId')).rejects.toBeDefined(); |
| 67 | + }); |
| 68 | +}); |
0 commit comments