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

172 lines
7.1 KiB
JavaScript
Raw Normal View History

2019-05-04 11:45:03 -07:00
/* global it:false */
/* global describe:false */
/* global before:false */
/* global after:false */
'use strict';
2022-04-04 13:54:57 -07:00
const appstore = require('../../appstore.js'),
common = require('./common.js'),
constants = require('../../constants.js'),
2019-05-04 11:45:03 -07:00
expect = require('expect.js'),
nock = require('nock'),
2019-07-26 10:49:29 -07:00
settings = require('../../settings.js'),
2021-06-03 22:39:26 -07:00
superagent = require('superagent');
2019-05-04 11:45:03 -07:00
2022-04-04 13:54:57 -07:00
const { setup, cleanup, serverUrl, owner, appstoreToken } = common;
2019-05-04 11:45:03 -07:00
2019-05-05 13:00:45 -07:00
describe('Appstore Apps API', function () {
2019-05-04 11:45:03 -07:00
before(setup);
after(cleanup);
2022-04-04 13:54:57 -07:00
it('cannot list apps when appstore is down', async function () {
2021-06-03 22:39:26 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/appstore/apps`)
.query({ access_token: owner.token })
.ok(() => true);
2022-04-04 13:54:57 -07:00
expect(response.statusCode).to.be(424);
2019-05-04 11:45:03 -07:00
});
2022-04-04 13:54:57 -07:00
it('cannot get app with bad token', async function () {
2023-08-04 15:34:38 +05:30
const scope1 = nock(await appstore.getApiServerOrigin())
2022-04-04 13:54:57 -07:00
.get(`/api/v1/apps/org.wordpress.cloudronapp?accessToken=${appstoreToken}`)
2022-09-19 17:21:55 +02:00
.reply(403, {});
2022-04-04 13:54:57 -07:00
2021-06-03 22:39:26 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/appstore/apps/org.wordpress.cloudronapp`)
.query({ access_token: owner.token })
.ok(() => true);
2022-09-19 17:21:55 +02:00
expect(response.statusCode).to.be(412);
2021-06-03 22:39:26 -07:00
expect(scope1.isDone()).to.be.ok();
2019-05-04 11:45:03 -07:00
});
2021-06-03 22:39:26 -07:00
it('can list apps', async function () {
2023-08-04 15:34:38 +05:30
const scope1 = nock(await appstore.getApiServerOrigin())
.get(`/api/v1/apps?accessToken=${appstoreToken}&boxVersion=${constants.VERSION}&unstable=true`, () => true)
2019-05-04 11:45:03 -07:00
.reply(200, { apps: [] });
2021-06-03 22:39:26 -07:00
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();
2019-05-04 11:45:03 -07:00
});
2021-06-03 22:39:26 -07:00
it('can get app', async function () {
2023-08-04 15:34:38 +05:30
const scope1 = nock(await appstore.getApiServerOrigin())
2022-04-04 13:54:57 -07:00
.get(`/api/v1/apps/org.wordpress.cloudronapp?accessToken=${appstoreToken}`, () => true)
2019-05-04 11:45:03 -07:00
.reply(200, { apps: [] });
2021-06-03 22:39:26 -07:00
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();
2019-05-04 11:45:03 -07:00
});
2021-06-03 22:39:26 -07:00
it('can get app version', async function () {
2023-08-04 15:34:38 +05:30
const scope1 = nock(await appstore.getApiServerOrigin())
2022-04-04 13:54:57 -07:00
.get(`/api/v1/apps/org.wordpress.cloudronapp/versions/3.4.2?accessToken=${appstoreToken}`, () => true)
2019-05-04 11:45:03 -07:00
.reply(200, { apps: [] });
2021-06-03 22:39:26 -07:00
const response = await superagent.get(`${serverUrl}/api/v1/appstore/apps/org.wordpress.cloudronapp/versions/3.4.2`)
.query({ access_token: owner.token });
2019-05-04 11:45:03 -07:00
2021-06-03 22:39:26 -07:00
expect(response.statusCode).to.equal(200);
expect(scope1.isDone()).to.be.ok();
});
2019-05-04 11:45:03 -07:00
});
2019-05-05 13:00:45 -07:00
2022-04-04 13:54:57 -07:00
describe('Appstore Cloudron Registration API - existing user', function () {
before(async function () {
await setup();
await appstore._unregister();
});
2019-05-05 13:00:45 -07:00
after(cleanup);
2021-06-03 22:39:26 -07:00
it('can setup subscription', async function () {
2023-08-04 15:34:38 +05:30
const scope1 = nock(await appstore.getApiServerOrigin())
.post('/api/v1/register_user', (body) => body.email && body.password && body.utmSource)
2022-04-04 13:54:57 -07:00
.reply(201, {});
2023-08-04 15:34:38 +05:30
const scope2 = nock(await appstore.getApiServerOrigin())
2019-05-05 13:00:45 -07:00
.post('/api/v1/login', (body) => body.email && body.password)
.reply(200, { userId: 'userId', accessToken: 'SECRET_TOKEN' });
2023-08-04 15:34:38 +05:30
const scope3 = nock(await appstore.getApiServerOrigin())
2019-05-07 14:15:35 -07:00
.post('/api/v1/register_cloudron', (body) => !!body.domain && body.accessToken === 'SECRET_TOKEN')
2022-03-31 12:31:35 -07:00
.reply(201, { cloudronId: 'cid', cloudronToken: 'CLOUDRON_TOKEN' });
2019-05-05 13:00:45 -07:00
2021-06-03 22:39:26 -07:00
const response = await superagent.post(`${serverUrl}/api/v1/appstore/register_cloudron`)
2019-05-05 13:00:45 -07:00
.send({ email: 'test@cloudron.io', password: 'secret', signup: false })
2022-11-22 22:14:59 +01:00
.query({ access_token: owner.token })
.ok(() => true);
2021-06-03 22:39:26 -07:00
expect(response.statusCode).to.equal(201);
2022-04-04 13:54:57 -07:00
expect(scope1.isDone()).to.not.be.ok(); // should not have called register_user since signup is false
2021-06-03 22:39:26 -07:00
expect(scope2.isDone()).to.be.ok();
2022-04-04 13:54:57 -07:00
expect(scope3.isDone()).to.be.ok();
2023-08-02 20:07:03 +05:30
expect(await settings.get(settings.APPSTORE_API_TOKEN_KEY)).to.be('CLOUDRON_TOKEN');
2022-04-04 13:54:57 -07:00
nock.cleanAll();
2019-05-05 13:00:45 -07:00
});
2022-04-04 13:54:57 -07:00
it('can get subscription', async function () {
2023-08-04 15:34:38 +05:30
const scope1 = nock(await appstore.getApiServerOrigin())
2022-04-04 13:54:57 -07:00
.get('/api/v1/subscription?accessToken=CLOUDRON_TOKEN', () => true)
.reply(200, { subscription: { plan: { id: 'free' } }, email: 'test@cloudron.io' });
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();
});
2019-05-06 20:20:07 -07:00
});
2022-04-04 13:54:57 -07:00
describe('Appstore Cloudron Registration API - new user signup', function () {
before(async function () {
await setup();
await appstore._unregister();
});
2019-05-06 20:20:07 -07:00
after(cleanup);
2021-06-03 22:39:26 -07:00
it('can setup subscription', async function () {
2023-08-04 15:34:38 +05:30
const scope1 = nock(await appstore.getApiServerOrigin())
.post('/api/v1/register_user', (body) => body.email && body.password && body.utmSource)
2022-04-04 13:54:57 -07:00
.reply(201, {});
2019-05-05 13:00:45 -07:00
2023-08-04 15:34:38 +05:30
const scope2 = nock(await appstore.getApiServerOrigin())
2019-05-05 13:00:45 -07:00
.post('/api/v1/login', (body) => body.email && body.password)
.reply(200, { userId: 'userId', accessToken: 'SECRET_TOKEN' });
2023-08-04 15:34:38 +05:30
const scope3 = nock(await appstore.getApiServerOrigin())
2019-05-07 14:15:35 -07:00
.post('/api/v1/register_cloudron', (body) => !!body.domain && body.accessToken === 'SECRET_TOKEN')
2022-03-31 12:31:35 -07:00
.reply(201, { cloudronId: 'cid', cloudronToken: 'CLOUDRON_TOKEN' });
2019-05-05 13:00:45 -07:00
2021-06-03 22:39:26 -07:00
const response = await superagent.post(`${serverUrl}/api/v1/appstore/register_cloudron`)
2019-05-05 13:00:45 -07:00
.send({ email: 'test@cloudron.io', password: 'secret', signup: true })
2021-06-03 22:39:26 -07:00
.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();
2023-08-02 20:07:03 +05:30
expect(await settings.get(settings.APPSTORE_API_TOKEN_KEY)).to.be('CLOUDRON_TOKEN');
2022-04-04 13:54:57 -07:00
});
2021-06-03 22:39:26 -07:00
it('can get subscription', async function () {
2023-08-04 15:34:38 +05:30
const scope1 = nock(await appstore.getApiServerOrigin())
2019-05-05 13:00:45 -07:00
.get('/api/v1/subscription?accessToken=CLOUDRON_TOKEN', () => true)
.reply(200, { subscription: { plan: { id: 'free' } }, email: 'test@cloudron.io' });
2021-06-03 22:39:26 -07:00
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();
2019-05-05 13:00:45 -07:00
});
});