refactor
This commit is contained in:
parent
e0242d003c
commit
84e81aace3
43
cloudron.js
43
cloudron.js
@ -3,6 +3,7 @@
|
||||
'use strict';
|
||||
|
||||
var assert = require('assert'),
|
||||
common = require('./common.js'),
|
||||
debug = require('debug')('e2e:cloudron'),
|
||||
querystring = require('querystring'),
|
||||
request = require('superagent-sync'),
|
||||
@ -23,14 +24,6 @@ function Cloudron(box) {
|
||||
};
|
||||
}
|
||||
|
||||
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;
|
||||
@ -62,7 +55,7 @@ Cloudron.prototype.getOauthToken = function (user) {
|
||||
|
||||
////////// 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');
|
||||
common.verifyResponse2(res, 'Unable to authorize');
|
||||
assert.strictEqual(res.statusCode, 302);
|
||||
sessionCookies = res.headers['set-cookie']; // always an array
|
||||
assert.notStrictEqual(sessionCookies.length, 0);
|
||||
@ -83,10 +76,10 @@ Cloudron.prototype.activate = function (user) {
|
||||
|
||||
////////// 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
|
||||
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();
|
||||
verifyResponse(res, 'Could not get Cloudron status');
|
||||
common.verifyResponse2(res, 'Could not get Cloudron status');
|
||||
assert.strictEqual(res.body.version, this._box.version);
|
||||
}
|
||||
|
||||
@ -99,7 +92,7 @@ Cloudron.prototype.waitForApp = function (appId) {
|
||||
sleep(10);
|
||||
process.stdout.write('.');
|
||||
res = request.get(this._origin + '/api/v1/apps/'+ appId).query({ access_token: this._credentials.accessToken }).end();
|
||||
verifyResponse(res, 'Could not query app status');
|
||||
common.verifyResponse2(res, 'Could not query app status');
|
||||
|
||||
if (res.body.installationState === 'installed' && res.body.runState === 'running' && res.body.health === 'healthy') {
|
||||
console.log();
|
||||
@ -137,7 +130,7 @@ Cloudron.prototype.installApp = function (location, manifest) {
|
||||
.query({ access_token: this._credentials.accessToken })
|
||||
.send({ manifest: manifest, appStoreId: '', location: location, accessRestriction: '' })
|
||||
.end();
|
||||
verifyResponse(res, 'Cannot install app');
|
||||
common.verifyResponse2(res, 'Cannot install app');
|
||||
debug('App installed at %s'.green, location);
|
||||
var appId = res.body.id;
|
||||
|
||||
@ -145,7 +138,7 @@ Cloudron.prototype.installApp = function (location, manifest) {
|
||||
debug('App is running'.green);
|
||||
|
||||
res = request.get('https://' + location + '-' + this._box.domain).end();
|
||||
verifyResponse(res, 'App is unreachable');
|
||||
common.verifyResponse2(res, 'App is unreachable');
|
||||
console.log('App is reachable'.green);
|
||||
|
||||
return appId;
|
||||
@ -153,20 +146,20 @@ Cloudron.prototype.installApp = function (location, manifest) {
|
||||
|
||||
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');
|
||||
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();
|
||||
verifyResponse(res, 'App is unreachable');
|
||||
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();
|
||||
verifyResponse(res, 'Cannot uninstall app');
|
||||
common.verifyResponse2(res, 'Cannot uninstall app');
|
||||
|
||||
while (true) {
|
||||
sleep(10);
|
||||
@ -189,7 +182,7 @@ Cloudron.prototype.update = function (toVersion) {
|
||||
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
|
||||
verifyResponse(res, 'Could not update');
|
||||
common.verifyResponse2(res, 'Could not update');
|
||||
break;
|
||||
}
|
||||
|
||||
@ -212,31 +205,31 @@ Cloudron.prototype.update = function (toVersion) {
|
||||
|
||||
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');
|
||||
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();
|
||||
verifyResponse(res, 'Could not get password setup site');
|
||||
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();
|
||||
verifyResponse(res, 'Could not setup password for user');
|
||||
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();
|
||||
verifyResponse(res, 'Could not get backups');
|
||||
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();
|
||||
verifyResponse(res, 'Could not schedule backup');
|
||||
common.verifyResponse2(res, 'Could not schedule backup');
|
||||
|
||||
while (true) {
|
||||
sleep(10); // backup sometimes takes a while to start
|
||||
@ -249,7 +242,7 @@ Cloudron.prototype.backup = function () {
|
||||
}
|
||||
|
||||
res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end();
|
||||
verifyResponse(res, 'Could not get backups');
|
||||
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 }
|
||||
@ -257,6 +250,6 @@ Cloudron.prototype.backup = function () {
|
||||
|
||||
Cloudron.prototype.reboot = function () {
|
||||
var res = request.post(this._origin + '/api/v1/cloudron/reboot').query({ access_token: this._credentials.accessToken }).send({ }).end();
|
||||
verifyResponse(res, 'Box could not be rebooted');
|
||||
common.verifyResponse2(res, 'Box could not be rebooted');
|
||||
this.waitForBox();
|
||||
};
|
||||
|
@ -36,6 +36,14 @@ function stripUnreachable(releases) {
|
||||
return _.pick(releases, reachableVersions);
|
||||
}
|
||||
|
||||
function verifyResponse2(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);
|
||||
}
|
||||
}
|
||||
|
||||
function verifyResponse(res, errorMessage) {
|
||||
if (res.statusCode < 200 || res.statusCode > 299) {
|
||||
console.log('Response error statusCode:%s error:%s body:%s', res.statusCode, res.error, res.body);
|
||||
|
Loading…
Reference in New Issue
Block a user