From c1c2cdc434b71f329c5fd0ce5113bdd85d1da0cb Mon Sep 17 00:00:00 2001 From: "girish@cloudron.io" Date: Wed, 27 Jan 2016 09:21:41 -0800 Subject: [PATCH] check for 2xx response --- appstore.js | 18 +++++++++--------- cloudron.js | 36 ++++++++++++++++++------------------ common.js | 14 ++++++++++++-- test/cloudron-update-test.js | 2 +- 4 files changed, 40 insertions(+), 30 deletions(-) diff --git a/appstore.js b/appstore.js index 04810e4..f917011 100644 --- a/appstore.js +++ b/appstore.js @@ -25,7 +25,7 @@ function AppStore(origin) { AppStore.prototype.getAccessToken = function (user) { var res = request.get(this._origin + '/api/v1/login').auth(user.email, user.password).end(); - common.verifyResponse2(res, 'Could not login as user:' + user.email + ' password:' + user.password); + common.verifyResponse2xx(res, 'Could not login as user:' + user.email + ' password:' + user.password); return res.body.accessToken; }; @@ -50,7 +50,7 @@ AppStore.prototype.getCloudrons = function () { AppStore.prototype.getCloudron = function (boxId) { var res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: this._credentials.accessToken, page: 1, per_page: 50 }).end(); if (res.statusCode === 404) return null; - common.verifyResponse2(res, 'Could not query cloudron status'); + common.verifyResponse2xx(res, 'Could not query cloudron status'); return res.body.box; }; @@ -63,7 +63,7 @@ AppStore.prototype.waitForIP = function (boxId) { sleep(10); process.stdout.write('.'); res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: this._credentials.accessToken }).end(); - common.verifyResponse2(res, 'Could not query cloudron status'); + common.verifyResponse2xx(res, 'Could not query cloudron status'); if (res.body.box.ip) { debug(); @@ -88,7 +88,7 @@ AppStore.prototype.waitForCloudron = function (boxId) { sleep(10); process.stdout.write('.'); res = request.get(this._origin + '/api/v1/cloudrons/' + boxId).query({ accessToken: this._credentials.accessToken }).end(); - common.verifyResponse2(res, 'Could not query cloudron status'); + common.verifyResponse2xx(res, 'Could not query cloudron status'); boxInfo = res.body.box; if (boxInfo.status === 'ready') { @@ -119,7 +119,7 @@ AppStore.prototype.waitForCloudron = function (boxId) { 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(); - common.verifyResponse2(res, 'Could not create cloudron %j', box); + common.verifyResponse2xx(res, 'Could not create cloudron %j', box); debug('Cloudron %s created'.green, box.domain); return res.body.box; @@ -132,7 +132,7 @@ AppStore.prototype.deleteCloudron = function (box) { .set('X-HTTP-Method-Override', 'DELETE') .send({ password: this._adminCredentials.password }) .end(); - common.verifyResponse2(res, 'Could not delete cloudron'); + common.verifyResponse2xx(res, 'Could not delete cloudron'); process.stdout.write('Waiting for Cloudron to disappear.'); for (var i = 0; i < 40; i++) { @@ -148,13 +148,13 @@ AppStore.prototype.deleteCloudron = function (box) { AppStore.prototype.getManifest = function (appId, version) { var res = request.get(this._origin + '/api/v1/apps/' + appId + '/versions/' + version).end(); - common.verifyResponse2(res, 'Could not get get app manifest'); + common.verifyResponse2xx(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(); - common.verifyResponse2(res, 'Could not restore cloudron'); + common.verifyResponse2xx(res, 'Could not restore cloudron'); }; AppStore.prototype.setupBilling = function (user, callback) { @@ -187,7 +187,7 @@ AppStore.prototype.setupBilling = function (user, callback) { }; var res = request.put(that._origin + '/api/v1/users/' + user.id).send(data).query({ accessToken: that._credentials.accessToken }).end(); - common.verifyResponse2(res, 'Could not setup billing'); + common.verifyResponse2xx(res, 'Could not setup billing'); callback(null); }); diff --git a/cloudron.js b/cloudron.js index 2b4d8cd..e2e91ec 100644 --- a/cloudron.js +++ b/cloudron.js @@ -62,7 +62,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(); - common.verifyResponse2(res, 'Unable to authorize'); + common.verifyResponse(res, 'Unable to authorize'); assert.strictEqual(res.statusCode, 302); sessionCookies = res.headers['set-cookie']; // always an array assert.notStrictEqual(sessionCookies.length, 0); @@ -83,10 +83,10 @@ Cloudron.prototype.activate = function (user) { ////////// activation var res = request.post(this._origin + '/api/v1/cloudron/activate').query({ setupToken: setupToken }).send(user).end(); - common.verifyResponse2(res, 'Could not activate the box'); // 409 - already activated + common.verifyResponse2xx(res, 'Could not activate the box'); // 409 - already activated res = request.get(this._origin + '/api/v1/cloudron/status').end(); - common.verifyResponse2(res, 'Could not get Cloudron status'); + common.verifyResponse2xx(res, 'Could not get Cloudron status'); assert.strictEqual(res.body.version, this._box.version); }; @@ -99,7 +99,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(); - common.verifyResponse2(res, 'Could not query app status'); + common.verifyResponse2xx(res, 'Could not query app status'); if (res.body.installationState === 'installed' && res.body.runState === 'running' && res.body.health === 'healthy') { console.log(); @@ -144,7 +144,7 @@ Cloudron.prototype.installApp = function (location, manifest, cloudronVersion) { .query({ access_token: this._credentials.accessToken }) .send({ manifest: manifest, appStoreId: '', location: location, accessRestriction: semver.lte(cloudronVersion, '0.0.73') ? '' : null, oauthProxy: false }) .end(); - common.verifyResponse2(res, 'Cannot install app'); + common.verifyResponse2xx(res, 'Cannot install app'); debug('App installed at %s'.green, location); var appId = res.body.id; @@ -152,7 +152,7 @@ Cloudron.prototype.installApp = function (location, manifest, cloudronVersion) { debug('App is running'.green); res = request.get('https://' + app.fqdn).end(); - common.verifyResponse2(res, 'App is unreachable'); + common.verifyResponse2xx(res, 'App is unreachable'); console.log('App is reachable'.green); return appId; @@ -160,20 +160,20 @@ Cloudron.prototype.installApp = function (location, manifest, cloudronVersion) { 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: null, oauthProxy: false, password: this._credentials.password }).end(); - common.verifyResponse2(res, 'App could not be configured'); + common.verifyResponse2xx(res, 'App could not be configured'); console.log('App moved to different location'.green); var app = this.waitForApp(appId); res = request.get('https://' + app.fqdn).end(); - common.verifyResponse2(res, 'App is unreachable'); + common.verifyResponse2xx(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(); - common.verifyResponse2(res, 'Cannot uninstall app'); + common.verifyResponse2xx(res, 'Cannot uninstall app'); for (var i = 0; i < 40; i++) { sleep(10); @@ -198,7 +198,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 - common.verifyResponse2(res, 'Could not update'); + common.verifyResponse2xx(res, 'Could not update'); break; } @@ -221,31 +221,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, invite: false }).end(); - common.verifyResponse2(res, 'Could not add user'); + common.verifyResponse2xx(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(); - common.verifyResponse2(res, 'Could not get password setup site'); + common.verifyResponse2xx(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(); - common.verifyResponse2(res, 'Could not setup password for user'); + common.verifyResponse2xx(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(); - common.verifyResponse2(res, 'Could not get backups'); + common.verifyResponse2xx(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(); - common.verifyResponse2(res, 'Could not schedule backup'); + common.verifyResponse2xx(res, 'Could not schedule backup'); for (var i = 0; i < 40; i++) { sleep(10); // backup sometimes takes a while to start @@ -260,7 +260,7 @@ Cloudron.prototype.backup = function () { if (i === 40) throw new Error('backup: timedout'); res = request.get(this._origin + '/api/v1/backups').query({ access_token: this._credentials.accessToken }).end(); - common.verifyResponse2(res, 'Could not get backups'); + common.verifyResponse2xx(res, 'Could not get backups'); var latestBackups = res.body.backups; assert.strictEqual(latestBackups.length, existingBackups.length + 1); return latestBackups[0]; // { creationTime, boxVersion, restoreKey, dependsOn } @@ -268,7 +268,7 @@ 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(); - common.verifyResponse2(res, 'Box could not be rebooted'); + common.verifyResponse2xx(res, 'Box could not be rebooted'); this.waitForBox(); }; @@ -350,5 +350,5 @@ Cloudron.prototype.checkAddons = function (location, owner) { Cloudron.prototype.setDnsConfig = function (dnsConfig) { var res = request.post(this._origin + '/api/v1/settings/dns_config').query({ access_token: this._credentials.accessToken }).send(dnsConfig).end(); - common.verifyResponse2(res, 'Could not set dns config'); + common.verifyResponse2xx(res, 'Could not set dns config'); }; diff --git a/common.js b/common.js index 995075b..df616c5 100644 --- a/common.js +++ b/common.js @@ -12,7 +12,8 @@ exports = module.exports = { TESTAPP_VERSION : '10.0.0', cloudronDomain: cloudronDomain, - verifyResponse2: verifyResponse2, + verifyResponse2xx: verifyResponse2xx, + verifyResponse: verifyResponse, getOwner: getOwner, getAdmin: getAdmin, stripeSecret: stripeSecret, @@ -39,7 +40,16 @@ function stripUnreachable(releases) { return _.pick(releases, reachableVersions); } -function verifyResponse2(res, args) { +function verifyResponse2xx(res, args) { + if (res.statusCode < 200 || res.statusCode > 299) { + debug('Response error statusCode:%s error:%s body:%j', res.statusCode, res.error, res.body); + var errorMessage = util.format.apply(util, Array.prototype.slice.call(arguments, 1)); + debug(errorMessage.red); + throw new Error(errorMessage); + } +} + +function verifyResponse(res, args) { if (res.statusCode < 200 || res.statusCode > 399) { debug('Response error statusCode:%s error:%s body:%j', res.statusCode, res.error, res.body); var errorMessage = util.format.apply(util, Array.prototype.slice.call(arguments, 1)); diff --git a/test/cloudron-update-test.js b/test/cloudron-update-test.js index 88200c9..a7cecdc 100644 --- a/test/cloudron-update-test.js +++ b/test/cloudron-update-test.js @@ -41,7 +41,7 @@ describe('Cloudron update testing', function () { it('can query versions', function () { res = request.get('https://s3.amazonaws.com/staging-cloudron-releases/versions.json').end(); - common.verifyResponse2(res); + common.verifyResponse2xx(res); var boxVersions = Object.keys(common.stripUnreachable(res.body)).sort(semver.rcompare); fromVersion = boxVersions[2]; // we released a new version in before() toVersion = boxVersions[1];