forked from renovatebot/renovate
/
index.spec.ts
212 lines (189 loc) · 6.19 KB
/
index.spec.ts
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
import * as httpMock from '../../../../test/http-mock';
import { getName, mocked } from '../../../../test/util';
import * as _hostRules from '../../../util/host-rules';
import { setBaseUrl } from '../../../util/http/gitea';
import { PRESET_INVALID_JSON, PRESET_NOT_FOUND } from '../util';
import * as gitea from '.';
jest.mock('../../../util/host-rules');
const hostRules = mocked(_hostRules);
const giteaApiHost = gitea.Endpoint;
const basePath = '/repos/some/repo/contents';
describe(getName(), () => {
beforeEach(() => {
httpMock.setup();
hostRules.find.mockReturnValue({ token: 'abc' });
setBaseUrl(giteaApiHost);
});
afterEach(() => httpMock.reset());
describe('fetchJSONFile()', () => {
it('returns JSON', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/some-filename.json`)
.reply(200, {
content: Buffer.from('{"from":"api"}').toString('base64'),
});
const res = await gitea.fetchJSONFile(
'some/repo',
'some-filename.json',
giteaApiHost
);
expect(res).toMatchSnapshot();
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
describe('getPreset()', () => {
it('tries default then renovate', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(404, {})
.get(`${basePath}/renovate.json`)
.reply(200, {});
await expect(
gitea.getPreset({ packageName: 'some/repo' })
).rejects.toThrow();
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws if no content', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(200, {});
await expect(
gitea.getPreset({ packageName: 'some/repo' })
).rejects.toThrow(PRESET_INVALID_JSON);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('throws if fails to parse', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(200, {
content: Buffer.from('not json').toString('base64'),
});
await expect(
gitea.getPreset({ packageName: 'some/repo' })
).rejects.toThrow(PRESET_INVALID_JSON);
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('should return default.json', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(200, {
content: Buffer.from('{"foo":"bar"}').toString('base64'),
});
const content = await gitea.getPreset({ packageName: 'some/repo' });
expect(content).toEqual({ foo: 'bar' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('should query preset within the file', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/somefile.json`)
.reply(200, {
content: Buffer.from('{"somename":{"foo":"bar"}}').toString('base64'),
});
const content = await gitea.getPreset({
packageName: 'some/repo',
presetName: 'somefile/somename',
});
expect(content).toEqual({ foo: 'bar' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('should query subpreset', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/somefile.json`)
.reply(200, {
content: Buffer.from(
'{"somename":{"somesubname":{"foo":"bar"}}}'
).toString('base64'),
});
const content = await gitea.getPreset({
packageName: 'some/repo',
presetName: 'somefile/somename/somesubname',
});
expect(content).toEqual({ foo: 'bar' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('should return custom.json', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/custom.json`)
.reply(200, {
content: Buffer.from('{"foo":"bar"}').toString('base64'),
});
const content = await gitea.getPreset({
packageName: 'some/repo',
presetName: 'custom',
});
expect(content).toEqual({ foo: 'bar' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('should query custom paths', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/path%2Fcustom.json`)
.reply(200, {
content: Buffer.from('{"foo":"bar"}').toString('base64'),
});
const content = await gitea.getPreset({
packageName: 'some/repo',
presetName: 'custom',
presetPath: 'path',
});
expect(content).toEqual({ foo: 'bar' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('should throws not-found', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/somefile.json`)
.reply(200, {
content: Buffer.from('{}').toString('base64'),
});
await expect(
gitea.getPreset({
packageName: 'some/repo',
presetName: 'somefile/somename/somesubname',
})
).rejects.toThrow(PRESET_NOT_FOUND);
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
describe('getPresetFromEndpoint()', () => {
it('uses default endpoint', async () => {
httpMock
.scope(giteaApiHost)
.get(`${basePath}/default.json`)
.reply(200, {
content: Buffer.from('{"from":"api"}').toString('base64'),
});
expect(
await gitea.getPresetFromEndpoint('some/repo', 'default', undefined)
).toEqual({ from: 'api' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
it('uses custom endpoint', async () => {
httpMock
.scope('https://api.gitea.example.org')
.get(`${basePath}/default.json`)
.reply(200, {
content: Buffer.from('{"from":"api"}').toString('base64'),
});
expect(
await gitea
.getPresetFromEndpoint(
'some/repo',
'default',
undefined,
'https://api.gitea.example.org'
)
.catch(() => ({ from: 'api' }))
).toEqual({ from: 'api' });
expect(httpMock.getTrace()).toMatchSnapshot();
});
});
});