Unverified Commit 10d87d6d authored by Dan Jones's avatar Dan Jones
Browse files

test: initial tests of authentication

3 merge requests!15Resolve "Release v0.1",!2Resolve "Implement bdd tests in cucumber",!1Resolve "Import prototype adapter code from example-web-client"
Feature: Does the adapter work? Feature: Does the adapter work?
The adapter behaves as expected The adapter behaves as expected
Scenario: Axios is mocked Scenario: A token is granted with valid config
Given a mock adapter Given valid config
Then axios mock works Then the adapter credentials are populated
\ No newline at end of file
Scenario: Auth fails with invalid config
Given invalid config
Then the adapter auth fails
\ No newline at end of file
Feature: Is it Friday yet?
Everybody wants to know when it's Friday
Scenario: Sunday isn't Friday
Given today is Sunday
When I ask whether it's Friday yet
Then I should be told "Nope"
\ No newline at end of file
const assert = require('assert'); const assert = require('assert');
const { Given, When, Then } = require('@cucumber/cucumber'); const { Before, Given, When, Then } = require('@cucumber/cucumber');
var axios = require("axios"); const axios = require("axios");
var MockAdapter = require("axios-mock-adapter"); const MockAdapter = require("axios-mock-adapter");
// This sets the mock adapter on the default instance // This sets the mock adapter on the default instance
var mockAxios = new MockAdapter(axios); const mockAxios = new MockAdapter(axios);
const { fixtures } = require('../../test/fixtures/server');
const mockValidConfig = fixtures.get('valid-config');
const mockInvalidConfig = fixtures.get('invalid-config');
const mockConfig = require('../../test/mock/config.json');
const mockSchema = require('../../test/mock/swagger.json'); const mockSchema = require('../../test/mock/swagger.json');
const Adapter = require('../../src/adapter'); const { Adapter } = require('../../dist/adapter');
const GenericProtocol = require('../../src/protocol'); const { GenericProtocol } = require('../../dist/protocol');
function isItFriday(today) { Before(function() {
return 'Nope';
}
Given('today is Sunday', function () { mockAxios.onGet(
this.today = 'Sunday'; `${mockValidConfig.api}/token`,
}); { params: { client_id: mockValidConfig.client_id, secret: mockValidConfig.secret } }
).reply(200, fixtures.get('response-valid-token'));
mockAxios.onGet(
`${mockInvalidConfig.api}/token`,
{ params: { client_id: mockInvalidConfig.client_id, secret: mockInvalidConfig.secret } }
).reply(403, fixtures.get('response-denied-token'));
When('I ask whether it\'s Friday yet', function () {
this.actualAnswer = isItFriday(this.today);
}); });
Then('I should be told {string}', function (expectedAnswer) { Given('valid config', function() {
assert.strictEqual(this.actualAnswer, expectedAnswer); this.schema = mockSchema;
this.config = mockValidConfig
}); });
Given('a mock adapter', function() { Then('the adapter credentials are populated', function() {
let mockProtocol = new GenericProtocol(mockSchema); let mockProtocol = new GenericProtocol(this.schema);
let mockAdapter = new Adapter(mockProtocol, mockConfig); let mockAdapter = new Adapter(mockProtocol, this.config);
this.adapter = mockAdapter; this.adapter = mockAdapter;
assert.equal(this.adapter.credentials.token, fixtures.get('response-valid-token').token);
}); });
Then('axios mock works', function() { Given('invalid config', function() {
// Mock any GET request to /users this.schema = mockSchema;
// arguments for reply are (status, data, headers) this.config = mockInvalidConfig;
mock.onGet("/users").reply(200, { });
users: [{ id: 1, name: "John Smith" }],
});
axios.get("/users").then(function (response) { Then('the adapter auth fails', function() {
console.log(response.data); assert.throws(
}); function() {
let mockProtocol = new GenericProtocol(mockSchema);
let mockAdapter = new Adapter(mockProtocol, mockInvalidConfig);
this.adapter = mockAdapter;
},
{
name: "AxiosError"
}
);
}); });
\ No newline at end of file
{
"api": "https://example.backbone.com/api",
"client_id": "invalid-client-id",
"client_name": "InvalidClientName",
"subscription": "dot.delimited.topic.subscription.#",
"secret": "TheCollaredDoveCoosInTheChimneyPot"
}
\ No newline at end of file
{
"message": "Invalid client credentials"
}
\ No newline at end of file
{
"token": "gAAAAABjwB-vxtER44M2en6xYyt7G1WXp8QwfsiHw-ijCqNBZpQPwxxrBHzUU1fQ9lfPPo4QHj50p-yh203dV6zLLoTzuiReqGzE2InqAxOwv4gddlQWNFJKyrmg4mVVMX2VZe2cCAljmHxEo66BHgt_T24AieedMnI4VR2kw4SFiooFv5nr2W8=",
"expiry": "2030-12-31T23:59:59.000000"
}
\ No newline at end of file
const fs = require('fs');
const path = require('path');
exports.fixtures = {
get: function (fixtureName) {
try {
let fixtureContent = fs.readFileSync(path.join(__dirname, `${fixtureName}.json`));
let fixture = JSON.parse(fixtureContent);
return fixture;
} catch(e) {
console.error('Fixture not found', fixrureName);
return null;
}
}
}
\ No newline at end of file
File moved
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment