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

test: initial tests of authentication

parent 43b2b99f
Feature: Does the adapter work?
The adapter behaves as expected
Scenario: Axios is mocked
Given a mock adapter
Then axios mock works
\ No newline at end of file
Scenario: A token is granted with valid config
Given valid config
Then the adapter credentials are populated
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 { Given, When, Then } = require('@cucumber/cucumber');
const { Before, Given, When, Then } = require('@cucumber/cucumber');
var axios = require("axios");
var MockAdapter = require("axios-mock-adapter");
const axios = require("axios");
const MockAdapter = require("axios-mock-adapter");
// 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 Adapter = require('../../src/adapter');
const GenericProtocol = require('../../src/protocol');
const { Adapter } = require('../../dist/adapter');
const { GenericProtocol } = require('../../dist/protocol');
function isItFriday(today) {
return 'Nope';
}
Before(function() {
Given('today is Sunday', function () {
this.today = 'Sunday';
});
mockAxios.onGet(
`${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) {
assert.strictEqual(this.actualAnswer, expectedAnswer);
Given('valid config', function() {
this.schema = mockSchema;
this.config = mockValidConfig
});
Given('a mock adapter', function() {
let mockProtocol = new GenericProtocol(mockSchema);
let mockAdapter = new Adapter(mockProtocol, mockConfig);
Then('the adapter credentials are populated', function() {
let mockProtocol = new GenericProtocol(this.schema);
let mockAdapter = new Adapter(mockProtocol, this.config);
this.adapter = mockAdapter;
assert.equal(this.adapter.credentials.token, fixtures.get('response-valid-token').token);
});
Then('axios mock works', function() {
// Mock any GET request to /users
// arguments for reply are (status, data, headers)
mock.onGet("/users").reply(200, {
users: [{ id: 1, name: "John Smith" }],
});
axios.get("/users").then(function (response) {
console.log(response.data);
});
Given('invalid config', function() {
this.schema = mockSchema;
this.config = mockInvalidConfig;
});
Then('the adapter auth fails', function() {
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