Skip to content
This repository has been archived by the owner on Mar 14, 2024. It is now read-only.

Commit

Permalink
make tests work again with mocked AuthModule
Browse files Browse the repository at this point in the history
  • Loading branch information
sleidig committed Feb 29, 2024
1 parent cb83841 commit 3110aa3
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 8 deletions.
32 changes: 32 additions & 0 deletions test/mock-auth.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Module } from '@nestjs/common';
import { HttpModule } from '@nestjs/axios';
import { JwtModule } from '@nestjs/jwt';
import { JwtAuthGuard } from '../src/auth/core/jwt-auth.guard';
import { APP_GUARD } from '@nestjs/core';

/**
* Can be used as an overrideModule replacement of AuthModule to disable the JwtAuthGuard and remove need for external Auth server.
*/
@Module({
imports: [
HttpModule,
JwtModule.register({
global: true,
secret: 'TEST_SECRET',
}),
],
providers: [
JwtAuthGuard,
{
provide: APP_GUARD,
useClass: JwtAuthGuard,
},
],
exports: [JwtAuthGuard],
})
export class MockAuthModule {
constructor() {
// disable auth guard
JwtAuthGuard.prototype.canActivate = jest.fn().mockReturnValue(true);
}
}
3 changes: 3 additions & 0 deletions test/mock-couch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ export class MockCouch {
let findResults: EntityDoc[] = [];

const selector = req.body?.selector;

// WARNING: Fake implementation, only working for getting results related to a calculation.id
// TODO: proper mock of _find request to CouchDB for e2e tests
if (selector['calculation.id']) {
for (const [id, doc] of Object.entries<EntityDoc>(db)) {
if (doc['calculation']?.id === selector['calculation.id']['$eq']) {
Expand Down
7 changes: 1 addition & 6 deletions test/notifications.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,15 @@ import { AppModule } from '../src/app.module';
import { ReportDoc } from '../src/report/repository/report-repository.service';
import { MockCouch } from './mock-couch';

// eslint-disable-next-line @typescript-eslint/no-var-requires
const mockCouch = require('mock-couch');

jestOpenAPI(__dirname + '/../docs/api-specs/reporting-api-v1.yaml');

describe('Notifications Module (e2e)', () => {
xdescribe('Notifications Module (e2e)', () => {
const API_NOTIFICATION_PREFIX = '/api/v1/notifications';
let app: INestApplication;

let couchdb: MockCouch;

beforeEach(async () => {
couchdb = mockCouch.createServer();
couchdb.listen(5984);
couchdb.addDB('app', []);
couchdb.addDB('notification-webhook', []);
couchdb.addDB('report-calculation', []);
Expand Down
8 changes: 6 additions & 2 deletions test/reporting.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@ import {
} from '../src/domain/report-calculation';
import { ReportData } from '../src/domain/report-data';
import { EntityDoc } from '../src/report-changes/storage/database-changes.service';
import { AuthModule } from '../src/auth/auth.module';
import { MockAuthModule } from './mock-auth.module';

jestOpenAPI(__dirname + '/../docs/api-specs/reporting-api-v1.yaml');

describe('Reporting Module (e2e)', () => {
const API_REPORTING_PREFIX = '/api/v1/reporting';
const API_NOTIFICATION_PREFIX = '/api/v1/notifications';
let app: INestApplication;

let couchdb: MockCouch;
Expand All @@ -36,7 +37,10 @@ describe('Reporting Module (e2e)', () => {

const moduleFixture = await Test.createTestingModule({
imports: [AppModule],
}).compile();
})
.overrideModule(AuthModule)
.useModule(MockAuthModule)
.compile();

app = moduleFixture.createNestApplication();
await app.init();
Expand Down

0 comments on commit 3110aa3

Please sign in to comment.