2021-03-04 09:20:58 +00:00
|
|
|
/*
|
2022-01-30 14:48:59 +00:00
|
|
|
* SPDX-FileCopyrightText: 2022 The HedgeDoc developers (see AUTHORS file)
|
2021-03-04 09:20:58 +00:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
2021-04-29 20:47:18 +00:00
|
|
|
import request from 'supertest';
|
2021-08-29 16:45:46 +00:00
|
|
|
|
|
|
|
import { HistoryEntryImportDto } from '../../src/history/history-entry-import.dto';
|
|
|
|
import { HistoryEntry } from '../../src/history/history-entry.entity';
|
|
|
|
import { HistoryService } from '../../src/history/history.service';
|
2021-10-13 18:43:56 +00:00
|
|
|
import { IdentityService } from '../../src/identity/identity.service';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { Note } from '../../src/notes/note.entity';
|
2021-03-04 09:20:58 +00:00
|
|
|
import { NotesService } from '../../src/notes/notes.service';
|
|
|
|
import { User } from '../../src/users/user.entity';
|
2021-08-29 16:45:46 +00:00
|
|
|
import { UsersService } from '../../src/users/users.service';
|
2022-01-06 20:41:36 +00:00
|
|
|
import { TestSetup, TestSetupBuilder } from '../test-setup';
|
2021-03-04 09:20:58 +00:00
|
|
|
|
|
|
|
describe('History', () => {
|
2021-10-14 20:44:30 +00:00
|
|
|
let testSetup: TestSetup;
|
2021-03-04 09:20:58 +00:00
|
|
|
let historyService: HistoryService;
|
2021-10-13 18:43:56 +00:00
|
|
|
let identityService: IdentityService;
|
2021-03-04 09:20:58 +00:00
|
|
|
let user: User;
|
|
|
|
let note: Note;
|
|
|
|
let note2: Note;
|
2021-04-12 08:55:16 +00:00
|
|
|
let forbiddenNoteId: string;
|
2021-03-04 09:20:58 +00:00
|
|
|
let content: string;
|
2021-10-13 18:43:56 +00:00
|
|
|
let agent: request.SuperAgentTest;
|
2021-03-04 09:20:58 +00:00
|
|
|
|
|
|
|
beforeAll(async () => {
|
2022-01-06 20:41:36 +00:00
|
|
|
testSetup = await TestSetupBuilder.create().build();
|
2021-10-14 20:44:30 +00:00
|
|
|
|
|
|
|
forbiddenNoteId =
|
2022-01-30 14:48:59 +00:00
|
|
|
testSetup.configService.get('noteConfig').forbiddenNoteIds[0];
|
2021-10-14 20:44:30 +00:00
|
|
|
|
|
|
|
const moduleRef = testSetup.moduleRef;
|
2022-09-25 00:05:30 +00:00
|
|
|
const username = 'hardcoded';
|
|
|
|
const password = 'AHardcodedStrongP@ssword123';
|
2021-03-04 09:20:58 +00:00
|
|
|
|
2021-10-14 20:44:30 +00:00
|
|
|
await testSetup.app.init();
|
2021-03-04 09:20:58 +00:00
|
|
|
content = 'This is a test note.';
|
|
|
|
historyService = moduleRef.get(HistoryService);
|
|
|
|
const userService = moduleRef.get(UsersService);
|
2021-10-13 18:43:56 +00:00
|
|
|
identityService = moduleRef.get(IdentityService);
|
2022-09-25 00:05:30 +00:00
|
|
|
user = await userService.createUser(username, 'Testy');
|
|
|
|
await identityService.createLocalIdentity(user, password);
|
2021-03-04 09:20:58 +00:00
|
|
|
const notesService = moduleRef.get(NotesService);
|
2021-11-14 20:44:59 +00:00
|
|
|
note = await notesService.createNote(content, user, 'note');
|
|
|
|
note2 = await notesService.createNote(content, user, 'note2');
|
2021-10-14 20:44:30 +00:00
|
|
|
agent = request.agent(testSetup.app.getHttpServer());
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/auth/local/login')
|
2022-09-25 00:05:30 +00:00
|
|
|
.send({ username: username, password: password })
|
2021-10-13 18:43:56 +00:00
|
|
|
.expect(201);
|
2021-03-04 09:20:58 +00:00
|
|
|
});
|
|
|
|
|
2022-03-04 12:13:46 +00:00
|
|
|
afterAll(async () => {
|
|
|
|
await testSetup.app.close();
|
|
|
|
await testSetup.cleanup();
|
|
|
|
});
|
|
|
|
|
2021-03-04 09:20:58 +00:00
|
|
|
it('GET /me/history', async () => {
|
2021-10-13 18:43:56 +00:00
|
|
|
const emptyResponse = await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.get('/api/private/me/history')
|
2021-03-04 09:20:58 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(emptyResponse.body.length).toEqual(0);
|
2021-10-14 20:44:30 +00:00
|
|
|
const entry = await testSetup.historyService.updateHistoryEntryTimestamp(
|
|
|
|
note,
|
|
|
|
user,
|
|
|
|
);
|
2021-11-30 15:46:07 +00:00
|
|
|
const entryDto = await testSetup.historyService.toHistoryEntryDto(entry);
|
2021-10-13 18:43:56 +00:00
|
|
|
const response = await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.get('/api/private/me/history')
|
2021-03-04 09:20:58 +00:00
|
|
|
.expect('Content-Type', /json/)
|
|
|
|
.expect(200);
|
|
|
|
expect(response.body.length).toEqual(1);
|
|
|
|
expect(response.body[0].identifier).toEqual(entryDto.identifier);
|
|
|
|
expect(response.body[0].title).toEqual(entryDto.title);
|
|
|
|
expect(response.body[0].tags).toEqual(entryDto.tags);
|
|
|
|
expect(response.body[0].pinStatus).toEqual(entryDto.pinStatus);
|
2022-01-16 21:04:34 +00:00
|
|
|
expect(response.body[0].lastVisitedAt).toEqual(
|
|
|
|
entryDto.lastVisitedAt.toISOString(),
|
2021-03-04 09:20:58 +00:00
|
|
|
);
|
|
|
|
});
|
|
|
|
|
2021-04-12 08:55:16 +00:00
|
|
|
describe('POST /me/history', () => {
|
|
|
|
it('works', async () => {
|
2021-10-14 20:44:30 +00:00
|
|
|
expect(
|
|
|
|
await testSetup.historyService.getEntriesByUser(user),
|
|
|
|
).toHaveLength(1);
|
2021-04-12 08:55:16 +00:00
|
|
|
const pinStatus = true;
|
|
|
|
const lastVisited = new Date('2020-12-01 12:23:34');
|
|
|
|
const postEntryDto = new HistoryEntryImportDto();
|
2021-11-30 15:46:07 +00:00
|
|
|
postEntryDto.note = (await note2.aliases).filter(
|
2021-06-06 15:55:41 +00:00
|
|
|
(alias) => alias.primary,
|
|
|
|
)[0].name;
|
2021-04-12 08:55:16 +00:00
|
|
|
postEntryDto.pinStatus = pinStatus;
|
2022-02-27 19:24:23 +00:00
|
|
|
postEntryDto.lastVisitedAt = lastVisited;
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/me/history')
|
2021-04-12 08:55:16 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify({ history: [postEntryDto] }))
|
|
|
|
.expect(201);
|
2021-10-14 20:44:30 +00:00
|
|
|
const userEntries = await testSetup.historyService.getEntriesByUser(user);
|
2021-04-12 08:55:16 +00:00
|
|
|
expect(userEntries.length).toEqual(1);
|
2022-09-18 16:59:10 +00:00
|
|
|
expect((await (await userEntries[0].note).aliases)[0].name).toEqual(
|
2021-11-30 15:46:07 +00:00
|
|
|
(await note2.aliases)[0].name,
|
2021-11-08 20:40:36 +00:00
|
|
|
);
|
2022-09-18 16:59:10 +00:00
|
|
|
expect((await (await userEntries[0].note).aliases)[0].primary).toEqual(
|
2021-11-30 15:46:07 +00:00
|
|
|
(await note2.aliases)[0].primary,
|
|
|
|
);
|
2022-09-18 16:59:10 +00:00
|
|
|
expect((await (await userEntries[0].note).aliases)[0].id).toEqual(
|
2021-11-30 15:46:07 +00:00
|
|
|
(await note2.aliases)[0].id,
|
2021-11-08 20:40:36 +00:00
|
|
|
);
|
2022-09-18 16:59:10 +00:00
|
|
|
expect((await userEntries[0].user).username).toEqual(user.username);
|
2021-04-12 08:55:16 +00:00
|
|
|
expect(userEntries[0].pinStatus).toEqual(pinStatus);
|
|
|
|
expect(userEntries[0].updatedAt).toEqual(lastVisited);
|
|
|
|
});
|
|
|
|
describe('fails', () => {
|
|
|
|
let pinStatus: boolean;
|
|
|
|
let lastVisited: Date;
|
|
|
|
let postEntryDto: HistoryEntryImportDto;
|
|
|
|
let prevEntry: HistoryEntry;
|
|
|
|
beforeAll(async () => {
|
2021-10-14 20:44:30 +00:00
|
|
|
const previousHistory = await testSetup.historyService.getEntriesByUser(
|
|
|
|
user,
|
|
|
|
);
|
2021-04-12 08:55:16 +00:00
|
|
|
expect(previousHistory).toHaveLength(1);
|
|
|
|
prevEntry = previousHistory[0];
|
|
|
|
pinStatus = !previousHistory[0].pinStatus;
|
|
|
|
lastVisited = new Date('2020-12-01 23:34:45');
|
|
|
|
postEntryDto = new HistoryEntryImportDto();
|
2021-11-30 15:46:07 +00:00
|
|
|
postEntryDto.note = (await note2.aliases).filter(
|
2021-06-06 15:55:41 +00:00
|
|
|
(alias) => alias.primary,
|
|
|
|
)[0].name;
|
2021-04-12 08:55:16 +00:00
|
|
|
postEntryDto.pinStatus = pinStatus;
|
2022-02-27 19:24:23 +00:00
|
|
|
postEntryDto.lastVisitedAt = lastVisited;
|
2021-04-12 08:55:16 +00:00
|
|
|
});
|
|
|
|
it('with forbiddenId', async () => {
|
|
|
|
const brokenEntryDto = new HistoryEntryImportDto();
|
|
|
|
brokenEntryDto.note = forbiddenNoteId;
|
|
|
|
brokenEntryDto.pinStatus = pinStatus;
|
2022-02-27 19:24:23 +00:00
|
|
|
brokenEntryDto.lastVisitedAt = lastVisited;
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/me/history')
|
2021-04-12 08:55:16 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify({ history: [brokenEntryDto] }))
|
|
|
|
.expect(400);
|
|
|
|
});
|
|
|
|
it('with non-existing note', async () => {
|
|
|
|
const brokenEntryDto = new HistoryEntryImportDto();
|
|
|
|
brokenEntryDto.note = 'i_dont_exist';
|
|
|
|
brokenEntryDto.pinStatus = pinStatus;
|
2022-02-27 19:24:23 +00:00
|
|
|
brokenEntryDto.lastVisitedAt = lastVisited;
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2021-10-15 15:06:56 +00:00
|
|
|
.post('/api/private/me/history')
|
2021-04-12 08:55:16 +00:00
|
|
|
.set('Content-Type', 'application/json')
|
|
|
|
.send(JSON.stringify({ history: [brokenEntryDto] }))
|
2022-01-23 21:23:00 +00:00
|
|
|
.expect(404);
|
2021-04-12 08:55:16 +00:00
|
|
|
});
|
|
|
|
afterEach(async () => {
|
2021-10-14 20:44:30 +00:00
|
|
|
const historyEntries = await testSetup.historyService.getEntriesByUser(
|
|
|
|
user,
|
|
|
|
);
|
2021-04-12 08:55:16 +00:00
|
|
|
expect(historyEntries).toHaveLength(1);
|
2022-09-18 16:59:10 +00:00
|
|
|
expect(await (await historyEntries[0].note).aliases).toEqual(
|
|
|
|
await (
|
|
|
|
await prevEntry.note
|
|
|
|
).aliases,
|
2022-08-07 20:31:48 +00:00
|
|
|
);
|
2022-09-18 16:59:10 +00:00
|
|
|
expect((await historyEntries[0].user).username).toEqual(
|
|
|
|
(await prevEntry.user).username,
|
2021-04-12 08:55:16 +00:00
|
|
|
);
|
|
|
|
expect(historyEntries[0].pinStatus).toEqual(prevEntry.pinStatus);
|
|
|
|
expect(historyEntries[0].updatedAt).toEqual(prevEntry.updatedAt);
|
|
|
|
});
|
|
|
|
});
|
2021-03-04 09:20:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('DELETE /me/history', async () => {
|
2021-10-14 20:44:30 +00:00
|
|
|
expect(
|
|
|
|
(await testSetup.historyService.getEntriesByUser(user)).length,
|
|
|
|
).toEqual(1);
|
2022-02-19 19:47:28 +00:00
|
|
|
await agent.delete('/api/private/me/history').expect(204);
|
2021-10-14 20:44:30 +00:00
|
|
|
expect(
|
|
|
|
(await testSetup.historyService.getEntriesByUser(user)).length,
|
|
|
|
).toEqual(0);
|
2021-03-04 09:20:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('PUT /me/history/:note', async () => {
|
2021-10-14 20:44:30 +00:00
|
|
|
const entry = await testSetup.historyService.updateHistoryEntryTimestamp(
|
|
|
|
note2,
|
|
|
|
user,
|
|
|
|
);
|
2021-03-04 09:20:58 +00:00
|
|
|
expect(entry.pinStatus).toBeFalsy();
|
2022-09-18 16:59:10 +00:00
|
|
|
const alias = (await (await entry.note).aliases).filter(
|
|
|
|
(alias) => alias.primary,
|
|
|
|
)[0].name;
|
2021-10-13 18:43:56 +00:00
|
|
|
await agent
|
2022-03-20 21:40:41 +00:00
|
|
|
.put(`/api/private/me/history/${alias || 'null'}`)
|
2021-03-04 09:20:58 +00:00
|
|
|
.send({ pinStatus: true })
|
|
|
|
.expect(200);
|
2021-10-14 20:44:30 +00:00
|
|
|
const userEntries = await testSetup.historyService.getEntriesByUser(user);
|
2021-03-04 09:20:58 +00:00
|
|
|
expect(userEntries.length).toEqual(1);
|
|
|
|
expect(userEntries[0].pinStatus).toBeTruthy();
|
2021-10-14 20:44:30 +00:00
|
|
|
await testSetup.historyService.deleteHistoryEntry(note2, user);
|
2021-03-04 09:20:58 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
it('DELETE /me/history/:note', async () => {
|
2021-08-29 19:19:53 +00:00
|
|
|
const entry = await historyService.updateHistoryEntryTimestamp(note2, user);
|
2022-09-18 16:59:10 +00:00
|
|
|
const alias = (await (await entry.note).aliases).filter(
|
|
|
|
(alias) => alias.primary,
|
|
|
|
)[0].name;
|
2021-08-29 19:19:53 +00:00
|
|
|
const entry2 = await historyService.updateHistoryEntryTimestamp(note, user);
|
2021-11-30 15:46:07 +00:00
|
|
|
const entryDto = await historyService.toHistoryEntryDto(entry2);
|
2021-10-15 15:06:56 +00:00
|
|
|
await agent
|
2022-03-20 21:40:41 +00:00
|
|
|
.delete(`/api/private/me/history/${alias || 'null'}`)
|
2022-02-19 19:47:28 +00:00
|
|
|
.expect(204);
|
2021-03-04 09:20:58 +00:00
|
|
|
const userEntries = await historyService.getEntriesByUser(user);
|
|
|
|
expect(userEntries.length).toEqual(1);
|
2021-11-30 15:46:07 +00:00
|
|
|
const userEntryDto = await historyService.toHistoryEntryDto(userEntries[0]);
|
2021-03-04 09:20:58 +00:00
|
|
|
expect(userEntryDto.identifier).toEqual(entryDto.identifier);
|
|
|
|
expect(userEntryDto.title).toEqual(entryDto.title);
|
|
|
|
expect(userEntryDto.tags).toEqual(entryDto.tags);
|
|
|
|
expect(userEntryDto.pinStatus).toEqual(entryDto.pinStatus);
|
2022-01-16 21:04:34 +00:00
|
|
|
expect(userEntryDto.lastVisitedAt).toEqual(entryDto.lastVisitedAt);
|
2021-03-04 09:20:58 +00:00
|
|
|
});
|
|
|
|
});
|