cloudron-e2e-test/cloudron.js

291 lines
12 KiB
JavaScript
Raw Normal View History

2015-06-14 04:33:42 +00:00
#!/usr/bin/env node
'use strict';
var assert = require('assert'),
2015-09-16 19:31:00 +00:00
common = require('./common.js'),
2015-06-14 04:33:42 +00:00
debug = require('debug')('e2e:cloudron'),
dns = require('dns'),
2015-06-14 04:33:42 +00:00
querystring = require('querystring'),
request = require('superagent-sync'),
sleep = require('sleep').sleep,
url = require('url');
exports = module.exports = Cloudron;
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
function Cloudron(box) {
this._box = box;
this._origin = 'https://my-' + box.domain;
this._credentials = {
password: null,
accessToken: null
};
}
// get oauth token for logged in as certain user { username, password, email }
Cloudron.prototype.getOauthToken = function (user) {
var username = user.username;
var password = user.password;
////////// try to authorize without a session
var res = request.get(this._origin + '/api/v1/oauth/dialog/authorize').query({ redirect_uri: 'https://self', client_id: 'cid-webadmin', response_type: 'token', scope: 'root,profile,apps,roleAdmin' }).end();
var sessionCookies = res.headers['set-cookie']; // always an array
///////// should get redirected to login form with a script tag (to workaround chrome issue with redirects+cookies)
var redirectUrl = res.text.match(/window.location.href = "(.*)"/);
if (!redirectUrl) {
2015-07-29 16:16:10 +00:00
debug('Could not determine redirected url', res.text, res.headers);
assert(false);
}
var urlp = url.parse(redirectUrl[1]);
2015-06-14 04:33:42 +00:00
////////// get the login form (api/v1/session/login)
res = request.get(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).query(urlp.query).end();
var csrf = res.text.match(/name="_csrf" value="(.*)"/)[1];
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// submit the login form with credentials
res = request.post(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).send({ _csrf: csrf, username: username, password: password }).redirects(0).end();
if (res.statusCode !== 302) return null;
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// authorize now with cookies
res = request.get(this._origin + '/api/v1/oauth/dialog/authorize').set('cookie', sessionCookies[0]).query({ redirect_uri: 'https://self', client_id: 'cid-webadmin', response_type: 'token', scope: 'root,profile,apps,roleAdmin' }).redirects(0).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Unable to authorize');
2015-06-15 06:11:07 +00:00
assert.strictEqual(res.statusCode, 302);
2015-06-14 04:33:42 +00:00
sessionCookies = res.headers['set-cookie']; // always an array
2015-06-15 06:11:07 +00:00
assert.notStrictEqual(sessionCookies.length, 0);
2015-06-14 04:33:42 +00:00
////////// success will get redirect to callback?redirectURI=xx#access_token=yy&token_type=Bearer' (content is a <script>)
urlp = url.parse(res.headers.location);
res = request.get(this._origin + urlp.pathname).set('cookie', sessionCookies[0]).query(urlp.query).redirects(0).end();
2015-06-15 06:11:07 +00:00
assert.strictEqual(res.statusCode, 200);
2015-06-14 04:33:42 +00:00
////////// simulate what the the script of callback call does
var accessToken = querystring.parse(urlp.hash.substr(1)).access_token;
return accessToken;
2015-09-29 01:43:51 +00:00
};
2015-06-14 04:33:42 +00:00
// activate the box
Cloudron.prototype.activate = function (user) {
var setupToken = this._box.setupToken;
////////// activation
var res = request.post(this._origin + '/api/v1/cloudron/activate').query({ setupToken: setupToken }).send(user).end();
2015-09-16 19:31:00 +00:00
if (res.statusCode !== 409) common.verifyResponse2(res, 'Could not activate the box'); // 409 - already activated
2015-06-14 04:33:42 +00:00
res = request.get(this._origin + '/api/v1/cloudron/status').end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not get Cloudron status');
2015-06-14 04:33:42 +00:00
assert.strictEqual(res.body.version, this._box.version);
2015-09-29 01:43:51 +00:00
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.waitForApp = function (appId) {
// wait for app to come up
process.stdout.write('Waiting for app to come up.');
2015-08-31 04:48:33 +00:00
var res;
2015-06-20 19:50:28 +00:00
for (var i = 0; i < 60; i++) {
2015-06-14 04:33:42 +00:00
sleep(10);
process.stdout.write('.');
2015-08-31 04:48:33 +00:00
res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not query app status');
2015-06-14 04:33:42 +00:00
2015-07-24 05:48:08 +00:00
if (res.body.installationState === 'installed' && res.body.runState === 'running' && res.body.health === 'healthy') {
2015-06-14 04:33:42 +00:00
console.log();
break;
}
}
2015-06-16 05:28:48 +00:00
assert.strictEqual(res.body.installationState, 'installed');
assert.strictEqual(res.body.runState, 'running');
2015-06-14 04:33:42 +00:00
};
2015-08-31 04:48:33 +00:00
Cloudron.prototype.waitForBox = function () {
process.stdout.write('Waiting for box.');
var res;
for (var i = 0; i < 40; i++) {
sleep(10);
res = request.get(this._origin + '/api/v1/cloudron/status').end();
if (res.statusCode === 202) {
console.log();
break;
}
process.stdout.write('.');
}
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.setCredentials = function (password, accessToken) {
this._credentials = {
password: password,
accessToken: accessToken
};
};
Cloudron.prototype.installApp = function (location, manifest) {
var res = request.post(this._origin + '/api/v1/apps/install')
.query({ access_token: this._credentials.accessToken })
.send({ manifest: manifest, appStoreId: '', location: location, accessRestriction: '' })
.end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Cannot install app');
2015-06-14 04:33:42 +00:00
debug('App installed at %s'.green, location);
var appId = res.body.id;
this.waitForApp(appId);
debug('App is running'.green);
res = request.get('https://' + location + '-' + this._box.domain).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'App is unreachable');
2015-06-14 04:33:42 +00:00
console.log('App is reachable'.green);
return appId;
};
Cloudron.prototype.configureApp = function (appId, newLocation) {
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/configure').query({ access_token: this._credentials.accessToken }).send({ location: newLocation, accessRestriction: '', password: this._credentials.password }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'App could not be configured');
2015-06-14 04:33:42 +00:00
console.log('App moved to different location'.green);
this.waitForApp(appId);
res = request.get('https://' + newLocation + '-' + this._box.domain).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'App is unreachable');
2015-06-14 04:33:42 +00:00
console.log('App is reachable'.green);
};
Cloudron.prototype.uninstallApp = function (appId) {
process.stdout.write('Uninstalling app');
var res = request.post(this._origin + '/api/v1/apps/' + appId + '/uninstall').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Cannot uninstall app');
2015-06-14 04:33:42 +00:00
while (true) {
sleep(10);
process.stdout.write('.');
res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).retry(0).end();
if (res.statusCode === 404) {
console.log();
break;
}
}
debug('App is uninstalled'.green);
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.update = function (toVersion) {
2015-07-16 22:05:06 +00:00
process.stdout.write('Trying to update');
2015-08-31 04:48:33 +00:00
var res;
2015-07-16 22:05:06 +00:00
while (true) {
sleep(10);
process.stdout.write('.');
2015-08-31 04:48:33 +00:00
res = request.post(this._origin + '/api/v1/cloudron/update').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password }).end();
2015-07-16 22:05:06 +00:00
if (res.statusCode === 422) continue; // box has not seen the update yet
if (res.statusCode === 409) break; // update is in progress, lock was acquired
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not update');
2015-07-16 22:05:06 +00:00
break;
}
2015-06-14 04:33:42 +00:00
2015-07-16 22:05:06 +00:00
console.log('Update started'.green);
2015-06-14 04:33:42 +00:00
process.stdout.write('Waiting for update.');
2015-06-16 05:28:48 +00:00
for (var i = 0; i < 40; i++) {
2015-06-14 04:33:42 +00:00
sleep(10);
res = request.get(this._origin + '/api/v1/cloudron/status').end();
if (res.statusCode === 200 && res.body.version === toVersion) {
console.log();
break;
}
process.stdout.write('.');
}
assert.strictEqual(res.body.version, toVersion);
assert.strictEqual(res.body.activated, true);
console.log('Updated successfully'.green);
2015-08-31 04:48:33 +00:00
};
2015-06-14 04:33:42 +00:00
Cloudron.prototype.addUser = function (username, email) {
var res = request.post(this._origin + '/api/v1/users').query({ access_token: this._credentials.accessToken }).send({ username: username, email: email }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not add user');
return res.body.userInfo;
};
Cloudron.prototype.resetPassword = function (resetToken, password) {
var res = request.get(this._origin + '/api/v1/session/password/setup.html').query({ reset_token: resetToken }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not get password setup site');
2015-06-15 06:11:07 +00:00
var sessionCookies = res.headers['set-cookie']; // always an array
var csrf = res.text.match(/name="_csrf" value="(.*)"/)[1];
2015-06-15 06:11:07 +00:00
res = request.post(this._origin + '/api/v1/session/password/reset')
.set('cookie', sessionCookies[0])
.type('form').send({ _csrf: csrf, resetToken: resetToken, password: password, passwordRepeat: password }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not setup password for user');
};
2015-06-16 19:00:46 +00:00
Cloudron.prototype.backup = function () {
var res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not get backups');
2015-06-16 19:00:46 +00:00
var existingBackups = res.body.backups;
2015-06-17 15:06:01 +00:00
res = request.post(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not schedule backup');
2015-06-16 19:00:46 +00:00
while (true) {
2015-07-10 03:27:30 +00:00
sleep(10); // backup sometimes takes a while to start
2015-06-16 19:00:46 +00:00
res = request.get(this._origin + '/api/v1/cloudron/progress').end();
if (res.body.backup === null || res.body.backup.percent === 100) {
debug('backup done');
break;
}
debug('Backing up: %s %s', res.body.backup.percent, res.body.backup.message);
}
res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Could not get backups');
2015-06-16 19:00:46 +00:00
var latestBackups = res.body.backups;
assert.strictEqual(latestBackups.length, existingBackups.length + 1);
return latestBackups[0]; // { creationTime, boxVersion, restoreKey, dependsOn }
};
2015-08-31 04:48:33 +00:00
Cloudron.prototype.reboot = function () {
2015-08-31 04:49:09 +00:00
var res = request.post(this._origin + '/api/v1/cloudron/reboot').query({ access_token: this._credentials.accessToken }).send({ }).end();
2015-09-16 19:31:00 +00:00
common.verifyResponse2(res, 'Box could not be rebooted');
2015-08-31 04:48:33 +00:00
this.waitForBox();
};
Cloudron.prototype.checkSPF = function (callback) {
dns.resolveTxt(this._box.domain, function (error, records) {
if (error) return callback(error);
if (records.length !== 1) return callback(new Error('Got ' + records.length + ' TXT records. Expecting 1'));
if (records[0].search(new RegExp('^v=spf1 ip4:' + this._box.ip + ' ~all$')) !== 0) return callback(new Error('Bad SPF record. ' + records[0]));
callback();
});
};
Cloudron.prototype.checkDKIM = function (callback) {
dns.resolveTxt('mail._domainkey.' + this._box.domain, function (error, records) {
if (error) return callback(error);
if (records.length !== 1) return callback(new Error('Got ' + records.length + ' TXT records. Expecting 1'));
if (records[0].search(/^"v=DKIM1; t=s; p=.*"$/) !== 0) return callback(new Error('Bad DKIM record. ' + records[0]));
callback();
});
};
Cloudron.prototype.checkDMARC = function (callback) {
dns.resolveTxt('_dmarc.' + this._box.domain, function (error, records) {
if (error) return callback(error);
if (records.length !== 1) return callback(new Error('Got ' + records.length + ' TXT records. Expecting 1'));
if (records[0].search(/^"v=DMARC1; p=none; pct=100; rua=mailto:.*; ruf=.*"$/) !== 0) return callback(new Error('Bad DMARC record. ' + records[0]));
callback();
});
};