/
host-rules.spec.ts
171 lines (170 loc) · 4.71 KB
/
host-rules.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
import { getName } from '../../test/util';
import { PLATFORM_TYPE_AZURE } from '../constants/platforms';
import * as datasourceNuget from '../datasource/nuget';
import { add, clear, find, findAll, hosts } from './host-rules';
describe(getName(), () => {
beforeEach(() => {
clear();
});
describe('add()', () => {
it('throws if both domainName and hostName', () => {
expect(() =>
add({
hostType: PLATFORM_TYPE_AZURE,
domainName: 'github.com',
hostName: 'api.github.com',
})
).toThrow();
});
it('throws if both domainName and baseUrl', () => {
expect(() =>
add({
hostType: PLATFORM_TYPE_AZURE,
domainName: 'github.com',
baseUrl: 'https://api.github.com',
})
).toThrow();
});
it('throws if both hostName and baseUrl', () => {
expect(() =>
add({
hostType: PLATFORM_TYPE_AZURE,
hostName: 'api.github.com',
baseUrl: 'https://api.github.com',
})
).toThrow();
});
it('supports baseUrl-only', () => {
add({
baseUrl: 'https://some.endpoint',
username: 'user1',
password: 'pass1',
});
expect(find({ url: 'https://some.endpoint/v3/' })).toMatchSnapshot();
});
});
describe('find()', () => {
beforeEach(() => {
clear();
});
it('warns and returns empty for bad search', () => {
expect(find({ abc: 'def' } as any)).toEqual({});
});
it('needs exact host matches', () => {
add({
hostType: datasourceNuget.id,
hostName: 'nuget.org',
username: 'root',
password: 'p4$$w0rd',
token: undefined,
});
expect(find({ hostType: datasourceNuget.id })).toMatchSnapshot();
expect(
find({ hostType: datasourceNuget.id, url: 'https://nuget.org' })
).not.toEqual({});
expect(
find({ hostType: datasourceNuget.id, url: 'https://not.nuget.org' })
).toEqual({});
expect(
find({ hostType: datasourceNuget.id, url: 'https://not-nuget.org' })
).toEqual({});
});
it('matches on empty rules', () => {
add({
enabled: true,
});
expect(
find({ hostType: datasourceNuget.id, url: 'https://api.github.com' })
).toEqual({ enabled: true });
});
it('matches on hostType', () => {
add({
hostType: datasourceNuget.id,
token: 'abc',
});
expect(
find({ hostType: datasourceNuget.id, url: 'https://nuget.local/api' })
).toMatchSnapshot();
});
it('matches on domainName', () => {
add({
domainName: 'github.com',
token: 'def',
});
expect(
find({ hostType: datasourceNuget.id, url: 'https://api.github.com' })
.token
).toEqual('def');
});
it('matches on hostName', () => {
add({
hostName: 'nuget.local',
token: 'abc',
});
expect(
find({ hostType: datasourceNuget.id, url: 'https://nuget.local/api' })
).toMatchSnapshot();
});
it('matches on hostType and endpoint', () => {
add({
hostType: datasourceNuget.id,
baseUrl: 'https://nuget.local/api',
token: 'abc',
});
expect(
find({ hostType: datasourceNuget.id, url: 'https://nuget.local/api' })
.token
).toEqual('abc');
});
it('matches on endpoint subresource', () => {
add({
hostType: datasourceNuget.id,
baseUrl: 'https://nuget.local/api',
token: 'abc',
});
expect(
find({
hostType: datasourceNuget.id,
url: 'https://nuget.local/api/sub-resource',
})
).toMatchSnapshot();
});
it('returns hosts', () => {
add({
hostType: datasourceNuget.id,
token: 'aaaaaa',
});
add({
hostType: datasourceNuget.id,
baseUrl: 'https://nuget.local/api',
token: 'abc',
});
add({
hostType: datasourceNuget.id,
hostName: 'my.local.registry',
token: 'def',
});
const res = hosts({
hostType: datasourceNuget.id,
});
expect(res).toMatchSnapshot();
expect(res).toHaveLength(2);
});
});
describe('findAll()', () => {
it('warns and returns empty for bad search', () => {
expect(findAll({ abc: 'def' } as any)).toEqual([]);
});
it('needs exact host matches', () => {
const hostRule = {
hostType: 'nuget',
hostName: 'nuget.org',
username: 'root',
password: 'p4$$w0rd',
};
add(hostRule);
expect(findAll({ hostType: 'nuget' })).toHaveLength(1);
expect(findAll({ hostType: 'nuget' })[0]).toMatchObject(hostRule);
});
});
});