2021-03-20 18:01:04 +00:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
|
|
|
*
|
|
|
|
* 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';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { promises as fs } from 'fs';
|
2021-04-29 20:47:18 +00:00
|
|
|
import request from 'supertest';
|
2021-08-29 16:45:46 +00:00
|
|
|
|
|
|
|
import { PrivateApiModule } from '../../src/api/private/private-api.module';
|
|
|
|
import { AuthModule } from '../../src/auth/auth.module';
|
2021-10-13 18:43:56 +00:00
|
|
|
import { AuthConfig } from '../../src/config/auth.config';
|
2021-03-20 18:01:04 +00:00
|
|
|
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';
|
2021-08-29 16:45:46 +00:00
|
|
|
import mediaConfigMock from '../../src/config/mock/media.config.mock';
|
|
|
|
import { NotInDBError } from '../../src/errors/errors';
|
2021-03-20 18:01:04 +00:00
|
|
|
import { GroupsModule } from '../../src/groups/groups.module';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { HistoryModule } from '../../src/history/history.module';
|
2021-10-13 18:43:56 +00:00
|
|
|
import { IdentityService } from '../../src/identity/identity.service';
|
2021-03-20 18:01:04 +00:00
|
|
|
import { LoggerModule } from '../../src/logger/logger.module';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { MediaModule } from '../../src/media/media.module';
|
|
|
|
import { MediaService } from '../../src/media/media.service';
|
|
|
|
import { Note } from '../../src/notes/note.entity';
|
2021-03-20 18:01:04 +00:00
|
|
|
import { NotesModule } from '../../src/notes/notes.module';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { NotesService } from '../../src/notes/notes.service';
|
2021-03-20 18:01:04 +00:00
|
|
|
import { PermissionsModule } from '../../src/permissions/permissions.module';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { UserInfoDto } from '../../src/users/user-info.dto';
|
2021-03-20 18:01:04 +00:00
|
|
|
import { User } from '../../src/users/user.entity';
|
|
|
|
import { UsersModule } from '../../src/users/users.module';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { UsersService } from '../../src/users/users.service';
|
2021-10-13 18:43:56 +00:00
|
|
|
import { setupSessionMiddleware } from '../../src/utils/session';
|
2021-03-20 18:01:04 +00:00
|
|
|
|
|
|
|
describe('Me', () => {
|
|
|
|
let app: INestApplication;
|
|
|
|
let userService: UsersService;
|
|
|
|
let mediaService: MediaService;
|
2021-10-13 18:43:56 +00:00
|
|
|
let identityService: IdentityService;
|
2021-03-20 18:01:04 +00:00
|
|
|
let uploadPath: string;
|
|
|
|
let user: User;
|
|
|
|
let content: string;
|
|
|
|
let note1: Note;
|
2021-06-06 15:55:41 +00:00
|
|
|
let alias2: string;
|
2021-03-20 18:01:04 +00:00
|
|
|
let note2: Note;
|
2021-10-13 18:43:56 +00:00
|
|
|
let agent: request.SuperAgentTest;
|
2021-03-20 18:01:04 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const moduleRef = await Test.createTestingModule({
|
|
|
|
imports: [
|
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
|
|
|
load: [
|
|
|
|
appConfigMock,
|
|
|
|
authConfigMock,
|
|
|
|
mediaConfigMock,
|
|
|
|
customizationConfigMock,
|
|
|
|
externalServicesConfigMock,
|
|
|
|
],
|
|
|
|
}),
|
|
|
|
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();
|
2021-10-13 18:43:56 +00:00
|
|
|
const authConfig = config.get('authConfig') as AuthConfig;
|
|
|
|
setupSessionMiddleware(app, authConfig);
|
2021-03-20 18:01:04 +00:00
|
|
|
await app.init();
|
|
|
|
//historyService = moduleRef.get();
|
|
|
|
userService = moduleRef.get(UsersService);
|
|
|
|
mediaService = moduleRef.get(MediaService);
|
2021-10-13 18:43:56 +00:00
|
|
|
identityService = moduleRef.get(IdentityService);
|
2021-03-20 18:01:04 +00:00
|
|
|
user = await userService.createUser('hardcoded', 'Testy');
|
2021-10-13 18:43:56 +00:00
|
|
|
await identityService.createLocalIdentity(user, 'test');
|
|
|
|
|
2021-03-20 18:01:04 +00:00
|
|
|
const notesService = moduleRef.get(NotesService);
|
|
|
|
content = 'This is a test note.';
|
2021-06-06 15:55:41 +00:00
|
|
|
alias2 = 'note2';
|
|
|
|
note1 = await notesService.createNote(content, undefined, user);
|
|
|
|
note2 = await notesService.createNote(content, alias2, user);
|
2021-10-13 18:43:56 +00:00
|
|
|
agent = request.agent(app.getHttpServer());
|
|
|
|
await agent
|
|
|
|
.post('/auth/local/login')
|
|
|
|
.send({ username: 'hardcoded', password: 'test' })
|
|
|
|
.expect(201);
|
2021-03-20 18:01:04 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('GET /me', async () => {
|
|
|
|
const userInfo = userService.toUserDto(user);
|
2021-10-13 18:43:56 +00:00
|
|
|
const response = await agent
|
2021-03-20 18:01:04 +00:00
|
|
|
.get('/me')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
const gotUser = response.body as UserInfoDto;
|
|
|
|
expect(gotUser).toEqual(userInfo);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('GET /me/media', async () => {
|
2021-10-13 18:43:56 +00:00
|
|
|
const responseBefore = await agent
|
2021-03-20 18:01:04 +00:00
|
|
|
.get('/me/media/')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(responseBefore.body).toHaveLength(0);
|
|
|
|
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2021-08-29 20:28:21 +00:00
|
|
|
const url0 = await mediaService.saveFile(testImage, user, note1);
|
|
|
|
const url1 = await mediaService.saveFile(testImage, user, note1);
|
|
|
|
const url2 = await mediaService.saveFile(testImage, user, note2);
|
|
|
|
const url3 = await mediaService.saveFile(testImage, user, note2);
|
2021-03-20 18:01:04 +00:00
|
|
|
|
2021-10-13 18:43:56 +00:00
|
|
|
const response = await agent
|
2021-03-20 18:01:04 +00:00
|
|
|
.get('/me/media/')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveLength(4);
|
|
|
|
expect(response.body[0].url).toEqual(url0);
|
|
|
|
expect(response.body[1].url).toEqual(url1);
|
|
|
|
expect(response.body[2].url).toEqual(url2);
|
|
|
|
expect(response.body[3].url).toEqual(url3);
|
|
|
|
const mediaUploads = await mediaService.listUploadsByUser(user);
|
|
|
|
for (const upload of mediaUploads) {
|
|
|
|
await mediaService.deleteFile(upload);
|
|
|
|
}
|
|
|
|
await fs.rmdir(uploadPath);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('POST /me/profile', async () => {
|
|
|
|
const newDisplayName = 'Another name';
|
|
|
|
expect(user.displayName).not.toEqual(newDisplayName);
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-03-20 18:01:04 +00:00
|
|
|
.post('/me/profile')
|
|
|
|
.send({
|
|
|
|
name: newDisplayName,
|
|
|
|
})
|
|
|
|
.expect(200);
|
|
|
|
const dbUser = await userService.getUserByUsername('hardcoded');
|
|
|
|
expect(dbUser.displayName).toEqual(newDisplayName);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('DELETE /me', async () => {
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2021-08-29 20:28:21 +00:00
|
|
|
const url0 = await mediaService.saveFile(testImage, user, note1);
|
2021-03-20 18:01:04 +00:00
|
|
|
const dbUser = await userService.getUserByUsername('hardcoded');
|
|
|
|
expect(dbUser).toBeInstanceOf(User);
|
|
|
|
const mediaUploads = await mediaService.listUploadsByUser(dbUser);
|
|
|
|
expect(mediaUploads).toHaveLength(1);
|
|
|
|
expect(mediaUploads[0].fileUrl).toEqual(url0);
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent.delete('/me').expect(204);
|
2021-03-20 18:01:04 +00:00
|
|
|
await expect(userService.getUserByUsername('hardcoded')).rejects.toThrow(
|
|
|
|
NotInDBError,
|
|
|
|
);
|
|
|
|
const mediaUploadsAfter = await mediaService.listUploadsByNote(note1);
|
|
|
|
expect(mediaUploadsAfter).toHaveLength(0);
|
|
|
|
});
|
|
|
|
});
|