tests: Fix tests as part of the DTO Refactor

Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
Philip Molares 2021-01-30 12:47:31 +01:00
parent b07d6d478c
commit a7f35aaeec
3 changed files with 21 additions and 15 deletions

View file

@ -5,14 +5,20 @@
*/ */
import { ExecutionContext, Injectable } from '@nestjs/common'; import { ExecutionContext, Injectable } from '@nestjs/common';
import { UsersService } from '../users/users.service';
import { User } from '../users/user.entity';
@Injectable() @Injectable()
export class MockAuthGuard { export class MockAuthGuard {
canActivate(context: ExecutionContext) { private user: User;
constructor(private usersService: UsersService) {}
async canActivate(context: ExecutionContext) {
const req = context.switchToHttp().getRequest(); const req = context.switchToHttp().getRequest();
req.user = { if (!this.user) {
userName: 'hardcoded', this.user = await this.usersService.createUser('hardcoded', 'Testy');
}; }
req.user = this.user;
return true; return true;
} }
} }

View file

@ -20,7 +20,6 @@ import { MediaService } from '../../src/media/media.service';
import { NotesModule } from '../../src/notes/notes.module'; import { NotesModule } from '../../src/notes/notes.module';
import { NotesService } from '../../src/notes/notes.service'; import { NotesService } from '../../src/notes/notes.service';
import { PermissionsModule } from '../../src/permissions/permissions.module'; import { PermissionsModule } from '../../src/permissions/permissions.module';
import { UsersService } from '../../src/users/users.service';
import { AuthModule } from '../../src/auth/auth.module'; import { AuthModule } from '../../src/auth/auth.module';
import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; import { TokenAuthGuard } from '../../src/auth/token-auth.guard';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard'; import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
@ -65,8 +64,6 @@ describe('Notes', () => {
app.useLogger(logger); app.useLogger(logger);
const notesService: NotesService = moduleRef.get('NotesService'); const notesService: NotesService = moduleRef.get('NotesService');
await notesService.createNote('test content', 'test_upload_media'); await notesService.createNote('test content', 'test_upload_media');
const usersService: UsersService = moduleRef.get('UsersService');
await usersService.createUser('hardcoded', 'Hard Coded');
mediaService = moduleRef.get('MediaService'); mediaService = moduleRef.get('MediaService');
}); });

View file

@ -20,7 +20,7 @@ import { PermissionsModule } from '../../src/permissions/permissions.module';
import { AuthModule } from '../../src/auth/auth.module'; import { AuthModule } from '../../src/auth/auth.module';
import { TokenAuthGuard } from '../../src/auth/token-auth.guard'; import { TokenAuthGuard } from '../../src/auth/token-auth.guard';
import { MockAuthGuard } from '../../src/auth/mock-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', () => { describe('Notes', () => {
let app: INestApplication; let app: INestApplication;
@ -46,6 +46,7 @@ describe('Notes', () => {
}), }),
LoggerModule, LoggerModule,
AuthModule, AuthModule,
UsersModule,
], ],
}) })
.overrideGuard(TokenAuthGuard) .overrideGuard(TokenAuthGuard)
@ -55,8 +56,6 @@ describe('Notes', () => {
app = moduleRef.createNestApplication(); app = moduleRef.createNestApplication();
await app.init(); await app.init();
notesService = moduleRef.get(NotesService); notesService = moduleRef.get(NotesService);
const usersService: UsersService = moduleRef.get('UsersService');
await usersService.createUser('testy', 'Testy McTestFace');
}); });
it(`POST /notes`, async () => { it(`POST /notes`, async () => {
@ -69,8 +68,9 @@ describe('Notes', () => {
.expect(201); .expect(201);
expect(response.body.metadata?.id).toBeDefined(); expect(response.body.metadata?.id).toBeDefined();
expect( expect(
(await notesService.getNoteDtoByIdOrAlias(response.body.metadata.id)) await notesService.getCurrentContent(
.content, await notesService.getNoteByIdOrAlias(response.body.metadata.id),
),
).toEqual(newNote); ).toEqual(newNote);
}); });
@ -100,8 +100,9 @@ describe('Notes', () => {
.expect(201); .expect(201);
expect(response.body.metadata?.id).toBeDefined(); expect(response.body.metadata?.id).toBeDefined();
return expect( return expect(
(await notesService.getNoteDtoByIdOrAlias(response.body.metadata.id)) await notesService.getCurrentContent(
.content, await notesService.getNoteByIdOrAlias(response.body.metadata?.id),
),
).toEqual(newNote); ).toEqual(newNote);
}); });
@ -125,7 +126,9 @@ describe('Notes', () => {
.send('New note text') .send('New note text')
.expect(200); .expect(200);
await expect( await expect(
(await notesService.getNoteDtoByIdOrAlias('test4')).content, await notesService.getCurrentContent(
await notesService.getNoteByIdOrAlias('test4'),
),
).toEqual('New note text'); ).toEqual('New note text');
expect(response.body.content).toEqual('New note text'); expect(response.body.content).toEqual('New note text');