diff --git a/src/appstore.js b/src/appstore.js index 49ba3d2f3..6cbd872fc 100644 --- a/src/appstore.js +++ b/src/appstore.js @@ -217,8 +217,7 @@ async function registerCloudron3(domain, version) { .ok(() => true)); if (error) throw new BoxError(BoxError.NETWORK_ERROR, error); - if (response.status === 401) throw new BoxError(BoxError.LICENSE_ERROR, 'Setup token invalid'); - if (response.status !== 201) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unable to register cloudron: ${response.status} ${error.message}`); + if (response.status !== 201) throw new BoxError(BoxError.EXTERNAL_ERROR, `Unable to register cloudron: ${response.status} ${response.text}`); if (!response.body.cloudronId) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Invalid response - no cloudron id'); if (!response.body.cloudronToken) throw new BoxError(BoxError.EXTERNAL_ERROR, 'Invalid response - no token'); diff --git a/src/routes/test/common.js b/src/routes/test/common.js index 21e46c78d..7a7548893 100644 --- a/src/routes/test/common.js +++ b/src/routes/test/common.js @@ -146,6 +146,7 @@ async function setup() { owner.token = response.body.token; owner.id = response.body.userId; expect(scope1.isDone()).to.be.ok(); + scope1.persist(false); // create an admin response = await superagent.post(`${serverUrl}/api/v1/users`) diff --git a/src/test/provision-test.js b/src/test/provision-test.js new file mode 100644 index 000000000..58e8d08aa --- /dev/null +++ b/src/test/provision-test.js @@ -0,0 +1,60 @@ +/* global it:false */ +/* global describe:false */ +/* global before:false */ +/* global after:false */ + +'use strict'; + +const appstore = require('../appstore.js'), + BoxError = require('../boxerror.js'), + common = require('./common.js'), + expect = require('expect.js'), + nock = require('nock'), + provision = require('../provision.js'), + safe = require('safetydance'); + +describe('Provision', function () { + const { domainSetup, auditSource, cleanup } = common; + + describe('Activate', function () { + before(domainSetup); + after(cleanup); + + it('cannot activate when appstore unreachable', async function () { + const [error] = await safe(provision.activate('username', 'password', 'test@cloudron.io', 'Some Name', '1.2.3.4', auditSource)); + expect(error.reason).to.be(BoxError.NETWORK_ERROR); + }); + + it('cannot activate with non-201', async function () { + const scope1 = nock(await appstore.getApiServerOrigin()) + .post('/api/v1/register_cloudron3', (body) => typeof body.domain === 'string' && typeof body.version === 'string') + .reply(401, {}); + + const [error] = await safe(provision.activate('username', 'password', 'test@cloudron.io', 'Some Name', '1.2.3.4', auditSource)); + expect(error.reason).to.be(BoxError.EXTERNAL_ERROR); + expect(scope1.isDone()).to.be.ok(); + scope1.persist(false); + }); + + it('cannot activate with incorrect fields', async function () { + const scope1 = nock(await appstore.getApiServerOrigin()) + .post('/api/v1/register_cloudron3', (body) => typeof body.domain === 'string' && typeof body.version === 'string') + .reply(201, { id: '32' }); + + const [error] = await safe(provision.activate('username', 'password', 'test@cloudron.io', 'Some Name', '1.2.3.4', auditSource)); + expect(error.reason).to.be(BoxError.EXTERNAL_ERROR); + expect(scope1.isDone()).to.be.ok(); + scope1.persist(false); + }); + + it('can activate with correct fields', async function () { + const scope1 = nock(await appstore.getApiServerOrigin()) + .post('/api/v1/register_cloudron3', (body) => typeof body.domain === 'string' && typeof body.version === 'string') + .reply(201, { cloudronId: '32', cloudronToken: 'xx' }); + + await provision.activate('username', 'password', 'test@cloudron.io', 'Some Name', '1.2.3.4', auditSource); + expect(scope1.isDone()).to.be.ok(); + scope1.persist(false); + }); + }); +});