-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: single use code request endpoint (#3915)
* feat: single use code request endpoint * fix: updating mocks * fix: update per Em and Emily * fix: updates after convo with eric * fix: updates to tests
- Loading branch information
1 parent
ff479ab
commit e5bf474
Showing
15 changed files
with
494 additions
and
9 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
10 changes: 10 additions & 0 deletions
10
api/prisma/migrations/05_single_use_code_translation_updates/migration.sql
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,10 @@ | ||
-- Adds new single-use code translations for emails. | ||
|
||
UPDATE translations | ||
SET translations = jsonb_set(translations, '{singleUseCodeEmail}', '{"greeting": "Hi","message": "Use the following code to sign in to your %{jurisdictionName} account. This code will be valid for 5 minutes. Never share this code.","singleUseCode": "%{singleUseCode}"}') | ||
WHERE jurisdiction_id IS NULL | ||
and language = 'en'; | ||
|
||
UPDATE translations | ||
SET translations = jsonb_set(translations, '{mfaCodeEmail, mfaCode}', '{"mfaCode": "Your access code is: %{singleUseCode}"}') | ||
WHERE language = 'en'; |
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
13 changes: 13 additions & 0 deletions
13
api/src/dtos/single-use-code/request-single-use-code.dto.ts
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,13 @@ | ||
import { ApiProperty } from '@nestjs/swagger'; | ||
import { Expose } from 'class-transformer'; | ||
import { IsEmail } from 'class-validator'; | ||
import { EnforceLowerCase } from '../../decorators/enforce-lower-case.decorator'; | ||
import { ValidationsGroupsEnum } from '../../enums/shared/validation-groups-enum'; | ||
|
||
export class RequestSingleUseCode { | ||
@Expose() | ||
@IsEmail({}, { groups: [ValidationsGroupsEnum.default] }) | ||
@EnforceLowerCase() | ||
@ApiProperty() | ||
email: string; | ||
} |
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,9 @@ | ||
<p>{{t "singleUseCodeEmail.greeting"}} {{> user-name}},</p> | ||
<p> | ||
{{t "singleUseCodeEmail.message" singleUseCodeOptions }} | ||
</p> | ||
<br /> | ||
<p> | ||
{{t "singleUseCodeEmail.singleUseCode" singleUseCodeOptions}} | ||
</p> | ||
{{> simple-footer }} |
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ import { | |
TOKEN_COOKIE_NAME, | ||
} from '../../src/services/auth.service'; | ||
import { SmsService } from '../../src/services/sms.service'; | ||
import { EmailService } from '../../src/services/email.service'; | ||
import { RequestMfaCode } from '../../src/dtos/mfa/request-mfa-code.dto'; | ||
import { UpdatePassword } from '../../src/dtos/auth/update-password.dto'; | ||
import { Confirm } from '../../src/dtos/auth/confirm.dto'; | ||
|
@@ -22,6 +23,7 @@ describe('Auth Controller Tests', () => { | |
let app: INestApplication; | ||
let prisma: PrismaService; | ||
let smsService: SmsService; | ||
let emailService: EmailService; | ||
|
||
beforeAll(async () => { | ||
const moduleFixture: TestingModule = await Test.createTestingModule({ | ||
|
@@ -32,6 +34,7 @@ describe('Auth Controller Tests', () => { | |
app.use(cookieParser()); | ||
prisma = moduleFixture.get<PrismaService>(PrismaService); | ||
smsService = moduleFixture.get<SmsService>(SmsService); | ||
emailService = moduleFixture.get<EmailService>(EmailService); | ||
await app.init(); | ||
}); | ||
|
||
|
@@ -344,4 +347,111 @@ describe('Auth Controller Tests', () => { | |
.set('Cookie', resLogIn.headers['set-cookie']) | ||
.expect(200); | ||
}); | ||
|
||
it('should request single use code successfully', async () => { | ||
const storedUser = await prisma.userAccounts.create({ | ||
data: await userFactory({ | ||
roles: { isAdmin: true }, | ||
mfaEnabled: true, | ||
confirmedAt: new Date(), | ||
phoneNumber: '111-111-1111', | ||
phoneNumberVerified: true, | ||
}), | ||
}); | ||
|
||
const jurisdiction = await prisma.jurisdictions.create({ | ||
data: { | ||
name: 'single_use_code_1', | ||
allowSingleUseCodeLogin: true, | ||
rentalAssistanceDefault: 'test', | ||
}, | ||
}); | ||
emailService.sendSingleUseCode = jest.fn(); | ||
|
||
const res = await request(app.getHttpServer()) | ||
.post('/auth/request-single-use-code') | ||
.send({ | ||
email: storedUser.email, | ||
} as RequestMfaCode) | ||
.set({ jurisdictionname: jurisdiction.name }) | ||
.expect(201); | ||
|
||
expect(res.body).toEqual({ success: true }); | ||
|
||
expect(emailService.sendSingleUseCode).toHaveBeenCalled(); | ||
|
||
const user = await prisma.userAccounts.findUnique({ | ||
where: { | ||
id: storedUser.id, | ||
}, | ||
}); | ||
|
||
expect(user.singleUseCode).not.toBeNull(); | ||
expect(user.singleUseCodeUpdatedAt).not.toBeNull(); | ||
}); | ||
|
||
it('should request single use code, but jurisdiction does not allow', async () => { | ||
const storedUser = await prisma.userAccounts.create({ | ||
data: await userFactory({ | ||
roles: { isAdmin: true }, | ||
mfaEnabled: true, | ||
confirmedAt: new Date(), | ||
phoneNumber: '111-111-1111', | ||
phoneNumberVerified: true, | ||
}), | ||
}); | ||
|
||
const jurisdiction = await prisma.jurisdictions.create({ | ||
data: { | ||
name: 'single_use_code_2', | ||
allowSingleUseCodeLogin: false, | ||
rentalAssistanceDefault: 'test', | ||
}, | ||
}); | ||
emailService.sendSingleUseCode = jest.fn(); | ||
|
||
const res = await request(app.getHttpServer()) | ||
.post('/auth/request-single-use-code') | ||
.send({ | ||
email: storedUser.email, | ||
} as RequestMfaCode) | ||
.set({ jurisdictionname: jurisdiction.name }) | ||
.expect(400); | ||
console.log('420:', res.body); | ||
expect(res.body.message).toEqual( | ||
'Single use code login is not setup for this jurisdiction', | ||
); | ||
|
||
expect(emailService.sendSingleUseCode).not.toHaveBeenCalled(); | ||
|
||
const user = await prisma.userAccounts.findUnique({ | ||
where: { | ||
id: storedUser.id, | ||
}, | ||
}); | ||
|
||
expect(user.singleUseCode).toBeNull(); | ||
}); | ||
|
||
it('should request single use code, but user does not exist', async () => { | ||
const jurisdiction = await prisma.jurisdictions.create({ | ||
data: { | ||
name: 'single_use_code_3', | ||
allowSingleUseCodeLogin: true, | ||
rentalAssistanceDefault: 'test', | ||
}, | ||
}); | ||
emailService.sendSingleUseCode = jest.fn(); | ||
|
||
const res = await request(app.getHttpServer()) | ||
.post('/auth/request-single-use-code') | ||
.send({ | ||
email: '[email protected]', | ||
} as RequestMfaCode) | ||
.set({ jurisdictionname: jurisdiction.name }) | ||
.expect(201); | ||
expect(res.body.success).toEqual(true); | ||
|
||
expect(emailService.sendSingleUseCode).not.toHaveBeenCalled(); | ||
}); | ||
}); |
Oops, something went wrong.