cloudron-e2e-test/appstore.js

122 lines
4.0 KiB
JavaScript
Raw Normal View History

2015-06-14 04:33:42 +00:00
'use strict';
var debug = require('debug')('e2e:appstore'),
2015-07-23 04:00:11 +00:00
fs = require('fs'),
path = require('path'),
2015-06-14 04:33:42 +00:00
request = require('superagent-sync'),
sleep = require('sleep').sleep,
stripe = require('stripe');
2015-06-14 04:33:42 +00:00
exports = module.exports = AppStore;
function AppStore(origin) {
this._origin = origin;
// credentials for api calls
this._credentials = {
password: null,
accessToken: null
};
2015-07-23 04:02:21 +00:00
if (fs.existsSync(path.join(__dirname, '../ecosystem.json'))) {
this._ecosystem = require(path.join(__dirname, '../ecosystem.json')); // staging appstore
2015-07-23 03:54:19 +00:00
} else {
2015-07-23 04:02:21 +00:00
this._ecosystem = require(path.join(__dirname, '../keys/appstore/ecosystem-staging.json'));
2015-07-23 03:54:19 +00:00
}
2015-06-14 04:33:42 +00:00
}
function verifyResponse(res, errorMessage) {
if (res.statusCode < 200 || res.statusCode > 299) {
2015-06-16 14:43:33 +00:00
debug('Response error statusCode:%s error:%s body:%j', res.statusCode, res.error, res.body);
2015-06-14 04:33:42 +00:00
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 };
};
2015-06-16 19:00:46 +00:00
AppStore.prototype.waitForCloudron = function (boxId) {
2015-06-14 04:33:42 +00:00
var creationTime = new Date();
process.stdout.write('Waiting for cloudron to come up.');
while (true) {
sleep(10);
process.stdout.write('.');
2015-06-16 19:00:46 +00:00
var res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: this._credentials.accessToken }).end();
2015-06-14 04:33:42 +00:00
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);
2015-06-16 19:00:46 +00:00
};
2015-06-14 04:33:42 +00:00
2015-06-16 19:00:46 +00:00
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');
2015-06-29 06:20:05 +00:00
debug('Cloudron %s created'.green, box.domain);
2015-06-14 04:33:42 +00:00
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;
};
2015-06-16 19:00:46 +00:00
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');
};
AppStore.prototype.setupBilling = function (callback) {
var stripeApi = stripe(this._ecosystem.env['STRIPE_SECRET']);
var that = this;
stripeApi.tokens.create({
card: {
number: '4242424242424242',
exp_month: 12,
exp_year: 2016,
cvc: '123'
}
}, function (error, token) {
if (error) return callback(error);
debug('Got stripe token', token.id);
var data = {
password: that._credentials.password,
billingToken: token.id
};
var res = request.put(that._origin + '/api/v1/users').send(data).query({ accessToken: that._credentials.accessToken }).end();
verifyResponse(res, 'Could not setup billing');
callback(null);
});
};