forked from ustaxcourt/ef-cms
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
10007: Extract sign up to user gateway function
- Loading branch information
1 parent
c895bc4
commit 19ef516
Showing
5 changed files
with
126 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,15 +6,15 @@ import { signUpUserInteractor } from './signUpUserInteractor'; | |
describe('signUpUserInteractor', () => { | ||
const email = '[email protected]'; | ||
const name = 'Antoninus Sara'; | ||
const userId = 'c3f56e3d-0e6e-44bb-98f1-7c4a91dca1b9'; | ||
const mockUserId = 'c3f56e3d-0e6e-44bb-98f1-7c4a91dca1b9'; | ||
const password = 'Pa$$w0rd!'; | ||
const mockConfirmationCode = '09d0322d-12da-47c8-8d8b-cc76f97022c2'; | ||
const user = { confirmPassword: password, email, name, password }; | ||
|
||
beforeEach(() => { | ||
applicationContext.getUniqueId.mockReturnValue(userId); | ||
|
||
applicationContext.getCognito().signUp.mockResolvedValue({}); | ||
applicationContext | ||
.getUserGateway() | ||
.signUp.mockResolvedValue({ userId: mockUserId }); | ||
|
||
applicationContext | ||
.getUseCaseHelpers() | ||
|
@@ -32,34 +32,19 @@ describe('signUpUserInteractor', () => { | |
user, | ||
}); | ||
|
||
expect( | ||
applicationContext.getCognito().signUp.mock.calls[0][0], | ||
).toMatchObject({ | ||
Password: password, | ||
UserAttributes: [ | ||
{ | ||
Name: 'email', | ||
Value: email, | ||
}, | ||
{ | ||
Name: 'name', | ||
Value: name, | ||
}, | ||
{ | ||
Name: 'custom:userId', | ||
Value: userId, | ||
}, | ||
{ | ||
Name: 'custom:role', | ||
Value: ROLES.petitioner, | ||
}, | ||
], | ||
Username: email, | ||
}); | ||
expect(applicationContext.getUserGateway().signUp).toHaveBeenCalledWith( | ||
applicationContext, | ||
{ | ||
email, | ||
name, | ||
password, | ||
role: ROLES.petitioner, | ||
}, | ||
); | ||
expect(result).toEqual({ | ||
confirmationCode: mockConfirmationCode, | ||
email: user.email, | ||
userId, | ||
userId: mockUserId, | ||
}); | ||
}); | ||
|
||
|
@@ -76,23 +61,25 @@ describe('signUpUserInteractor', () => { | |
expect(result).toEqual({ | ||
confirmationCode: undefined, | ||
email: user.email, | ||
userId, | ||
userId: mockUserId, | ||
}); | ||
}); | ||
|
||
it('should throw an error when an error occurs while trying to create a new user account', async () => { | ||
applicationContext | ||
.getCognito() | ||
.listUsers.mockResolvedValue({ Users: undefined }); | ||
applicationContext.getCognito().signUp.mockRejectedValue(new Error('abc')); | ||
applicationContext | ||
.getUserGateway() | ||
.signUp.mockRejectedValue(new Error('abc')); | ||
|
||
await expect( | ||
signUpUserInteractor(applicationContext, { | ||
user, | ||
}), | ||
).rejects.toThrow(); | ||
|
||
expect(applicationContext.getCognito().signUp).toHaveBeenCalled(); | ||
expect(applicationContext.getUserGateway().signUp).toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw an error when the new user is not valid', async () => { | ||
|
@@ -112,16 +99,14 @@ describe('signUpUserInteractor', () => { | |
).rejects.toThrow( | ||
'The NewPetitionerUser entity was invalid. {"password":"Must contain number","confirmPassword":"Passwords must match"}', | ||
); | ||
|
||
expect(applicationContext.getCognito().signUp).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw an error when the provided email already exists for an account in the system and it has been confirmed', async () => { | ||
applicationContext.getCognito().listUsers.mockResolvedValue({ | ||
Users: [ | ||
{ | ||
UserStatus: UserStatusType.CONFIRMED, | ||
userId, | ||
userId: mockUserId, | ||
}, | ||
], | ||
}); | ||
|
@@ -132,15 +117,15 @@ describe('signUpUserInteractor', () => { | |
}), | ||
).rejects.toThrow('User already exists'); | ||
|
||
expect(applicationContext.getCognito().signUp).not.toHaveBeenCalled(); | ||
expect(applicationContext.getUserGateway().signUp).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should throw an error when the provided email already exists for an account in the system and the account has not yet been confirmed', async () => { | ||
applicationContext.getCognito().listUsers.mockResolvedValue({ | ||
Users: [ | ||
{ | ||
UserStatus: UserStatusType.UNCONFIRMED, | ||
userId, | ||
userId: mockUserId, | ||
}, | ||
], | ||
}); | ||
|
@@ -151,6 +136,6 @@ describe('signUpUserInteractor', () => { | |
}), | ||
).rejects.toThrow('User exists, email unconfirmed'); | ||
|
||
expect(applicationContext.getCognito().signUp).not.toHaveBeenCalled(); | ||
expect(applicationContext.getUserGateway().signUp).not.toHaveBeenCalled(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { ROLES } from '@shared/business/entities/EntityConstants'; | ||
import { applicationContext } from '../../../../shared/src/business/test/createTestApplicationContext'; | ||
import { signUp } from '@web-api/gateways/user/signUp'; | ||
|
||
describe('signUp', () => { | ||
it('should make a call to disable the user with the provided email', async () => { | ||
const mockEmail = '[email protected]'; | ||
const mockName = 'Test Petitioner'; | ||
const mockPassword = 'P@ssword!'; | ||
const mockRole = ROLES.petitioner; | ||
const mockUserId = '2a1aa887-6350-48dc-bb3b-9fe699eae776'; | ||
const mockCognitoClientId = 'test'; | ||
applicationContext.environment.cognitoClientId = mockCognitoClientId; | ||
applicationContext.getUniqueId.mockReturnValue(mockUserId); | ||
|
||
await signUp(applicationContext, { | ||
email: mockEmail, | ||
name: mockName, | ||
password: mockPassword, | ||
role: mockRole, | ||
}); | ||
|
||
expect(applicationContext.getCognito().signUp).toHaveBeenCalledWith({ | ||
ClientId: mockCognitoClientId, | ||
Password: mockPassword, | ||
UserAttributes: [ | ||
{ | ||
Name: 'email', | ||
Value: mockEmail, | ||
}, | ||
{ | ||
Name: 'name', | ||
Value: mockName, | ||
}, | ||
{ | ||
Name: 'custom:userId', | ||
Value: mockUserId, | ||
}, | ||
{ | ||
Name: 'custom:role', | ||
Value: mockRole, | ||
}, | ||
], | ||
Username: mockEmail, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { Role } from '@shared/business/entities/EntityConstants'; | ||
import { ServerApplicationContext } from '@web-api/applicationContext'; | ||
|
||
export async function signUp( | ||
applicationContext: ServerApplicationContext, | ||
{ | ||
email, | ||
name, | ||
password, | ||
role, | ||
}: { | ||
password: string; | ||
email: string; | ||
name: string; | ||
role: Role; | ||
}, | ||
): Promise<{ userId: string }> { | ||
const userId = applicationContext.getUniqueId(); | ||
|
||
await applicationContext.getCognito().signUp({ | ||
ClientId: applicationContext.environment.cognitoClientId, | ||
Password: password, | ||
UserAttributes: [ | ||
{ | ||
Name: 'email', | ||
Value: email, | ||
}, | ||
{ | ||
Name: 'name', | ||
Value: name, | ||
}, | ||
{ | ||
Name: 'custom:userId', | ||
Value: userId, | ||
}, | ||
{ | ||
Name: 'custom:role', | ||
Value: role, | ||
}, | ||
], | ||
Username: email, | ||
}); | ||
|
||
return { userId }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters