Migrate private history API E2E test to global TestSetup

Signed-off-by: David Mehren <git@herrmehren.de>
This commit is contained in:
David Mehren 2021-10-14 22:44:30 +02:00
parent 209a9b93a6
commit 0bb333ca69
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -3,37 +3,23 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { INestApplication } from '@nestjs/common'; import { ConfigService } from '@nestjs/config';
import { ConfigModule, ConfigService } from '@nestjs/config';
import { Test } from '@nestjs/testing';
import { TypeOrmModule } from '@nestjs/typeorm';
import request from 'supertest'; import request from 'supertest';
import { PrivateApiModule } from '../../src/api/private/private-api.module';
import { AuthModule } from '../../src/auth/auth.module';
import { AuthConfig } from '../../src/config/auth.config'; import { AuthConfig } from '../../src/config/auth.config';
import appConfigMock from '../../src/config/mock/app.config.mock';
import authConfigMock from '../../src/config/mock/auth.config.mock';
import customizationConfigMock from '../../src/config/mock/customization.config.mock';
import externalServicesConfigMock from '../../src/config/mock/external-services.config.mock';
import mediaConfigMock from '../../src/config/mock/media.config.mock';
import { GroupsModule } from '../../src/groups/groups.module';
import { HistoryEntryImportDto } from '../../src/history/history-entry-import.dto'; import { HistoryEntryImportDto } from '../../src/history/history-entry-import.dto';
import { HistoryEntry } from '../../src/history/history-entry.entity'; import { HistoryEntry } from '../../src/history/history-entry.entity';
import { HistoryService } from '../../src/history/history.service'; 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 { Note } from '../../src/notes/note.entity'; import { Note } from '../../src/notes/note.entity';
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 { User } from '../../src/users/user.entity'; import { User } from '../../src/users/user.entity';
import { UsersModule } from '../../src/users/users.module';
import { UsersService } from '../../src/users/users.service'; import { UsersService } from '../../src/users/users.service';
import { setupSessionMiddleware } from '../../src/utils/session'; import { setupSessionMiddleware } from '../../src/utils/session';
import { TestSetup } from '../test-setup';
describe('History', () => { describe('History', () => {
let app: INestApplication; let testSetup: TestSetup;
let historyService: HistoryService; let historyService: HistoryService;
let identityService: IdentityService; let identityService: IdentityService;
let user: User; let user: User;
@ -44,41 +30,19 @@ describe('History', () => {
let agent: request.SuperAgentTest; let agent: request.SuperAgentTest;
beforeAll(async () => { beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ testSetup = await TestSetup.create();
imports: [
ConfigModule.forRoot({ forbiddenNoteId =
isGlobal: true, testSetup.configService.get('appConfig').forbiddenNoteIds[0];
load: [
appConfigMock, const moduleRef = testSetup.moduleRef;
mediaConfigMock,
authConfigMock,
customizationConfigMock,
externalServicesConfigMock,
],
}),
PrivateApiModule,
NotesModule,
PermissionsModule,
GroupsModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: './hedgedoc-e2e-private-history.sqlite',
autoLoadEntities: true,
synchronize: true,
dropSchema: true,
}),
LoggerModule,
AuthModule,
UsersModule,
],
}).compile();
const config = moduleRef.get<ConfigService>(ConfigService); const config = moduleRef.get<ConfigService>(ConfigService);
forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0]; forbiddenNoteId = config.get('appConfig').forbiddenNoteIds[0];
app = moduleRef.createNestApplication();
const authConfig = config.get('authConfig') as AuthConfig; const authConfig = config.get('authConfig') as AuthConfig;
setupSessionMiddleware(app, authConfig); setupSessionMiddleware(testSetup.app, authConfig);
await app.init(); await testSetup.app.init();
content = 'This is a test note.'; content = 'This is a test note.';
historyService = moduleRef.get(HistoryService); historyService = moduleRef.get(HistoryService);
const userService = moduleRef.get(UsersService); const userService = moduleRef.get(UsersService);
@ -88,7 +52,7 @@ describe('History', () => {
const notesService = moduleRef.get(NotesService); const notesService = moduleRef.get(NotesService);
note = await notesService.createNote(content, 'note', user); note = await notesService.createNote(content, 'note', user);
note2 = await notesService.createNote(content, 'note2', user); note2 = await notesService.createNote(content, 'note2', user);
agent = request.agent(app.getHttpServer()); agent = request.agent(testSetup.app.getHttpServer());
await agent await agent
.post('/auth/local/login') .post('/auth/local/login')
.send({ username: 'hardcoded', password: 'test' }) .send({ username: 'hardcoded', password: 'test' })
@ -101,8 +65,11 @@ describe('History', () => {
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200); .expect(200);
expect(emptyResponse.body.length).toEqual(0); expect(emptyResponse.body.length).toEqual(0);
const entry = await historyService.updateHistoryEntryTimestamp(note, user); const entry = await testSetup.historyService.updateHistoryEntryTimestamp(
const entryDto = historyService.toHistoryEntryDto(entry); note,
user,
);
const entryDto = testSetup.historyService.toHistoryEntryDto(entry);
const response = await agent const response = await agent
.get('/me/history') .get('/me/history')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -119,7 +86,9 @@ describe('History', () => {
describe('POST /me/history', () => { describe('POST /me/history', () => {
it('works', async () => { it('works', async () => {
expect(await historyService.getEntriesByUser(user)).toHaveLength(1); expect(
await testSetup.historyService.getEntriesByUser(user),
).toHaveLength(1);
const pinStatus = true; const pinStatus = true;
const lastVisited = new Date('2020-12-01 12:23:34'); const lastVisited = new Date('2020-12-01 12:23:34');
const postEntryDto = new HistoryEntryImportDto(); const postEntryDto = new HistoryEntryImportDto();
@ -133,7 +102,7 @@ describe('History', () => {
.set('Content-Type', 'application/json') .set('Content-Type', 'application/json')
.send(JSON.stringify({ history: [postEntryDto] })) .send(JSON.stringify({ history: [postEntryDto] }))
.expect(201); .expect(201);
const userEntries = await historyService.getEntriesByUser(user); const userEntries = await testSetup.historyService.getEntriesByUser(user);
expect(userEntries.length).toEqual(1); expect(userEntries.length).toEqual(1);
expect(userEntries[0].note.aliases).toEqual(note2.aliases); expect(userEntries[0].note.aliases).toEqual(note2.aliases);
expect(userEntries[0].user.username).toEqual(user.username); expect(userEntries[0].user.username).toEqual(user.username);
@ -146,7 +115,9 @@ describe('History', () => {
let postEntryDto: HistoryEntryImportDto; let postEntryDto: HistoryEntryImportDto;
let prevEntry: HistoryEntry; let prevEntry: HistoryEntry;
beforeAll(async () => { beforeAll(async () => {
const previousHistory = await historyService.getEntriesByUser(user); const previousHistory = await testSetup.historyService.getEntriesByUser(
user,
);
expect(previousHistory).toHaveLength(1); expect(previousHistory).toHaveLength(1);
prevEntry = previousHistory[0]; prevEntry = previousHistory[0];
pinStatus = !previousHistory[0].pinStatus; pinStatus = !previousHistory[0].pinStatus;
@ -181,7 +152,9 @@ describe('History', () => {
.expect(400); .expect(400);
}); });
afterEach(async () => { afterEach(async () => {
const historyEntries = await historyService.getEntriesByUser(user); const historyEntries = await testSetup.historyService.getEntriesByUser(
user,
);
expect(historyEntries).toHaveLength(1); expect(historyEntries).toHaveLength(1);
expect(historyEntries[0].note.aliases).toEqual(prevEntry.note.aliases); expect(historyEntries[0].note.aliases).toEqual(prevEntry.note.aliases);
expect(historyEntries[0].user.username).toEqual( expect(historyEntries[0].user.username).toEqual(
@ -194,23 +167,30 @@ describe('History', () => {
}); });
it('DELETE /me/history', async () => { it('DELETE /me/history', async () => {
expect((await historyService.getEntriesByUser(user)).length).toEqual(1); expect(
(await testSetup.historyService.getEntriesByUser(user)).length,
).toEqual(1);
await agent.delete('/me/history').expect(200); await agent.delete('/me/history').expect(200);
expect((await historyService.getEntriesByUser(user)).length).toEqual(0); expect(
(await testSetup.historyService.getEntriesByUser(user)).length,
).toEqual(0);
}); });
it('PUT /me/history/:note', async () => { it('PUT /me/history/:note', async () => {
const entry = await historyService.updateHistoryEntryTimestamp(note2, user); const entry = await testSetup.historyService.updateHistoryEntryTimestamp(
note2,
user,
);
expect(entry.pinStatus).toBeFalsy(); expect(entry.pinStatus).toBeFalsy();
const alias = entry.note.aliases.filter((alias) => alias.primary)[0].name; const alias = entry.note.aliases.filter((alias) => alias.primary)[0].name;
await agent await agent
.put(`/me/history/${alias || 'undefined'}`) .put(`/me/history/${alias || 'undefined'}`)
.send({ pinStatus: true }) .send({ pinStatus: true })
.expect(200); .expect(200);
const userEntries = await historyService.getEntriesByUser(user); const userEntries = await testSetup.historyService.getEntriesByUser(user);
expect(userEntries.length).toEqual(1); expect(userEntries.length).toEqual(1);
expect(userEntries[0].pinStatus).toBeTruthy(); expect(userEntries[0].pinStatus).toBeTruthy();
await historyService.deleteHistoryEntry(note2, user); await testSetup.historyService.deleteHistoryEntry(note2, user);
}); });
it('DELETE /me/history/:note', async () => { it('DELETE /me/history/:note', async () => {
@ -230,6 +210,6 @@ describe('History', () => {
}); });
afterAll(async () => { afterAll(async () => {
await app.close(); await testSetup.app.close();
}); });
}); });