From 716ec0defa331dda6dcdbf80880636ac7055eeae Mon Sep 17 00:00:00 2001 From: Dan Jones <dan.jones@noc.ac.uk> Date: Fri, 13 Jan 2023 10:23:05 +0000 Subject: [PATCH] test: add tests for auth method + Return 200 token response for valid credentials + Return 403 denied response for invalid credentials --- dist/adapter.esm.js | 5 ++++- dist/adapter.js | 5 ++++- features/adapter_authenticates.feature | 13 ++++++++++++ features/adapter_works.feature | 10 --------- features/step_definitions/stepdefs.js | 29 ++++++++++++++------------ src/adapter/index.js | 4 +++- 6 files changed, 40 insertions(+), 26 deletions(-) create mode 100644 features/adapter_authenticates.feature delete mode 100644 features/adapter_works.feature diff --git a/dist/adapter.esm.js b/dist/adapter.esm.js index ac53dcc..21ae71a 100644 --- a/dist/adapter.esm.js +++ b/dist/adapter.esm.js @@ -10,7 +10,7 @@ class Adapter { this.config = config; this.axios = axios; this.validator = new Validator(protocol.schema); - this.auth(); + //this.auth(); } /** @@ -79,6 +79,9 @@ class Adapter { .then((response) => { this.credentials = response.data; return response; + }) + .catch((error) => { + return Promise.reject(error); }); } diff --git a/dist/adapter.js b/dist/adapter.js index f68489d..fb5072a 100644 --- a/dist/adapter.js +++ b/dist/adapter.js @@ -12,7 +12,7 @@ class Adapter { this.config = config; this.axios = axios; this.validator = new Validator(protocol.schema); - this.auth(); + //this.auth(); } /** @@ -81,6 +81,9 @@ class Adapter { .then((response) => { this.credentials = response.data; return response; + }) + .catch((error) => { + return Promise.reject(error); }); } diff --git a/features/adapter_authenticates.feature b/features/adapter_authenticates.feature new file mode 100644 index 0000000..c55e271 --- /dev/null +++ b/features/adapter_authenticates.feature @@ -0,0 +1,13 @@ +Feature: Does the adapter authenticate? + When an adapter instance is created it authenticates and receives a token + + Scenario: A token is granted with valid config + Given valid config + When the adapter instance is created + When the auth method is called + Then the adapter credentials are populated + + Scenario: Auth fails with invalid config + Given invalid config + When the adapter instance is created + Then the adapter auth fails \ No newline at end of file diff --git a/features/adapter_works.feature b/features/adapter_works.feature deleted file mode 100644 index a86687a..0000000 --- a/features/adapter_works.feature +++ /dev/null @@ -1,10 +0,0 @@ -Feature: Does the adapter work? - The adapter behaves as expected - - 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/step_definitions/stepdefs.js b/features/step_definitions/stepdefs.js index ececa01..926115a 100644 --- a/features/step_definitions/stepdefs.js +++ b/features/step_definitions/stepdefs.js @@ -17,6 +17,8 @@ const { Adapter } = require('../../dist/adapter'); const { GenericProtocol } = require('../../dist/protocol'); Before(function() { + + mockAxios.reset(); mockAxios.onGet( `${mockValidConfig.api}/token`, @@ -35,10 +37,17 @@ Given('valid config', function() { this.config = mockValidConfig }); -Then('the adapter credentials are populated', function() { +When('the adapter instance is created', function() { let mockProtocol = new GenericProtocol(this.schema); let mockAdapter = new Adapter(mockProtocol, this.config); - this.adapter = mockAdapter; + this.adapter = mockAdapter; +}); + +When('the auth method is called', async function() { + await this.adapter.auth(); +}); + +Then(('the adapter credentials are populated'), function() { assert.equal(this.adapter.credentials.token, fixtures.get('response-valid-token').token); }); @@ -47,15 +56,9 @@ Given('invalid config', function() { 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" - } - ); +Then('the adapter auth fails', async function() { + this.adapter.auth() + .catch((error) => { + assert.equal(error.response.status, 403); + }); }); \ No newline at end of file diff --git a/src/adapter/index.js b/src/adapter/index.js index 4796b55..8eff44c 100644 --- a/src/adapter/index.js +++ b/src/adapter/index.js @@ -10,7 +10,6 @@ export class Adapter { this.config = config; this.axios = axios; this.validator = new Validator(protocol.schema); - this.auth(); } /** @@ -79,6 +78,9 @@ export class Adapter { .then((response) => { this.credentials = response.data; return response; + }) + .catch((error) => { + return Promise.reject(error); }); } -- GitLab