Skip to content

Commit

Permalink
fixed authguard test, added backend workflow test and created a globa…
Browse files Browse the repository at this point in the history
…l test
  • Loading branch information
Glydric committed Dec 6, 2023
1 parent 47cf35a commit abeb18c
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 51 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/backend.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
on:
push:
pull_request:
branches:
- main

jobs:
jest-test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Working Directory
run: cd Backend
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm test
18 changes: 18 additions & 0 deletions Backend/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"globalSetup": "./env.spec.ts",
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
19 changes: 1 addition & 18 deletions Backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"start:dev": "npm start -- --watch",
"start:debug": "npm start -- --debug --watch",
"start:prod": "node dist/main",
"test": "npm ci && jest --detectOpenHandles",
"test": "jest --detectOpenHandles",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
Expand Down Expand Up @@ -67,22 +67,5 @@
"ts-node": "^10.9.1",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.3"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
26 changes: 18 additions & 8 deletions Backend/src/auth/auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import { JwtService } from '@nestjs/jwt';
import { AuthGuard } from './auth.guard';

describe('AuthGuard', () => {
const payload = 'complexUserNamePayload';
let service: JwtService;
let jwtToken: string;
let auth: AuthGuard;
let headers: Record<string, string>;

beforeAll(async () => {
service = new JwtService({
secret: process.env.JWT_SECRET,
secret: process.env.JWT_SECRET ?? 'keytest',
});
jwtToken = await service.signAsync(payload);

auth = new AuthGuard(service);
headers = tokenToHeaders(await service.signAsync(payload));
});

it('Should be defined', () => {
expect(service).toBeDefined();
expect(jwtToken).toBeDefined();
expect(auth).toBeDefined();
expect(headers).toBeDefined();
});

it('Get Payload from token', () => {
const user = service.verify(jwtToken);
const user = auth.checkToken(headers);
expect(user).toBe(payload);
});

it('Should fail JwtVerify of another token', () => {
expect(() =>
service.verify(
'eyJhbGciOiJIUzI1NiJ9.QmFzaWM.MTnCJYESf5QRL9N8gqn5Di5PEZX8eZB5sN8W4TJTDKF',
auth.checkToken(
tokenToHeaders(
'eyJhbGciOiJIUzI1NiJ9.QmFzaWM.MTnCJYESf5QRL9N8gqn5Di5PEZX8eZB5sN8W4TJTDKF',
),
),
).toThrow();
});

function tokenToHeaders(token: string) {
return { authorization: `Bearer ${token}` };
}
});
2 changes: 0 additions & 2 deletions Backend/src/cameraStream/cameraStream.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import {
UseFilters,
UseGuards,
} from '@nestjs/common';
import * as console from 'console';
import { JwtService } from '@nestjs/jwt';

@Catch(WsException, HttpException)
Expand Down Expand Up @@ -57,7 +56,6 @@ export class CameraStreamGateway implements OnGatewayConnection {
) {
try {
const message = JSON.parse(data) as Message;
console.log(message.id, message.data);

client.to('clients').emit(message.id.toString(), message.data);
} catch (e) {
Expand Down
46 changes: 23 additions & 23 deletions Backend/src/env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,26 @@
* Copyright (c) 2023. Leonardo Migliorelli <Glydric>
*/

describe('FiltersPipe', () => {
it('MONGO_INITDB_ROOT_USERNAME should be defined', () => {
expect(process.env.MONGO_INITDB_ROOT_USERNAME).toBeDefined();
});
it('MONGO_INITDB_ROOT_PASSWORD should be defined', () => {
expect(process.env.MONGO_INITDB_ROOT_PASSWORD).toBeDefined();
});
it('JWT_SECRET should be defined', () => {
expect(process.env.JWT_SECRET).toBeDefined();
});
it('CSD_USER should be defined', () => {
expect(process.env.CSD_USER).toBeDefined();
});
it('CSD_PASSWORD should be defined', () => {
expect(process.env.CSD_PASSWORD).toBeDefined();
});
it('TELEGRAM_TOKEN should be defined', () => {
expect(process.env.TELEGRAM_TOKEN).toBeDefined();
});
it('MONGO_HOST should be defined', () => {
expect(process.env.MONGO_HOST).toBeDefined();
});
});
import * as process from 'process';

function throwsIfNull(value: any, message: string) {
if (!value) {
throw new Error(message);
}
}

export default () => {
throwsIfNull(
process.env.MONGO_INITDB_ROOT_USERNAME,
'MONGO_INITDB_ROOT_USERNAME not defined',
);
throwsIfNull(
process.env.MONGO_INITDB_ROOT_PASSWORD,
'MONGO_INITDB_ROOT_PASSWORD not defined',
);
throwsIfNull(process.env.JWT_SECRET, 'JWT_SECRET not defined');
throwsIfNull(process.env.CSD_USER, 'CSD_USER not defined');
throwsIfNull(process.env.CSD_PASSWORD, 'CSD_PASSWORD not defined');
throwsIfNull(process.env.TELEGRAM_TOKEN, 'TELEGRAM_TOKEN not defined');
throwsIfNull(process.env.MONGO_HOST, 'MONGO_HOST not defined');
};

0 comments on commit abeb18c

Please sign in to comment.