Skip to content

Commit

Permalink
Merge pull request #36 from B-ki/auth-test
Browse files Browse the repository at this point in the history
Auth and User tests
  • Loading branch information
Bilaboz authored Oct 16, 2023
2 parents 6df1dd3 + b505e59 commit 80d57db
Show file tree
Hide file tree
Showing 11 changed files with 190 additions and 17 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/backend-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- api/**

jobs:
lint:
build:
runs-on: ubuntu-latest

defaults:
Expand All @@ -30,6 +30,9 @@ jobs:

- name: Install dependencies
run: pnpm install

- name: Generate prisma client
run: pnpm prisma:generate

- name: Build
run: pnpm build
2 changes: 1 addition & 1 deletion .github/workflows/frontend-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- front/**

jobs:
lint:
build:
runs-on: ubuntu-latest

defaults:
Expand Down
1 change: 0 additions & 1 deletion api/TODO.txt

This file was deleted.

10 changes: 7 additions & 3 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"lint": "eslint . --ext .ts --ignore-path .gitignore",
"lint:fix": "eslint . --ext .ts --fix --ignore-path .gitignore",
"lint:fix:report": "eslint . --output-file eslint_report.json --format json --ext .ts --fix --ignore-path .gitignore",
"test": "jest",
"test": "NODE_ENV=development jest",
"test:cov": "jest --coverage",
"test:e2e": "jest --config ./test/jest-e2e.json"
"prisma:generate": "prisma generate"
},
"dependencies": {
"@nestjs/common": "^10.0.0",
Expand Down Expand Up @@ -66,6 +66,7 @@
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-simple-import-sort": "^10.0.0",
"jest": "^29.5.0",
"jest-mock-extended": "^3.0.5",
"morgan": "^1.10.0",
"prettier": "^3.0.0",
"source-map-support": "^0.5.21",
Expand All @@ -82,7 +83,10 @@
"json",
"ts"
],
"rootDir": "src",
"rootDir": ".",
"moduleNameMapper": {
"@/(.*)$": "<rootDir>/src/$1"
},
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
Expand Down
22 changes: 22 additions & 0 deletions api/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/prisma/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from '.';
4 changes: 2 additions & 2 deletions api/src/modules/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export class UserService {
return user;
}

async createUser(profile: FortyTwoProfile) {
await this.prisma.user.create({ data: profile });
async createUser(profile: FortyTwoProfile): Promise<User> {
return await this.prisma.user.create({ data: profile });
}

async testGetFirstName(user: User): Promise<string | undefined> {
Expand Down
10 changes: 10 additions & 0 deletions api/src/prisma/prisma.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ import { PrismaClient } from '@prisma/client';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
constructor() {
super({
log: [
{
emit: 'event',
level: 'query',
},
],
});
}
async onModuleInit() {
await this.$connect();
Logger.log('Prisma connected', 'PrismaService');
Expand Down
47 changes: 47 additions & 0 deletions api/test/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { JwtService } from '@nestjs/jwt';
import { Test } from '@nestjs/testing';

import { AuthService } from '../../src/modules/auth/auth.service';
import { UserService } from '../../src/modules/user';

describe('AuthService', () => {
let authService: AuthService;

const mockJwtService = {};

const mockUserService = {
getAll: jest.fn(),
getUnique: jest.fn(),
createUser: jest.fn(),
};

beforeEach(async () => {
// initialize a NestJS module with authService
const module = await Test.createTestingModule({
providers: [
AuthService,
{
provide: JwtService,
useValue: mockJwtService,
},
{
provide: UserService,
useValue: mockUserService,
},
],
}).compile();

authService = module.get(AuthService);
});

// it = "test case"
it('should be defined', () => {
expect(authService).toBeDefined();
});

it('should have functions : login, fetchProfileInformation and generateJWT', () => {
expect(authService.login).toBeDefined();
expect(authService.fetchProfileInformations).toBeDefined();
expect(authService.generateJWT).toBeDefined();
});
});
9 changes: 0 additions & 9 deletions api/test/jest-e2e.json

This file was deleted.

96 changes: 96 additions & 0 deletions api/test/user/user.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import { Test } from '@nestjs/testing';
import { PrismaClient } from '@prisma/client';
import { DeepMockProxy, mockDeep } from 'jest-mock-extended';

import { FortyTwoProfile } from '@/modules/auth';

import { UserService } from '../../src/modules/user';
import { PrismaService } from '../../src/prisma';

describe('UserService', () => {
let userService: UserService;
let prismaService: DeepMockProxy<PrismaClient>;

beforeEach(async () => {
const module = await Test.createTestingModule({
providers: [UserService, PrismaService],
})
.overrideProvider(PrismaService)
.useValue(mockDeep<PrismaClient>())
.compile();

userService = module.get(UserService);
prismaService = module.get(PrismaService);
});

it('should be defined', () => {
expect(userService).toBeDefined();
});

it('should have functions', () => {
expect(userService.getAll).toBeDefined();
expect(userService.getUnique).toBeDefined();
expect(userService.createUser).toBeDefined();
});

const user = {
id: '1',
login: 'testLogin',
email: 'testMail',
imageUrl: 'testUrl',
displayName: 'testLogin',
firstName: 'testFirstName',
lastName: 'testLastName',
createdAt: new Date(),
};

const profile: FortyTwoProfile = {
login: 'testLogin',
email: 'testMail',
imageUrl: 'testUrl',
displayName: 'testLogin',
firstName: 'testFirstName',
lastName: 'testLastName',
};
it('should create users correctly', async () => {
prismaService.user.create.mockResolvedValue(user);
const result = await userService.createUser(profile);
expect(result.id).toEqual('1');
expect(result.login).toEqual(profile.login);
expect(result.email).toEqual(profile.email);
expect(result.imageUrl).toEqual(profile.imageUrl);
expect(result.displayName).toEqual(profile.displayName);
expect(result.firstName).toEqual(profile.firstName);
expect(result.lastName).toEqual(profile.lastName);
});

it('should get a user correctly', async () => {
prismaService.user.findUnique.mockResolvedValue(user);
const result = await userService.getUnique('testLogin');
expect(result.id).toEqual('1');
expect(result.login).toEqual(profile.login);
expect(result.email).toEqual(profile.email);
expect(result.imageUrl).toEqual(profile.imageUrl);
expect(result.displayName).toEqual(profile.displayName);
expect(result.firstName).toEqual(profile.firstName);
expect(result.lastName).toEqual(profile.lastName);
});

it('should throw an error for non-existing user', async () => {
prismaService.user.findUnique.mockResolvedValue(null);

try {
await userService.getUnique('nonExistentUser');
} catch (error) {
expect(error.message).toBe('User nonExistentUser not found');
}
});

it('should get all users correctly', () => {
const users = [user];

prismaService.user.findMany.mockResolvedValue(users);

expect(userService.getAll()).resolves.toEqual(users);
});
});

0 comments on commit 80d57db

Please sign in to comment.