Skip to content

Commit

Permalink
add tests for basic signup and login flows
Browse files Browse the repository at this point in the history
  • Loading branch information
alexeh committed Sep 9, 2024
1 parent 54992f5 commit d6fcb37
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 28 deletions.
4 changes: 2 additions & 2 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class AuthenticationController {
return this.authService.signup(signupDto);
}

@Public()
@UseGuards(LocalAuthGuard)
@Post('login')
async login(@GetUser() user: User) {
Expand Down
3 changes: 1 addition & 2 deletions api/src/modules/config/app-config.module.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down
3 changes: 0 additions & 3 deletions api/src/modules/config/app-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
1 change: 0 additions & 1 deletion api/src/modules/config/path-resolver.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { join } from 'path';
import { readdirSync } from 'fs';

// TODO: Workaround: This should be prob fixed in the jest conf

Expand Down
51 changes: 39 additions & 12 deletions api/test/auth/auth.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('Authentication', () => {
let testManager: TestManager;

beforeAll(async () => {
console.log('ENV', process.env.NODE_ENV);
testManager = await TestManager.createTestManager();
});

Expand All @@ -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: '[email protected]', 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)
Expand All @@ -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: '[email protected]',
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();
});
});
});
8 changes: 2 additions & 6 deletions api/test/utils/test-manager.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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
Expand All @@ -33,9 +31,7 @@ export class TestManager {
this.moduleFixture = moduleFixture;
}

static async createTestManager<FixtureType>(options?: {
fixtures: FixtureType;
}) {
static async createTestManager() {
const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
Expand Down
1 change: 1 addition & 0 deletions api/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"extends": "../tsconfig.json",
"compilerOptions": {
"types": ["node", "jest"],
"module": "commonjs",
"declaration": true,
"removeComments": true,
Expand Down
4 changes: 2 additions & 2 deletions pnpm-lock.yaml

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

0 comments on commit d6fcb37

Please sign in to comment.