forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpushes.integration.test.ts
More file actions
259 lines (210 loc) · 7.92 KB
/
pushes.integration.test.ts
File metadata and controls
259 lines (210 loc) · 7.92 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import { describe, it, expect, beforeEach } from 'vitest';
import {
writeAudit,
getPush,
getPushes,
deletePush,
authorise,
reject,
cancel,
} from '../../../src/db/mongo/pushes';
import { Action } from '../../../src/proxy/actions';
// Only run in CI where MongoDB is available, or when explicitly enabled locally
const shouldRunMongoTests = process.env.CI === 'true' || process.env.RUN_MONGO_TESTS === 'true';
describe.runIf(shouldRunMongoTests)('MongoDB Pushes Integration Tests', () => {
const createTestAction = (overrides: Partial<Action> = {}): Action => {
const timestamp = Date.now();
const action = new Action(
overrides.id || `test-push-${timestamp}`,
overrides.type || 'push',
overrides.method || 'POST',
overrides.timestamp || timestamp,
overrides.url || 'https://github.com/test/repo.git',
);
// Set default values for query-relevant fields
action.error = overrides.error ?? false;
action.blocked = overrides.blocked ?? true;
action.allowPush = overrides.allowPush ?? false;
action.authorised = overrides.authorised ?? false;
action.canceled = overrides.canceled ?? false;
action.rejected = overrides.rejected ?? false;
return action;
};
describe('writeAudit', () => {
it('should write an action to the database', async () => {
const action = createTestAction({ id: 'write-audit-test' });
await writeAudit(action);
const retrieved = await getPush('write-audit-test');
expect(retrieved).not.toBeNull();
expect(retrieved?.id).toBe('write-audit-test');
});
it('should upsert an existing action', async () => {
const action = createTestAction({ id: 'upsert-test' });
await writeAudit(action);
action.blocked = false;
action.allowPush = true;
await writeAudit(action);
const retrieved = await getPush('upsert-test');
expect(retrieved?.blocked).toBe(false);
expect(retrieved?.allowPush).toBe(true);
});
it('should throw error for invalid id', async () => {
const action = createTestAction();
(action as any).id = 123; // Invalid: should be string
await expect(writeAudit(action)).rejects.toThrow('Invalid id');
});
it('should strip _id from action before saving', async () => {
const action = createTestAction({ id: 'strip-id-test' });
(action as any)._id = 'should-be-removed';
await writeAudit(action);
const retrieved = await getPush('strip-id-test');
expect(retrieved).not.toBeNull();
expect(retrieved?.id).toBe('strip-id-test');
});
});
describe('getPush', () => {
it('should retrieve a push by id', async () => {
const action = createTestAction({ id: 'get-push-test' });
await writeAudit(action);
const result = await getPush('get-push-test');
expect(result).not.toBeNull();
expect(result?.id).toBe('get-push-test');
expect(result?.type).toBe('push');
});
it('should return null for non-existent push', async () => {
const result = await getPush('non-existent-push');
expect(result).toBeNull();
});
it('should return an Action instance', async () => {
const action = createTestAction({ id: 'action-instance-test' });
await writeAudit(action);
const result = await getPush('action-instance-test');
expect(Object.getPrototypeOf(result)).toBe(Action.prototype);
});
});
describe('getPushes', () => {
beforeEach(async () => {
// Create test data with default query-matching values
await writeAudit(
createTestAction({
id: 'push-list-1',
blocked: true,
allowPush: false,
authorised: false,
error: false,
}),
);
await writeAudit(
createTestAction({
id: 'push-list-2',
blocked: true,
allowPush: false,
authorised: false,
error: false,
}),
);
await writeAudit(
createTestAction({
id: 'push-authorised',
blocked: true,
allowPush: false,
authorised: true,
error: false,
}),
);
});
it('should retrieve pushes matching default query', async () => {
const result = await getPushes();
// Default query: error=false, blocked=true, allowPush=false, authorised=false, type=push
const matchingPushes = result.filter((p) => ['push-list-1', 'push-list-2'].includes(p.id));
expect(matchingPushes.length).toBe(2);
});
it('should filter pushes by custom query', async () => {
const result = await getPushes({ authorised: true });
const authorisedPush = result.find((p) => p.id === 'push-authorised');
expect(authorisedPush).toBeDefined();
});
it('should return projected fields only', async () => {
const result = await getPushes();
// Check that projection is applied (no _id, but has id)
result.forEach((push) => {
expect((push as any)._id).toBeUndefined();
expect(push.id).toBeDefined();
});
});
});
describe('deletePush', () => {
it('should delete a push by id', async () => {
const action = createTestAction({ id: 'delete-test' });
await writeAudit(action);
await deletePush('delete-test');
const result = await getPush('delete-test');
expect(result).toBeNull();
});
it('should not throw when deleting non-existent push', async () => {
await expect(deletePush('non-existent')).resolves.not.toThrow();
});
});
describe('authorise', () => {
it('should authorise a push and update flags', async () => {
const action = createTestAction({
id: 'authorise-test',
authorised: false,
canceled: true,
rejected: true,
});
await writeAudit(action);
const result = await authorise('authorise-test', { note: 'approved' });
expect(result.message).toBe('authorised authorise-test');
const updated = await getPush('authorise-test');
expect(updated?.authorised).toBe(true);
expect(updated?.canceled).toBe(false);
expect(updated?.rejected).toBe(false);
expect(updated?.attestation).toEqual({ note: 'approved' });
});
it('should throw error for non-existent push', async () => {
await expect(authorise('non-existent', {})).rejects.toThrow('push non-existent not found');
});
});
describe('reject', () => {
it('should reject a push and update flags', async () => {
const action = createTestAction({
id: 'reject-test',
authorised: true,
canceled: true,
rejected: false,
});
await writeAudit(action);
const result = await reject('reject-test', { reason: 'policy violation' });
expect(result.message).toBe('reject reject-test');
const updated = await getPush('reject-test');
expect(updated?.authorised).toBe(false);
expect(updated?.canceled).toBe(false);
expect(updated?.rejected).toBe(true);
expect(updated?.attestation).toEqual({ reason: 'policy violation' });
});
it('should throw error for non-existent push', async () => {
await expect(reject('non-existent', {})).rejects.toThrow('push non-existent not found');
});
});
describe('cancel', () => {
it('should cancel a push and update flags', async () => {
const action = createTestAction({
id: 'cancel-test',
authorised: true,
canceled: false,
rejected: true,
});
await writeAudit(action);
const result = await cancel('cancel-test');
expect(result.message).toBe('canceled cancel-test');
const updated = await getPush('cancel-test');
expect(updated?.authorised).toBe(false);
expect(updated?.canceled).toBe(true);
expect(updated?.rejected).toBe(false);
});
it('should throw error for non-existent push', async () => {
await expect(cancel('non-existent')).rejects.toThrow('push non-existent not found');
});
});
});