forked from medic/cht-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.conf.js
129 lines (115 loc) · 4.17 KB
/
base.conf.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
const fs = require('fs');
const utils = require('./utils');
const constants = require('./constants');
const auth = require('./auth')();
const browserLogStream = fs.createWriteStream(__dirname + '/../tests/logs/browser.console.log');
class BaseConfig {
constructor(testSrcDir, { headless=true }={}) {
const chromeArgs = [ '--window-size=1024,768' ];
utils.setDebug(!headless);
if (headless) {
chromeArgs.push('--headless', '--disable-gpu');
}
this.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
specs: [`${testSrcDir}/**/*.js`],
framework: 'jasmine2',
capabilities: {
browserName: 'chrome',
chromeOptions: {
// chromedriver 75 is w3c enabled by default and causes some actions to be impossible to perform
// eg: browser.actions().sendKeys(protractor.Key.TAB).perform()
// https://github.com/angular/protractor/issues/5261
w3c: false,
args: chromeArgs
},
},
jasmineNodeOpts: {
// makes default jasmine reporter not display dots for every spec
print: () => {}
},
beforeLaunch: function() {
process.on('uncaughtException', function() {
utils.reporter.jasmineDone();
utils.reporter.afterLaunch();
});
return new Promise(function(resolve) {
utils.reporter.beforeLaunch(resolve);
});
},
afterLaunch: function(exitCode) {
return new Promise(function(resolve) {
utils.reporter.afterLaunch(resolve.bind(this, exitCode));
});
},
onPrepare: () => {
jasmine.getEnv().addReporter(utils.specReporter);
jasmine.getEnv().addReporter(utils.reporter);
browser.waitForAngularEnabled(false);
browser.driver.wait(startApi(), 135 * 1000, 'API took too long to start up');
browser.driver.sleep(10000); // wait for startup to complete
afterEach(() => {
browser.manage().logs().get('browser').then(logs => {
logs
.map(log => `[${log.level.name_}] ${log.message}\n`)
.forEach(log => browserLogStream.write(log));
browserLogStream.write('\n~~~~~~~~~~~~~~~~~~~~~\n\n');
});
});
return login(browser);
},
};
}
}
module.exports = BaseConfig;
const runAndLog = (msg, func) => {
console.log(`API startup: ${msg}`);
return func();
};
const startApi = () => listenForApi()
.then(() => runAndLog('Settings setup', setupSettings))
.then(() => runAndLog('User contact doc setup', utils.setUserContactDoc))
.then(() => runAndLog('User setup', setupUser));
const listenForApi = () => {
console.log('Checking API');
return utils.request({ path: '/api/info' })
.catch(() => {
console.log('API check failed, trying again in 5 seconds');
return new Promise(resolve => {
setTimeout(() => {
resolve(listenForApi());
}, 5000);
});
});
};
const getLoginUrl = () => {
const redirectUrl = encodeURIComponent(`/${constants.DB_NAME}/_design/${constants.MAIN_DDOC_NAME}/_rewrite/#/messages`);
return `http://${constants.API_HOST}:${constants.API_PORT}/${constants.DB_NAME}/login?redirect=${redirectUrl}`;
};
const login = browser => {
browser.driver.get(getLoginUrl());
browser.driver.findElement(by.name('user')).sendKeys(auth.user);
browser.driver.findElement(by.name('password')).sendKeys(auth.pass);
browser.driver.findElement(by.id('login')).click();
// Login takes some time, so wait until it's done.
const bootstrappedCheck = () => element(by.css('body.bootstrapped')).isPresent();
return browser.driver.wait(bootstrappedCheck, 20 * 1000, 'Login should be complete within 20 seconds');
};
const setupSettings = () => {
return utils.request({
path: '/api/v1/settings',
method: 'PUT',
body: JSON.stringify({ setup_complete: true }),
headers: { 'Content-Type': 'application/json' }
});
};
const setupUser = () => {
return utils.getDoc('org.couchdb.user:' + auth.user)
.then(doc => {
doc.contact_id = constants.USER_CONTACT_ID;
doc.known = true;
doc.language = 'en';
doc.roles = ['_admin'];
return utils.saveDoc(doc);
});
};