Add API to get subscription
This commit is contained in:
@@ -61,17 +61,6 @@ AppstoreError.BILLING_REQUIRED = 'Billing Required';
|
|||||||
|
|
||||||
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
var NOOP_CALLBACK = function (error) { if (error) debug(error); };
|
||||||
|
|
||||||
function getAppstoreConfig(callback) {
|
|
||||||
assert.strictEqual(typeof callback, 'function');
|
|
||||||
|
|
||||||
settings.getAppstoreConfig(function (error, result) {
|
|
||||||
if (error) return callback(new AppstoreError(AppstoreError.INTERNAL_ERROR, error));
|
|
||||||
if (!result.token) return callback(new AppstoreError(AppstoreError.BILLING_REQUIRED));
|
|
||||||
|
|
||||||
callback(null, result);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
function getCloudronToken(callback) {
|
function getCloudronToken(callback) {
|
||||||
assert.strictEqual(typeof callback, 'function');
|
assert.strictEqual(typeof callback, 'function');
|
||||||
|
|
||||||
@@ -128,18 +117,18 @@ function registerUser(email, password, callback) {
|
|||||||
function getSubscription(callback) {
|
function getSubscription(callback) {
|
||||||
assert.strictEqual(typeof callback, 'function');
|
assert.strictEqual(typeof callback, 'function');
|
||||||
|
|
||||||
getAppstoreConfig(function (error, appstoreConfig) {
|
getCloudronToken(function (error, token) {
|
||||||
if (error) return callback(error);
|
if (error) return callback(error);
|
||||||
|
|
||||||
const url = config.apiServerOrigin() + '/api/v1/users/' + appstoreConfig.userId + '/cloudrons/' + appstoreConfig.cloudronId + '/subscription';
|
const url = config.apiServerOrigin() + '/api/v1/subscription';
|
||||||
superagent.get(url).query({ accessToken: appstoreConfig.token }).timeout(30 * 1000).end(function (error, result) {
|
superagent.get(url).query({ accessToken: token }).timeout(30 * 1000).end(function (error, result) {
|
||||||
if (error && !error.response) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, error.message));
|
if (error && !error.response) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, error.message));
|
||||||
if (result.statusCode === 401) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, 'invalid appstore token'));
|
if (result.statusCode === 401) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, 'invalid appstore token'));
|
||||||
if (result.statusCode === 403) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, 'wrong user'));
|
if (result.statusCode === 403) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, 'wrong user'));
|
||||||
if (result.statusCode === 502) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, 'stripe error'));
|
if (result.statusCode === 502) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, 'stripe error'));
|
||||||
if (result.statusCode !== 200) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, 'unknown error'));
|
if (result.statusCode !== 200) return callback(new AppstoreError(AppstoreError.EXTERNAL_ERROR, 'unknown error'));
|
||||||
|
|
||||||
callback(null, result.body.subscription);
|
callback(null, result.body); // { email, subscription}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
exports = module.exports = {
|
exports = module.exports = {
|
||||||
subscribeCloudron: subscribeCloudron
|
subscribeCloudron: subscribeCloudron,
|
||||||
|
getSubscription: getSubscription
|
||||||
};
|
};
|
||||||
|
|
||||||
var appstore = require('../appstore.js'),
|
var appstore = require('../appstore.js'),
|
||||||
@@ -25,3 +26,14 @@ function subscribeCloudron(req, res, next) {
|
|||||||
next(new HttpSuccess(200, {}));
|
next(new HttpSuccess(200, {}));
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getSubscription(req, res, next) {
|
||||||
|
assert.strictEqual(typeof req.body, 'object');
|
||||||
|
|
||||||
|
appstore.getSubscription(function (error, result) {
|
||||||
|
if (error && error.reason === AppstoreError.EXTERNAL_ERROR) return next(new HttpError(424, error.message));
|
||||||
|
if (error) return next(new HttpError(500, error));
|
||||||
|
|
||||||
|
next(new HttpSuccess(200, result)); // { email, subscription }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|||||||
@@ -70,7 +70,7 @@ describe('Subscription API', function () {
|
|||||||
|
|
||||||
var scope2 = nock(config.apiServerOrigin())
|
var scope2 = nock(config.apiServerOrigin())
|
||||||
.post('/api/v1/register_cloudron?accessToken=SECRET_TOKEN', (body) => !!body.domain)
|
.post('/api/v1/register_cloudron?accessToken=SECRET_TOKEN', (body) => !!body.domain)
|
||||||
.reply(201, { cloudronId: 'cid', cloudronToken: 'token', licenseKey: 'lkey' });
|
.reply(201, { cloudronId: 'cid', cloudronToken: 'CLOUDRON_TOKEN', licenseKey: 'lkey' });
|
||||||
|
|
||||||
superagent.post(SERVER_URL + '/api/v1/subscription')
|
superagent.post(SERVER_URL + '/api/v1/subscription')
|
||||||
.send({ email: 'test@cloudron.io', password: 'secret', signup: false })
|
.send({ email: 'test@cloudron.io', password: 'secret', signup: false })
|
||||||
@@ -82,4 +82,20 @@ describe('Subscription API', function () {
|
|||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('can get subscription', function (done) {
|
||||||
|
var scope1 = nock(config.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/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();
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -138,6 +138,7 @@ function initializeExpressSync() {
|
|||||||
|
|
||||||
// subscription routes
|
// subscription routes
|
||||||
router.post('/api/v1/subscription', subscriptionScope, routes.subscription.subscribeCloudron);
|
router.post('/api/v1/subscription', subscriptionScope, routes.subscription.subscribeCloudron);
|
||||||
|
router.get ('/api/v1/subscription', subscriptionScope, routes.subscription.getSubscription);
|
||||||
|
|
||||||
// tasks
|
// tasks
|
||||||
router.get ('/api/v1/tasks', settingsScope, routes.tasks.list);
|
router.get ('/api/v1/tasks', settingsScope, routes.tasks.list);
|
||||||
|
|||||||
Reference in New Issue
Block a user