From d6fcb37d2045970888cbbb64bb585a6b0c1762af Mon Sep 17 00:00:00 2001 From: alexeh Date: Mon, 9 Sep 2024 06:14:46 +0200 Subject: [PATCH] add tests for basic signup and login flows --- api/package.json | 4 +- .../authentication.controller.ts | 1 + api/src/modules/config/app-config.module.ts | 3 +- api/src/modules/config/app-config.service.ts | 3 -- api/src/modules/config/path-resolver.ts | 1 - api/test/auth/auth.spec.ts | 51 ++++++++++++++----- api/test/utils/test-manager.ts | 8 +-- api/tsconfig.json | 1 + pnpm-lock.yaml | 4 +- 9 files changed, 48 insertions(+), 28 deletions(-) diff --git a/api/package.json b/api/package.json index 48de8541..360c7833 100644 --- a/api/package.json +++ b/api/package.json @@ -40,7 +40,7 @@ "@nestjs/testing": "^10.0.0", "@types/bcrypt": "^5.0.2", "@types/express": "^4.17.17", - "@types/jest": "^29.5.2", + "@types/jest": "^29.5.12", "@types/lodash": "^4.17.7", "@types/node": "^20.3.1", "@types/passport-jwt": "^4.0.1", @@ -55,7 +55,7 @@ "prettier": "^3.0.0", "source-map-support": "^0.5.21", "supertest": "^7.0.0", - "ts-jest": "^29.1.0", + "ts-jest": "^29.2.5", "ts-loader": "^9.4.3", "ts-node": "^10.9.1", "tsconfig-paths": "^4.2.0", diff --git a/api/src/modules/auth/authentication/authentication.controller.ts b/api/src/modules/auth/authentication/authentication.controller.ts index 33fc5a6a..b9c4017d 100644 --- a/api/src/modules/auth/authentication/authentication.controller.ts +++ b/api/src/modules/auth/authentication/authentication.controller.ts @@ -16,6 +16,7 @@ export class AuthenticationController { return this.authService.signup(signupDto); } + @Public() @UseGuards(LocalAuthGuard) @Post('login') async login(@GetUser() user: User) { diff --git a/api/src/modules/config/app-config.module.ts b/api/src/modules/config/app-config.module.ts index 0aa60ef5..7e9c6ea5 100644 --- a/api/src/modules/config/app-config.module.ts +++ b/api/src/modules/config/app-config.module.ts @@ -1,6 +1,5 @@ -import { Global, Module, OnModuleInit } from '@nestjs/common'; +import { Global, Module } from '@nestjs/common'; import { ConfigModule, ConfigService } from '@nestjs/config'; -import { join } from 'path'; import { ApiConfigService } from '@api/modules/config/app-config.service'; import { DatabaseModule } from '@api/modules/config/database/database.module'; import { resolveConfigPath } from '@api/modules/config/path-resolver'; diff --git a/api/src/modules/config/app-config.service.ts b/api/src/modules/config/app-config.service.ts index 25da0f87..71f03e2b 100644 --- a/api/src/modules/config/app-config.service.ts +++ b/api/src/modules/config/app-config.service.ts @@ -20,9 +20,6 @@ export class ApiConfigService { * @note: Maybe it's a good idea to move the datasource config to shared folder, to be used potentially for a e2e test agent */ getDatabaseConfig() { - console.log('ALL ENVSSS'); - console.log(readdirSync(join(__dirname, '../../../../shared/config'))); - return { host: this.configService.get('DB_HOST'), port: this.configService.get('DB_PORT'), diff --git a/api/src/modules/config/path-resolver.ts b/api/src/modules/config/path-resolver.ts index 96ef3e2b..01909aad 100644 --- a/api/src/modules/config/path-resolver.ts +++ b/api/src/modules/config/path-resolver.ts @@ -1,5 +1,4 @@ import { join } from 'path'; -import { readdirSync } from 'fs'; // TODO: Workaround: This should be prob fixed in the jest conf diff --git a/api/test/auth/auth.spec.ts b/api/test/auth/auth.spec.ts index ef1c04d2..ca39a978 100644 --- a/api/test/auth/auth.spec.ts +++ b/api/test/auth/auth.spec.ts @@ -6,7 +6,6 @@ describe('Authentication', () => { let testManager: TestManager; beforeAll(async () => { - console.log('ENV', process.env.NODE_ENV); testManager = await TestManager.createTestManager(); }); @@ -32,17 +31,13 @@ describe('Authentication', () => { expect(response.body.message).toEqual( `Email ${user.email} already exists`, ); - console.log('RESPONSE', response.body); }); test(`it should sign up a new user`, async () => { const newUser = { email: 'test@test.com', password: '12345678' }; - const response = await testManager - .request() - .post('/authentication/signup') - .send({ - email: newUser.email, - password: newUser.password, - }); + await testManager.request().post('/authentication/signup').send({ + email: newUser.email, + password: newUser.password, + }); const user = await testManager .getDataSource() .getRepository(User) @@ -54,8 +49,40 @@ describe('Authentication', () => { }); }); describe('Sign In', () => { - test(`it should throw an error if no user exists with provided credentials`, async () => {}); - test(`it should throw an error if password is incorrect`, async () => {}); - test(`it should sign in a user`, async () => {}); + test(`it should throw an error if no user exists with provided credentials`, async () => { + const response = await testManager + .request() + .post('/authentication/login') + .send({ + email: 'non-existing@user.com', + password: '12345567', + }); + expect(response.status).toBe(401); + expect(response.body.message).toEqual('Invalid credentials'); + }); + test(`it should throw an error if password is incorrect`, async () => { + const user = await testManager.mocks().createUser({}); + const response = await testManager + .request() + .post('/authentication/login') + .send({ + email: user.email, + password: 'wrongpassword', + }); + expect(response.status).toBe(401); + expect(response.body.message).toEqual('Invalid credentials'); + }); + test(`it should sign in a user`, async () => { + const user = await testManager.mocks().createUser({}); + const response = await testManager + .request() + .post('/authentication/login') + .send({ + email: user.email, + password: user.password, + }); + expect(response.status).toBe(201); + expect(response.body.accessToken).toBeDefined(); + }); }); }); diff --git a/api/test/utils/test-manager.ts b/api/test/utils/test-manager.ts index 1227f7b9..e4ab1326 100644 --- a/api/test/utils/test-manager.ts +++ b/api/test/utils/test-manager.ts @@ -1,6 +1,6 @@ import { AppModule } from '@api/app.module'; import { Test, TestingModule } from '@nestjs/testing'; -import { INestApplication, ValidationPipe } from '@nestjs/common'; +import { INestApplication } from '@nestjs/common'; import { DataSource } from 'typeorm'; import { logUserIn } from './user.auth'; @@ -11,8 +11,6 @@ import { getDataSourceToken } from '@nestjs/typeorm'; import { clearTestDataFromDatabase } from './db-helpers'; import { createUser } from './mocks/entity-mocks'; import { User } from '@shared/entities/users/user.entity'; -import { ApiConfigModule } from '@api/modules/config/app-config.module'; -import { ApiConfigService } from '@api/modules/config/app-config.service'; /** * @description: Abstraction for NestJS testing workflow. For now its a basic implementation to create a test app, but can be extended to encapsulate @@ -33,9 +31,7 @@ export class TestManager { this.moduleFixture = moduleFixture; } - static async createTestManager(options?: { - fixtures: FixtureType; - }) { + static async createTestManager() { const moduleFixture = await Test.createTestingModule({ imports: [AppModule], }).compile(); diff --git a/api/tsconfig.json b/api/tsconfig.json index acf1e7a5..465486dd 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -1,6 +1,7 @@ { "extends": "../tsconfig.json", "compilerOptions": { + "types": ["node", "jest"], "module": "commonjs", "declaration": true, "removeComments": true, diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ea128a5a..f83fc460 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -96,7 +96,7 @@ importers: specifier: ^4.17.17 version: 4.17.21 '@types/jest': - specifier: ^29.5.2 + specifier: ^29.5.12 version: 29.5.12 '@types/lodash': specifier: ^4.17.7 @@ -141,7 +141,7 @@ importers: specifier: ^7.0.0 version: 7.0.0 ts-jest: - specifier: ^29.1.0 + specifier: ^29.2.5 version: 29.2.5(@babel/core@7.25.2)(@jest/transform@29.7.0)(@jest/types@29.6.3)(babel-jest@29.7.0(@babel/core@7.25.2))(jest@29.7.0(@types/node@20.16.5)(ts-node@10.9.2(@types/node@20.16.5)(typescript@5.5.4)))(typescript@5.5.4) ts-loader: specifier: ^9.4.3