-
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.
test(api): ✅ add unit tests for auth
- Loading branch information
Showing
7 changed files
with
178 additions
and
19 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,46 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
/* eslint-disable @typescript-eslint/unbound-method */ | ||
import { TestBed } from '@automock/jest' | ||
import { createMock } from '@golevelup/ts-jest' | ||
import { Request, Response } from 'express' | ||
|
||
import { AuthController } from './auth.controller' | ||
import { AuthService } from './auth.service' | ||
|
||
describe('AuthController', () => { | ||
let controller: AuthController | ||
let authService: jest.Mocked<AuthService> | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
controllers: [AuthController] | ||
}).compile() | ||
const { unit, unitRef } = TestBed.create(AuthController).compile() | ||
|
||
controller = module.get<AuthController>(AuthController) | ||
controller = unit | ||
authService = unitRef.get(AuthService) | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(controller).toBeDefined() | ||
}) | ||
|
||
it('should set cookies on google login callback', async () => { | ||
const req = createMock<Request>({ | ||
user: { | ||
email: 'test-email', | ||
name: 'test-name', | ||
picture: 'test-picture' | ||
} | ||
}) | ||
|
||
const res = createMock<Response>() | ||
jest.spyOn(authService, 'signIn').mockResolvedValue('token') | ||
|
||
await controller.googleLoginCallback(req, res) | ||
|
||
expect(authService.signIn).toHaveBeenCalledWith({ | ||
email: 'test-email', | ||
name: 'test-name', | ||
picture: 'test-picture' | ||
}) | ||
|
||
expect(res.cookie).toHaveBeenCalledWith('access_token', 'token') | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,19 +1,60 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { TestBed } from '@automock/jest' | ||
import { JwtService } from '@nestjs/jwt' | ||
|
||
import { UsersService } from '../users/users.service' | ||
|
||
import { AuthService } from './auth.service' | ||
|
||
describe('AuthService', () => { | ||
let service: AuthService | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [AuthService] | ||
}).compile() | ||
const { unit } = TestBed.create(AuthService) | ||
.mock(UsersService) | ||
.using({ | ||
findOrCreate: jest.fn().mockResolvedValue({ id: 1, email: '[email protected]' }) | ||
}) | ||
.mock(JwtService) | ||
.using({ | ||
sign: jest.fn().mockReturnValue('jwt') | ||
}) | ||
.compile() | ||
|
||
service = module.get<AuthService>(AuthService) | ||
service = unit | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
|
||
it('should return a user and token', async () => { | ||
const user = await service.signIn({ | ||
email: 'test', | ||
name: 'test', | ||
picture: 'test' | ||
}) | ||
expect(user).toEqual('jwt') | ||
}) | ||
|
||
it('should throw an error if no profile is provided', async () => { | ||
try { | ||
// @ts-expect-error: Testing OAuth provider does not return profile | ||
await service.signIn(null) | ||
} catch (e) { | ||
expect(e.message).toEqual('Unauthenticated') | ||
} | ||
}) | ||
|
||
it('should throw an error if no email is provided', async () => { | ||
try { | ||
await service.signIn({ | ||
// @ts-expect-error: Testing when OAuth provider does not return email | ||
email: null, | ||
name: 'test', | ||
picture: 'test' | ||
}) | ||
} catch (e) { | ||
expect(e.message).toEqual('Email not found') | ||
} | ||
}) | ||
}) |
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 |
---|---|---|
@@ -1,19 +1,98 @@ | ||
import { Test, TestingModule } from '@nestjs/testing' | ||
import { TestBed } from '@automock/jest' | ||
|
||
import { userFactory } from '../database/factories/user' | ||
import { PrismaService } from '../database/prisma.service' | ||
|
||
import { UsersService } from './users.service' | ||
|
||
const mockUser = userFactory.build({ | ||
email: 'test-email', | ||
name: 'test-name', | ||
picture: 'test-picture' | ||
}) | ||
|
||
describe('UsersService', () => { | ||
let service: UsersService | ||
const prisma = { | ||
user: { | ||
create: jest.fn().mockResolvedValue(mockUser), | ||
upsert: jest.fn().mockResolvedValue(mockUser), | ||
findMany: jest.fn().mockResolvedValue([mockUser]), | ||
findUnique: jest.fn().mockResolvedValue(mockUser), | ||
update: jest.fn().mockResolvedValue(mockUser), | ||
delete: jest.fn().mockResolvedValue(mockUser) | ||
} | ||
} | ||
|
||
beforeEach(async () => { | ||
const module: TestingModule = await Test.createTestingModule({ | ||
providers: [UsersService] | ||
}).compile() | ||
const { unit } = TestBed.create(UsersService) | ||
.mock(PrismaService) | ||
.using(prisma) | ||
.compile() | ||
|
||
service = module.get<UsersService>(UsersService) | ||
service = unit | ||
}) | ||
|
||
it('should be defined', () => { | ||
expect(service).toBeDefined() | ||
}) | ||
|
||
it('should create a user', async () => { | ||
const user = await service.create({ | ||
email: 'test-email', | ||
name: 'test-name', | ||
picture: 'test-picture' | ||
}) | ||
|
||
expect(user).toMatchObject({ id: 1, email: 'test-email', name: 'test-name', picture: 'test-picture' }) | ||
}) | ||
|
||
it('should find or create a user', async () => { | ||
const user = await service.findOrCreate({ | ||
email: 'test-email', | ||
name: 'test-name', | ||
picture: 'test-picture' | ||
}) | ||
|
||
expect(user).toEqual(mockUser) | ||
}) | ||
|
||
it('should return an array of users', async () => { | ||
const users = await service.findAll() | ||
|
||
expect(users).toEqual([mockUser]) | ||
}) | ||
|
||
it('should return a user by id', async () => { | ||
const user = await service.findOne(1) | ||
|
||
expect(user).toEqual(mockUser) | ||
}) | ||
|
||
it('should update a user', async () => { | ||
const user = await service.update(1, { | ||
email: 'test-email', | ||
name: 'test-name', | ||
picture: 'test-picture' | ||
}) | ||
|
||
expect(user).toEqual(mockUser) | ||
}) | ||
|
||
it('should remove a user', async () => { | ||
prisma.user.delete.mockResolvedValueOnce(mockUser) | ||
const user = await service.remove(1) | ||
|
||
expect(user).toEqual(mockUser) | ||
}) | ||
|
||
describe('when user does not exist', () => { | ||
const error = new Error('Prisma error') | ||
Object.assign(error, { code: 'P2025' }) | ||
|
||
it('should throw an error', async () => { | ||
prisma.user.delete.mockRejectedValueOnce(error) | ||
await expect(service.remove(2)).rejects.toThrow('User not found') | ||
}) | ||
}) | ||
}) |
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
Empty file.