236 lines
9.8 KiB
JavaScript
236 lines
9.8 KiB
JavaScript
#!/usr/bin/env node
|
|
|
|
'use strict';
|
|
|
|
var assert = require('assert'),
|
|
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
|
|
};
|
|
}
|
|
|
|
function verifyResponse(res, errorMessage) {
|
|
if (res.statusCode < 200 || res.statusCode > 399) {
|
|
debug('Response error statusCode:%s error:%s body:%j', res.statusCode, res.error, res.body);
|
|
debug(errorMessage.red);
|
|
throw new Error(errorMessage);
|
|
}
|
|
}
|
|
|
|
// 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 urlp = url.parse(res.text.match(/window.location.href = "(.*)"/)[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();
|
|
verifyResponse(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) verifyResponse(res, 'Could not activate the box'); // 409 - already activated
|
|
|
|
res = request.get(this._origin + '/api/v1/cloudron/status').end();
|
|
verifyResponse(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.');
|
|
|
|
for (var i = 0; i < 60; i++) {
|
|
sleep(10);
|
|
process.stdout.write('.');
|
|
var res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).end();
|
|
verifyResponse(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.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();
|
|
verifyResponse(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();
|
|
verifyResponse(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();
|
|
verifyResponse(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();
|
|
verifyResponse(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();
|
|
verifyResponse(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');
|
|
while (true) {
|
|
sleep(10);
|
|
process.stdout.write('.');
|
|
var 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
|
|
verifyResponse(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();
|
|
verifyResponse(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();
|
|
verifyResponse(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();
|
|
verifyResponse(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();
|
|
verifyResponse(res, 'Could not get backups');
|
|
|
|
var existingBackups = res.body.backups;
|
|
console.dir(existingBackups);
|
|
|
|
res = request.post(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
|
|
verifyResponse(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();
|
|
verifyResponse(res, 'Could not get backups');
|
|
var latestBackups = res.body.backups;
|
|
assert.strictEqual(latestBackups.length, existingBackups.length + 1);
|
|
return latestBackups[0]; // { creationTime, boxVersion, restoreKey, dependsOn }
|
|
};
|
|
|