85 lines
2.8 KiB
JavaScript
85 lines
2.8 KiB
JavaScript
'use strict';
|
|
|
|
var debug = require('debug')('e2e:appstore'),
|
|
request = require('superagent-sync'),
|
|
sleep = require('sleep').sleep,
|
|
|
|
exports = module.exports = AppStore;
|
|
|
|
function AppStore(origin) {
|
|
this._origin = origin;
|
|
|
|
// credentials for api calls
|
|
this._credentials = {
|
|
password: null,
|
|
accessToken: null
|
|
};
|
|
}
|
|
|
|
function verifyResponse(res, errorMessage) {
|
|
if (res.statusCode < 200 || res.statusCode > 299) {
|
|
debug('Response error statusCode:%s error:%s body:%j', res.statusCode, res.error, res.body);
|
|
debug(errorMessage.red);
|
|
throw new Error(errorMessage);
|
|
}
|
|
}
|
|
|
|
AppStore.prototype.getAccessToken = function (user) {
|
|
var res = request.get(this._origin + '/api/v1/login').auth(user.email, user.password).end();
|
|
verifyResponse(res, 'Could not login');
|
|
return res.body.accessToken;
|
|
};
|
|
|
|
AppStore.prototype.setCredentials = function (password, accessToken) {
|
|
this._credentials = { password: password, accessToken: accessToken };
|
|
};
|
|
|
|
AppStore.prototype.waitForCloudron = function (boxId) {
|
|
var creationTime = new Date();
|
|
process.stdout.write('Waiting for cloudron to come up.');
|
|
|
|
while (true) {
|
|
sleep(10);
|
|
process.stdout.write('.');
|
|
var res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: this._credentials.accessToken }).end();
|
|
verifyResponse(res, 'Could not query cloudron status');
|
|
|
|
if (res.body.box.status === 'ready') {
|
|
debug();
|
|
break;
|
|
}
|
|
}
|
|
|
|
debug('Box created in %s minutes'.green, (new Date() - creationTime) / 60000);
|
|
};
|
|
|
|
AppStore.prototype.createCloudron = function (box) {
|
|
var accessToken = this._credentials.accessToken;
|
|
var res = request.post(this._origin + '/api/v1/cloudrons').send(box).query({ accessToken: accessToken }).end();
|
|
verifyResponse(res, 'Could not create cloudron');
|
|
|
|
debug('Cloudron %s created'.green, box.name);
|
|
return res.body.box;
|
|
};
|
|
|
|
AppStore.prototype.deleteCloudron = function (box) {
|
|
var res = request.post(this._origin + '/api/v1/cloudrons/' + box.id)
|
|
.query({ accessToken: this._credentials.accessToken })
|
|
.set('X-HTTP-Method-Override', 'DELETE')
|
|
.send({ password: this._credentials.password })
|
|
.end();
|
|
verifyResponse(res, 'Could not delete cloudron');
|
|
};
|
|
|
|
AppStore.prototype.getManifest = function (appId, version) {
|
|
var res = request.get(this._origin + '/api/v1/apps/' + appId + '/versions/' + version).end();
|
|
verifyResponse(res, 'Could not get get app manifest');
|
|
return res.body.manifest;
|
|
};
|
|
|
|
AppStore.prototype.restore = function (boxId, backupId) {
|
|
var res = request.post(this._origin + '/api/v1/cloudrons/' + boxId + '/restore/' + backupId).query({ accessToken: this._credentials.accessToken }).end();
|
|
verifyResponse(res, 'Could not restore cloudron');
|
|
};
|
|
|