forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.controller.spec.ts
More file actions
96 lines (80 loc) · 2.98 KB
/
Copy pathgithub.controller.spec.ts
File metadata and controls
96 lines (80 loc) · 2.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { ForbiddenException, NotFoundException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { GithubController } from './github.controller';
import { GithubSyncService } from './github-sync.service';
import { UserRole } from '../common/enums';
import { Repository, User } from '../common/entities';
describe('GithubController', () => {
let controller: GithubController;
let syncService: {
syncRepository: jest.Mock;
findRepositoryByOwnerAndName: jest.Mock;
};
beforeEach(async () => {
syncService = {
syncRepository: jest.fn(),
findRepositoryByOwnerAndName: jest.fn(),
};
const module: TestingModule = await Test.createTestingModule({
controllers: [GithubController],
providers: [{ provide: GithubSyncService, useValue: syncService }],
}).compile();
controller = module.get<GithubController>(GithubController);
});
it('should be defined', () => {
expect(controller).toBeDefined();
});
it('rejects unprivileged user role (e.g. contributor only)', async () => {
const mockReq = {
user: {
id: 'u-1',
roles: [UserRole.CONTRIBUTOR],
} as User,
} as any;
await expect(
controller.sync('acme', 'widgets', mockReq),
).rejects.toThrow(ForbiddenException);
expect(syncService.syncRepository).not.toHaveBeenCalled();
});
it('rejects sync for untracked repository', async () => {
const mockReq = {
user: {
id: 'u-2',
roles: [UserRole.MAINTAINER],
} as User,
} as any;
syncService.findRepositoryByOwnerAndName.mockResolvedValue(null);
await expect(
controller.sync('torvalds', 'linux', mockReq),
).rejects.toThrow(NotFoundException);
expect(syncService.syncRepository).not.toHaveBeenCalled();
});
it('allows maintainer to sync tracked repository', async () => {
const mockReq = {
user: {
id: 'u-2',
roles: [UserRole.MAINTAINER],
} as User,
} as any;
const mockRepo = { id: 'repo-1', owner: 'acme', name: 'widgets' } as Repository;
syncService.findRepositoryByOwnerAndName.mockResolvedValue(mockRepo);
syncService.syncRepository.mockResolvedValue(mockRepo);
const result = await controller.sync('acme', 'widgets', mockReq);
expect(result).toEqual(mockRepo);
expect(syncService.findRepositoryByOwnerAndName).toHaveBeenCalledWith('acme', 'widgets');
expect(syncService.syncRepository).toHaveBeenCalledWith('acme', 'widgets');
});
it('allows sponsor to sync tracked repository', async () => {
const mockReq = {
user: {
id: 'u-3',
roles: [UserRole.SPONSOR],
} as User,
} as any;
const mockRepo = { id: 'repo-1', owner: 'acme', name: 'widgets' } as Repository;
syncService.findRepositoryByOwnerAndName.mockResolvedValue(mockRepo);
syncService.syncRepository.mockResolvedValue(mockRepo);
const result = await controller.sync('acme', 'widgets', mockReq);
expect(result).toEqual(mockRepo);
});
});