chore: added nestjs version into auth module

This commit is contained in:
Balu Babu
2023-02-01 19:19:39 +05:30
parent b5e7877912
commit a1be3a3e77
3 changed files with 21 additions and 17 deletions

View File

@@ -21,7 +21,7 @@ import { RTCookie } from 'src/decorators/rt-cookie.decorator';
import { AuthGuard } from '@nestjs/passport'; import { AuthGuard } from '@nestjs/passport';
import { authCookieHandler, throwHTTPErr } from './helper'; import { authCookieHandler, throwHTTPErr } from './helper';
@Controller('/v1/auth') @Controller({ path: 'auth', version: '1' })
export class AuthController { export class AuthController {
constructor(private authService: AuthService) {} constructor(private authService: AuthService) {}

View File

@@ -75,7 +75,7 @@ const encodedRefreshToken =
'$argon2id$v=19$m=65536,t=3,p=4$JTP8yZ8YXMHdafb5pB9Rfg$tdZrILUxMb9dQbu0uuyeReLgKxsgYnyUNbc5ZxQmy5I'; '$argon2id$v=19$m=65536,t=3,p=4$JTP8yZ8YXMHdafb5pB9Rfg$tdZrILUxMb9dQbu0uuyeReLgKxsgYnyUNbc5ZxQmy5I';
describe('signInMagicLink', () => { describe('signInMagicLink', () => {
test('should throw error if email is not in valid format', async () => { test('Should throw error if email is not in valid format', async () => {
const result = await authService.signInMagicLink('bbbgmail.com'); const result = await authService.signInMagicLink('bbbgmail.com');
expect(result).toEqualLeft({ expect(result).toEqualLeft({
message: INVALID_EMAIL, message: INVALID_EMAIL,
@@ -83,7 +83,7 @@ describe('signInMagicLink', () => {
}); });
}); });
test('should successfully create a new user account and return the passwordless details', async () => { test('Should successfully create a new user account and return the passwordless details', async () => {
// check to see if user exists, return none // check to see if user exists, return none
mockUser.findUserByEmail.mockResolvedValue(O.none); mockUser.findUserByEmail.mockResolvedValue(O.none);
// create new user // create new user
@@ -99,7 +99,7 @@ describe('signInMagicLink', () => {
}); });
}); });
test('should successfully return the passwordless details for a pre-existing user account', async () => { test('Should successfully return the passwordless details for a pre-existing user account', async () => {
// check to see if user exists, return error // check to see if user exists, return error
mockUser.findUserByEmail.mockResolvedValueOnce(O.some(user)); mockUser.findUserByEmail.mockResolvedValueOnce(O.some(user));
// create new entry in VerificationToken table // create new entry in VerificationToken table
@@ -115,7 +115,7 @@ describe('signInMagicLink', () => {
}); });
describe('verifyMagicLinkTokens', () => { describe('verifyMagicLinkTokens', () => {
test('should throw INVALID_MAGIC_LINK_DATA if data is invalid', async () => { test('Should throw INVALID_MAGIC_LINK_DATA if data is invalid', async () => {
mockPrisma.verificationToken.findUniqueOrThrow.mockRejectedValueOnce( mockPrisma.verificationToken.findUniqueOrThrow.mockRejectedValueOnce(
'NotFoundError', 'NotFoundError',
); );
@@ -127,7 +127,7 @@ describe('verifyMagicLinkTokens', () => {
}); });
}); });
test('should throw USER_NOT_FOUND if user is invalid', async () => { test('Should throw USER_NOT_FOUND if user is invalid', async () => {
// validatePasswordlessTokens // validatePasswordlessTokens
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce( mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce(
passwordlessData, passwordlessData,
@@ -142,7 +142,7 @@ describe('verifyMagicLinkTokens', () => {
}); });
}); });
test('should successfully return auth token pair with provider account existing', async () => { test('Should successfully return auth token pair with provider account existing', async () => {
// validatePasswordlessTokens // validatePasswordlessTokens
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({ mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({
...passwordlessData, ...passwordlessData,
@@ -167,7 +167,7 @@ describe('verifyMagicLinkTokens', () => {
}); });
}); });
test('should successfully return auth token pair with provider account not existing', async () => { test('Should successfully return auth token pair with provider account not existing', async () => {
// validatePasswordlessTokens // validatePasswordlessTokens
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({ mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({
...passwordlessData, ...passwordlessData,
@@ -192,7 +192,7 @@ describe('verifyMagicLinkTokens', () => {
}); });
}); });
test('should throw MAGIC_LINK_EXPIRED if passwordless token is expired', async () => { test('Should throw MAGIC_LINK_EXPIRED if passwordless token is expired', async () => {
// validatePasswordlessTokens // validatePasswordlessTokens
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce( mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce(
passwordlessData, passwordlessData,
@@ -209,7 +209,7 @@ describe('verifyMagicLinkTokens', () => {
}); });
}); });
test('should throw USER_NOT_FOUND when updating refresh tokens fails', async () => { test('Should throw USER_NOT_FOUND when updating refresh tokens fails', async () => {
// validatePasswordlessTokens // validatePasswordlessTokens
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({ mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({
...passwordlessData, ...passwordlessData,
@@ -234,7 +234,7 @@ describe('verifyMagicLinkTokens', () => {
}); });
}); });
test('should throw PASSWORDLESS_DATA_NOT_FOUND when deleting passwordlessVerification entry from DB', async () => { test('Should throw PASSWORDLESS_DATA_NOT_FOUND when deleting passwordlessVerification entry from DB', async () => {
// validatePasswordlessTokens // validatePasswordlessTokens
mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({ mockPrisma.verificationToken.findUniqueOrThrow.mockResolvedValueOnce({
...passwordlessData, ...passwordlessData,
@@ -261,7 +261,7 @@ describe('verifyMagicLinkTokens', () => {
}); });
describe('generateAuthTokens', () => { describe('generateAuthTokens', () => {
test('should successfully generate tokens with valid inputs', async () => { test('Should successfully generate tokens with valid inputs', async () => {
mockJWT.sign.mockReturnValue(user.refreshToken); mockJWT.sign.mockReturnValue(user.refreshToken);
// UpdateUserRefreshToken // UpdateUserRefreshToken
mockUser.UpdateUserRefreshToken.mockResolvedValueOnce(E.right(user)); mockUser.UpdateUserRefreshToken.mockResolvedValueOnce(E.right(user));
@@ -273,7 +273,7 @@ describe('generateAuthTokens', () => {
}); });
}); });
test('should throw USER_NOT_FOUND when updating refresh tokens fails', async () => { test('Should throw USER_NOT_FOUND when updating refresh tokens fails', async () => {
mockJWT.sign.mockReturnValue(user.refreshToken); mockJWT.sign.mockReturnValue(user.refreshToken);
// UpdateUserRefreshToken // UpdateUserRefreshToken
mockUser.UpdateUserRefreshToken.mockResolvedValueOnce( mockUser.UpdateUserRefreshToken.mockResolvedValueOnce(
@@ -299,7 +299,7 @@ jest.mock('argon2', () => {
}); });
describe('refreshAuthTokens', () => { describe('refreshAuthTokens', () => {
test('should throw USER_NOT_FOUND when updating refresh tokens fails', async () => { test('Should throw USER_NOT_FOUND when updating refresh tokens fails', async () => {
// generateAuthTokens // generateAuthTokens
mockJWT.sign.mockReturnValue(user.refreshToken); mockJWT.sign.mockReturnValue(user.refreshToken);
// UpdateUserRefreshToken // UpdateUserRefreshToken
@@ -317,7 +317,7 @@ describe('refreshAuthTokens', () => {
}); });
}); });
test('should throw USER_NOT_FOUND when user is invalid', async () => { test('Should throw USER_NOT_FOUND when user is invalid', async () => {
const result = await authService.refreshAuthTokens( const result = await authService.refreshAuthTokens(
'jshdcbjsdhcbshdbc', 'jshdcbjsdhcbshdbc',
null, null,
@@ -328,7 +328,7 @@ describe('refreshAuthTokens', () => {
}); });
}); });
test('should successfully refresh the tokens and generate a new auth token pair', async () => { test('Should successfully refresh the tokens and generate a new auth token pair', async () => {
// generateAuthTokens // generateAuthTokens
mockJWT.sign.mockReturnValue('sdhjcbjsdhcbshjdcb'); mockJWT.sign.mockReturnValue('sdhjcbjsdhcbshjdcb');
// UpdateUserRefreshToken // UpdateUserRefreshToken
@@ -349,7 +349,7 @@ describe('refreshAuthTokens', () => {
}); });
}); });
test('should throw INVALID_REFRESH_TOKEN when the refresh token is invalid', async () => { test('Should throw INVALID_REFRESH_TOKEN when the refresh token is invalid', async () => {
// generateAuthTokens // generateAuthTokens
mockJWT.sign.mockReturnValue('sdhjcbjsdhcbshjdcb'); mockJWT.sign.mockReturnValue('sdhjcbjsdhcbshjdcb');
mockPrisma.user.update.mockResolvedValueOnce({ mockPrisma.user.update.mockResolvedValueOnce({

View File

@@ -2,6 +2,7 @@ import { NestFactory } from '@nestjs/core';
import { json } from 'express'; import { json } from 'express';
import { AppModule } from './app.module'; import { AppModule } from './app.module';
import * as cookieParser from 'cookie-parser'; import * as cookieParser from 'cookie-parser';
import { VersioningType } from '@nestjs/common';
async function bootstrap() { async function bootstrap() {
console.log(`Running in production: ${process.env.PRODUCTION}`); console.log(`Running in production: ${process.env.PRODUCTION}`);
@@ -31,6 +32,9 @@ async function bootstrap() {
origin: true, origin: true,
}); });
} }
app.enableVersioning({
type: VersioningType.URI,
});
app.use(cookieParser(process.env.SIGNED_COOKIE_SECRET)); app.use(cookieParser(process.env.SIGNED_COOKIE_SECRET));
await app.listen(process.env.PORT || 3170); await app.listen(process.env.PORT || 3170);
} }