test: make appstore-test use common.js
This commit is contained in:
@@ -5,174 +5,119 @@
|
||||
|
||||
'use strict';
|
||||
|
||||
var async = require('async'),
|
||||
const common = require('./common.js'),
|
||||
constants = require('../../constants.js'),
|
||||
database = require('../../database.js'),
|
||||
expect = require('expect.js'),
|
||||
nock = require('nock'),
|
||||
settings = require('../../settings.js'),
|
||||
superagent = require('superagent'),
|
||||
server = require('../../server.js');
|
||||
superagent = require('superagent');
|
||||
|
||||
var SERVER_URL = 'http://localhost:' + constants.PORT;
|
||||
|
||||
var USERNAME = 'superadmin', PASSWORD = 'Foobar?1337', EMAIL ='silly@me.com';
|
||||
var token = null;
|
||||
|
||||
function setup(done) {
|
||||
nock.cleanAll();
|
||||
|
||||
async.series([
|
||||
server.start.bind(server),
|
||||
|
||||
database._clear,
|
||||
|
||||
settings._setApiServerOrigin.bind(null, 'http://localhost:6060'),
|
||||
settings.setDashboardLocation.bind(null, 'appstore-test.example.com', 'my.appstore-test.example.com'),
|
||||
|
||||
function createAdmin(callback) {
|
||||
superagent.post(SERVER_URL + '/api/v1/cloudron/activate')
|
||||
.query({ setupToken: 'somesetuptoken' })
|
||||
.send({ username: USERNAME, password: PASSWORD, email: EMAIL })
|
||||
.end(function (error, result) {
|
||||
expect(result).to.be.ok();
|
||||
expect(result.statusCode).to.eql(201);
|
||||
|
||||
// stash token for further use
|
||||
token = result.body.token;
|
||||
|
||||
callback();
|
||||
});
|
||||
}
|
||||
], done);
|
||||
}
|
||||
|
||||
function cleanup(done) {
|
||||
database._clear(function (error) {
|
||||
expect(error).to.not.be.ok();
|
||||
|
||||
server.stop(done);
|
||||
});
|
||||
}
|
||||
const { setup, cleanup, serverUrl, owner } = common;
|
||||
|
||||
describe('Appstore Apps API', function () {
|
||||
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
it('cannot list apps without subscription', function (done) {
|
||||
superagent.get(SERVER_URL + '/api/v1/appstore/apps')
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(402); // not registered yet; invalid credentials
|
||||
done();
|
||||
});
|
||||
it('cannot list apps without subscription', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/appstore/apps`)
|
||||
.query({ access_token: owner.token })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.be(402); // not registered yet; invalid credentials
|
||||
});
|
||||
|
||||
it('cannot get app without subscription', function (done) {
|
||||
superagent.get(SERVER_URL + '/api/v1/appstore/apps/org.wordpress.cloudronapp')
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(402); // not registered yet; invalid credentials
|
||||
done();
|
||||
});
|
||||
it('cannot get app without subscription', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/appstore/apps/org.wordpress.cloudronapp`)
|
||||
.query({ access_token: owner.token })
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.be(402); // not registered yet; invalid credentials
|
||||
});
|
||||
|
||||
it('register cloudron', function (done) {
|
||||
var scope1 = nock(settings.apiServerOrigin())
|
||||
it('register cloudron', async function () {
|
||||
const scope1 = nock(settings.apiServerOrigin())
|
||||
.post('/api/v1/login', (body) => body.email && body.password)
|
||||
.reply(200, { userId: 'userId', accessToken: 'SECRET_TOKEN' });
|
||||
|
||||
var scope2 = nock(settings.apiServerOrigin())
|
||||
const scope2 = nock(settings.apiServerOrigin())
|
||||
.post('/api/v1/register_cloudron', (body) => !!body.domain && body.accessToken === 'SECRET_TOKEN')
|
||||
.reply(201, { cloudronId: 'cid', cloudronToken: 'CLOUDRON_TOKEN', licenseKey: 'lkey' });
|
||||
|
||||
superagent.post(SERVER_URL + '/api/v1/appstore/register_cloudron')
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/appstore/register_cloudron`)
|
||||
.send({ email: 'test@cloudron.io', password: 'secret', signup: false })
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(201);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
expect(scope2.isDone()).to.be.ok();
|
||||
done();
|
||||
});
|
||||
.query({ access_token: owner.token });
|
||||
|
||||
expect(response.statusCode).to.equal(201);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
expect(scope2.isDone()).to.be.ok();
|
||||
});
|
||||
|
||||
it('can list apps', function (done) {
|
||||
var scope1 = nock(settings.apiServerOrigin())
|
||||
it('can list apps', async function () {
|
||||
const scope1 = nock(settings.apiServerOrigin())
|
||||
.get(`/api/v1/apps?accessToken=CLOUDRON_TOKEN&boxVersion=${constants.VERSION}&unstable=true`, () => true)
|
||||
.reply(200, { apps: [] });
|
||||
|
||||
superagent.get(SERVER_URL + '/api/v1/appstore/apps')
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(200);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
done();
|
||||
});
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/appstore/apps`)
|
||||
.query({ access_token: owner.token });
|
||||
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
});
|
||||
|
||||
it('can get app', function (done) {
|
||||
var scope1 = nock(settings.apiServerOrigin())
|
||||
it('can get app', async function () {
|
||||
const scope1 = nock(settings.apiServerOrigin())
|
||||
.get('/api/v1/apps/org.wordpress.cloudronapp?accessToken=CLOUDRON_TOKEN', () => true)
|
||||
.reply(200, { apps: [] });
|
||||
|
||||
superagent.get(SERVER_URL + '/api/v1/appstore/apps/org.wordpress.cloudronapp')
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(200);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
done();
|
||||
});
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/appstore/apps/org.wordpress.cloudronapp`)
|
||||
.query({ access_token: owner.token });
|
||||
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
});
|
||||
|
||||
it('can get app version', function (done) {
|
||||
it('can get app version', async function () {
|
||||
var scope1 = nock(settings.apiServerOrigin())
|
||||
.get('/api/v1/apps/org.wordpress.cloudronapp/versions/3.4.2?accessToken=CLOUDRON_TOKEN', () => true)
|
||||
.reply(200, { apps: [] });
|
||||
|
||||
superagent.get(SERVER_URL + '/api/v1/appstore/apps/org.wordpress.cloudronapp/versions/3.4.2')
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(200);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
done();
|
||||
});
|
||||
});
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/appstore/apps/org.wordpress.cloudronapp/versions/3.4.2`)
|
||||
.query({ access_token: owner.token });
|
||||
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
});
|
||||
});
|
||||
|
||||
describe('Subscription API - no signup', function () {
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
it('can setup subscription', function (done) {
|
||||
var scope1 = nock(settings.apiServerOrigin())
|
||||
it('can setup subscription', async function () {
|
||||
const scope1 = nock(settings.apiServerOrigin())
|
||||
.post('/api/v1/login', (body) => body.email && body.password)
|
||||
.reply(200, { userId: 'userId', accessToken: 'SECRET_TOKEN' });
|
||||
|
||||
var scope2 = nock(settings.apiServerOrigin())
|
||||
const scope2 = nock(settings.apiServerOrigin())
|
||||
.post('/api/v1/register_cloudron', (body) => !!body.domain && body.accessToken === 'SECRET_TOKEN')
|
||||
.reply(201, { cloudronId: 'cid', cloudronToken: 'CLOUDRON_TOKEN', licenseKey: 'lkey' });
|
||||
|
||||
superagent.post(SERVER_URL + '/api/v1/appstore/register_cloudron')
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/appstore/register_cloudron`)
|
||||
.send({ email: 'test@cloudron.io', password: 'secret', signup: false })
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(201);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
expect(scope2.isDone()).to.be.ok();
|
||||
done();
|
||||
});
|
||||
.query({ access_token: owner.token });
|
||||
|
||||
expect(response.statusCode).to.equal(201);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
expect(scope2.isDone()).to.be.ok();
|
||||
});
|
||||
|
||||
it('cannot re-setup subscription - already registered', function (done) {
|
||||
superagent.post(SERVER_URL + '/api/v1/appstore/register_cloudron')
|
||||
it('cannot re-setup subscription - already registered', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/appstore/register_cloudron`)
|
||||
.send({ email: 'test@cloudron.io', password: 'secret', signup: false })
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(409);
|
||||
done();
|
||||
});
|
||||
.query({ access_token: owner.token })
|
||||
.ok(() => true);
|
||||
|
||||
expect(response.statusCode).to.equal(409);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -180,44 +125,40 @@ describe('Subscription API - signup', function () {
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
it('can setup subscription', function (done) {
|
||||
var scope1 = nock(settings.apiServerOrigin())
|
||||
it('can setup subscription', async function () {
|
||||
const scope1 = nock(settings.apiServerOrigin())
|
||||
.post('/api/v1/register_user', (body) => body.email && body.password)
|
||||
.reply(201, { });
|
||||
|
||||
var scope2 = nock(settings.apiServerOrigin())
|
||||
const scope2 = nock(settings.apiServerOrigin())
|
||||
.post('/api/v1/login', (body) => body.email && body.password)
|
||||
.reply(200, { userId: 'userId', accessToken: 'SECRET_TOKEN' });
|
||||
|
||||
var scope3 = nock(settings.apiServerOrigin())
|
||||
const scope3 = nock(settings.apiServerOrigin())
|
||||
.post('/api/v1/register_cloudron', (body) => !!body.domain && body.accessToken === 'SECRET_TOKEN')
|
||||
.reply(201, { cloudronId: 'cid', cloudronToken: 'CLOUDRON_TOKEN', licenseKey: 'lkey' });
|
||||
|
||||
superagent.post(SERVER_URL + '/api/v1/appstore/register_cloudron')
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/appstore/register_cloudron`)
|
||||
.send({ email: 'test@cloudron.io', password: 'secret', signup: true })
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(201);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
expect(scope2.isDone()).to.be.ok();
|
||||
expect(scope3.isDone()).to.be.ok();
|
||||
done();
|
||||
});
|
||||
.query({ access_token: owner.token });
|
||||
|
||||
expect(response.statusCode).to.equal(201);
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
expect(scope2.isDone()).to.be.ok();
|
||||
expect(scope3.isDone()).to.be.ok();
|
||||
});
|
||||
|
||||
it('can get subscription', function (done) {
|
||||
var scope1 = nock(settings.apiServerOrigin())
|
||||
it('can get subscription', async function () {
|
||||
const scope1 = nock(settings.apiServerOrigin())
|
||||
.get('/api/v1/subscription?accessToken=CLOUDRON_TOKEN', () => true)
|
||||
.reply(200, { subscription: { plan: { id: 'free' } }, email: 'test@cloudron.io' });
|
||||
|
||||
superagent.get(SERVER_URL + '/api/v1/appstore/subscription')
|
||||
.query({ access_token: token })
|
||||
.end(function (error, result) {
|
||||
expect(result.statusCode).to.equal(200);
|
||||
expect(result.body.email).to.be('test@cloudron.io');
|
||||
expect(result.body.subscription).to.be.an('object');
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
done();
|
||||
});
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/appstore/subscription`)
|
||||
.query({ access_token: owner.token });
|
||||
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(response.body.email).to.be('test@cloudron.io');
|
||||
expect(response.body.subscription).to.be.an('object');
|
||||
expect(scope1.isDone()).to.be.ok();
|
||||
});
|
||||
});
|
||||
|
||||
@@ -5,6 +5,7 @@ const async = require('async'),
|
||||
database = require('../../database.js'),
|
||||
expect = require('expect.js'),
|
||||
server = require('../../server.js'),
|
||||
settings = require('../../settings.js'),
|
||||
superagent = require('superagent'),
|
||||
tokendb = require('../../tokendb.js');
|
||||
|
||||
@@ -28,6 +29,10 @@ exports = module.exports = {
|
||||
token: null
|
||||
},
|
||||
|
||||
MOCK_API_SERVER_ORIGIN: 'http://localhost:6060',
|
||||
DASHBOARD_DOMAIN: 'test.example.com',
|
||||
DASHBOARD_FQDN: 'my.test.example.com',
|
||||
|
||||
serverUrl: `http://localhost:${constants.PORT}`
|
||||
};
|
||||
|
||||
@@ -38,6 +43,9 @@ function setup(done) {
|
||||
server.start.bind(null),
|
||||
database._clear.bind(null),
|
||||
|
||||
settings._setApiServerOrigin.bind(null, exports.MOCK_API_SERVER_ORIGIN),
|
||||
settings.setDashboardLocation.bind(null, exports.DASHBOARD_DOMAIN, exports.DASHBOARD_FQDN),
|
||||
|
||||
function createAdmin(callback) {
|
||||
superagent.post(`${serverUrl}/api/v1/cloudron/activate`)
|
||||
.query({ setupToken: 'somesetuptoken' })
|
||||
|
||||
Reference in New Issue
Block a user