mirror of
https://github.com/hedgedoc/hedgedoc.git
synced 2025-05-13 14:44:43 -04:00
auth: fixes unit and e2e tests
adds MockAuthGuard which always return user 'hardcoded' Signed-off-by: Philip Molares <philip.molares@udo.edu>
This commit is contained in:
parent
33d9c455b8
commit
cce1626c48
4 changed files with 47 additions and 11 deletions
|
@ -1,23 +1,24 @@
|
||||||
import { Test, TestingModule } from '@nestjs/testing';
|
import { Test, TestingModule } from '@nestjs/testing';
|
||||||
import { AuthService } from './auth.service';
|
import { AuthService } from './auth.service';
|
||||||
import { UsersModule } from '../users/users.module';
|
import { PassportModule } from '@nestjs/passport';
|
||||||
import { getRepositoryToken } from '@nestjs/typeorm';
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
||||||
|
import { AuthToken } from '../users/auth-token.entity';
|
||||||
import { User } from '../users/user.entity';
|
import { User } from '../users/user.entity';
|
||||||
|
import { UsersModule } from '../users/users.module';
|
||||||
|
import { Identity } from '../users/identity.entity';
|
||||||
|
|
||||||
describe('AuthService', () => {
|
describe('AuthService', () => {
|
||||||
let service: AuthService;
|
let service: AuthService;
|
||||||
|
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
const module: TestingModule = await Test.createTestingModule({
|
const module: TestingModule = await Test.createTestingModule({
|
||||||
providers: [
|
providers: [AuthService],
|
||||||
AuthService,
|
imports: [PassportModule, UsersModule],
|
||||||
{
|
|
||||||
provide: getRepositoryToken(User),
|
|
||||||
useValue: {},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
imports: [UsersModule],
|
|
||||||
})
|
})
|
||||||
|
.overrideProvider(getRepositoryToken(AuthToken))
|
||||||
|
.useValue({})
|
||||||
|
.overrideProvider(getRepositoryToken(Identity))
|
||||||
|
.useValue({})
|
||||||
.overrideProvider(getRepositoryToken(User))
|
.overrideProvider(getRepositoryToken(User))
|
||||||
.useValue({})
|
.useValue({})
|
||||||
.compile();
|
.compile();
|
||||||
|
|
18
src/auth/mock-auth.guard.ts
Normal file
18
src/auth/mock-auth.guard.ts
Normal file
|
@ -0,0 +1,18 @@
|
||||||
|
/*
|
||||||
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only
|
||||||
|
*/
|
||||||
|
|
||||||
|
import { ExecutionContext, Injectable } from '@nestjs/common';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class MockAuthGuard {
|
||||||
|
canActivate(context: ExecutionContext) {
|
||||||
|
const req = context.switchToHttp().getRequest();
|
||||||
|
req.user = {
|
||||||
|
userName: 'hardcoded',
|
||||||
|
};
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
|
@ -21,6 +21,9 @@ 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 { 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';
|
||||||
|
|
||||||
describe('Notes', () => {
|
describe('Notes', () => {
|
||||||
let app: NestExpressApplication;
|
let app: NestExpressApplication;
|
||||||
|
@ -46,8 +49,12 @@ describe('Notes', () => {
|
||||||
PermissionsModule,
|
PermissionsModule,
|
||||||
GroupsModule,
|
GroupsModule,
|
||||||
LoggerModule,
|
LoggerModule,
|
||||||
|
AuthModule,
|
||||||
],
|
],
|
||||||
}).compile();
|
})
|
||||||
|
.overrideGuard(TokenAuthGuard)
|
||||||
|
.useClass(MockAuthGuard)
|
||||||
|
.compile();
|
||||||
app = moduleRef.createNestApplication<NestExpressApplication>();
|
app = moduleRef.createNestApplication<NestExpressApplication>();
|
||||||
app.useStaticAssets('uploads', {
|
app.useStaticAssets('uploads', {
|
||||||
prefix: '/uploads',
|
prefix: '/uploads',
|
||||||
|
|
|
@ -17,6 +17,10 @@ import { LoggerModule } from '../../src/logger/logger.module';
|
||||||
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 { 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';
|
||||||
|
|
||||||
describe('Notes', () => {
|
describe('Notes', () => {
|
||||||
let app: INestApplication;
|
let app: INestApplication;
|
||||||
|
@ -41,12 +45,18 @@ describe('Notes', () => {
|
||||||
dropSchema: true,
|
dropSchema: true,
|
||||||
}),
|
}),
|
||||||
LoggerModule,
|
LoggerModule,
|
||||||
|
AuthModule,
|
||||||
],
|
],
|
||||||
}).compile();
|
})
|
||||||
|
.overrideGuard(TokenAuthGuard)
|
||||||
|
.useClass(MockAuthGuard)
|
||||||
|
.compile();
|
||||||
|
|
||||||
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 () => {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue