2021-01-05 16:12:38 -05:00
|
|
|
/*
|
2021-01-06 15:36:07 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 16:12:38 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2020-09-26 17:27:24 -04:00
|
|
|
import { INestApplication } from '@nestjs/common';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { ConfigModule, ConfigService } from '@nestjs/config';
|
2020-09-26 17:27:24 -04:00
|
|
|
import { Test } from '@nestjs/testing';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import { join } from 'path';
|
2021-04-29 16:47:18 -04:00
|
|
|
import request from 'supertest';
|
2021-08-29 12:45:46 -04:00
|
|
|
|
|
|
|
import { PublicApiModule } from '../../src/api/public/public-api.module';
|
|
|
|
import { AuthModule } from '../../src/auth/auth.module';
|
|
|
|
import { MockAuthGuard } from '../../src/auth/mock-auth.guard';
|
2021-09-04 12:03:41 -04:00
|
|
|
import { TokenAuthGuard } from '../../src/auth/token.strategy';
|
2021-08-29 12:45:46 -04:00
|
|
|
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';
|
2020-09-26 17:27:24 -04:00
|
|
|
import { HistoryEntryUpdateDto } from '../../src/history/history-entry-update.dto';
|
|
|
|
import { HistoryEntryDto } from '../../src/history/history-entry.dto';
|
2021-02-03 15:46:36 -05:00
|
|
|
import { HistoryEntry } from '../../src/history/history-entry.entity';
|
|
|
|
import { HistoryModule } from '../../src/history/history.module';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { HistoryService } from '../../src/history/history.service';
|
|
|
|
import { LoggerModule } from '../../src/logger/logger.module';
|
2021-03-14 12:47:16 -04:00
|
|
|
import { MediaModule } from '../../src/media/media.module';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { MediaService } from '../../src/media/media.service';
|
2021-03-05 06:04:06 -05:00
|
|
|
import { NoteMetadataDto } from '../../src/notes/note-metadata.dto';
|
2021-08-29 12:45:46 -04:00
|
|
|
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 { UsersModule } from '../../src/users/users.module';
|
|
|
|
import { UsersService } from '../../src/users/users.service';
|
2020-09-26 17:27:24 -04:00
|
|
|
|
|
|
|
// TODO Tests have to be reworked using UserService functions
|
|
|
|
|
2021-03-24 06:18:26 -04:00
|
|
|
describe('Me', () => {
|
2020-09-26 17:27:24 -04:00
|
|
|
let app: INestApplication;
|
|
|
|
let historyService: HistoryService;
|
|
|
|
let notesService: NotesService;
|
2021-02-03 10:20:27 -05:00
|
|
|
let userService: UsersService;
|
2021-03-14 12:47:16 -04:00
|
|
|
let mediaService: MediaService;
|
2021-03-24 06:18:26 -04:00
|
|
|
let uploadPath: string;
|
2021-02-03 15:46:36 -05:00
|
|
|
let user: User;
|
2020-09-26 17:27:24 -04:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
|
|
|
const moduleRef = await Test.createTestingModule({
|
2021-02-03 15:46:36 -05:00
|
|
|
imports: [
|
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
2021-02-20 16:15:04 -05:00
|
|
|
load: [mediaConfigMock, appConfigMock],
|
2021-02-03 15:46:36 -05:00
|
|
|
}),
|
|
|
|
PublicApiModule,
|
|
|
|
NotesModule,
|
|
|
|
PermissionsModule,
|
|
|
|
GroupsModule,
|
|
|
|
TypeOrmModule.forRoot({
|
|
|
|
type: 'sqlite',
|
|
|
|
database: './hedgedoc-e2e-me.sqlite',
|
|
|
|
autoLoadEntities: true,
|
|
|
|
synchronize: true,
|
|
|
|
dropSchema: true,
|
|
|
|
}),
|
|
|
|
LoggerModule,
|
|
|
|
AuthModule,
|
|
|
|
UsersModule,
|
|
|
|
HistoryModule,
|
2021-03-14 12:47:16 -04:00
|
|
|
MediaModule,
|
2021-02-03 15:46:36 -05:00
|
|
|
],
|
|
|
|
})
|
|
|
|
.overrideGuard(TokenAuthGuard)
|
|
|
|
.useClass(MockAuthGuard)
|
|
|
|
.compile();
|
2021-03-24 06:18:26 -04:00
|
|
|
const config = moduleRef.get<ConfigService>(ConfigService);
|
|
|
|
uploadPath = config.get('mediaConfig').backend.filesystem.uploadPath;
|
2020-09-26 17:27:24 -04:00
|
|
|
app = moduleRef.createNestApplication();
|
2021-02-03 15:46:36 -05:00
|
|
|
notesService = moduleRef.get(NotesService);
|
|
|
|
historyService = moduleRef.get(HistoryService);
|
2021-02-03 10:20:27 -05:00
|
|
|
userService = moduleRef.get(UsersService);
|
2021-03-14 12:47:16 -04:00
|
|
|
mediaService = moduleRef.get(MediaService);
|
2021-02-03 15:46:36 -05:00
|
|
|
user = await userService.createUser('hardcoded', 'Testy');
|
2020-09-26 17:27:24 -04:00
|
|
|
await app.init();
|
|
|
|
});
|
|
|
|
|
2021-02-03 10:20:27 -05:00
|
|
|
it(`GET /me`, async () => {
|
|
|
|
const userInfo = userService.toUserDto(user);
|
2020-09-26 17:27:24 -04:00
|
|
|
const response = await request(app.getHttpServer())
|
2021-02-03 10:20:27 -05:00
|
|
|
.get('/me')
|
2020-09-26 17:27:24 -04:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2021-02-03 10:20:27 -05:00
|
|
|
expect(response.body).toEqual(userInfo);
|
2020-09-26 17:27:24 -04:00
|
|
|
});
|
|
|
|
|
2021-02-03 15:46:36 -05:00
|
|
|
it(`GET /me/history`, async () => {
|
|
|
|
const noteName = 'testGetNoteHistory1';
|
|
|
|
const note = await notesService.createNote('', noteName);
|
2021-08-29 15:42:46 -04:00
|
|
|
const createdHistoryEntry =
|
|
|
|
await historyService.updateHistoryEntryTimestamp(note, user);
|
2020-09-26 17:27:24 -04:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/me/history')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2021-08-29 12:45:46 -04:00
|
|
|
const history: HistoryEntryDto[] = response.body;
|
2021-03-05 06:04:06 -05:00
|
|
|
expect(history.length).toEqual(1);
|
|
|
|
const historyDto = historyService.toHistoryEntryDto(createdHistoryEntry);
|
2021-02-03 15:46:36 -05:00
|
|
|
for (const historyEntry of history) {
|
2021-03-05 06:04:06 -05:00
|
|
|
expect(historyEntry.identifier).toEqual(historyDto.identifier);
|
|
|
|
expect(historyEntry.title).toEqual(historyDto.title);
|
|
|
|
expect(historyEntry.tags).toEqual(historyDto.tags);
|
|
|
|
expect(historyEntry.pinStatus).toEqual(historyDto.pinStatus);
|
|
|
|
expect(historyEntry.lastVisited).toEqual(
|
|
|
|
historyDto.lastVisited.toISOString(),
|
|
|
|
);
|
2020-09-26 17:27:24 -04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-05 06:04:06 -05:00
|
|
|
describe(`GET /me/history/{note}`, () => {
|
|
|
|
it('works with an existing note', async () => {
|
|
|
|
const noteName = 'testGetNoteHistory2';
|
|
|
|
const note = await notesService.createNote('', noteName);
|
2021-05-15 15:13:44 -04:00
|
|
|
const createdHistoryEntry =
|
2021-08-29 15:19:53 -04:00
|
|
|
await historyService.updateHistoryEntryTimestamp(note, user);
|
2021-03-05 06:04:06 -05:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get(`/me/history/${noteName}`)
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2021-08-29 12:45:46 -04:00
|
|
|
const historyEntry: HistoryEntryDto = response.body;
|
2021-05-15 15:13:44 -04:00
|
|
|
const historyEntryDto =
|
|
|
|
historyService.toHistoryEntryDto(createdHistoryEntry);
|
2021-03-05 06:04:06 -05:00
|
|
|
expect(historyEntry.identifier).toEqual(historyEntryDto.identifier);
|
|
|
|
expect(historyEntry.title).toEqual(historyEntryDto.title);
|
|
|
|
expect(historyEntry.tags).toEqual(historyEntryDto.tags);
|
|
|
|
expect(historyEntry.pinStatus).toEqual(historyEntryDto.pinStatus);
|
|
|
|
expect(historyEntry.lastVisited).toEqual(
|
|
|
|
historyEntryDto.lastVisited.toISOString(),
|
|
|
|
);
|
|
|
|
});
|
|
|
|
it('fails with a non-existing note', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.get('/me/history/i_dont_exist')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(`PUT /me/history/{note}`, () => {
|
|
|
|
it('works', async () => {
|
|
|
|
const noteName = 'testGetNoteHistory3';
|
|
|
|
const note = await notesService.createNote('', noteName);
|
2021-08-29 15:19:53 -04:00
|
|
|
await historyService.updateHistoryEntryTimestamp(note, user);
|
2021-03-05 06:04:06 -05:00
|
|
|
const historyEntryUpdateDto = new HistoryEntryUpdateDto();
|
|
|
|
historyEntryUpdateDto.pinStatus = true;
|
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.put('/me/history/' + noteName)
|
|
|
|
.send(historyEntryUpdateDto)
|
|
|
|
.expect(200);
|
|
|
|
const history = await historyService.getEntriesByUser(user);
|
2021-06-06 11:55:41 -04:00
|
|
|
const historyEntry: HistoryEntryDto = response.body;
|
2021-03-05 06:04:06 -05:00
|
|
|
expect(historyEntry.pinStatus).toEqual(true);
|
2021-06-06 11:55:41 -04:00
|
|
|
let theEntry: HistoryEntryDto;
|
|
|
|
for (const entry of history) {
|
|
|
|
if (entry.note.aliases.find((element) => element.name === noteName)) {
|
|
|
|
theEntry = historyService.toHistoryEntryDto(entry);
|
2021-03-05 06:04:06 -05:00
|
|
|
}
|
2020-09-26 17:27:24 -04:00
|
|
|
}
|
2021-06-06 11:55:41 -04:00
|
|
|
expect(theEntry.pinStatus).toEqual(true);
|
2021-03-05 06:04:06 -05:00
|
|
|
});
|
|
|
|
it('fails with a non-existing note', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.put('/me/history/i_dont_exist')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
2021-02-03 15:46:36 -05:00
|
|
|
});
|
|
|
|
|
2021-03-05 06:04:06 -05:00
|
|
|
describe(`DELETE /me/history/{note}`, () => {
|
|
|
|
it('works', async () => {
|
|
|
|
const noteName = 'testGetNoteHistory4';
|
|
|
|
const note = await notesService.createNote('', noteName);
|
2021-08-29 15:19:53 -04:00
|
|
|
await historyService.updateHistoryEntryTimestamp(note, user);
|
2021-03-05 06:04:06 -05:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.delete(`/me/history/${noteName}`)
|
|
|
|
.expect(204);
|
|
|
|
expect(response.body).toEqual({});
|
|
|
|
const history = await historyService.getEntriesByUser(user);
|
2021-06-06 11:55:41 -04:00
|
|
|
for (const entry of history) {
|
|
|
|
if (entry.note.aliases.find((element) => element.name === noteName)) {
|
|
|
|
throw new Error('Deleted history entry still in history');
|
2021-03-05 06:04:06 -05:00
|
|
|
}
|
2020-09-26 17:27:24 -04:00
|
|
|
}
|
2021-03-05 06:04:06 -05:00
|
|
|
});
|
|
|
|
describe('fails', () => {
|
|
|
|
it('with a non-existing note', async () => {
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete('/me/history/i_dont_exist')
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
it('with a non-existing history entry', async () => {
|
|
|
|
const noteName = 'testGetNoteHistory5';
|
|
|
|
await notesService.createNote('', noteName);
|
|
|
|
await request(app.getHttpServer())
|
|
|
|
.delete(`/me/history/${noteName}`)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
2020-09-26 17:27:24 -04:00
|
|
|
});
|
|
|
|
|
2021-03-05 06:04:06 -05:00
|
|
|
it(`GET /me/notes/`, async () => {
|
|
|
|
const noteName = 'testNote';
|
|
|
|
await notesService.createNote('', noteName, user);
|
2020-09-26 17:27:24 -04:00
|
|
|
const response = await request(app.getHttpServer())
|
|
|
|
.get('/me/notes/')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2021-03-05 06:04:06 -05:00
|
|
|
const noteMetaDtos = response.body as NoteMetadataDto[];
|
|
|
|
expect(noteMetaDtos).toHaveLength(1);
|
2021-06-06 11:55:41 -04:00
|
|
|
expect(noteMetaDtos[0].primaryAlias).toEqual(noteName);
|
|
|
|
expect(noteMetaDtos[0].updateUser?.userName).toEqual(user.userName);
|
2020-09-26 17:27:24 -04:00
|
|
|
});
|
|
|
|
|
2021-03-14 12:47:16 -04:00
|
|
|
it('GET /me/media', async () => {
|
|
|
|
const note1 = await notesService.createNote(
|
|
|
|
'This is a test note.',
|
|
|
|
'test8',
|
|
|
|
await userService.getUserByUsername('hardcoded'),
|
|
|
|
);
|
|
|
|
const note2 = await notesService.createNote(
|
|
|
|
'This is a test note.',
|
|
|
|
'test9',
|
|
|
|
await userService.getUserByUsername('hardcoded'),
|
|
|
|
);
|
|
|
|
const httpServer = app.getHttpServer();
|
|
|
|
const response1 = await request(httpServer)
|
|
|
|
.get('/me/media/')
|
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response1.body).toHaveLength(0);
|
|
|
|
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2021-08-29 16:28:21 -04: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-14 12:47:16 -04:00
|
|
|
|
|
|
|
const response = await request(httpServer)
|
|
|
|
.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);
|
2021-03-24 06:18:26 -04:00
|
|
|
for (const fileUrl of [url0, url1, url2, url3]) {
|
|
|
|
const fileName = fileUrl.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
}
|
2021-03-31 16:42:56 -04:00
|
|
|
await fs.rmdir(uploadPath, { recursive: true });
|
2021-03-14 12:47:16 -04:00
|
|
|
});
|
|
|
|
|
2020-09-26 17:27:24 -04:00
|
|
|
afterAll(async () => {
|
|
|
|
await app.close();
|
|
|
|
});
|
|
|
|
});
|