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 committed by David Mehren
parent 0d5b9dea00
commit 5920a1c72f
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
3 changed files with 21 additions and 15 deletions

View file

@ -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;
}
}

View file

@ -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');
});

View file

@ -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');