Initial version

This commit is contained in:
Girish Ramakrishnan 2015-06-13 21:33:42 -07:00
commit b308e5d6b8
4 changed files with 378 additions and 0 deletions

80
appstore.js Normal file
View File

@ -0,0 +1,80 @@
'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:%s', 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.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');
var boxId = res.body.box.id;
debug('Cloudron %s created'.green, box.name);
////////// wait for cloudron to come up
var creationTime = new Date();
process.stdout.write('Waiting for cloudron to come up.');
while (true) {
sleep(10);
process.stdout.write('.');
res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: 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);
return res.body.box;
};
AppStore.prototype.deleteCloudron = function (box) {
debug('Deleting cloudron');
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;
};

176
cloudron.js Normal file
View File

@ -0,0 +1,176 @@
#!/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 > 299) {
debug('Response error statusCode:%s error:%s body:%s', 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
////////// 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
////////// 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();
if (res.statusCode !== 302) return null;
sessionCookies = res.headers['set-cookie']; // always an array
////////// 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();
if (res.statusCode !== 200) return null;
////////// 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.');
while (true) {
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') {
console.log();
break;
}
}
};
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) {
var res = request.post(this._origin + '/api/v1/cloudron/update').query({ access_token: this._credentials.accessToken }).send({ password: this._credentials.password }).end();
verifyResponse(res, 'Could not update');
console.log('Update started'.green);
process.stdout.write('Waiting for update.');
for (var i = 0; i < 30; 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);
}

25
package.json Normal file
View File

@ -0,0 +1,25 @@
{
"name": "e2e-test",
"version": "1.0.0",
"description": "End to end testing",
"main": "test.js",
"scripts": {
"test": "DEBUG=superagent-sync,e2e mocha test.js"
},
"repository": {
"type": "git",
"url": "ssh://git@gitlab-yellowtent.cloudron.me:6000/yellowtent/e2e-test.git"
},
"author": "",
"license": "ISC",
"dependencies": {
"colors": "^1.1.0",
"debug": "^2.2.0",
"mocha": "^2.2.5",
"readline-sync": "^1.2.19",
"semver": "^4.3.6",
"should": "^6.0.3",
"sleep": "^2.0.0",
"superagent-sync": "^0.1.0"
}
}

97
test.js Executable file
View File

@ -0,0 +1,97 @@
#!/usr/bin/env node
'use strict';
var AppStore = require('./appstore.js'),
assert = require('assert'),
Cloudron = require('./cloudron.js'),
readlineSync = require('readline-sync'),
request = require('superagent-sync'),
semver = require('semver'),
sleep = require('sleep').sleep,
util = require('util');
require('colors');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
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);
console.log(errorMessage.red);
process.exit(1);
}
}
describe('End to end testing', function () {
this.timeout(0);
var appStore = new AppStore('https://api.staging.cloudron.io');
var owner = {
username: 'test',
password: 'test1234',
email: 'test@cloudron.io'
};
var res, fromVersion, toVersion, cloudron, appId, box;
it('can query versions', function () {
res = request.get('https://s3.amazonaws.com/staging-cloudron-releases/versions.json').end();
verifyResponse(res);
var boxVersions = Object.keys(res.body).sort(semver.rcompare);
fromVersion = boxVersions[1];
toVersion = boxVersions[0];
console.log('Will test update from %s to %s', fromVersion.yellow, toVersion.yellow);
});
it('can login to the store', function () {
var accessToken = appStore.getAccessToken(owner);
appStore.setCredentials(owner.password, accessToken);
});
it('can create a cloudron', function () {
box = appStore.createCloudron({
name: 'testbox' + (Math.random() * 1000).toFixed() + '.smartserver.io',
zoneName: 'smartserver.io',
region: 'sfo1',
size: '512mb',
version: fromVersion
});
cloudron = new Cloudron(box);
});
it('can activate the box', function () {
cloudron.activate(owner);
});
it('can login to the box', function () {
var token = cloudron.getOauthToken(owner);
cloudron.setCredentials(owner.password, token);
});
var location = 'haste' + (Math.random() * 10000).toFixed();
it('can install app', function () {
var manifest = appStore.getManifest('com.hastebin.cloudronapp', '0.1.0');
appId = cloudron.installApp(location, manifest);
});
it('can configure app', function () {
cloudron.configureApp(appId, location + 'x');
});
it('can update the box', function () {
cloudron.update(toVersion);
});
it('can uninstall app', function () {
cloudron.uninstallApp(appId);
});
it('can delete the cloudron', function () {
appStore.deleteCloudron(box);
});
});