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
|
|
|
|
*/
|
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';
|
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';
|
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-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';
|
2020-09-22 15:59:09 -04:00
|
|
|
import { Identity } from '../users/identity.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,
|
|
|
|
load: [appConfigMock],
|
|
|
|
}),
|
|
|
|
],
|
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-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 () => {
|
|
|
|
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
|
|
|
});
|