2021-01-05 16:12:38 -05:00
|
|
|
/*
|
2022-01-30 09:48:59 -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 { ConfigModule } from '@nestjs/config';
|
2020-07-26 14:57:28 -04:00
|
|
|
import { Test, TestingModule } from '@nestjs/testing';
|
2020-09-22 15:59:09 -04:00
|
|
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
2022-07-22 17:05:38 -04:00
|
|
|
import { Mock } from 'ts-mockery';
|
2021-04-29 09:03:44 -04:00
|
|
|
import { Repository } from 'typeorm';
|
2021-08-29 12:45:46 -04:00
|
|
|
|
|
|
|
import { AuthToken } from '../auth/auth-token.entity';
|
2021-05-17 16:28:35 -04:00
|
|
|
import { Author } from '../authors/author.entity';
|
2021-08-29 12:45:46 -04:00
|
|
|
import appConfigMock from '../config/mock/app.config.mock';
|
2022-04-02 17:45:46 -04:00
|
|
|
import authConfigMock from '../config/mock/auth.config.mock';
|
2022-06-21 10:16:40 -04:00
|
|
|
import databaseConfigMock from '../config/mock/database.config.mock';
|
2022-01-30 09:48:59 -05:00
|
|
|
import noteConfigMock from '../config/mock/note.config.mock';
|
2021-04-29 09:03:44 -04:00
|
|
|
import { NotInDBError } from '../errors/errors';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { Group } from '../groups/group.entity';
|
2021-08-08 15:53:20 -04:00
|
|
|
import { Identity } from '../identity/identity.entity';
|
2020-09-27 15:48:42 -04:00
|
|
|
import { LoggerModule } from '../logger/logger.module';
|
2021-06-06 11:53:07 -04:00
|
|
|
import { Alias } from '../notes/alias.entity';
|
2020-09-22 15:59:09 -04:00
|
|
|
import { Note } from '../notes/note.entity';
|
|
|
|
import { NotesModule } from '../notes/notes.module';
|
2021-08-29 12:45:46 -04:00
|
|
|
import { Tag } from '../notes/tag.entity';
|
|
|
|
import { NoteGroupPermission } from '../permissions/note-group-permission.entity';
|
|
|
|
import { NoteUserPermission } from '../permissions/note-user-permission.entity';
|
2021-05-17 16:28:35 -04:00
|
|
|
import { Session } from '../users/session.entity';
|
2020-09-22 15:59:09 -04:00
|
|
|
import { User } from '../users/user.entity';
|
2021-05-31 15:46:41 -04:00
|
|
|
import { Edit } from './edit.entity';
|
2022-02-13 13:07:52 -05:00
|
|
|
import { EditService } from './edit.service';
|
2020-09-22 15:59:09 -04:00
|
|
|
import { Revision } from './revision.entity';
|
2020-07-26 14:57:28 -04:00
|
|
|
import { RevisionsService } from './revisions.service';
|
|
|
|
|
|
|
|
describe('RevisionsService', () => {
|
|
|
|
let service: RevisionsService;
|
2021-04-29 09:03:44 -04:00
|
|
|
let revisionRepo: Repository<Revision>;
|
2020-07-26 14:57:28 -04:00
|
|
|
|
|
|
|
beforeEach(async () => {
|
|
|
|
const module: TestingModule = await Test.createTestingModule({
|
2020-09-22 15:59:09 -04:00
|
|
|
providers: [
|
|
|
|
RevisionsService,
|
2022-02-13 13:07:52 -05:00
|
|
|
EditService,
|
2020-09-22 15:59:09 -04:00
|
|
|
{
|
|
|
|
provide: getRepositoryToken(Revision),
|
2021-04-29 09:03:44 -04:00
|
|
|
useClass: Repository,
|
2020-09-22 15:59:09 -04:00
|
|
|
},
|
|
|
|
],
|
2021-02-20 16:15:04 -05:00
|
|
|
imports: [
|
|
|
|
NotesModule,
|
|
|
|
LoggerModule,
|
|
|
|
ConfigModule.forRoot({
|
|
|
|
isGlobal: true,
|
2022-04-02 17:45:46 -04:00
|
|
|
load: [
|
|
|
|
appConfigMock,
|
|
|
|
databaseConfigMock,
|
|
|
|
authConfigMock,
|
|
|
|
noteConfigMock,
|
|
|
|
],
|
2021-02-20 16:15:04 -05:00
|
|
|
}),
|
|
|
|
],
|
2020-09-22 15:59:09 -04:00
|
|
|
})
|
2021-05-31 15:46:41 -04:00
|
|
|
.overrideProvider(getRepositoryToken(Edit))
|
2020-09-22 15:59:09 -04:00
|
|
|
.useValue({})
|
|
|
|
.overrideProvider(getRepositoryToken(User))
|
|
|
|
.useValue({})
|
|
|
|
.overrideProvider(getRepositoryToken(AuthToken))
|
|
|
|
.useValue({})
|
|
|
|
.overrideProvider(getRepositoryToken(Identity))
|
|
|
|
.useValue({})
|
|
|
|
.overrideProvider(getRepositoryToken(Note))
|
|
|
|
.useValue({})
|
|
|
|
.overrideProvider(getRepositoryToken(Revision))
|
2021-04-29 09:03:44 -04:00
|
|
|
.useClass(Repository)
|
2020-10-03 11:24:42 -04:00
|
|
|
.overrideProvider(getRepositoryToken(Tag))
|
|
|
|
.useValue({})
|
2021-01-27 16:58:55 -05:00
|
|
|
.overrideProvider(getRepositoryToken(NoteGroupPermission))
|
|
|
|
.useValue({})
|
|
|
|
.overrideProvider(getRepositoryToken(NoteUserPermission))
|
|
|
|
.useValue({})
|
2021-02-20 05:41:15 -05:00
|
|
|
.overrideProvider(getRepositoryToken(Group))
|
|
|
|
.useValue({})
|
2021-06-06 11:53:07 -04:00
|
|
|
.overrideProvider(getRepositoryToken(Alias))
|
|
|
|
.useValue({})
|
2021-05-17 16:28:35 -04:00
|
|
|
.overrideProvider(getRepositoryToken(Session))
|
|
|
|
.useValue({})
|
|
|
|
.overrideProvider(getRepositoryToken(Author))
|
|
|
|
.useValue({})
|
2020-09-22 15:59:09 -04:00
|
|
|
.compile();
|
2020-07-26 14:57:28 -04:00
|
|
|
|
|
|
|
service = module.get<RevisionsService>(RevisionsService);
|
2021-04-29 09:03:44 -04:00
|
|
|
revisionRepo = module.get<Repository<Revision>>(
|
|
|
|
getRepositoryToken(Revision),
|
|
|
|
);
|
2020-07-26 14:57:28 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
it('should be defined', () => {
|
|
|
|
expect(service).toBeDefined();
|
|
|
|
});
|
2021-04-29 09:03:44 -04:00
|
|
|
|
|
|
|
describe('getRevision', () => {
|
|
|
|
it('returns a revision', async () => {
|
2022-07-22 17:05:38 -04:00
|
|
|
const note = Mock.of<Note>({});
|
2021-09-25 05:55:35 -04:00
|
|
|
const revision = Revision.create('', '', note) as Revision;
|
2021-04-29 09:03:44 -04:00
|
|
|
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(revision);
|
|
|
|
expect(await service.getRevision({} as Note, 1)).toEqual(revision);
|
|
|
|
});
|
|
|
|
it('throws if the revision is not in the databse', async () => {
|
2022-03-20 17:40:41 -04:00
|
|
|
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(null);
|
2021-04-29 09:03:44 -04:00
|
|
|
await expect(service.getRevision({} as Note, 1)).rejects.toThrow(
|
|
|
|
NotInDBError,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2021-08-29 20:07:35 -04:00
|
|
|
|
|
|
|
describe('purgeRevisions', () => {
|
|
|
|
it('purges the revision history', async () => {
|
|
|
|
const note = {} as Note;
|
|
|
|
note.id = 'test';
|
|
|
|
let revisions: Revision[] = [];
|
2021-09-25 05:55:35 -04:00
|
|
|
const revision1 = Revision.create('a', 'a', note) as Revision;
|
2021-08-29 20:07:35 -04:00
|
|
|
revision1.id = 1;
|
2021-09-25 05:55:35 -04:00
|
|
|
const revision2 = Revision.create('b', 'b', note) as Revision;
|
2021-08-29 20:07:35 -04:00
|
|
|
revision2.id = 2;
|
2021-09-25 05:55:35 -04:00
|
|
|
const revision3 = Revision.create('c', 'c', note) as Revision;
|
2021-08-29 20:07:35 -04:00
|
|
|
revision3.id = 3;
|
|
|
|
revisions.push(revision1, revision2, revision3);
|
|
|
|
note.revisions = Promise.resolve(revisions);
|
|
|
|
jest.spyOn(revisionRepo, 'find').mockResolvedValueOnce(revisions);
|
|
|
|
jest.spyOn(service, 'getLatestRevision').mockResolvedValueOnce(revision3);
|
|
|
|
revisionRepo.remove = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation((deleteList: Revision[]) => {
|
|
|
|
revisions = revisions.filter(
|
|
|
|
(item: Revision) => !deleteList.includes(item),
|
|
|
|
);
|
|
|
|
return Promise.resolve(deleteList);
|
|
|
|
});
|
|
|
|
|
|
|
|
// expected to return all the purged revisions
|
|
|
|
expect(await service.purgeRevisions(note)).toHaveLength(2);
|
|
|
|
|
|
|
|
// expected to have only the latest revision
|
|
|
|
const updatedRevisions: Revision[] = [revision3];
|
|
|
|
expect(revisions).toEqual(updatedRevisions);
|
|
|
|
});
|
|
|
|
it('has no effect on revision history when a single revision is present', async () => {
|
|
|
|
const note = {} as Note;
|
|
|
|
note.id = 'test';
|
|
|
|
let revisions: Revision[] = [];
|
2021-09-25 05:55:35 -04:00
|
|
|
const revision1 = Revision.create('a', 'a', note) as Revision;
|
2021-08-29 20:07:35 -04:00
|
|
|
revision1.id = 1;
|
|
|
|
revisions.push(revision1);
|
|
|
|
note.revisions = Promise.resolve(revisions);
|
|
|
|
jest.spyOn(revisionRepo, 'find').mockResolvedValueOnce(revisions);
|
|
|
|
jest.spyOn(service, 'getLatestRevision').mockResolvedValueOnce(revision1);
|
|
|
|
revisionRepo.remove = jest
|
|
|
|
.fn()
|
|
|
|
.mockImplementation((deleteList: Revision[]) => {
|
|
|
|
revisions = revisions.filter(
|
|
|
|
(item: Revision) => !deleteList.includes(item),
|
|
|
|
);
|
|
|
|
return Promise.resolve(deleteList);
|
|
|
|
});
|
|
|
|
|
|
|
|
// expected to return all the purged revisions
|
|
|
|
expect(await service.purgeRevisions(note)).toHaveLength(0);
|
|
|
|
|
|
|
|
// expected to have only the latest revision
|
|
|
|
const updatedRevisions: Revision[] = [revision1];
|
|
|
|
expect(revisions).toEqual(updatedRevisions);
|
|
|
|
});
|
|
|
|
});
|
2022-05-22 15:27:48 -04:00
|
|
|
|
|
|
|
describe('getRevisionUserInfo', () => {
|
|
|
|
it('counts users correctly', async () => {
|
|
|
|
const user = User.create('test', 'test') as User;
|
|
|
|
const author = Author.create(123) as Author;
|
|
|
|
author.user = Promise.resolve(user);
|
|
|
|
const anonAuthor = Author.create(123) as Author;
|
|
|
|
const anonAuthor2 = Author.create(123) as Author;
|
|
|
|
const edits = [Edit.create(author, 12, 15) as Edit];
|
|
|
|
edits.push(Edit.create(author, 16, 18) as Edit);
|
|
|
|
edits.push(Edit.create(author, 29, 20) as Edit);
|
|
|
|
edits.push(Edit.create(anonAuthor, 29, 20) as Edit);
|
|
|
|
edits.push(Edit.create(anonAuthor, 29, 20) as Edit);
|
|
|
|
edits.push(Edit.create(anonAuthor2, 29, 20) as Edit);
|
|
|
|
const revision = Revision.create('', '', {} as Note) as Revision;
|
|
|
|
revision.edits = Promise.resolve(edits);
|
|
|
|
|
|
|
|
const userInfo = await service.getRevisionUserInfo(revision);
|
|
|
|
expect(userInfo.usernames.length).toEqual(1);
|
|
|
|
expect(userInfo.anonymousUserCount).toEqual(2);
|
|
|
|
});
|
|
|
|
});
|
2022-07-22 17:05:38 -04:00
|
|
|
|
|
|
|
describe('createRevision', () => {
|
|
|
|
it('creates a new revision', async () => {
|
|
|
|
const note = Mock.of<Note>({});
|
|
|
|
const oldContent = 'old content\n';
|
|
|
|
const newContent = 'new content\n';
|
|
|
|
|
|
|
|
const oldRevision = Mock.of<Revision>({ content: oldContent });
|
|
|
|
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(oldRevision);
|
|
|
|
jest
|
|
|
|
.spyOn(revisionRepo, 'save')
|
|
|
|
.mockImplementation((revision) =>
|
|
|
|
Promise.resolve(revision as Revision),
|
|
|
|
);
|
|
|
|
|
|
|
|
const createdRevision = await service.createRevision(note, newContent);
|
|
|
|
expect(createdRevision).not.toBeUndefined();
|
|
|
|
expect(createdRevision?.content).toBe(newContent);
|
|
|
|
await expect(createdRevision?.note).resolves.toBe(note);
|
|
|
|
expect(createdRevision?.patch).toMatchInlineSnapshot(`
|
|
|
|
"Index: markdownContent
|
|
|
|
===================================================================
|
|
|
|
--- markdownContent
|
|
|
|
+++ markdownContent
|
|
|
|
@@ -1,1 +1,1 @@
|
|
|
|
-old content
|
|
|
|
+new content
|
|
|
|
"
|
|
|
|
`);
|
|
|
|
});
|
|
|
|
|
|
|
|
it("won't create a revision if content is unchanged", async () => {
|
|
|
|
const note = Mock.of<Note>({});
|
|
|
|
const oldContent = 'old content\n';
|
|
|
|
|
|
|
|
const oldRevision = Mock.of<Revision>({ content: oldContent });
|
|
|
|
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(oldRevision);
|
|
|
|
const saveSpy = jest.spyOn(revisionRepo, 'save').mockImplementation();
|
|
|
|
|
|
|
|
const createdRevision = await service.createRevision(note, oldContent);
|
|
|
|
expect(createdRevision).toBeUndefined();
|
2022-08-28 20:43:29 -04:00
|
|
|
expect(saveSpy).not.toHaveBeenCalled();
|
2022-07-22 17:05:38 -04:00
|
|
|
});
|
|
|
|
});
|
2020-07-26 14:57:28 -04:00
|
|
|
});
|