-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c63d1e5
commit 38a03cd
Showing
5 changed files
with
50 additions
and
31 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
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
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,32 @@ | ||
export class CustomPasswordError extends Error { | ||
constructor(message: string) { | ||
super(message); | ||
this.name = 'CustomPasswordError'; | ||
} | ||
} | ||
|
||
export async function checkPasswordPolicy(password: string): Promise<void> { | ||
if (password.length < 8) { | ||
throw new CustomPasswordError('Password must be at least 8 characters'); | ||
} | ||
|
||
if (password.length > 20) { | ||
throw new CustomPasswordError('Password cannot exceed 20 characters'); | ||
} | ||
|
||
if (!/[A-Z]/.test(password)) { | ||
throw new CustomPasswordError('Password must contain at least one uppercase letter'); | ||
} | ||
|
||
if (!/[a-z]/.test(password)) { | ||
throw new CustomPasswordError('Password must contain at least one lowercase letter'); | ||
} | ||
|
||
if (!/[!@#$%^&*(),.?":{}|<>]/.test(password)) { | ||
throw new CustomPasswordError('Password must contain at least one special character'); | ||
} | ||
|
||
if (!/[0-9]/.test(password)) { | ||
throw new CustomPasswordError('Password must contain at least one number'); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -10,7 +10,8 @@ jest.mock('../../../src/service/email/emailVerifiedToken'); | |
describe('passwordReset', () => { | ||
const mockStoredEmailVerifiedToken = 'stored-email-verified-token'; | ||
const mockEmail = '[email protected]'; | ||
const mockNewPassword = 'newPassword123'; | ||
const mockNewPassword = 'newPassword@!#123'; | ||
const mockInvalidPassword = 'newPassword'; | ||
|
||
const invokeHandler = async (event: Partial<APIGatewayProxyEvent>) => { | ||
const context = {} as Context; | ||
|
@@ -67,6 +68,16 @@ describe('passwordReset', () => { | |
expect(handlerUtil.errorResponse).toHaveBeenCalledWith('New password is required', 400); | ||
}); | ||
|
||
it('should call errorResponse when password is shorter than 8 characters', async () => { | ||
await invokeHandler({ | ||
queryStringParameters: { email: mockEmail, emailVerifiedToken: mockStoredEmailVerifiedToken }, | ||
body: JSON.stringify({ newPassword: mockInvalidPassword }), | ||
}); | ||
expect(handlerUtil.errorResponse).toHaveBeenCalledWith(expect.stringMatching(/Password/), 400); | ||
expect(resetUserPassword).not.toHaveBeenCalled(); | ||
expect(deleteEmailVerifiedToken).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should reset password successfully with valid inputs', async () => { | ||
await invokeHandler({ | ||
queryStringParameters: { email: mockEmail, emailVerifiedToken: mockStoredEmailVerifiedToken }, | ||
|