diff --git a/features/adapter_works.feature b/features/adapter_works.feature index 73e96dd437376309e65cfd37ab6284ec6bd471e1..a86687a3c53409d9bdd2050084773ec4f68624a8 100644 --- a/features/adapter_works.feature +++ b/features/adapter_works.feature @@ -1,6 +1,10 @@ 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 diff --git a/features/is_it_friday_yet.feature b/features/is_it_friday_yet.feature deleted file mode 100644 index 3786d9f5d009e42579c405f7fd020e705f923502..0000000000000000000000000000000000000000 --- a/features/is_it_friday_yet.feature +++ /dev/null @@ -1,7 +0,0 @@ -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 diff --git a/features/step_definitions/stepdefs.js b/features/step_definitions/stepdefs.js index 9a908483f422743b467fbe9628a8af3867070140..ececa01d59052882e405d30535dc160ab9fc3e69 100644 --- a/features/step_definitions/stepdefs.js +++ b/features/step_definitions/stepdefs.js @@ -1,49 +1,61 @@ 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 diff --git a/test/fixtures/invalid-config.json b/test/fixtures/invalid-config.json new file mode 100644 index 0000000000000000000000000000000000000000..82625f6ebf737eb84933aa05088f3047bcebbd62 --- /dev/null +++ b/test/fixtures/invalid-config.json @@ -0,0 +1,7 @@ +{ + "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 diff --git a/test/fixtures/response-denied-token.json b/test/fixtures/response-denied-token.json new file mode 100644 index 0000000000000000000000000000000000000000..d5fb311657bbafe5d89d35d1a24313c0ad784feb --- /dev/null +++ b/test/fixtures/response-denied-token.json @@ -0,0 +1,3 @@ +{ + "message": "Invalid client credentials" +} \ No newline at end of file diff --git a/test/fixtures/response-valid-token.json b/test/fixtures/response-valid-token.json new file mode 100644 index 0000000000000000000000000000000000000000..87306ff195913ce9ba866065ede968128439d3fa --- /dev/null +++ b/test/fixtures/response-valid-token.json @@ -0,0 +1,4 @@ +{ + "token": "gAAAAABjwB-vxtER44M2en6xYyt7G1WXp8QwfsiHw-ijCqNBZpQPwxxrBHzUU1fQ9lfPPo4QHj50p-yh203dV6zLLoTzuiReqGzE2InqAxOwv4gddlQWNFJKyrmg4mVVMX2VZe2cCAljmHxEo66BHgt_T24AieedMnI4VR2kw4SFiooFv5nr2W8=", + "expiry": "2030-12-31T23:59:59.000000" +} \ No newline at end of file diff --git a/test/fixtures/server.js b/test/fixtures/server.js new file mode 100644 index 0000000000000000000000000000000000000000..1b1f9ed6673b0e38e60eaedc568837ecefdaa00d --- /dev/null +++ b/test/fixtures/server.js @@ -0,0 +1,15 @@ +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 diff --git a/test/mock/config.json b/test/fixtures/valid-config.json similarity index 100% rename from test/mock/config.json rename to test/fixtures/valid-config.json