diff --git a/.github/workflows/backend.yml b/.github/workflows/backend.yml new file mode 100644 index 0000000..fde4611 --- /dev/null +++ b/.github/workflows/backend.yml @@ -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 diff --git a/Backend/jest.config.json b/Backend/jest.config.json new file mode 100644 index 0000000..5c0cbcd --- /dev/null +++ b/Backend/jest.config.json @@ -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" +} \ No newline at end of file diff --git a/Backend/package.json b/Backend/package.json index 8fd2285..b5e16f9 100644 --- a/Backend/package.json +++ b/Backend/package.json @@ -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", @@ -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" } } diff --git a/Backend/src/auth/auth.guard.spec.ts b/Backend/src/auth/auth.guard.spec.ts index 7c10573..be99f16 100644 --- a/Backend/src/auth/auth.guard.spec.ts +++ b/Backend/src/auth/auth.guard.spec.ts @@ -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; 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}` }; + } }); diff --git a/Backend/src/cameraStream/cameraStream.gateway.ts b/Backend/src/cameraStream/cameraStream.gateway.ts index 4fb8cd8..b5ddafc 100644 --- a/Backend/src/cameraStream/cameraStream.gateway.ts +++ b/Backend/src/cameraStream/cameraStream.gateway.ts @@ -16,7 +16,6 @@ import { UseFilters, UseGuards, } from '@nestjs/common'; -import * as console from 'console'; import { JwtService } from '@nestjs/jwt'; @Catch(WsException, HttpException) @@ -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) { diff --git a/Backend/src/env.spec.ts b/Backend/src/env.spec.ts index df587da..8d47c48 100644 --- a/Backend/src/env.spec.ts +++ b/Backend/src/env.spec.ts @@ -2,26 +2,26 @@ * Copyright (c) 2023. Leonardo Migliorelli */ -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'); +};