2021-01-05 16:12:38 -05:00
|
|
|
/*
|
2021-01-06 15:36:07 -05:00
|
|
|
* SPDX-FileCopyrightText: 2021 The HedgeDoc developers (see AUTHORS file)
|
2021-01-05 16:12:38 -05:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
*/
|
|
|
|
|
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';
|
|
|
|
import { NotInDBError } from '../errors/errors';
|
2020-09-27 15:48:42 -04:00
|
|
|
import { LoggerModule } from '../logger/logger.module';
|
2020-09-22 15:59:09 -04:00
|
|
|
import { Note } from '../notes/note.entity';
|
|
|
|
import { NotesModule } from '../notes/notes.module';
|
2021-01-22 09:29:10 -05:00
|
|
|
import { AuthToken } from '../auth/auth-token.entity';
|
2020-09-22 15:59:09 -04:00
|
|
|
import { Identity } from '../users/identity.entity';
|
|
|
|
import { User } from '../users/user.entity';
|
|
|
|
import { Authorship } from './authorship.entity';
|
|
|
|
import { Revision } from './revision.entity';
|
2020-07-26 14:57:28 -04:00
|
|
|
import { RevisionsService } from './revisions.service';
|
2020-10-03 11:24:42 -04:00
|
|
|
import { Tag } from '../notes/tag.entity';
|
2021-01-27 16:58:55 -05:00
|
|
|
import { NoteGroupPermission } from '../permissions/note-group-permission.entity';
|
|
|
|
import { NoteUserPermission } from '../permissions/note-user-permission.entity';
|
2021-02-20 05:41:15 -05:00
|
|
|
import { Group } from '../groups/group.entity';
|
2021-02-20 16:15:04 -05:00
|
|
|
import { ConfigModule } from '@nestjs/config';
|
2021-03-01 15:12:01 -05:00
|
|
|
import appConfigMock from '../config/mock/app.config.mock';
|
2020-07-26 14:57:28 -04:00
|
|
|
|
|
|
|
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,
|
|
|
|
load: [appConfigMock],
|
|
|
|
}),
|
|
|
|
],
|
2020-09-22 15:59:09 -04:00
|
|
|
})
|
|
|
|
.overrideProvider(getRepositoryToken(Authorship))
|
|
|
|
.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({})
|
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 () => {
|
|
|
|
const revision = Revision.create('', '');
|
|
|
|
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,
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
2020-07-26 14:57:28 -04:00
|
|
|
});
|