Migrate public me API E2E test to global TestSetup

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-10-14 21:17:42 +02:00
parent 5b2b2e9a44
commit cddd28f082
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3
2 changed files with 51 additions and 98 deletions

View file

@ -3,89 +3,37 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { INestApplication } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import { promises as fs } from 'fs'; import { promises as fs } from 'fs';
import { join } from 'path'; import { join } from 'path';
import request from 'supertest'; import request from 'supertest';
import { PublicApiModule } from '../../src/api/public/public-api.module';
import { AuthModule } from '../../src/auth/auth.module';
import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
import { TokenAuthGuard } from '../../src/auth/token.strategy';
import appConfigMock from '../../src/config/mock/app.config.mock';
import mediaConfigMock from '../../src/config/mock/media.config.mock';
import { GroupsModule } from '../../src/groups/groups.module';
import { HistoryEntryUpdateDto } from '../../src/history/history-entry-update.dto'; import { HistoryEntryUpdateDto } from '../../src/history/history-entry-update.dto';
import { HistoryEntryDto } from '../../src/history/history-entry.dto'; import { HistoryEntryDto } from '../../src/history/history-entry.dto';
import { HistoryModule } from '../../src/history/history.module';
import { HistoryService } from '../../src/history/history.service';
import { LoggerModule } from '../../src/logger/logger.module';
import { MediaModule } from '../../src/media/media.module';
import { MediaService } from '../../src/media/media.service';
import { NoteMetadataDto } from '../../src/notes/note-metadata.dto'; import { NoteMetadataDto } from '../../src/notes/note-metadata.dto';
import { NotesModule } from '../../src/notes/notes.module';
import { NotesService } from '../../src/notes/notes.service';
import { PermissionsModule } from '../../src/permissions/permissions.module';
import { User } from '../../src/users/user.entity'; import { User } from '../../src/users/user.entity';
import { UsersModule } from '../../src/users/users.module'; import { TestSetup } from '../test-setup';
import { UsersService } from '../../src/users/users.service';
// TODO Tests have to be reworked using UserService functions // TODO Tests have to be reworked using UserService functions
describe('Me', () => { describe('Me', () => {
let app: INestApplication; let testSetup: TestSetup;
let historyService: HistoryService;
let notesService: NotesService;
let userService: UsersService;
let mediaService: MediaService;
let uploadPath: string; let uploadPath: string;
let user: User; let user: User;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ testSetup = await TestSetup.create();
imports: [
ConfigModule.forRoot({ uploadPath =
isGlobal: true, testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
load: [mediaConfigMock, appConfigMock],
}), user = await testSetup.userService.createUser('hardcoded', 'Testy');
PublicApiModule, await testSetup.app.init();
NotesModule,
PermissionsModule,
GroupsModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: './hedgedoc-e2e-me.sqlite',
autoLoadEntities: true,
synchronize: true,
dropSchema: true,
}),
LoggerModule,
AuthModule,
UsersModule,
HistoryModule,
MediaModule,
],
})
.overrideGuard(TokenAuthGuard)
.useClass(MockAuthGuard)
.compile();
const config = moduleRef.get<ConfigService>(ConfigService);
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
app = moduleRef.createNestApplication();
notesService = moduleRef.get(NotesService);
historyService = moduleRef.get(HistoryService);
userService = moduleRef.get(UsersService);
mediaService = moduleRef.get(MediaService);
user = await userService.createUser('hardcoded', 'Testy');
await app.init();
}); });
it(`GET /me`, async () => { it(`GET /me`, async () => {
const userInfo = userService.toUserDto(user); const userInfo = testSetup.userService.toUserDto(user);
const response = await request(app.getHttpServer()) const response = await request(testSetup.app.getHttpServer())
.get('/me') .get('/me')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
@ -94,16 +42,17 @@ describe('Me', () => {
it(`GET /me/history`, async () => { it(`GET /me/history`, async () => {
const noteName = 'testGetNoteHistory1'; const noteName = 'testGetNoteHistory1';
const note = await notesService.createNote('', noteName); const note = await testSetup.notesService.createNote('', noteName);
const createdHistoryEntry = const createdHistoryEntry =
await historyService.updateHistoryEntryTimestamp(note, user); await testSetup.historyService.updateHistoryEntryTimestamp(note, user);
const response = await request(app.getHttpServer()) const response = await request(testSetup.app.getHttpServer())
.get('/me/history') .get('/me/history')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
const history: HistoryEntryDto[] = response.body; const history: HistoryEntryDto[] = response.body;
expect(history.length).toEqual(1); expect(history.length).toEqual(1);
const historyDto = historyService.toHistoryEntryDto(createdHistoryEntry); const historyDto =
testSetup.historyService.toHistoryEntryDto(createdHistoryEntry);
for (const historyEntry of history) { for (const historyEntry of history) {
expect(historyEntry.identifier).toEqual(historyDto.identifier); expect(historyEntry.identifier).toEqual(historyDto.identifier);
expect(historyEntry.title).toEqual(historyDto.title); expect(historyEntry.title).toEqual(historyDto.title);
@ -118,16 +67,16 @@ describe('Me', () => {
describe(`GET /me/history/{note}`, () => { describe(`GET /me/history/{note}`, () => {
it('works with an existing note', async () => { it('works with an existing note', async () => {
const noteName = 'testGetNoteHistory2'; const noteName = 'testGetNoteHistory2';
const note = await notesService.createNote('', noteName); const note = await testSetup.notesService.createNote('', noteName);
const createdHistoryEntry = const createdHistoryEntry =
await historyService.updateHistoryEntryTimestamp(note, user); await testSetup.historyService.updateHistoryEntryTimestamp(note, user);
const response = await request(app.getHttpServer()) const response = await request(testSetup.app.getHttpServer())
.get(`/me/history/${noteName}`) .get(`/me/history/${noteName}`)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
const historyEntry: HistoryEntryDto = response.body; const historyEntry: HistoryEntryDto = response.body;
const historyEntryDto = const historyEntryDto =
historyService.toHistoryEntryDto(createdHistoryEntry); testSetup.historyService.toHistoryEntryDto(createdHistoryEntry);
expect(historyEntry.identifier).toEqual(historyEntryDto.identifier); expect(historyEntry.identifier).toEqual(historyEntryDto.identifier);
expect(historyEntry.title).toEqual(historyEntryDto.title); expect(historyEntry.title).toEqual(historyEntryDto.title);
expect(historyEntry.tags).toEqual(historyEntryDto.tags); expect(historyEntry.tags).toEqual(historyEntryDto.tags);
@ -137,7 +86,7 @@ describe('Me', () => {
); );
}); });
it('fails with a non-existing note', async () => { it('fails with a non-existing note', async () => {
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.get('/me/history/i_dont_exist') .get('/me/history/i_dont_exist')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404); .expect(404);
@ -147,27 +96,27 @@ describe('Me', () => {
describe(`PUT /me/history/{note}`, () => { describe(`PUT /me/history/{note}`, () => {
it('works', async () => { it('works', async () => {
const noteName = 'testGetNoteHistory3'; const noteName = 'testGetNoteHistory3';
const note = await notesService.createNote('', noteName); const note = await testSetup.notesService.createNote('', noteName);
await historyService.updateHistoryEntryTimestamp(note, user); await testSetup.historyService.updateHistoryEntryTimestamp(note, user);
const historyEntryUpdateDto = new HistoryEntryUpdateDto(); const historyEntryUpdateDto = new HistoryEntryUpdateDto();
historyEntryUpdateDto.pinStatus = true; historyEntryUpdateDto.pinStatus = true;
const response = await request(app.getHttpServer()) const response = await request(testSetup.app.getHttpServer())
.put('/me/history/' + noteName) .put('/me/history/' + noteName)
.send(historyEntryUpdateDto) .send(historyEntryUpdateDto)
.expect(200); .expect(200);
const history = await historyService.getEntriesByUser(user); const history = await testSetup.historyService.getEntriesByUser(user);
const historyEntry: HistoryEntryDto = response.body; const historyEntry: HistoryEntryDto = response.body;
expect(historyEntry.pinStatus).toEqual(true); expect(historyEntry.pinStatus).toEqual(true);
let theEntry: HistoryEntryDto; let theEntry: HistoryEntryDto;
for (const entry of history) { for (const entry of history) {
if (entry.note.aliases.find((element) => element.name === noteName)) { if (entry.note.aliases.find((element) => element.name === noteName)) {
theEntry = historyService.toHistoryEntryDto(entry); theEntry = testSetup.historyService.toHistoryEntryDto(entry);
} }
} }
expect(theEntry.pinStatus).toEqual(true); expect(theEntry.pinStatus).toEqual(true);
}); });
it('fails with a non-existing note', async () => { it('fails with a non-existing note', async () => {
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.put('/me/history/i_dont_exist') .put('/me/history/i_dont_exist')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404); .expect(404);
@ -177,13 +126,13 @@ describe('Me', () => {
describe(`DELETE /me/history/{note}`, () => { describe(`DELETE /me/history/{note}`, () => {
it('works', async () => { it('works', async () => {
const noteName = 'testGetNoteHistory4'; const noteName = 'testGetNoteHistory4';
const note = await notesService.createNote('', noteName); const note = await testSetup.notesService.createNote('', noteName);
await historyService.updateHistoryEntryTimestamp(note, user); await testSetup.historyService.updateHistoryEntryTimestamp(note, user);
const response = await request(app.getHttpServer()) const response = await request(testSetup.app.getHttpServer())
.delete(`/me/history/${noteName}`) .delete(`/me/history/${noteName}`)
.expect(204); .expect(204);
expect(response.body).toEqual({}); expect(response.body).toEqual({});
const history = await historyService.getEntriesByUser(user); const history = await testSetup.historyService.getEntriesByUser(user);
for (const entry of history) { for (const entry of history) {
if (entry.note.aliases.find((element) => element.name === noteName)) { if (entry.note.aliases.find((element) => element.name === noteName)) {
throw new Error('Deleted history entry still in history'); throw new Error('Deleted history entry still in history');
@ -192,14 +141,14 @@ describe('Me', () => {
}); });
describe('fails', () => { describe('fails', () => {
it('with a non-existing note', async () => { it('with a non-existing note', async () => {
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.delete('/me/history/i_dont_exist') .delete('/me/history/i_dont_exist')
.expect(404); .expect(404);
}); });
it('with a non-existing history entry', async () => { it('with a non-existing history entry', async () => {
const noteName = 'testGetNoteHistory5'; const noteName = 'testGetNoteHistory5';
await notesService.createNote('', noteName); await testSetup.notesService.createNote('', noteName);
await request(app.getHttpServer()) await request(testSetup.app.getHttpServer())
.delete(`/me/history/${noteName}`) .delete(`/me/history/${noteName}`)
.expect(404); .expect(404);
}); });
@ -208,8 +157,8 @@ describe('Me', () => {
it(`GET /me/notes/`, async () => { it(`GET /me/notes/`, async () => {
const noteName = 'testNote'; const noteName = 'testNote';
await notesService.createNote('', noteName, user); await testSetup.notesService.createNote('', noteName, user);
const response = await request(app.getHttpServer()) const response = await request(testSetup.app.getHttpServer())
.get('/me/notes/') .get('/me/notes/')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
@ -220,17 +169,17 @@ describe('Me', () => {
}); });
it('GET /me/media', async () => { it('GET /me/media', async () => {
const note1 = await notesService.createNote( const note1 = await testSetup.notesService.createNote(
'This is a test note.', 'This is a test note.',
'test8', 'test8',
await userService.getUserByUsername('hardcoded'), await testSetup.userService.getUserByUsername('hardcoded'),
); );
const note2 = await notesService.createNote( const note2 = await testSetup.notesService.createNote(
'This is a test note.', 'This is a test note.',
'test9', 'test9',
await userService.getUserByUsername('hardcoded'), await testSetup.userService.getUserByUsername('hardcoded'),
); );
const httpServer = app.getHttpServer(); const httpServer = testSetup.app.getHttpServer();
const response1 = await request(httpServer) const response1 = await request(httpServer)
.get('/me/media/') .get('/me/media/')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -238,10 +187,10 @@ describe('Me', () => {
expect(response1.body).toHaveLength(0); expect(response1.body).toHaveLength(0);
const testImage = await fs.readFile('test/public-api/fixtures/test.png'); const testImage = await fs.readFile('test/public-api/fixtures/test.png');
const url0 = await mediaService.saveFile(testImage, user, note1); const url0 = await testSetup.mediaService.saveFile(testImage, user, note1);
const url1 = await mediaService.saveFile(testImage, user, note1); const url1 = await testSetup.mediaService.saveFile(testImage, user, note1);
const url2 = await mediaService.saveFile(testImage, user, note2); const url2 = await testSetup.mediaService.saveFile(testImage, user, note2);
const url3 = await mediaService.saveFile(testImage, user, note2); const url3 = await testSetup.mediaService.saveFile(testImage, user, note2);
const response = await request(httpServer) const response = await request(httpServer)
.get('/me/media/') .get('/me/media/')
@ -261,6 +210,6 @@ describe('Me', () => {
}); });
afterAll(async () => { afterAll(async () => {
await app.close(); await testSetup.app.close();
}); });
}); });

View file

@ -20,6 +20,7 @@ import externalServicesConfigMock from '../src/config/mock/external-services.con
import mediaConfigMock from '../src/config/mock/media.config.mock'; import mediaConfigMock from '../src/config/mock/media.config.mock';
import { GroupsModule } from '../src/groups/groups.module'; import { GroupsModule } from '../src/groups/groups.module';
import { HistoryModule } from '../src/history/history.module'; import { HistoryModule } from '../src/history/history.module';
import { HistoryService } from '../src/history/history.service';
import { IdentityService } from '../src/identity/identity.service'; import { IdentityService } from '../src/identity/identity.service';
import { LoggerModule } from '../src/logger/logger.module'; import { LoggerModule } from '../src/logger/logger.module';
import { MediaModule } from '../src/media/media.module'; import { MediaModule } from '../src/media/media.module';
@ -39,6 +40,7 @@ export class TestSetup {
identityService: IdentityService; identityService: IdentityService;
notesService: NotesService; notesService: NotesService;
mediaService: MediaService; mediaService: MediaService;
historyService: HistoryService;
public static async create(): Promise<TestSetup> { public static async create(): Promise<TestSetup> {
const testSetup = new TestSetup(); const testSetup = new TestSetup();
@ -87,6 +89,8 @@ export class TestSetup {
testSetup.moduleRef.get<NotesService>(NotesService); testSetup.moduleRef.get<NotesService>(NotesService);
testSetup.mediaService = testSetup.mediaService =
testSetup.moduleRef.get<MediaService>(MediaService); testSetup.moduleRef.get<MediaService>(MediaService);
testSetup.historyService =
testSetup.moduleRef.get<HistoryService>(HistoryService);
testSetup.app = testSetup.moduleRef.createNestApplication(); testSetup.app = testSetup.moduleRef.createNestApplication();