-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathpython.test.ts
259 lines (221 loc) · 7.03 KB
/
python.test.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
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 fs from 'fs/promises';
import path from 'path';
import * as native from '../js';
import { PyConfiguration } from '../js';
const suite = native.isFallbackBuild() ? xdescribe : describe;
// TODO(ovr): Find what is going wrong with parallel tests & python on Linux
const darwinSuite = process.platform === 'darwin' && !native.isFallbackBuild() ? describe : xdescribe;
async function loadConfigurationFile(fileName: string) {
const fullFileName = path.join(process.cwd(), 'test', fileName);
const content = await fs.readFile(fullFileName, 'utf8');
console.log('content', {
content,
fileName: fullFileName
});
const config = await native.pythonLoadConfig(
content,
{
fileName: fullFileName
}
);
console.log(`loaded config ${fileName}`, config);
return config;
}
const nativeInstance = new native.NativeInstance();
suite('Python Models', () => {
test('models import', async () => {
const fullFileName = path.join(process.cwd(), 'test', 'globals.py');
const content = await fs.readFile(fullFileName, 'utf8');
// Just checking it won't fail
await nativeInstance.loadPythonContext(fullFileName, content);
});
test('models import with sys.path changed', async () => {
const fullFileName = path.join(process.cwd(), 'test', 'globals_w_import_path.py');
const content = await fs.readFile(fullFileName, 'utf8');
// Just checking it won't fail
await nativeInstance.loadPythonContext(fullFileName, content);
});
});
suite('Python Config', () => {
let config: PyConfiguration;
beforeAll(async () => {
config = await loadConfigurationFile('config.py');
});
test('async checkAuth', async () => {
expect(config).toEqual({
schemaPath: 'models',
telemetry: false,
contextToApiScopes: expect.any(Function),
logger: expect.any(Function),
pgSqlPort: 5555,
preAggregationsSchema: expect.any(Function),
checkAuth: expect.any(Function),
extendContext: expect.any(Function),
queryRewrite: expect.any(Function),
repositoryFactory: expect.any(Function),
schemaVersion: expect.any(Function),
contextToRoles: expect.any(Function),
scheduledRefreshContexts: expect.any(Function),
scheduledRefreshTimeZones: expect.any(Function),
});
if (!config.checkAuth) {
throw new Error('checkAuth was not defined in config.py');
}
const result = await config.checkAuth(
{ requestId: 'test' },
'MY_SECRET_TOKEN'
);
expect(result).toEqual({
security_context: {
sub: '1234567890',
iat: 1516239022,
user_id: 42
},
});
});
test('context_to_roles', async () => {
if (!config.contextToRoles) {
throw new Error('contextToRoles was not defined in config.py');
}
expect(await config.contextToRoles({})).toEqual(['admin']);
});
test('context_to_api_scopes', async () => {
if (!config.contextToApiScopes) {
throw new Error('contextToApiScopes was not defined in config.py');
}
expect(await config.contextToApiScopes()).toEqual(['meta', 'data', 'jobs']);
});
test('scheduled_refresh_time_zones', async () => {
if (!config.scheduledRefreshTimeZones) {
throw new Error('scheduledRefreshTimeZones was not defined in config.py');
}
expect(await config.scheduledRefreshTimeZones({})).toEqual(['Europe/Kyiv', 'Antarctica/Troll', 'Australia/Sydney']);
});
test('scheduled_refresh_contexts', async () => {
if (!config.scheduledRefreshContexts) {
throw new Error('scheduledRefreshContexts was not defined in config.py');
}
expect(await config.scheduledRefreshContexts({})).toEqual([
{
securityContext: {
appid: 'test1', u: { prop1: 'value1' }
}
},
{
securityContext: {
appid: 'test2', u: { prop1: 'value2' }
}
},
{
securityContext: {
appid: 'test3', u: { prop1: 'value3' }
}
},
]);
});
test('extend_context', async () => {
if (!config.extendContext) {
throw new Error('extendContext was not defined in config.py');
}
// Without security context
expect(await config.extendContext({})).toEqual({
security_context: {
error: 'missing',
},
});
// With security context
expect(await config.extendContext({
securityContext: { sub: '1234567890', iat: 1516239022, user_id: 42 }
})).toEqual({
security_context: {
extended_by_config: true,
sub: '1234567890',
iat: 1516239022,
user_id: 42
},
});
});
test('repository factory', async () => {
if (!config.repositoryFactory) {
throw new Error('repositoryFactory was not defined in config.py');
}
const ctx = {
securityContext: { schemaPath: path.join(process.cwd(), 'test', 'fixtures', 'schema-tenant-1') }
};
const repository: any = await config.repositoryFactory(ctx);
expect(repository).toEqual({
dataSchemaFiles: expect.any(Function)
});
const files = await repository.dataSchemaFiles();
expect(files).toContainEqual({
fileName: 'test.yml',
content: expect.any(String),
});
expect(files).toContainEqual({
fileName: 'test.yml.jinja',
content: expect.any(String),
});
});
test('cross language converting (js -> python -> js)', async () => {
if (!config.queryRewrite) {
throw new Error('queryRewrite was not defined in config.py');
}
const input = {
str: 'string',
int_number: 1,
int_max_number: Number.MAX_VALUE,
int_min_number: Number.MIN_VALUE,
float_number: 3.1415,
nan_number: NaN,
infinity_number: 10 ** 10000,
bool_true: true,
bool_false: false,
undefined_field: undefined,
obj: {
field_str: 'string',
},
obj_with_nested_object: {
sub_object: {
sub_field_str: 'string'
}
},
array_int: [1, 2, 3, 4, 5],
array_obj: [{
field_str_first: 'string',
}, {
field_str_second: 'string',
}]
};
expect(await config.queryRewrite(input, {})).toEqual(
input
);
});
});
darwinSuite('Old Python Config', () => {
test('test', async () => {
const config = await loadConfigurationFile('old-config.py');
expect(config).toEqual({
schemaPath: 'models',
telemetry: false,
contextToApiScopes: expect.any(Function),
extendContext: expect.any(Function),
logger: expect.any(Function),
pgSqlPort: 5555,
preAggregationsSchema: expect.any(Function),
checkAuth: expect.any(Function),
queryRewrite: expect.any(Function),
repositoryFactory: expect.any(Function),
schemaVersion: expect.any(Function),
contextToRoles: expect.any(Function),
scheduledRefreshContexts: expect.any(Function),
scheduledRefreshTimeZones: expect.any(Function),
});
if (!config.checkAuth) {
throw new Error('checkAuth was not defined in config.py');
}
await config.checkAuth(
{ requestId: 'test' },
'MY_SECRET_TOKEN'
);
});
});