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