cc-tracking/useful/good-e2e-tests.spec.js
2018-05-04 11:21:51 +01:00

145 lines
4.0 KiB
JavaScript

/**
* @fileOverview End-to-end testing of the get all ATPs related to a payable RTP
*/
'use strict';
const helper = require('../../utils/test/e2e-helper');
const request = require('supertest');
const express = require('express');
const chai = require('chai');
const initDevApi = require('../dev_server.js');
const expect = chai.expect;
/**
* Correct auth method (Bearer), correct token
*/
const TOKEN_VALID = 'YTM2ZGQ1NzUtOWFmNS01MjMyLTg5MjYtM2NkZjA5ZDU2ZGU1';
const HEADER_VALID = 'Bearer ' + TOKEN_VALID;
/**
* Correct auth method (Bearer), correct token
*/
const TOKEN_INVALID = 'MTM2ZGQ1NzUtOWN1nKYN0nKMP1nKYP0nKDU2ZGU1';
const HEADER_INVALID = 'Bearer ' + TOKEN_INVALID;
let app;
// Standard errored values
const TOO_LONG = '0000000000000000000000001';
const TOO_SHORT = '0123';
const BAD_UUID = '00000000000000000000000Z';
const VALID_UUID = '000000000000000000000000';
const MISSING_UUID = '';
// Valid test data
/**
* Make a useable path
*
* @param segment
* @returns {string}
*/
function makePath(segment) {
return '/dev/v0/payables/rtps/' + segment + '/atps';
}
/**
* Attempt to make an authenticated request
*
* @param expApp
* @param auth
* @param requestPath
* @returns {*}
*/
function makeAuthenticatedRequest(expApp, auth, requestPath) {
return request(expApp)
.get(requestPath)
.set('Accept', 'application/json')
.set('Authorization', auth);
}
describe('E2E: get All ATPs', () => {
/**
* Load the dev API router to handle `/dev/*` routes
*/
describe('objectID tests', () => {
before(() => {
app = new helper.DevValidatorApp();
});
it('with a valid objectID', () => {
app.setRequestPath(makePath(VALID_UUID));
return app.getAndValidate(502);
});
it('objectID with an invalid pattern', () => {
app.setRequestPath(makePath(BAD_UUID));
return app.getAndValidate(400, undefined, helper.errors.failedPattern('objectID'));
});
it('objectID is too short', () => {
app.setRequestPath(makePath(TOO_SHORT));
return app.getAndValidate(400, undefined, helper.errors.minLength('objectID'));
});
it('objectID is too long', () => {
app.setRequestPath(makePath(TOO_LONG));
return app.getAndValidate(400, undefined, helper.errors.maxLength('objectID'));
});
});
describe('invalid path', () => {
let expressApp;
/**
* Initialise the app before running any tests
*/
// Test missing RTP ObjectID
before(() => {
expressApp = express();
const devApiRouter = initDevApi.init();
expressApp.use('/dev', devApiRouter);
});
it('responds with 404', () => {
return makeAuthenticatedRequest(expressApp, HEADER_VALID, makePath(MISSING_UUID))
.expect(404);
});
it('responds with error body', () => {
return makeAuthenticatedRequest(expressApp, HEADER_VALID, makePath(MISSING_UUID))
.expect(404)
.then((response) => {
return expect(response.body).to.deep.equal({
code: 30000,
description: 'API path not found'
});
});
});
});
describe('authorization', () => {
// eslint-disable-next-line mocha/no-hooks-for-single-case
after(() => {
// Reset the authorization header after these tests, whether they
// pass or fail
app.resetAuthorizationHeader();
});
it('is required to access the path', () => {
app.setRequestAuthorization(HEADER_INVALID);
return app.getAndValidate(401).then((response) => {
return expect(response.body).to.deep.include({
code: -1,
info: 'Not authorised'
});
});
});
});
});