-
Notifications
You must be signed in to change notification settings - Fork 264
Expand file tree
/
Copy pathgerrit.test.ts
More file actions
136 lines (115 loc) · 3.95 KB
/
gerrit.test.ts
File metadata and controls
136 lines (115 loc) · 3.95 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
import { expect, test, vi, describe, beforeEach } from 'vitest';
import { getGerritReposFromConfig } from './gerrit';
import fetch from 'cross-fetch';
vi.mock('cross-fetch', () => {
return {
default: vi.fn(),
};
});
vi.mock('@sourcebot/shared', () => ({
createLogger: () => ({
debug: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
}),
}));
vi.mock('./utils', () => ({
measure: async (fn: () => any) => {
const data = await fn();
return { durationMs: 0, data };
},
fetchWithRetry: async (fn: () => any) => fn(),
}));
describe('getGerritReposFromConfig', () => {
beforeEach(() => {
vi.clearAllMocks();
});
test('sends Basic Auth header when username and password are provided', async () => {
const mockFetch = fetch as unknown as ReturnType<typeof vi.fn>;
mockFetch.mockResolvedValue({
ok: true,
text: async () => '[]',
json: async () => ([]),
});
const config = {
type: 'gerrit' as const,
url: 'https://gerrit.example.com',
username: 'user',
password: 'password',
};
await getGerritReposFromConfig(config);
const expectedToken = Buffer.from('user:password').toString('base64');
expect(mockFetch).toHaveBeenCalledWith(
expect.stringContaining('https://gerrit.example.com/projects/?S=0'),
expect.objectContaining({
headers: {
Authorization: `Basic ${expectedToken}`,
},
})
);
});
test('does not send Authorization header when credentials are missing', async () => {
const mockFetch = fetch as unknown as ReturnType<typeof vi.fn>;
mockFetch.mockResolvedValue({
ok: true,
text: async () => '[]',
json: async () => ([]),
});
const config = {
type: 'gerrit' as const,
url: 'https://gerrit.example.com',
};
await getGerritReposFromConfig(config);
expect(mockFetch).toHaveBeenCalledWith(
expect.stringContaining('https://gerrit.example.com/projects/?S=0'),
expect.objectContaining({
headers: {},
})
);
});
test('does not send Authorization header when only one credential is provided', async () => {
const mockFetch = fetch as unknown as ReturnType<typeof vi.fn>;
mockFetch.mockResolvedValue({
ok: true,
text: async () => '[]',
json: async () => ([]),
});
const config = {
type: 'gerrit' as const,
url: 'https://gerrit.example.com',
username: 'user',
};
await getGerritReposFromConfig(config);
expect(mockFetch).toHaveBeenCalledWith(
expect.stringContaining('https://gerrit.example.com/projects/?S=0'),
expect.objectContaining({
headers: {},
})
);
});
test('correctly encodes credentials with special characters', async () => {
const mockFetch = fetch as unknown as ReturnType<typeof vi.fn>;
mockFetch.mockResolvedValue({
ok: true,
text: async () => '[]',
json: async () => ([]),
});
const config = {
type: 'gerrit' as const,
url: 'https://gerrit.example.com',
username: 'user@example.com',
password: 'p@ss:w0rd',
};
await getGerritReposFromConfig(config);
const expectedToken = Buffer.from('user@example.com:p@ss:w0rd').toString('base64');
expect(mockFetch).toHaveBeenCalledWith(
expect.stringContaining('https://gerrit.example.com/projects/?S=0'),
expect.objectContaining({
headers: {
Authorization: `Basic ${expectedToken}`,
},
})
);
});
});