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';
|
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-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';
|
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,
|
|
|
|
{
|
|
|
|
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-01-30 09:48:59 -05:00
|
|
|
load: [appConfigMock, 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 () => {
|
2021-09-25 05:55:35 -04:00
|
|
|
const note = {} as Note;
|
|
|
|
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 () => {
|
|
|
|
jest.spyOn(revisionRepo, 'findOne').mockResolvedValueOnce(undefined);
|
|
|
|
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);
|
|
|
|
});
|
|
|
|
});
|
2020-07-26 14:57:28 -04:00
|
|
|
});
|