applinks: add tests
This commit is contained in:
@@ -6,7 +6,9 @@ exports = module.exports = {
|
||||
get,
|
||||
update,
|
||||
del,
|
||||
getIcon
|
||||
getIcon,
|
||||
|
||||
load
|
||||
};
|
||||
|
||||
const assert = require('assert'),
|
||||
@@ -16,6 +18,18 @@ const assert = require('assert'),
|
||||
HttpError = require('connect-lastmile').HttpError,
|
||||
HttpSuccess = require('connect-lastmile').HttpSuccess;
|
||||
|
||||
async function load(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.id, 'string');
|
||||
|
||||
const [error, result] = await safe(applinks.get(req.params.id));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
if (!result) return next(new HttpError(404, 'Applink not found'));
|
||||
|
||||
req.resource = result;
|
||||
|
||||
next();
|
||||
}
|
||||
|
||||
async function listByUser(req, res, next) {
|
||||
assert.strictEqual(typeof req.user, 'object');
|
||||
|
||||
@@ -31,50 +45,49 @@ async function listByUser(req, res, next) {
|
||||
async function add(req, res, next) {
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
// required
|
||||
if (!req.body.upstreamUri || typeof req.body.upstreamUri !== 'string') return next(new HttpError(400, 'upstreamUri must be a non-empty string'));
|
||||
if (typeof req.body.accessRestriction !== 'object') return next(new HttpError(400, 'accessRestriction must be an object'));
|
||||
|
||||
if ('label' in req.body && typeof req.body.label !== 'string') return next(new HttpError(400, 'label must be a string'));
|
||||
if ('tags' in req.body && !Array.isArray(req.body.tags)) return next(new HttpError(400, 'tags must be an array with strings'));
|
||||
if ('accessRestriction' in req.body && typeof req.body.accessRestriction !== 'object') return next(new HttpError(400, 'accessRestriction must be an object'));
|
||||
if ('icon' in req.body && typeof req.body.icon !== 'string') return next(new HttpError(400, 'icon must be a string'));
|
||||
|
||||
const [error] = await safe(applinks.add(req.body));
|
||||
const [error, id] = await safe(applinks.add(req.body));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(201, {}));
|
||||
next(new HttpSuccess(201, { id }));
|
||||
}
|
||||
|
||||
async function get(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.id, 'string');
|
||||
|
||||
const [error, result] = await safe(applinks.get(req.params.id));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
if (!result) return next(new HttpError(404, 'Applink not found'));
|
||||
|
||||
// we have a separate route for this
|
||||
delete result.icon;
|
||||
delete req.resource.icon;
|
||||
|
||||
next(new HttpSuccess(200, result));
|
||||
next(new HttpSuccess(200, req.resource));
|
||||
}
|
||||
|
||||
async function update(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.id, 'string');
|
||||
assert.strictEqual(typeof req.body, 'object');
|
||||
|
||||
if (!req.body.upstreamUri || typeof req.body.upstreamUri !== 'string') return next(new HttpError(400, 'upstreamUri must be a non-empty string'));
|
||||
if ('upstreamUri' in req.body && (!req.body.upstreamUri || typeof req.body.upstreamUri !== 'string')) return next(new HttpError(400, 'upstreamUri must be a non-empty string'));
|
||||
if ('accessRestriction' in req.body && typeof req.body.accessRestriction !== 'object') return next(new HttpError(400, 'accessRestriction must be an object'));
|
||||
if ('label' in req.body && typeof req.body.label !== 'string') return next(new HttpError(400, 'label must be a string'));
|
||||
if ('tags' in req.body && !Array.isArray(req.body.tags)) return next(new HttpError(400, 'tags must be an array with strings'));
|
||||
if ('accessRestriction' in req.body && typeof req.body.accessRestriction !== 'object') return next(new HttpError(400, 'accessRestriction must be an object'));
|
||||
if ('icon' in req.body && typeof req.body.icon !== 'string') return next(new HttpError(400, 'icon must be a string'));
|
||||
|
||||
const [error] = await safe(applinks.update(req.params.id, req.body));
|
||||
const [error] = await safe(applinks.update(req.resource, req.body));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(202, {}));
|
||||
next(new HttpSuccess(200, {}));
|
||||
}
|
||||
|
||||
async function del(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.id, 'string');
|
||||
|
||||
const [error] = await safe(applinks.del(req.params.id));
|
||||
const [error] = await safe(applinks.del(req.resource));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(204));
|
||||
@@ -83,9 +96,5 @@ async function del(req, res, next) {
|
||||
async function getIcon(req, res, next) {
|
||||
assert.strictEqual(typeof req.params.id, 'string');
|
||||
|
||||
const [error, icon] = await safe(applinks.getIcon(req.params.id, { original: req.query.original }));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
if (!icon) return next(new HttpError(404, 'no such icon'));
|
||||
|
||||
res.send(icon);
|
||||
res.send(req.resource.icon);
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ async function setName(req, res, next) {
|
||||
const [error] = await safe(groups.setName(req.resource, req.body.name, AuditSource.fromRequest(req)));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { }));
|
||||
next(new HttpSuccess(200, {}));
|
||||
}
|
||||
|
||||
async function setMembers(req, res, next) {
|
||||
@@ -69,7 +69,7 @@ async function setMembers(req, res, next) {
|
||||
const [error] = await safe(groups.setMembers(req.resource, req.body.userIds, { skipSourceCheck: false }, AuditSource.fromRequest(req)));
|
||||
if (error) return next(BoxError.toHttpError(error));
|
||||
|
||||
next(new HttpSuccess(200, { }));
|
||||
next(new HttpSuccess(200, {}));
|
||||
}
|
||||
|
||||
async function list(req, res, next) {
|
||||
|
||||
88
src/routes/test/applinks-test.js
Normal file
88
src/routes/test/applinks-test.js
Normal file
@@ -0,0 +1,88 @@
|
||||
'use strict';
|
||||
|
||||
/* global it:false */
|
||||
/* global describe:false */
|
||||
/* global before:false */
|
||||
/* global after:false */
|
||||
|
||||
const applinks = require('../../applinks.js'),
|
||||
common = require('./common.js'),
|
||||
expect = require('expect.js'),
|
||||
superagent = require('superagent');
|
||||
|
||||
describe('AppLinks API', function () {
|
||||
const { setup, cleanup, serverUrl, owner } = common;
|
||||
|
||||
before(setup);
|
||||
after(cleanup);
|
||||
|
||||
let applinkId;
|
||||
|
||||
it('can add applink', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/applinks`)
|
||||
.query({ access_token: owner.token })
|
||||
.send({ label: 'Berlin', tags: ['city'], upstreamUri: 'https://www.berlin.de', accessRestriction: null });
|
||||
expect(response.statusCode).to.equal(201);
|
||||
expect(response.body.id).to.be.ok();
|
||||
|
||||
applinkId = response.body.id;
|
||||
});
|
||||
|
||||
it('cannot get random applink', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/applinks/random`)
|
||||
.query({ access_token: owner.token })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.equal(404);
|
||||
});
|
||||
|
||||
it('cannot get valid applink', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/applinks/${applinkId}`)
|
||||
.query({ access_token: owner.token })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(response.body.upstreamUri).to.be('https://www.berlin.de');
|
||||
});
|
||||
|
||||
it('can get get icon', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/applinks/${applinkId}/icon`)
|
||||
.query({ access_token: owner.token })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(response.headers['content-type']).to.be('application/octet-stream');
|
||||
});
|
||||
|
||||
it('can update applink tags', async function () {
|
||||
const response = await superagent.post(`${serverUrl}/api/v1/applinks/${applinkId}`)
|
||||
.query({ access_token: owner.token })
|
||||
.send({ tags: ['city', 'germany'] });
|
||||
expect(response.statusCode).to.equal(200);
|
||||
|
||||
const result = await applinks.get(applinkId);
|
||||
expect(result.tags).to.eql(['city', 'germany']);
|
||||
});
|
||||
|
||||
it('can list applinks', async function () {
|
||||
const response = await superagent.get(`${serverUrl}/api/v1/applinks`)
|
||||
.query({ access_token: owner.token });
|
||||
expect(response.statusCode).to.equal(200);
|
||||
expect(response.body.applinks.length).to.equal(1);
|
||||
expect(response.body.applinks[0].upstreamUri).to.be('https://www.berlin.de');
|
||||
});
|
||||
|
||||
it('cannot del random applink', async function () {
|
||||
const response = await superagent.del(`${serverUrl}/api/v1/applinks/random`)
|
||||
.query({ access_token: owner.token })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.equal(404);
|
||||
});
|
||||
|
||||
it('can del applink', async function () {
|
||||
const response = await superagent.del(`${serverUrl}/api/v1/applinks/${applinkId}`)
|
||||
.query({ access_token: owner.token })
|
||||
.ok(() => true);
|
||||
expect(response.statusCode).to.equal(204);
|
||||
|
||||
const result = await applinks.get(applinkId);
|
||||
expect(result).to.be(null);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user