256 lines
11 KiB
JavaScript
256 lines
11 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
var assert = require('assert'),
|
|
common = require('./common.js'),
|
|
debug = require('debug')('e2e:cloudron'),
|
|
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) {
|
|
debug('Could not determine redirected url', res.text, res.headers);
|
|
assert(false);
|
|
}
|
|
var urlp = url.parse(redirectUrl[1]);
|
|
|
|
////////// 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
|
|
assert.notStrictEqual(sessionCookies.length, 0);
|
|
|
|
////////// 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
|
|
assert.notStrictEqual(sessionCookies.length, 0);
|
|
|
|
////////// 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();
|
|
common.verifyResponse2(res, 'Unable to authorize');
|
|
assert.strictEqual(res.statusCode, 302);
|
|
sessionCookies = res.headers['set-cookie']; // always an array
|
|
assert.notStrictEqual(sessionCookies.length, 0);
|
|
|
|
////////// 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();
|
|
assert.strictEqual(res.statusCode, 200);
|
|
|
|
////////// simulate what the the script of callback call does
|
|
var accessToken = querystring.parse(urlp.hash.substr(1)).access_token;
|
|
return accessToken;
|
|
}
|
|
|
|
// 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();
|
|
if (res.statusCode !== 409) common.verifyResponse2(res, 'Could not activate the box'); // 409 - already activated
|
|
|
|
res = request.get(this._origin + '/api/v1/cloudron/status').end();
|
|
common.verifyResponse2(res, 'Could not get Cloudron status');
|
|
assert.strictEqual(res.body.version, this._box.version);
|
|
}
|
|
|
|
Cloudron.prototype.waitForApp = function (appId) {
|
|
// wait for app to come up
|
|
process.stdout.write('Waiting for app to come up.');
|
|
|
|
var res;
|
|
for (var i = 0; i < 60; i++) {
|
|
sleep(10);
|
|
process.stdout.write('.');
|
|
res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).end();
|
|
common.verifyResponse2(res, 'Could not query app status');
|
|
|
|
if (res.body.installationState === 'installed' && res.body.runState === 'running' && res.body.health === 'healthy') {
|
|
console.log();
|
|
break;
|
|
}
|
|
}
|
|
assert.strictEqual(res.body.installationState, 'installed');
|
|
assert.strictEqual(res.body.runState, 'running');
|
|
};
|
|
|
|
|
|
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('.');
|
|
}
|
|
};
|
|
|
|
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();
|
|
common.verifyResponse2(res, 'Cannot install app');
|
|
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();
|
|
common.verifyResponse2(res, 'App is unreachable');
|
|
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();
|
|
common.verifyResponse2(res, 'App could not be configured');
|
|
|
|
console.log('App moved to different location'.green);
|
|
this.waitForApp(appId);
|
|
|
|
res = request.get('https://' + newLocation + '-' + this._box.domain).end();
|
|
common.verifyResponse2(res, 'App is unreachable');
|
|
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();
|
|
common.verifyResponse2(res, 'Cannot uninstall app');
|
|
|
|
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);
|
|
};
|
|
|
|
Cloudron.prototype.update = function (toVersion) {
|
|
process.stdout.write('Trying to update');
|
|
var res;
|
|
while (true) {
|
|
sleep(10);
|
|
process.stdout.write('.');
|
|
res = request.post(this._origin + '/api/v1/cloudron/update').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password }).end();
|
|
if (res.statusCode === 422) continue; // box has not seen the update yet
|
|
if (res.statusCode === 409) break; // update is in progress, lock was acquired
|
|
common.verifyResponse2(res, 'Could not update');
|
|
break;
|
|
}
|
|
|
|
console.log('Update started'.green);
|
|
process.stdout.write('Waiting for update.');
|
|
for (var i = 0; i < 40; i++) {
|
|
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);
|
|
};
|
|
|
|
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();
|
|
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();
|
|
common.verifyResponse2(res, 'Could not get password setup site');
|
|
var sessionCookies = res.headers['set-cookie']; // always an array
|
|
|
|
var csrf = res.text.match(/name="_csrf" value="(.*)"/)[1];
|
|
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();
|
|
common.verifyResponse2(res, 'Could not setup password for user');
|
|
};
|
|
|
|
Cloudron.prototype.backup = function () {
|
|
var res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
|
|
common.verifyResponse2(res, 'Could not get backups');
|
|
|
|
var existingBackups = res.body.backups;
|
|
|
|
res = request.post(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
|
|
common.verifyResponse2(res, 'Could not schedule backup');
|
|
|
|
while (true) {
|
|
sleep(10); // backup sometimes takes a while to start
|
|
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();
|
|
common.verifyResponse2(res, 'Could not get backups');
|
|
var latestBackups = res.body.backups;
|
|
assert.strictEqual(latestBackups.length, existingBackups.length + 1);
|
|
return latestBackups[0]; // { creationTime, boxVersion, restoreKey, dependsOn }
|
|
};
|
|
|
|
Cloudron.prototype.reboot = function () {
|
|
var res = request.post(this._origin + '/api/v1/cloudron/reboot').query({ access_token: this._credentials.accessToken }).send({ }).end();
|
|
common.verifyResponse2(res, 'Box could not be rebooted');
|
|
this.waitForBox();
|
|
};
|