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