-
Notifications
You must be signed in to change notification settings - Fork 296
Expand file tree
/
Copy pathadmin-feature-flags.controller.spec.ts
More file actions
132 lines (115 loc) · 4.11 KB
/
admin-feature-flags.controller.spec.ts
File metadata and controls
132 lines (115 loc) · 4.11 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { NotFoundException } from '@nestjs/common';
import { Test, type TestingModule } from '@nestjs/testing';
jest.mock('@db', () => ({
db: {
organization: {
findUnique: jest.fn(),
},
},
}));
jest.mock('../auth/platform-admin.guard', () => ({
PlatformAdminGuard: class MockGuard {
canActivate() {
return true;
}
},
}));
jest.mock('../admin-organizations/admin-audit-log.interceptor', () => ({
AdminAuditLogInterceptor: class MockInterceptor {
intercept(_ctx: unknown, next: { handle: () => unknown }) {
return next.handle();
}
},
}));
// eslint-disable-next-line @typescript-eslint/no-require-imports
import { AdminFeatureFlagsController } from './admin-feature-flags.controller';
import { AdminFeatureFlagsService } from './admin-feature-flags.service';
import { PlatformAdminGuard } from '../auth/platform-admin.guard';
import { AdminAuditLogInterceptor } from '../admin-organizations/admin-audit-log.interceptor';
// eslint-disable-next-line @typescript-eslint/no-require-imports, @typescript-eslint/no-var-requires
const mockDb = require('@db').db as {
organization: { findUnique: jest.Mock };
};
describe('AdminFeatureFlagsController', () => {
let controller: AdminFeatureFlagsController;
let service: {
listForOrganization: jest.Mock;
setFlagForOrganization: jest.Mock;
};
beforeEach(async () => {
jest.clearAllMocks();
service = {
listForOrganization: jest.fn(),
setFlagForOrganization: jest.fn(),
};
const module: TestingModule = await Test.createTestingModule({
controllers: [AdminFeatureFlagsController],
providers: [{ provide: AdminFeatureFlagsService, useValue: service }],
})
.overrideGuard(PlatformAdminGuard)
.useValue({ canActivate: () => true })
.overrideInterceptor(AdminAuditLogInterceptor)
.useValue({ intercept: (_ctx: unknown, next: { handle: () => unknown }) => next.handle() })
.compile();
controller = module.get(AdminFeatureFlagsController);
});
describe('list', () => {
it('throws NotFoundException when the org does not exist', async () => {
mockDb.organization.findUnique.mockResolvedValue(null);
await expect(controller.list('org_missing')).rejects.toBeInstanceOf(
NotFoundException,
);
expect(service.listForOrganization).not.toHaveBeenCalled();
});
it('returns flags wrapped in { data } when the org exists', async () => {
mockDb.organization.findUnique.mockResolvedValue({ id: 'org_1' });
service.listForOrganization.mockResolvedValue([
{
key: 'is-timeline-enabled',
name: 'is-timeline-enabled',
description: '',
active: true,
enabled: true,
createdAt: null,
},
]);
const result = await controller.list('org_1');
expect(service.listForOrganization).toHaveBeenCalledWith('org_1');
expect(result.data).toHaveLength(1);
expect(result.data[0].key).toBe('is-timeline-enabled');
});
});
describe('update', () => {
it('throws NotFoundException when the org does not exist', async () => {
mockDb.organization.findUnique.mockResolvedValue(null);
await expect(
controller.update('org_missing', {
flagKey: 'is-timeline-enabled',
enabled: true,
}),
).rejects.toBeInstanceOf(NotFoundException);
expect(service.setFlagForOrganization).not.toHaveBeenCalled();
});
it('delegates to the service with orgId, orgName, flagKey, and enabled', async () => {
mockDb.organization.findUnique.mockResolvedValue({
id: 'org_1',
name: 'Acme',
});
service.setFlagForOrganization.mockResolvedValue({
key: 'is-timeline-enabled',
enabled: false,
});
const result = await controller.update('org_1', {
flagKey: 'is-timeline-enabled',
enabled: false,
});
expect(service.setFlagForOrganization).toHaveBeenCalledWith({
orgId: 'org_1',
orgName: 'Acme',
flagKey: 'is-timeline-enabled',
enabled: false,
});
expect(result.data.enabled).toBe(false);
});
});
});