forked from bcgov/nrts-prc-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_helper.js
107 lines (96 loc) · 2.61 KB
/
test_helper.js
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
const express = require('express');
const bodyParser = require('body-parser');
const DatabaseCleaner = require('database-cleaner');
const dbCleaner = new DatabaseCleaner('mongodb');
const mongoose = require('mongoose');
const mongooseOpts = require('../../config/mongoose_options').mongooseOptions;
const mongoDbMemoryServer = require('mongodb-memory-server');
const _ = require('lodash');
const app = express();
let mongoServer;
mongoose.Promise = global.Promise;
setupAppServer();
jest.setTimeout(10000);
beforeAll(async () => {
mongoServer = new mongoDbMemoryServer.default({
instance: {},
binary: {
version: '3.2.21', // Mongo Version
},
});
const mongoUri = await mongoServer.getConnectionString();
await mongoose.connect(mongoUri, mongooseOpts, (err) => {
if (err) console.error(err);
});
});
afterEach(done => {
if (mongoose.connection && mongoose.connection.db) {
dbCleaner.clean(mongoose.connection.db, () => {
done()
});
} else {
done();
}
});
afterAll(async () => {
await mongoose.disconnect();
await mongoServer.stop();
});
function setupAppServer() {
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
}
function createSwaggerParams(fieldNames, additionalValues = {}, username = null) {
let defaultParams = defaultProtectedParams(fieldNames, username);
let swaggerObject = {
swagger: {
params: _.merge(defaultParams, additionalValues),
operation: {
'x-security-scopes': ['sysadmin', 'public']
}
}
}
return swaggerObject;
}
function createPublicSwaggerParams(fieldNames, additionalValues = {}) {
let defaultParams = defaultPublicParams(fieldNames);
let swaggerObject = {
swagger: {
params: _.merge(defaultParams, additionalValues)
}
}
return swaggerObject;
}
function defaultProtectedParams(fieldNames, username = null) {
return {
auth_payload: {
scopes: ['sysadmin', 'public'],
// This value in the real world is pulled from the keycloak user. It will look something like
// idir/arwhilla
preferred_username: username
},
fields: {
value: _.cloneDeep(fieldNames)
}
};
}
function defaultPublicParams(fieldNames) {
return {
fields: {
value: _.cloneDeep(fieldNames)
}
};
}
function buildParams(nameValueMapping) {
let paramObj = {}
_.mapKeys(nameValueMapping, function(value, key) {
paramObj[key] = { value: value };
});
return paramObj;
}
exports.createSwaggerParams = createSwaggerParams;
exports.createPublicSwaggerParams = createPublicSwaggerParams;
exports.buildParams = buildParams;
exports.app = app;