From a7f35aaeec1626c1af558d9b91b7debb9f18faf1 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sat, 30 Jan 2021 12:47:31 +0100 Subject: [PATCH] tests: Fix tests as part of the DTO Refactor Signed-off-by: Philip Molares --- src/auth/mock-auth.guard.ts | 14 ++++++++++---- test/public-api/media.e2e-spec.ts | 3 --- test/public-api/notes.e2e-spec.ts | 19 +++++++++++-------- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/src/auth/mock-auth.guard.ts b/src/auth/mock-auth.guard.ts index 9d15bc83e..d4f1236b0 100644 --- a/src/auth/mock-auth.guard.ts +++ b/src/auth/mock-auth.guard.ts @@ -5,14 +5,20 @@ */ import { ExecutionContext, Injectable } from '@nestjs/common'; +import { UsersService } from '../users/users.service'; +import { User } from '../users/user.entity'; @Injectable() export class MockAuthGuard { - canActivate(context: ExecutionContext) { + private user: User; + constructor(private usersService: UsersService) {} + + async canActivate(context: ExecutionContext) { const req = context.switchToHttp().getRequest(); - req.user = { - userName: 'hardcoded', - }; + if (!this.user) { + this.user = await this.usersService.createUser('hardcoded', 'Testy'); + } + req.user = this.user; return true; } } diff --git a/test/public-api/media.e2e-spec.ts b/test/public-api/media.e2e-spec.ts index f4b57ebf0..26ee76d27 100644 --- a/test/public-api/media.e2e-spec.ts +++ b/test/public-api/media.e2e-spec.ts @@ -20,7 +20,6 @@ import { MediaService } from '../../src/media/media.service'; import { NotesModule } from '../../src/notes/notes.module'; import { NotesService } from '../../src/notes/notes.service'; import { PermissionsModule } from '../../src/permissions/permissions.module'; -import { UsersService } from '../../src/users/users.service'; import { AuthModule } from '../../src/auth/auth.module'; import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; @@ -65,8 +64,6 @@ describe('Notes', () => { app.useLogger(logger); const notesService: NotesService = moduleRef.get('NotesService'); await notesService.createNote('test content', 'test_upload_media'); - const usersService: UsersService = moduleRef.get('UsersService'); - await usersService.createUser('hardcoded', 'Hard Coded'); mediaService = moduleRef.get('MediaService'); }); diff --git a/test/public-api/notes.e2e-spec.ts b/test/public-api/notes.e2e-spec.ts index bf9ca8c55..5059f3e93 100644 --- a/test/public-api/notes.e2e-spec.ts +++ b/test/public-api/notes.e2e-spec.ts @@ -20,7 +20,7 @@ import { PermissionsModule } from '../../src/permissions/permissions.module'; import { AuthModule } from '../../src/auth/auth.module'; import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; -import { UsersService } from '../../src/users/users.service'; +import { UsersModule } from '../../src/users/users.module'; describe('Notes', () => { let app: INestApplication; @@ -46,6 +46,7 @@ describe('Notes', () => { }), LoggerModule, AuthModule, + UsersModule, ], }) .overrideGuard(TokenAuthGuard) @@ -55,8 +56,6 @@ describe('Notes', () => { app = moduleRef.createNestApplication(); await app.init(); notesService = moduleRef.get(NotesService); - const usersService: UsersService = moduleRef.get('UsersService'); - await usersService.createUser('testy', 'Testy McTestFace'); }); it(`POST /notes`, async () => { @@ -69,8 +68,9 @@ describe('Notes', () => { .expect(201); expect(response.body.metadata?.id).toBeDefined(); expect( - (await notesService.getNoteDtoByIdOrAlias(response.body.metadata.id)) - .content, + await notesService.getCurrentContent( + await notesService.getNoteByIdOrAlias(response.body.metadata.id), + ), ).toEqual(newNote); }); @@ -100,8 +100,9 @@ describe('Notes', () => { .expect(201); expect(response.body.metadata?.id).toBeDefined(); return expect( - (await notesService.getNoteDtoByIdOrAlias(response.body.metadata.id)) - .content, + await notesService.getCurrentContent( + await notesService.getNoteByIdOrAlias(response.body.metadata?.id), + ), ).toEqual(newNote); }); @@ -125,7 +126,9 @@ describe('Notes', () => { .send('New note text') .expect(200); await expect( - (await notesService.getNoteDtoByIdOrAlias('test4')).content, + await notesService.getCurrentContent( + await notesService.getNoteByIdOrAlias('test4'), + ), ).toEqual('New note text'); expect(response.body.content).toEqual('New note text');