-
Notifications
You must be signed in to change notification settings - Fork 59
Expand file tree
/
Copy pathconfig.test.ts
More file actions
212 lines (184 loc) · 8.34 KB
/
config.test.ts
File metadata and controls
212 lines (184 loc) · 8.34 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
import { type UrlAbapTarget, isUrlTarget } from '@sap-ux/system-access';
import { getConfigForLogging, validateConfig } from '../../../src/base/config';
import type { AbapDeployConfig } from '../../../src/types';
import { isAppStudio } from '@sap-ux/btp-utils';
jest.mock('@sap-ux/btp-utils');
const mockIsAppStudio = isAppStudio as jest.Mock;
describe('base/config', () => {
test('isUrlTarget', () => {
expect(isUrlTarget({ url: '~url' })).toBe(true);
expect(isUrlTarget({ destination: '~destination' })).toBe(false);
});
describe('getConfigForLogging', () => {
test('no credentials included', () => {
const config = {
app: {
name: 'world'
}
} as AbapDeployConfig;
const configForLogging = getConfigForLogging(config);
expect(configForLogging).toBe(config);
});
test('credentials are being removed', () => {
const config = {
app: {
name: 'world'
},
credentials: {
username: '~user',
password: '~password'
}
} as AbapDeployConfig;
const configForLogging = getConfigForLogging(config);
expect(configForLogging.credentials).toBe('hidden');
expect(configForLogging.app).toEqual(config.app);
});
});
describe('validateConfig', () => {
const validConfig = {
app: {
name: '~name',
desription: '~description',
package: '~package',
transport: '~transport'
},
target: {
url: 'http://target.example'
} as UrlAbapTarget
};
const mockLogger = { warn: jest.fn() } as any;
beforeEach(() => {
mockLogger.warn.mockClear();
});
test('valid config', () => {
mockIsAppStudio.mockReturnValueOnce(false);
expect(() => validateConfig(validConfig)).not.toThrow();
mockIsAppStudio.mockReturnValueOnce(true);
expect(() => validateConfig({ app: validConfig.app, target: { destination: '~dest' } })).not.toThrow();
});
test('config missing', () => {
expect(() => validateConfig(undefined)).toThrow();
});
test('incorrect app', () => {
const config = { app: { ...validConfig.app }, target: validConfig.target };
delete (config as any).app;
expect(() => validateConfig(config)).toThrow();
});
test('incorrect target', () => {
const config = { app: validConfig.app, target: { ...validConfig.target } };
delete (config.target as any).url;
mockIsAppStudio.mockReturnValueOnce(false);
expect(() => validateConfig(config)).toThrow('target-url');
mockIsAppStudio.mockReturnValueOnce(true);
expect(() => validateConfig(config)).toThrow('target-destination');
delete (config as any).target;
expect(() => validateConfig(config)).toThrow('target');
});
test('zeros added to client', () => {
const config = { app: validConfig.app, target: { ...validConfig.target } };
config.target.client = '1';
validateConfig(config);
expect(config.target.client).toBe('001');
});
test('lowercase package is normalized to uppercase and warning is logged', () => {
const config = {
app: { ...validConfig.app, package: '$tmp' },
target: { ...validConfig.target }
};
validateConfig(config, mockLogger);
expect(config.app.package).toBe('$TMP');
expect(mockLogger.warn).toHaveBeenCalledWith(expect.stringContaining("'$tmp' was normalized to '$TMP'"));
});
test('mixed-case package is normalized to uppercase and warning is logged', () => {
const config = {
app: { ...validConfig.app, package: 'MyPackage' },
target: { ...validConfig.target }
};
validateConfig(config, mockLogger);
expect(config.app.package).toBe('MYPACKAGE');
expect(mockLogger.warn).toHaveBeenCalledTimes(1);
});
test('already-uppercase package is left unchanged and no warning is logged', () => {
const config = {
app: { ...validConfig.app, package: '$TMP' },
target: { ...validConfig.target }
};
validateConfig(config, mockLogger);
expect(config.app.package).toBe('$TMP');
expect(mockLogger.warn).not.toHaveBeenCalled();
});
test('normalization works without a logger (no crash)', () => {
const config = {
app: { ...validConfig.app, package: '$tmp' },
target: { ...validConfig.target }
};
expect(() => validateConfig(config)).not.toThrow();
expect(config.app.package).toBe('$TMP');
});
describe('validateCredentials', () => {
test('throws when username is plaintext (no env: prefix)', () => {
// given a config with a plaintext username
const config = {
...validConfig,
credentials: { username: 'admin', password: 'env:MY_PASSWORD' }
} as AbapDeployConfig;
// when validateConfig is called
// then it throws with the expected message
expect(() => validateConfig(config)).toThrow(
'Credentials must be provided as environment variable references'
);
});
test('throws when password is plaintext (no env: prefix)', () => {
// given a config with a plaintext password
const config = {
...validConfig,
credentials: { username: 'env:MY_USER', password: 'secret' }
} as AbapDeployConfig;
// when validateConfig is called
// then it throws with the expected message
expect(() => validateConfig(config)).toThrow(
'Credentials must be provided as environment variable references'
);
});
test('throws when both username and password are plaintext', () => {
// given a config with plaintext username and password
const config = {
...validConfig,
credentials: { username: 'admin', password: 'secret' }
} as AbapDeployConfig;
// when validateConfig is called
// then it throws with the expected message
expect(() => validateConfig(config)).toThrow(
'Credentials must be provided as environment variable references'
);
});
test('passes when both username and password use env: prefix', () => {
// given a config with env-referenced credentials
const config = {
...validConfig,
credentials: { username: 'env:MY_USER', password: 'env:MY_PASSWORD' }
} as AbapDeployConfig;
// when validateConfig is called
// then it does not throw
expect(() => validateConfig(config)).not.toThrow();
});
test('passes when credentials are absent', () => {
// given a config without credentials
const config = { ...validConfig } as AbapDeployConfig;
// when validateConfig is called
// then it does not throw
expect(() => validateConfig(config)).not.toThrow();
});
test('passes when only username is set with env: prefix and password is absent', () => {
// given a config with only username set via env:
const config = {
...validConfig,
credentials: { username: 'env:MY_USER' }
} as unknown as AbapDeployConfig;
// when validateConfig is called
// then it does not throw
expect(() => validateConfig(config)).not.toThrow();
});
});
});
});