From 73605335ddb9b5f975caf9e0f334c29e44f87a70 Mon Sep 17 00:00:00 2001
From: Dan Jones <dan.jones@noc.ac.uk>
Date: Tue, 17 Jan 2023 13:42:08 +0000
Subject: [PATCH] test: add tests for token handling

+ getAuthorizationHeader
+ tokenValid
---
 .../adapter_get-authorization-header.feature  | 21 +++++++++++++++
 features/adapter_token-valid.feature          | 21 +++++++++++++++
 test/cucumber/adapter/broadcast.steps.js      |  3 +--
 test/cucumber/adapter/common.steps.js         |  0
 .../adapter/get-authorization-header.steps.js | 15 +++++++++++
 test/cucumber/adapter/token-valid.steps.js    | 26 +++++++++++++++++++
 6 files changed, 84 insertions(+), 2 deletions(-)
 create mode 100644 features/adapter_get-authorization-header.feature
 create mode 100644 features/adapter_token-valid.feature
 create mode 100644 test/cucumber/adapter/common.steps.js
 create mode 100644 test/cucumber/adapter/get-authorization-header.steps.js
 create mode 100644 test/cucumber/adapter/token-valid.steps.js

diff --git a/features/adapter_get-authorization-header.feature b/features/adapter_get-authorization-header.feature
new file mode 100644
index 0000000..59de9a4
--- /dev/null
+++ b/features/adapter_get-authorization-header.feature
@@ -0,0 +1,21 @@
+Feature: Does the adapter authenticate?
+  When an adapter instance is created it authenticates and receives a token
+
+  Scenario: getAuthorizationHeader returns a bearer token
+    Given valid config
+    When the adapter instance is created
+    When the auth method is called
+    When the getAuthorizationHeader method is called
+    Then a headers object is returned containing a bearer token authorization header
+
+  Scenario: getAuthorizationHeader implicitly calls auth if required
+    Given valid config
+    When the adapter instance is created
+    When the getAuthorizationHeader method is called
+    Then a headers object is returned containing a bearer token authorization header
+
+  Scenario: getAuthorizationHeader implicitly calls auth if required
+    Given invalid config
+    When the adapter instance is created
+    When the getAuthorizationHeader method is called
+    Then an error response is returned with status 403
\ No newline at end of file
diff --git a/features/adapter_token-valid.feature b/features/adapter_token-valid.feature
new file mode 100644
index 0000000..53fe89d
--- /dev/null
+++ b/features/adapter_token-valid.feature
@@ -0,0 +1,21 @@
+Feature: Is the token valid?
+  The adapter tokenValid method works as expected
+
+  Scenario: If adapter has not authed token is invalid
+    Given valid config
+    When the adapter instance is created
+    Then tokenValid returns false  
+
+  Scenario: If credentials.expiry is in the future token is valid
+    Given valid config
+    When the adapter instance is created
+    When the auth method is called
+    When the token expiry is in the future
+    Then tokenValid returns true  
+
+  Scenario: If credentials.expiry is in the past token is invalid
+    Given valid config
+    When the adapter instance is created
+    When the auth method is called
+    When the token expiry is in the past
+    Then tokenValid returns false 
diff --git a/test/cucumber/adapter/broadcast.steps.js b/test/cucumber/adapter/broadcast.steps.js
index 38e495d..ad08de5 100644
--- a/test/cucumber/adapter/broadcast.steps.js
+++ b/test/cucumber/adapter/broadcast.steps.js
@@ -1,5 +1,4 @@
-const assert = require('assert');
-const { When, Then } = require('@cucumber/cucumber');
+const { When } = require('@cucumber/cucumber');
 
 const { fixtures } = require('../../fixtures/server');
 
diff --git a/test/cucumber/adapter/common.steps.js b/test/cucumber/adapter/common.steps.js
new file mode 100644
index 0000000..e69de29
diff --git a/test/cucumber/adapter/get-authorization-header.steps.js b/test/cucumber/adapter/get-authorization-header.steps.js
new file mode 100644
index 0000000..af31d0b
--- /dev/null
+++ b/test/cucumber/adapter/get-authorization-header.steps.js
@@ -0,0 +1,15 @@
+const assert = require('assert');
+const { When, Then } = require('@cucumber/cucumber');
+
+When('the getAuthorizationHeader method is called', function() {
+  this.call = this.adapter.getAuthorizationHeader()
+});
+
+Then('a headers object is returned containing a bearer token authorization header', function() {
+  this.call.then(headers => {
+    assert.ok('Authorization' in headers);
+    const authHeaderWords = headers.Authorization.split(" ");
+    assert.ok(authHeaderWords[0] === 'Bearer');
+    assert.ok(authHeaderWords[1] === this.adapter.credentials.token);
+  });
+});
\ No newline at end of file
diff --git a/test/cucumber/adapter/token-valid.steps.js b/test/cucumber/adapter/token-valid.steps.js
new file mode 100644
index 0000000..8ac886d
--- /dev/null
+++ b/test/cucumber/adapter/token-valid.steps.js
@@ -0,0 +1,26 @@
+const assert = require('assert');
+const { When, Then } = require('@cucumber/cucumber');
+
+When('the token expiry is in the future', function() {
+  const expiry = new Date();
+  expiry.setHours(expiry.getHours()+1);
+  this.adapter.credentials.expiry = expiry.toISOString();
+});
+
+When('the token expiry is in the past', function() {
+  const expiry = new Date();
+  expiry.setHours(expiry.getHours()-1);
+  this.adapter.credentials.expiry = expiry.toISOString();  
+});
+
+// Boolean parameters are not supported
+// and returns "true" would be misleading
+Then('tokenValid returns true', function() {
+  const isValid = this.adapter.tokenValid();
+  assert.ok(isValid);
+});
+
+Then('tokenValid returns false', function() {
+  const isValid = this.adapter.tokenValid();
+  assert.ok(!isValid);
+});
\ No newline at end of file
-- 
GitLab