Migrate private 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 22:01:18 +02:00
parent 60bbe42447
commit 6ee9589b2c
No known key found for this signature in database
GPG key ID: 185982BA4C42B7C3

View file

@ -3,48 +3,20 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
/* eslint-disable
@typescript-eslint/no-unsafe-assignment,
@typescript-eslint/no-unsafe-member-access
*/
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 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 { NotInDBError } from '../../src/errors/errors'; import { NotInDBError } from '../../src/errors/errors';
import { GroupsModule } from '../../src/groups/groups.module';
import { HistoryModule } from '../../src/history/history.module';
import { IdentityService } from '../../src/identity/identity.service';
import { LoggerModule } from '../../src/logger/logger.module';
import { MediaModule } from '../../src/media/media.module';
import { MediaService } from '../../src/media/media.service';
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 { PermissionsModule } from '../../src/permissions/permissions.module';
import { UserInfoDto } from '../../src/users/user-info.dto'; import { UserInfoDto } from '../../src/users/user-info.dto';
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 { setupSessionMiddleware } from '../../src/utils/session'; import { setupSessionMiddleware } from '../../src/utils/session';
import { TestSetup } from '../test-setup';
describe('Me', () => { describe('Me', () => {
let app: INestApplication; let testSetup: TestSetup;
let userService: UsersService;
let mediaService: MediaService;
let identityService: IdentityService;
let uploadPath: string; let uploadPath: string;
let user: User; let user: User;
let content: string; let content: string;
@ -54,55 +26,23 @@ describe('Me', () => {
let agent: request.SuperAgentTest; let agent: request.SuperAgentTest;
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: [
appConfigMock, const authConfig = testSetup.configService.get('authConfig') as AuthConfig;
authConfigMock, setupSessionMiddleware(testSetup.app, authConfig);
mediaConfigMock, await testSetup.app.init();
customizationConfigMock,
externalServicesConfigMock, user = await testSetup.userService.createUser('hardcoded', 'Testy');
], await testSetup.identityService.createLocalIdentity(user, 'test');
}),
PrivateApiModule,
NotesModule,
PermissionsModule,
GroupsModule,
TypeOrmModule.forRoot({
type: 'sqlite',
database: './hedgedoc-e2e-private-me.sqlite',
autoLoadEntities: true,
synchronize: true,
dropSchema: true,
}),
LoggerModule,
AuthModule,
UsersModule,
MediaModule,
HistoryModule,
],
}).compile();
const config = moduleRef.get<ConfigService>(ConfigService);
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
app = moduleRef.createNestApplication();
const authConfig = config.get('authConfig') as AuthConfig;
setupSessionMiddleware(app, authConfig);
await app.init();
//historyService = moduleRef.get();
userService = moduleRef.get(UsersService);
mediaService = moduleRef.get(MediaService);
identityService = moduleRef.get(IdentityService);
user = await userService.createUser('hardcoded', 'Testy');
await identityService.createLocalIdentity(user, 'test');
const notesService = moduleRef.get(NotesService);
content = 'This is a test note.'; content = 'This is a test note.';
alias2 = 'note2'; alias2 = 'note2';
note1 = await notesService.createNote(content, undefined, user); note1 = await testSetup.notesService.createNote(content, undefined, user);
note2 = await notesService.createNote(content, alias2, user); note2 = await testSetup.notesService.createNote(content, alias2, 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' })
@ -110,7 +50,7 @@ describe('Me', () => {
}); });
it('GET /me', async () => { it('GET /me', async () => {
const userInfo = userService.toUserDto(user); const userInfo = testSetup.userService.toUserDto(user);
const response = await agent const response = await agent
.get('/me') .get('/me')
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -127,10 +67,10 @@ describe('Me', () => {
expect(responseBefore.body).toHaveLength(0); expect(responseBefore.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 agent const response = await agent
.get('/me/media/') .get('/me/media/')
@ -141,9 +81,9 @@ describe('Me', () => {
expect(response.body[1].url).toEqual(url1); expect(response.body[1].url).toEqual(url1);
expect(response.body[2].url).toEqual(url2); expect(response.body[2].url).toEqual(url2);
expect(response.body[3].url).toEqual(url3); expect(response.body[3].url).toEqual(url3);
const mediaUploads = await mediaService.listUploadsByUser(user); const mediaUploads = await testSetup.mediaService.listUploadsByUser(user);
for (const upload of mediaUploads) { for (const upload of mediaUploads) {
await mediaService.deleteFile(upload); await testSetup.mediaService.deleteFile(upload);
} }
await fs.rmdir(uploadPath); await fs.rmdir(uploadPath);
}); });
@ -157,23 +97,25 @@ describe('Me', () => {
name: newDisplayName, name: newDisplayName,
}) })
.expect(200); .expect(200);
const dbUser = await userService.getUserByUsername('hardcoded'); const dbUser = await testSetup.userService.getUserByUsername('hardcoded');
expect(dbUser.displayName).toEqual(newDisplayName); expect(dbUser.displayName).toEqual(newDisplayName);
}); });
it('DELETE /me', async () => { it('DELETE /me', async () => {
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 dbUser = await userService.getUserByUsername('hardcoded'); const dbUser = await testSetup.userService.getUserByUsername('hardcoded');
expect(dbUser).toBeInstanceOf(User); expect(dbUser).toBeInstanceOf(User);
const mediaUploads = await mediaService.listUploadsByUser(dbUser); const mediaUploads = await testSetup.mediaService.listUploadsByUser(dbUser);
expect(mediaUploads).toHaveLength(1); expect(mediaUploads).toHaveLength(1);
expect(mediaUploads[0].fileUrl).toEqual(url0); expect(mediaUploads[0].fileUrl).toEqual(url0);
await agent.delete('/me').expect(204); await agent.delete('/me').expect(204);
await expect(userService.getUserByUsername('hardcoded')).rejects.toThrow( await expect(
NotInDBError, testSetup.userService.getUserByUsername('hardcoded'),
).rejects.toThrow(NotInDBError);
const mediaUploadsAfter = await testSetup.mediaService.listUploadsByNote(
note1,
); );
const mediaUploadsAfter = await mediaService.listUploadsByNote(note1);
expect(mediaUploadsAfter).toHaveLength(0); expect(mediaUploadsAfter).toHaveLength(0);
}); });
}); });