From 10d87d6d881f5c5906f42ff2c08d06a457788532 Mon Sep 17 00:00:00 2001
From: Dan Jones <dan.jones@noc.ac.uk>
Date: Thu, 12 Jan 2023 16:17:18 +0000
Subject: [PATCH] test: initial tests of authentication

---
 features/adapter_works.feature                | 10 ++-
 features/is_it_friday_yet.feature             |  7 --
 features/step_definitions/stepdefs.js         | 72 +++++++++++--------
 test/fixtures/invalid-config.json             |  7 ++
 test/fixtures/response-denied-token.json      |  3 +
 test/fixtures/response-valid-token.json       |  4 ++
 test/fixtures/server.js                       | 15 ++++
 .../valid-config.json}                        |  0
 8 files changed, 78 insertions(+), 40 deletions(-)
 delete mode 100644 features/is_it_friday_yet.feature
 create mode 100644 test/fixtures/invalid-config.json
 create mode 100644 test/fixtures/response-denied-token.json
 create mode 100644 test/fixtures/response-valid-token.json
 create mode 100644 test/fixtures/server.js
 rename test/{mock/config.json => fixtures/valid-config.json} (100%)

diff --git a/features/adapter_works.feature b/features/adapter_works.feature
index 73e96dd..a86687a 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 3786d9f..0000000
--- 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 9a90848..ececa01 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 0000000..82625f6
--- /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 0000000..d5fb311
--- /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 0000000..87306ff
--- /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 0000000..1b1f9ed
--- /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
-- 
GitLab