2021-01-05 21:12:38 +00:00
|
|
|
/*
|
2022-01-30 21:00:17 +00:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 21:12:38 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2021-08-29 16:45:46 +00:00
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
import { join } from 'path';
|
2021-04-29 20:47:18 +00:00
|
|
|
import request from 'supertest';
|
2021-08-29 16:45:46 +00:00
|
|
|
|
2020-09-26 21:27:24 +00:00
|
|
|
import { HistoryEntryUpdateDto } from '../../src/history/history-entry-update.dto';
|
|
|
|
import { HistoryEntryDto } from '../../src/history/history-entry.dto';
|
2021-03-05 11:04:06 +00:00
|
|
|
import { NoteMetadataDto } from '../../src/notes/note-metadata.dto';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { User } from '../../src/users/user.entity';
|
2022-01-06 20:41:36 +00:00
|
|
|
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
2020-09-26 21:27:24 +00:00
|
|
|
|
|
|
|
// TODO Tests have to be reworked using UserService functions
|
|
|
|
|
2021-03-24 10:18:26 +00:00
|
|
|
describe('Me', () => {
|
2021-10-14 19:17:42 +00:00
|
|
|
let testSetup: TestSetup;
|
|
|
|
|
2021-03-24 10:18:26 +00:00
|
|
|
let uploadPath: string;
|
2021-02-03 20:46:36 +00:00
|
|
|
let user: User;
|
2020-09-26 21:27:24 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-01-06 20:41:36 +00:00
|
|
|
testSetup = await TestSetupBuilder.create().withMockAuth().build();
|
2021-10-14 19:17:42 +00:00
|
|
|
|
|
|
|
uploadPath =
|
|
|
|
testSetup.configService.get('mediaConfig').backend.filesystem.uploadPath;
|
|
|
|
|
|
|
|
user = await testSetup.userService.createUser('hardcoded', 'Testy');
|
|
|
|
await testSetup.app.init();
|
2020-09-26 21:27:24 +00:00
|
|
|
});
|
|
|
|
|
2022-03-04 12:13:46 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
await testSetup.app.close();
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
2021-02-03 15:20:27 +00:00
|
|
|
it(`GET /me`, async () => {
|
2022-02-20 21:18:51 +00:00
|
|
|
const userInfo = testSetup.userService.toFullUserDto(user);
|
2021-10-14 19:17:42 +00:00
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.get('/api/v2/me')
|
2020-09-26 21:27:24 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2021-02-03 15:20:27 +00:00
|
|
|
expect(response.body).toEqual(userInfo);
|
2020-09-26 21:27:24 +00:00
|
|
|
});
|
|
|
|
|
2021-02-03 20:46:36 +00:00
|
|
|
it(`GET /me/history`, async () => {
|
|
|
|
const noteName = 'testGetNoteHistory1';
|
2021-11-14 20:44:59 +00:00
|
|
|
const note = await testSetup.notesService.createNote('', null, noteName);
|
2021-08-29 19:42:46 +00:00
|
|
|
const createdHistoryEntry =
|
2021-10-14 19:17:42 +00:00
|
|
|
await testSetup.historyService.updateHistoryEntryTimestamp(note, user);
|
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.get('/api/v2/me/history')
|
2020-09-26 21:27:24 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2021-08-29 16:45:46 +00:00
|
|
|
const history: HistoryEntryDto[] = response.body;
|
2021-03-05 11:04:06 +00:00
|
|
|
expect(history.length).toEqual(1);
|
2021-11-30 15:46:07 +00:00
|
|
|
const historyDto = await testSetup.historyService.toHistoryEntryDto(
|
|
|
|
createdHistoryEntry,
|
|
|
|
);
|
2021-02-03 20:46:36 +00:00
|
|
|
for (const historyEntry of history) {
|
2021-03-05 11:04:06 +00: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);
|
2022-01-16 21:04:34 +00:00
|
|
|
expect(historyEntry.lastVisitedAt).toEqual(
|
|
|
|
historyDto.lastVisitedAt.toISOString(),
|
2021-03-05 11:04:06 +00:00
|
|
|
);
|
2020-09-26 21:27:24 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-05 11:04:06 +00:00
|
|
|
describe(`GET /me/history/{note}`, () => {
|
|
|
|
it('works with an existing note', async () => {
|
|
|
|
const noteName = 'testGetNoteHistory2';
|
2021-11-14 20:44:59 +00:00
|
|
|
const note = await testSetup.notesService.createNote('', null, noteName);
|
2021-05-15 19:13:44 +00:00
|
|
|
const createdHistoryEntry =
|
2021-10-14 19:17:42 +00:00
|
|
|
await testSetup.historyService.updateHistoryEntryTimestamp(note, user);
|
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.get(`/api/v2/me/history/${noteName}`)
|
2021-03-05 11:04:06 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2021-08-29 16:45:46 +00:00
|
|
|
const historyEntry: HistoryEntryDto = response.body;
|
2021-11-30 15:46:07 +00:00
|
|
|
const historyEntryDto = await testSetup.historyService.toHistoryEntryDto(
|
|
|
|
createdHistoryEntry,
|
|
|
|
);
|
2021-03-05 11:04:06 +00: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);
|
2022-01-16 21:04:34 +00:00
|
|
|
expect(historyEntry.lastVisitedAt).toEqual(
|
|
|
|
historyEntryDto.lastVisitedAt.toISOString(),
|
2021-03-05 11:04:06 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
it('fails with a non-existing note', async () => {
|
2021-10-14 19:17:42 +00:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.get('/api/v2/me/history/i_dont_exist')
|
2021-03-05 11:04:06 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
describe(`PUT /me/history/{note}`, () => {
|
|
|
|
it('works', async () => {
|
|
|
|
const noteName = 'testGetNoteHistory3';
|
2021-11-14 20:44:59 +00:00
|
|
|
const note = await testSetup.notesService.createNote('', null, noteName);
|
2021-10-14 19:17:42 +00:00
|
|
|
await testSetup.historyService.updateHistoryEntryTimestamp(note, user);
|
2021-03-05 11:04:06 +00:00
|
|
|
const historyEntryUpdateDto = new HistoryEntryUpdateDto();
|
|
|
|
historyEntryUpdateDto.pinStatus = true;
|
2021-10-14 19:17:42 +00:00
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.put('/api/v2/me/history/' + noteName)
|
2021-03-05 11:04:06 +00:00
|
|
|
.send(historyEntryUpdateDto)
|
|
|
|
.expect(200);
|
2021-10-14 19:17:42 +00:00
|
|
|
const history = await testSetup.historyService.getEntriesByUser(user);
|
2021-06-06 15:55:41 +00:00
|
|
|
const historyEntry: HistoryEntryDto = response.body;
|
2021-03-05 11:04:06 +00:00
|
|
|
expect(historyEntry.pinStatus).toEqual(true);
|
2022-03-20 21:40:41 +00:00
|
|
|
|
2021-06-06 15:55:41 +00:00
|
|
|
let theEntry: HistoryEntryDto;
|
|
|
|
for (const entry of history) {
|
2021-11-30 15:46:07 +00:00
|
|
|
if (
|
2022-09-18 16:59:10 +00:00
|
|
|
(await (await entry.note).aliases).find(
|
2021-11-30 15:46:07 +00:00
|
|
|
(element) => element.name === noteName,
|
|
|
|
)
|
|
|
|
) {
|
|
|
|
theEntry = await testSetup.historyService.toHistoryEntryDto(entry);
|
2021-03-05 11:04:06 +00:00
|
|
|
}
|
2020-09-26 21:27:24 +00:00
|
|
|
}
|
2021-06-06 15:55:41 +00:00
|
|
|
expect(theEntry.pinStatus).toEqual(true);
|
2021-03-05 11:04:06 +00:00
|
|
|
});
|
|
|
|
it('fails with a non-existing note', async () => {
|
2021-10-14 19:17:42 +00:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.put('/api/v2/me/history/i_dont_exist')
|
2021-03-05 11:04:06 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(404);
|
|
|
|
});
|
2021-02-03 20:46:36 +00:00
|
|
|
});
|
|
|
|
|
2021-03-05 11:04:06 +00:00
|
|
|
describe(`DELETE /me/history/{note}`, () => {
|
|
|
|
it('works', async () => {
|
|
|
|
const noteName = 'testGetNoteHistory4';
|
2021-11-14 20:44:59 +00:00
|
|
|
const note = await testSetup.notesService.createNote('', null, noteName);
|
2021-10-14 19:17:42 +00:00
|
|
|
await testSetup.historyService.updateHistoryEntryTimestamp(note, user);
|
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.delete(`/api/v2/me/history/${noteName}`)
|
2021-03-05 11:04:06 +00:00
|
|
|
.expect(204);
|
|
|
|
expect(response.body).toEqual({});
|
2021-10-14 19:17:42 +00:00
|
|
|
const history = await testSetup.historyService.getEntriesByUser(user);
|
2021-06-06 15:55:41 +00:00
|
|
|
for (const entry of history) {
|
2021-11-30 15:46:07 +00:00
|
|
|
if (
|
2022-09-18 16:59:10 +00:00
|
|
|
(await (await entry.note).aliases).find(
|
2021-11-30 15:46:07 +00:00
|
|
|
(element) => element.name === noteName,
|
|
|
|
)
|
|
|
|
) {
|
2021-06-06 15:55:41 +00:00
|
|
|
throw new Error('Deleted history entry still in history');
|
2021-03-05 11:04:06 +00:00
|
|
|
}
|
2020-09-26 21:27:24 +00:00
|
|
|
}
|
2021-03-05 11:04:06 +00:00
|
|
|
});
|
|
|
|
describe('fails', () => {
|
|
|
|
it('with a non-existing note', async () => {
|
2021-10-14 19:17:42 +00:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.delete('/api/v2/me/history/i_dont_exist')
|
2021-03-05 11:04:06 +00:00
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
it('with a non-existing history entry', async () => {
|
|
|
|
const noteName = 'testGetNoteHistory5';
|
2021-11-14 20:44:59 +00:00
|
|
|
await testSetup.notesService.createNote('', null, noteName);
|
2021-10-14 19:17:42 +00:00
|
|
|
await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.delete(`/api/v2/me/history/${noteName}`)
|
2021-03-05 11:04:06 +00:00
|
|
|
.expect(404);
|
|
|
|
});
|
|
|
|
});
|
2020-09-26 21:27:24 +00:00
|
|
|
});
|
|
|
|
|
2021-03-05 11:04:06 +00:00
|
|
|
it(`GET /me/notes/`, async () => {
|
|
|
|
const noteName = 'testNote';
|
2021-11-14 20:44:59 +00:00
|
|
|
await testSetup.notesService.createNote('', user, noteName);
|
2021-10-14 19:17:42 +00:00
|
|
|
const response = await request(testSetup.app.getHttpServer())
|
2021-10-15 14:53:10 +00:00
|
|
|
.get('/api/v2/me/notes/')
|
2020-09-26 21:27:24 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
2021-03-05 11:04:06 +00:00
|
|
|
const noteMetaDtos = response.body as NoteMetadataDto[];
|
|
|
|
expect(noteMetaDtos).toHaveLength(1);
|
2022-02-14 14:26:29 +00:00
|
|
|
expect(noteMetaDtos[0].primaryAddress).toEqual(noteName);
|
2022-01-16 21:54:53 +00:00
|
|
|
expect(noteMetaDtos[0].updateUsername).toEqual(user.username);
|
2020-09-26 21:27:24 +00:00
|
|
|
});
|
|
|
|
|
2021-03-14 16:47:16 +00:00
|
|
|
it('GET /me/media', async () => {
|
2021-10-14 19:17:42 +00:00
|
|
|
const note1 = await testSetup.notesService.createNote(
|
2021-03-14 16:47:16 +00:00
|
|
|
'This is a test note.',
|
2021-10-14 19:17:42 +00:00
|
|
|
await testSetup.userService.getUserByUsername('hardcoded'),
|
2021-11-14 20:44:59 +00:00
|
|
|
'test8',
|
2021-03-14 16:47:16 +00:00
|
|
|
);
|
2021-10-14 19:17:42 +00:00
|
|
|
const note2 = await testSetup.notesService.createNote(
|
2021-03-14 16:47:16 +00:00
|
|
|
'This is a test note.',
|
2021-10-14 19:17:42 +00:00
|
|
|
await testSetup.userService.getUserByUsername('hardcoded'),
|
2021-11-14 20:44:59 +00:00
|
|
|
'test9',
|
2021-03-14 16:47:16 +00:00
|
|
|
);
|
2021-10-14 19:17:42 +00:00
|
|
|
const httpServer = testSetup.app.getHttpServer();
|
2021-03-14 16:47:16 +00:00
|
|
|
const response1 = await request(httpServer)
|
2021-10-15 14:53:10 +00:00
|
|
|
.get('/api/v2/me/media/')
|
2021-03-14 16:47:16 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response1.body).toHaveLength(0);
|
|
|
|
|
|
|
|
const testImage = await fs.readFile('test/public-api/fixtures/test.png');
|
2022-03-05 17:47:45 +00:00
|
|
|
const imageUrls = [];
|
|
|
|
imageUrls.push(
|
2022-01-29 17:57:44 +00:00
|
|
|
(await testSetup.mediaService.saveFile(testImage, user, note1)).fileUrl,
|
2022-03-05 17:47:45 +00:00
|
|
|
);
|
|
|
|
imageUrls.push(
|
2022-01-29 17:57:44 +00:00
|
|
|
(await testSetup.mediaService.saveFile(testImage, user, note1)).fileUrl,
|
2022-03-05 17:47:45 +00:00
|
|
|
);
|
|
|
|
imageUrls.push(
|
2022-01-29 17:57:44 +00:00
|
|
|
(await testSetup.mediaService.saveFile(testImage, user, note2)).fileUrl,
|
2022-03-05 17:47:45 +00:00
|
|
|
);
|
|
|
|
imageUrls.push(
|
2022-01-29 17:57:44 +00:00
|
|
|
(await testSetup.mediaService.saveFile(testImage, user, note2)).fileUrl,
|
2022-03-05 17:47:45 +00:00
|
|
|
);
|
2021-03-14 16:47:16 +00:00
|
|
|
|
|
|
|
const response = await request(httpServer)
|
2021-10-15 14:53:10 +00:00
|
|
|
.get('/api/v2/me/media/')
|
2021-03-14 16:47:16 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body).toHaveLength(4);
|
2022-03-05 17:47:45 +00:00
|
|
|
expect(imageUrls).toContain(response.body[0].url);
|
|
|
|
expect(imageUrls).toContain(response.body[1].url);
|
|
|
|
expect(imageUrls).toContain(response.body[2].url);
|
|
|
|
expect(imageUrls).toContain(response.body[3].url);
|
|
|
|
for (const fileUrl of imageUrls) {
|
2021-03-24 10:18:26 +00:00
|
|
|
const fileName = fileUrl.replace('/uploads/', '');
|
|
|
|
// delete the file afterwards
|
|
|
|
await fs.unlink(join(uploadPath, fileName));
|
|
|
|
}
|
2022-01-30 21:00:17 +00:00
|
|
|
await fs.rm(uploadPath, { recursive: true });
|
2021-03-14 16:47:16 +00:00
|
|
|
});
|
2020-09-26 21:27:24 +00:00
|
|
|
});
|