@gp_jcisneros/aws-utils
Version:
AWS SDK utilities for GreenPay microservices
579 lines (470 loc) • 17.6 kB
JavaScript
const { CognitoUtils } = require('../src/CognitoUtils');
const { AWSError, IntegrationError } = require('@gp_jcisneros/errors');
const {
CognitoIdentityProviderClient,
SignUpCommand,
InitiateAuthCommand,
ConfirmSignUpCommand,
ForgotPasswordCommand,
ConfirmForgotPasswordCommand,
CreateGroupCommand,
RevokeTokenCommand,
DeleteGroupCommand,
ChangePasswordCommand,
AdminAddUserToGroupCommand,
AdminRemoveUserFromGroupCommand,
AdminDeleteUserCommand,
} = require('@aws-sdk/client-cognito-identity-provider');
// Mock functions
const mockSend = jest.fn();
// Reset mocks before each test
beforeEach(() => {
jest.clearAllMocks();
// Setup default mock implementation
CognitoIdentityProviderClient.mockImplementation(() => ({
send: mockSend,
destroy: jest.fn().mockResolvedValue(undefined),
}));
// Mock all command constructors
SignUpCommand.mockImplementation(() => ({}));
InitiateAuthCommand.mockImplementation(() => ({}));
ConfirmSignUpCommand.mockImplementation(() => ({}));
ForgotPasswordCommand.mockImplementation(() => ({}));
ConfirmForgotPasswordCommand.mockImplementation(() => ({}));
CreateGroupCommand.mockImplementation(() => ({}));
RevokeTokenCommand.mockImplementation(() => ({}));
DeleteGroupCommand.mockImplementation(() => ({}));
ChangePasswordCommand.mockImplementation(() => ({}));
AdminAddUserToGroupCommand.mockImplementation(() => ({}));
AdminRemoveUserFromGroupCommand.mockImplementation(() => ({}));
AdminDeleteUserCommand.mockImplementation(() => ({}));
});
describe('CognitoUtils', () => {
const mockPoolData = {
ClientId: 'test-client-id',
UserPoolId: 'test-user-pool-id',
SecretHash: 'test-secret-hash',
};
describe('constructor', () => {
it('should create CognitoUtils instance', () => {
const cognitoUtils = new CognitoUtils();
expect(cognitoUtils).toBeInstanceOf(CognitoUtils);
expect(cognitoUtils.client).toBeDefined();
});
it('should create CognitoUtils instance with custom region', () => {
const cognitoUtils = new CognitoUtils('us-west-2');
expect(cognitoUtils).toBeInstanceOf(CognitoUtils);
});
});
describe('login', () => {
it('should login successfully', async () => {
const mockResponse = {
AuthenticationResult: {
AccessToken: 'test-access-token',
IdToken: 'test-id-token',
RefreshToken: 'test-refresh-token',
},
};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.login(
'testuser',
'password123',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle login error', async () => {
mockSend.mockRejectedValue(new Error('Login failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.login('testuser', 'wrongpassword', mockPoolData)
).rejects.toThrow(AWSError);
});
it('should re-throw custom errors', async () => {
const customError = new AWSError(
'Custom login error',
'COGNITO',
'LOGIN_ERROR'
);
mockSend.mockRejectedValue(customError);
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.login('testuser', 'password123', mockPoolData)
).rejects.toBe(customError);
});
});
describe('signUp', () => {
it('should sign up successfully', async () => {
const mockResponse = {
UserSub: 'test-user-sub',
CodeDeliveryDetails: {
Destination: 'test@example.com',
DeliveryMedium: 'EMAIL',
},
};
mockSend.mockResolvedValue(mockResponse);
const userAttributes = [{ Name: 'email', Value: 'test@example.com' }];
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.signUp(
'testuser',
'password123',
mockPoolData,
userAttributes
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle sign up error', async () => {
mockSend.mockRejectedValue(new Error('Sign up failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.signUp('testuser', 'password123', mockPoolData, [])
).rejects.toThrow(AWSError);
});
});
describe('verify', () => {
it('should verify user successfully', async () => {
const mockResponse = {};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.verify(
'testuser',
'123456',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle verification error', async () => {
mockSend.mockRejectedValue(new Error('Verification failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.verify('testuser', 'wrongcode', mockPoolData)
).rejects.toThrow(AWSError);
});
});
describe('forgotPassword', () => {
it('should initiate forgot password successfully', async () => {
const mockResponse = {
CodeDeliveryDetails: {
Destination: 'test@example.com',
DeliveryMedium: 'EMAIL',
},
};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.forgotPassword(
'testuser',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle forgot password error', async () => {
mockSend.mockRejectedValue(new Error('Forgot password failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.forgotPassword('testuser', mockPoolData)
).rejects.toThrow(AWSError);
});
});
describe('confirmPassword', () => {
it('should confirm password successfully', async () => {
const mockResponse = {};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.confirmPassword(
'testuser',
'123456',
'newpassword',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle confirm password error', async () => {
mockSend.mockRejectedValue(new Error('Confirm password failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.confirmPassword(
'testuser',
'wrongcode',
'newpassword',
mockPoolData
)
).rejects.toThrow(AWSError);
});
});
describe('refreshToken', () => {
it('should refresh token successfully', async () => {
const mockResponse = {
AuthenticationResult: {
AccessToken: 'new-access-token',
IdToken: 'new-id-token',
},
};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.refreshToken(
'testuser',
'refresh-token',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle refresh token error', async () => {
mockSend.mockRejectedValue(new Error('Refresh token failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.refreshToken('testuser', 'invalid-token', mockPoolData)
).rejects.toThrow(AWSError);
});
});
describe('revokeToken', () => {
it('should revoke token successfully', async () => {
const mockResponse = {};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.revokeToken('test-token', mockPoolData);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle revoke token error', async () => {
mockSend.mockRejectedValue(new Error('Revoke token failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.revokeToken('test-token', mockPoolData)
).rejects.toThrow(AWSError);
});
});
describe('changePassword', () => {
it('should change password successfully', async () => {
const mockResponse = {};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.changePassword(
'test-access-token',
'old-password',
'new-password'
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle change password error', async () => {
mockSend.mockRejectedValue(new Error('Change password failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.changePassword(
'test-access-token',
'old-password',
'new-password'
)
).rejects.toThrow(AWSError);
});
});
describe('adminAddUserToGroup', () => {
it('should add user to group successfully', async () => {
const mockResponse = {};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.adminAddUserToGroup(
'test-user',
'test-group',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle add user to group error', async () => {
mockSend.mockRejectedValue(new Error('Add user to group failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.adminAddUserToGroup(
'test-user',
'test-group',
mockPoolData
)
).rejects.toThrow(AWSError);
});
});
describe('adminRemoveUserFromGroup', () => {
it('should remove user from group successfully', async () => {
const mockResponse = {};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.adminRemoveUserFromGroup(
'test-user',
'test-group',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle remove user from group error', async () => {
mockSend.mockRejectedValue(new Error('Remove user from group failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.adminRemoveUserFromGroup(
'test-user',
'test-group',
mockPoolData
)
).rejects.toThrow(AWSError);
});
});
describe('addGroupPool', () => {
it('should add group to pool successfully', async () => {
const mockResponse = {
Group: {
GroupName: 'test-group',
UserPoolId: 'test-user-pool-id',
Description: 'Test group',
},
};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.addGroupPool(
'test-group',
'Test group',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle add group error', async () => {
mockSend.mockRejectedValue(new Error('Add group failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.addGroupPool('test-group', 'Test group', mockPoolData)
).rejects.toThrow(AWSError);
});
});
describe('deleteGroupPool', () => {
it('should delete group from pool successfully', async () => {
const mockResponse = {};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.deleteGroupPool(
'test-group',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle delete group error', async () => {
mockSend.mockRejectedValue(new Error('Delete group failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.deleteGroupPool('test-group', mockPoolData)
).rejects.toThrow(AWSError);
});
});
describe('deleteUserPool', () => {
it('should delete user from pool successfully', async () => {
const mockResponse = {};
mockSend.mockResolvedValue(mockResponse);
const cognitoUtils = new CognitoUtils();
const result = await cognitoUtils.deleteUserPool(
'test-user',
mockPoolData
);
expect(result).toEqual(mockResponse);
expect(mockSend).toHaveBeenCalledTimes(1);
});
it('should handle delete user error', async () => {
mockSend.mockRejectedValue(new Error('Delete user failed'));
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.deleteUserPool('test-user', mockPoolData)
).rejects.toThrow(AWSError);
});
});
describe('static methods', () => {
it('should have static login method', () => {
expect(typeof CognitoUtils.login).toBe('function');
});
it('should have static signUp method', () => {
expect(typeof CognitoUtils.signUp).toBe('function');
});
it('should have static verify method', () => {
expect(typeof CognitoUtils.verify).toBe('function');
});
it('should have static forgotPassword method', () => {
expect(typeof CognitoUtils.forgotPassword).toBe('function');
});
it('should have static confirmPassword method', () => {
expect(typeof CognitoUtils.confirmPassword).toBe('function');
});
it('should have static refreshToken method', () => {
expect(typeof CognitoUtils.refreshToken).toBe('function');
});
it('should have static revokeToken method', () => {
expect(typeof CognitoUtils.revokeToken).toBe('function');
});
it('should have static changePassword method', () => {
expect(typeof CognitoUtils.changePassword).toBe('function');
});
it('should have static adminAddUserToGroup method', () => {
expect(typeof CognitoUtils.adminAddUserToGroup).toBe('function');
});
it('should have static adminRemoveUserFromGroup method', () => {
expect(typeof CognitoUtils.adminRemoveUserFromGroup).toBe('function');
});
it('should have static addGroupPool method', () => {
expect(typeof CognitoUtils.addGroupPool).toBe('function');
});
it('should have static deleteGroupPool method', () => {
expect(typeof CognitoUtils.deleteGroupPool).toBe('function');
});
it('should have static deleteUserPool method', () => {
expect(typeof CognitoUtils.deleteUserPool).toBe('function');
});
it('should call static login method successfully', async () => {
const mockResponse = { AccessToken: 'token' };
mockSend.mockResolvedValue(mockResponse);
const result = await CognitoUtils.login(
'testuser',
'password123',
mockPoolData
);
expect(result).toEqual(mockResponse);
});
});
describe('error handling', () => {
it('should create AWSError with correct properties for COGNITO service', () => {
const error = new AWSError('Test error', 'COGNITO', 'LOGIN_ERROR');
expect(error).toBeInstanceOf(AWSError);
expect(error.message).toBe('Test error');
expect(error.service).toBe('COGNITO');
expect(error.awsErrorCode).toBe('LOGIN_ERROR');
});
it('should create IntegrationError with correct properties', () => {
const error = new IntegrationError(
'Test error',
'COGNITO_AUTH_ERROR',
'cognito',
{ username: 'testuser' }
);
expect(error).toBeInstanceOf(IntegrationError);
expect(error.message).toBe('Test error');
expect(error.integrationCode).toBe('COGNITO_AUTH_ERROR');
expect(error.integrationName).toBe('cognito');
expect(error.context).toEqual({ username: 'testuser' });
});
it('should re-throw IntegrationError from login', async () => {
const customError = new IntegrationError(
'Custom error',
'AUTH_ERROR',
'cognito'
);
mockSend.mockRejectedValue(customError);
const cognitoUtils = new CognitoUtils();
await expect(
cognitoUtils.login('testuser', 'password123', mockPoolData)
).rejects.toBe(customError);
});
});
});