Skip to content
This repository has been archived by the owner on Oct 4, 2024. It is now read-only.

Commit

Permalink
Final configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
henrybrink committed Apr 15, 2024
1 parent bbb80af commit 1ee4d14
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 9 deletions.
4 changes: 2 additions & 2 deletions backend/src/api/user/user.controller.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('UserController', () => {
expect(controller).toBeDefined();
});

it('Should return a SanatizedUser object in a gest request', () => {
it('Should return a SanatizedUser object in a gest request', async () => {
const user : User = {
id: '1',
displayName: 'Max Mustermann',
Expand All @@ -31,7 +31,7 @@ describe('UserController', () => {
user
};

const response = await controller.me(req);
const response = (await controller.me(req)) as any;

expect(response?.password).toBeUndefined();
});
Expand Down
2 changes: 1 addition & 1 deletion backend/src/api/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Controller, Get, Request, UseGuards } from '@nestjs/common';
import { User } from '@prisma/client';
import { AutoGuard } from 'src/auth/auto.guard';
import { AutoGuard } from '../../auth/auto.guard';

type SanatizedUser = Omit<User, 'password'>;

Expand Down
2 changes: 1 addition & 1 deletion backend/src/auth/auth.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { PassportModule } from '@nestjs/passport';
import { HTTPStrategy } from './strategies/http.strategy';
import { PrismaModule } from '../db/prisma.module';
import { AutoGuard } from './auto.guard';
import { PrismaModule } from 'src/db/prisma.module';

@Module({
imports: [PrismaModule, PassportModule],
Expand Down
25 changes: 21 additions & 4 deletions backend/src/auth/auth.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from './auth.service';
import { UserRepository } from 'src/db/repositories/user.repository';
import { DeepMockProxy, mockDeep } from 'jest-mock-extended';
import { hashSync } from 'bcrypt';
import { PrismaClient } from '@prisma/client';
import { UserRepository } from '../db/repositories/user.repository';

describe('AuthService', () => {
let service: AuthService;
Expand All @@ -24,7 +24,7 @@ describe('AuthService', () => {
})
.overrideProvider(UserRepository)
.useValue(mockDeep<UserRepository>())
.compile();
.compile();

service = module.get<AuthService>(AuthService);
userRepository = await module.resolve(UserRepository);
Expand All @@ -37,7 +37,24 @@ describe('AuthService', () => {
it('should return a user when a valid email and password is supplied', async () => {
userRepository.findByEmail.mockResolvedValue(exampleUser);

expect(await service.validateUserPassword("[email protected]", "1234")).toBeDefined();
})
expect(
await service.validateUserPassword('[email protected]', '1234'),
).toBeDefined();
});

it('should return null if the user is invalid', async () => {
userRepository.findByEmail.mockResolvedValue(null);

expect(
await service.validateUserPassword('[email protected]', '1234'),
).toBeNull();
});

it('should return null if the user is found, but the password is incorrect', async () => {
userRepository.findByEmail.mockResolvedValue(exampleUser);

expect(
await service.validateUserPassword('[email protected]', 'incorrect'),
).toBeNull();
});
});
2 changes: 1 addition & 1 deletion backend/src/auth/auth.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable } from '@nestjs/common';
import { User } from '@prisma/client';
import { compare } from 'bcrypt';
import { UserRepository } from 'src/db/repositories/user.repository';
import { UserRepository } from '../db/repositories/user.repository';

@Injectable()
export class AuthService {
Expand Down
40 changes: 40 additions & 0 deletions backend/src/auth/strategies/http.strategy.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AuthService } from '../auth.service';
import { DeepMockProxy, mockDeep } from 'jest-mock-extended';
import { TestConstants } from '../../../test/lib/constants';
import { HTTPStrategy } from './http.strategy';
import { UnauthorizedException } from '@nestjs/common';

describe('HTTPStrategy tests', () => {
let authService: DeepMockProxy<AuthService>;
let cut: HTTPStrategy;

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

authService = module.get(AuthService);

cut = new HTTPStrategy(authService);
});

it('should return the user if the credentials are correct', async () => {
authService.validateUserPassword.mockResolvedValue(
TestConstants.database.users.exampleUser,
);

expect(await cut.validate('[email protected]', '1234')).toEqual(
TestConstants.database.users.exampleUser,
);
});

it('should fail with an exception if the credentials are invalid', async () => {
authService.validateUserPassword.mockResolvedValue(null);

expect(async () => {await cut.validate('[email protected]', '1234')}).rejects.toThrow();
});
});
5 changes: 5 additions & 0 deletions backend/test/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { Users } from './database.constants';

export const TestConstants = {
database: { users: Users },
};
10 changes: 10 additions & 0 deletions backend/test/lib/database.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const Users = {
exampleUser: {
id: '0',
email: '[email protected]',
displayName: 'Max Mustermann',
password: '$2a$04$7r2EqdYAla6UUw9rxMlfc.pACgmONvT/0jFLC2xTLdrIvoxOGmzAC', // 1234
enabled: true,
verified: false,
},
};

0 comments on commit 1ee4d14

Please sign in to comment.