-
-
Notifications
You must be signed in to change notification settings - Fork 116
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
Showing
5 changed files
with
227 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { | ||
FastifyAdapter, | ||
NestFastifyApplication | ||
} from '@nestjs/platform-fastify' | ||
import { PrismaService } from '../prisma/prisma.service' | ||
import { Test } from '@nestjs/testing' | ||
import { AuthModule } from './auth.module' | ||
import { MAIL_SERVICE } from '../mail/services/interface.service' | ||
import { MockMailService } from '../mail/services/mock.service' | ||
import { AppModule } from '../app/app.module' | ||
import { Otp } from '@prisma/client' | ||
|
||
describe('Auth Controller Tests', () => { | ||
let app: NestFastifyApplication | ||
let prisma: PrismaService | ||
|
||
let otp: Otp | ||
|
||
beforeAll(async () => { | ||
const moduleRef = await Test.createTestingModule({ | ||
imports: [AppModule, AuthModule] | ||
}) | ||
.overrideProvider(MAIL_SERVICE) | ||
.useClass(MockMailService) | ||
.compile() | ||
|
||
app = moduleRef.createNestApplication<NestFastifyApplication>( | ||
new FastifyAdapter() | ||
) | ||
prisma = moduleRef.get(PrismaService) | ||
|
||
await app.init() | ||
await app.getHttpAdapter().getInstance().ready() | ||
}) | ||
|
||
it('should be defined', async () => { | ||
expect(app).toBeDefined() | ||
expect(prisma).toBeDefined() | ||
}) | ||
|
||
it('should not send otp if email is blank', async () => { | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/auth/send-otp/' | ||
}) | ||
|
||
expect(response.statusCode).toBe(400) | ||
expect(response.json().message).toBe('Please enter a valid email address') | ||
}) | ||
|
||
it('should not send otp if email is invalid', async () => { | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/auth/send-otp/abcdef' | ||
}) | ||
|
||
expect(response.statusCode).toBe(400) | ||
expect(response.json().message).toBe('Please enter a valid email address') | ||
}) | ||
|
||
it('should send otp if email is valid', async () => { | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/auth/send-otp/[email protected]' | ||
}) | ||
|
||
expect(response.statusCode).toBe(201) | ||
}) | ||
|
||
it('should have generated an otp', async () => { | ||
otp = await prisma.otp.findFirst({ | ||
where: { | ||
user: { | ||
email: '[email protected]' | ||
} | ||
} | ||
}) | ||
|
||
expect(otp).toBeDefined() | ||
expect(otp.code).toBeDefined() | ||
expect(otp.expiresAt).toBeDefined() | ||
expect(otp.code.length).toBe(6) | ||
}) | ||
|
||
it('should upsert otp if regenerated', async () => { | ||
await app.inject({ | ||
method: 'POST', | ||
url: '/auth/send-otp/[email protected]' | ||
}) | ||
|
||
const regenerated = await prisma.otp.findFirst({ | ||
where: { | ||
user: { | ||
email: '[email protected]' | ||
} | ||
} | ||
}) | ||
|
||
expect(regenerated).toBeDefined() | ||
expect(regenerated.code).toBeDefined() | ||
expect(regenerated.expiresAt).toBeDefined() | ||
expect(regenerated.code.length).toBe(6) | ||
expect(regenerated.code).not.toBe(otp.code) | ||
|
||
otp = regenerated | ||
}) | ||
|
||
it('should not be able to validate otp with invalid email', async () => { | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/auth/validate-otp?email=abcdef&otp=123456' | ||
}) | ||
|
||
expect(response.statusCode).toBe(404) | ||
}) | ||
|
||
it('should not be able to validate otp with invalid otp', async () => { | ||
const response = await app.inject({ | ||
method: 'POST', | ||
url: '/auth/[email protected]&otp=123456' | ||
}) | ||
|
||
expect(response.statusCode).toBe(401) | ||
}) | ||
}) |
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