Files
cloudron-box/src/test/appstore-test.js
T

104 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-02-18 21:36:44 -08:00
/* jslint node:true */
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
2018-02-18 22:43:11 -08:00
/* global beforeEach:false */
2018-02-18 21:36:44 -08:00
'use strict';
var async = require('async'),
appstore = require('../appstore.js'),
database = require('../database.js'),
expect = require('expect.js'),
nock = require('nock'),
2019-04-30 23:35:49 -07:00
settings = require('../settings.js'),
settingsdb = require('../settingsdb.js');
2018-02-18 21:36:44 -08:00
2021-05-05 12:29:04 -07:00
const DASHBOARD_DOMAIN = 'appstore-test.example.com';
2018-02-18 21:36:44 -08:00
const APPSTORE_TOKEN = 'appstoretoken';
2018-02-18 22:43:11 -08:00
const APP_ID = 'appid';
const APPSTORE_APP_ID = 'appstoreappid';
2019-07-26 10:49:29 -07:00
const MOCK_API_SERVER_ORIGIN = 'http://localhost:6060';
2018-02-18 21:36:44 -08:00
function setup(done) {
nock.cleanAll();
async.series([
database.initialize,
2019-07-26 10:49:29 -07:00
database._clear,
settings._setApiServerOrigin.bind(null, MOCK_API_SERVER_ORIGIN),
2021-05-05 12:29:04 -07:00
settings.setDashboardLocation.bind(null, DASHBOARD_DOMAIN, 'my.' + DASHBOARD_DOMAIN),
2019-10-29 15:46:33 -07:00
settings.initCache
2018-02-18 21:36:44 -08:00
], done);
}
function cleanup(done) {
nock.cleanAll();
async.series([
database._clear,
database.uninitialize
], done);
}
describe('Appstore', function () {
before(setup);
after(cleanup);
2018-02-18 22:43:11 -08:00
beforeEach(nock.cleanAll);
2019-05-03 16:27:47 -07:00
it('can set cloudron token', function (done) {
settingsdb.set(settings.CLOUDRON_TOKEN_KEY, APPSTORE_TOKEN, done);
2018-02-18 22:43:11 -08:00
});
it('can purchase an app', function (done) {
2019-07-26 10:49:29 -07:00
var scope1 = nock(MOCK_API_SERVER_ORIGIN)
2019-05-03 14:00:21 -07:00
.post(`/api/v1/cloudronapps?accessToken=${APPSTORE_TOKEN}`, function () { return true; })
2018-02-18 22:43:11 -08:00
.reply(201, {});
2019-05-06 14:29:56 -07:00
appstore.purchaseApp({ appId: APP_ID, appstoreId: APPSTORE_APP_ID, manifestId: APPSTORE_APP_ID }, function (error) {
2018-02-18 22:43:11 -08:00
expect(error).to.not.be.ok();
2018-05-29 23:24:08 +02:00
expect(scope1.isDone()).to.be.ok();
2018-02-18 22:43:11 -08:00
done();
});
});
it('unpurchase succeeds if app was never purchased', function (done) {
2019-07-26 10:49:29 -07:00
var scope1 = nock(MOCK_API_SERVER_ORIGIN)
2019-05-03 14:00:21 -07:00
.get(`/api/v1/cloudronapps/${APP_ID}?accessToken=${APPSTORE_TOKEN}`)
2018-02-18 22:43:11 -08:00
.reply(404, {});
2019-07-26 10:49:29 -07:00
var scope2 = nock(MOCK_API_SERVER_ORIGIN)
2019-05-03 14:00:21 -07:00
.delete(`/api/v1/cloudronapps/${APP_ID}?accessToken=${APPSTORE_TOKEN}`, function () { return true; })
2018-02-18 22:43:11 -08:00
.reply(204, {});
2019-05-06 14:29:56 -07:00
appstore.unpurchaseApp(APP_ID, { appstoreId: APPSTORE_APP_ID, manifestId: APPSTORE_APP_ID }, function (error) {
2018-02-18 22:43:11 -08:00
expect(error).to.not.be.ok();
expect(scope1.isDone()).to.be.ok();
expect(scope2.isDone()).to.not.be.ok();
done();
});
});
it('can unpurchase an app', function (done) {
2019-07-26 10:49:29 -07:00
var scope1 = nock(MOCK_API_SERVER_ORIGIN)
2019-05-03 14:00:21 -07:00
.get(`/api/v1/cloudronapps/${APP_ID}?accessToken=${APPSTORE_TOKEN}`)
2018-02-18 22:43:11 -08:00
.reply(200, {});
2019-07-26 10:49:29 -07:00
var scope2 = nock(MOCK_API_SERVER_ORIGIN)
2019-05-03 14:00:21 -07:00
.delete(`/api/v1/cloudronapps/${APP_ID}?accessToken=${APPSTORE_TOKEN}`, function () { return true; })
2018-02-18 22:43:11 -08:00
.reply(204, {});
2019-05-06 14:29:56 -07:00
appstore.unpurchaseApp(APP_ID, { appstoreId: APPSTORE_APP_ID, manifestId: APPSTORE_APP_ID }, function (error) {
2018-02-18 22:43:11 -08:00
expect(error).to.not.be.ok();
expect(scope1.isDone()).to.be.ok();
expect(scope2.isDone()).to.be.ok();
done();
2018-02-18 21:36:44 -08:00
});
});
});