test('Should allow access to the route with get request method', async () => { const response = await server.inject({ method: 'GET', url: '/route-request-get-allow', headers: { authorization: 'Basic ' + (new Buffer('sg1002:pwtest', 'utf8')).toString('base64') } }); expect(response.statusCode).to.equal(200); });
test(`should be defined`, async () => { // Conditions const fakeHistoryEntity = new fakeHistory(fakeValidHistoryAttrs); // Assertions expect(fakeHistoryEntity). and.not.to.be.undefined(). and.to.be.instanceof(fakeHistory); expect(fakeHistoryEntity.addObservationPoints). and.to.be.a.function(); });
suite(`class`, () => { test(`should be defined when instantiated with an empty Object`, async () => { // Conditions const fakeWeatherEntity = new fakeWeather({}); // Assertions expect(fakeWeatherEntity). and.not.to.be.undefined(). and.to.be.instanceof(fakeWeather); }); });
test('should not override a valid retriever (single handler)', (done) => { const retriever1 = (source, key, context) => { return key + '-1'; }; const retriever2 = (source, key, context) => { return key + '-2'; }; dataRetriever.register('test-override-error', retriever1); expect(dataRetriever.register.bind(dataRetriever, 'test-override-error', retriever2)).to.throw(); done(); });
test('should have error on missing policy', (done) => { const information = { username: 'user00003', group: ['publisher'], premium: true, blocked: false }; Rbac.evaluatePolicy(null, dataRetriever.createChild(information), (err, applies) => { expect(err).to.exist(); done(); }); });
experiment('Target unit tests', () => { const dataRetriever = new DataRetrievalRouter(); test('should apply (partial match)', (done) => { const invalidTarget = []; Rbac.evaluateTarget(invalidTarget, dataRetriever, (err, applies) => { expect(err).to.exist(); done(); }); }); });
test('should use parent synchronous retriever', () => { const retriever = (source, key, context) => { return 'key-' + key; }; dataRetriever.register('sync-parent-test-1', retriever); const childDataRetriever = dataRetriever.createChild(); return childDataRetriever.get('sync-parent-test-1:x') .then((result) => { expect(result).to.equal('key-x'); }); });
test('should have error on invalid data retriever', (done) => { Rbac.evaluatePolicy(policySet, 'test', (err, applies) => { expect(err).to.exist(); done(); }); });
test(`should return an empty array if null`, async () => { // Conditions const fakeHistoryEntity = new fakeHistory(fakeValidHistoryAttrs); const fakeResponse = fakeHistoryEntity.getDailyStartOfDay(null); // Assertions expect(fakeHistoryEntity). and.not.to.be.undefined(). and.to.be.instanceof(fakeHistory); expect(fakeResponse). and.to.be.an.array(). and.to.be.empty(); });
suite(`class`, () => { test(`should be defined when instantiated with all injected dependencies`, async () => { // Conditions const fakeAddressWeatherHistoryInteractor = new FakeDefaultAddressWeatherHistoryInteractor(fakeConstructorParams); // Assertions expect(fakeAddressWeatherHistoryInteractor). and.not.to.be.undefined(). and.to.be.instanceof(FakeDefaultAddressWeatherHistoryInteractor); }); });
test('Should deny access to the route with non-matching param and query fields', async () => { const response = await server.inject({ method: 'GET', url: '/match-fields/123?test=456', headers: { authorization: 'Basic ' + (new Buffer('sg1002:pwtest', 'utf8')).toString('base64') } }); expect(response.statusCode).to.equal(401); });
test('Should deny access to the route with get request method', async () => { const response = await server.inject({ method: 'GET', url: '/route-non-existing-field', headers: { authorization: 'Basic ' + (new Buffer('sg1002:pwtest', 'utf8')).toString('base64') } }); expect(response.statusCode).to.equal(401); });
test('Should not have access to the route, with policy targeting a regex of last name', async () => { const response = await server.inject({ method: 'GET', url: '/regex-match', headers: { authorization: 'Basic ' + (new Buffer('sg1001:pwtest', 'utf8')).toString('base64') } }); expect(response.statusCode).to.equal(401); });
test('should not override a valid retriever (multiple handlers)', (done) => { const retriever1 = (source, key, context) => { return key + '-1'; }; const retriever2 = (source, key, context) => { return key + '-2'; }; dataRetriever.register(['test-override-error-multiple-1', 'test-override-error-multiple-2', 'test-override-error-multiple-3'], retriever1); expect(dataRetriever.register.bind(dataRetriever, ['test-override-error-multiple-2', 'test-override-error-multiple-4'], retriever2)).to.throw(Error, 'There is a data retriever already registered for the source: test-override-error-multiple-2'); done(); });
test('Should not have access to the route, with overriden policy targeting the username', async () => { const response = await server.inject({ method: 'GET', url: '/overriden-policy', headers: { authorization: 'Basic ' + (new Buffer('sg1001:pwtest', 'utf8')).toString('base64') } }); expect(response.statusCode).to.equal(401); });