Unverified Commit 716ec0de authored by Dan Jones's avatar Dan Jones
Browse files

test: add tests for auth method

+ Return 200 token response for valid credentials
+ Return 403 denied response for invalid credentials
parent 10d87d6d
...@@ -10,7 +10,7 @@ class Adapter { ...@@ -10,7 +10,7 @@ class Adapter {
this.config = config; this.config = config;
this.axios = axios; this.axios = axios;
this.validator = new Validator(protocol.schema); this.validator = new Validator(protocol.schema);
this.auth(); //this.auth();
} }
/** /**
...@@ -79,6 +79,9 @@ class Adapter { ...@@ -79,6 +79,9 @@ class Adapter {
.then((response) => { .then((response) => {
this.credentials = response.data; this.credentials = response.data;
return response; return response;
})
.catch((error) => {
return Promise.reject(error);
}); });
} }
......
...@@ -12,7 +12,7 @@ class Adapter { ...@@ -12,7 +12,7 @@ class Adapter {
this.config = config; this.config = config;
this.axios = axios; this.axios = axios;
this.validator = new Validator(protocol.schema); this.validator = new Validator(protocol.schema);
this.auth(); //this.auth();
} }
/** /**
...@@ -81,6 +81,9 @@ class Adapter { ...@@ -81,6 +81,9 @@ class Adapter {
.then((response) => { .then((response) => {
this.credentials = response.data; this.credentials = response.data;
return response; return response;
})
.catch((error) => {
return Promise.reject(error);
}); });
} }
......
Feature: Does the adapter work? Feature: Does the adapter authenticate?
The adapter behaves as expected When an adapter instance is created it authenticates and receives a token
Scenario: A token is granted with valid config Scenario: A token is granted with valid config
Given valid config Given valid config
When the adapter instance is created
When the auth method is called
Then the adapter credentials are populated Then the adapter credentials are populated
Scenario: Auth fails with invalid config Scenario: Auth fails with invalid config
Given invalid config Given invalid config
When the adapter instance is created
Then the adapter auth fails Then the adapter auth fails
\ No newline at end of file
...@@ -17,6 +17,8 @@ const { Adapter } = require('../../dist/adapter'); ...@@ -17,6 +17,8 @@ const { Adapter } = require('../../dist/adapter');
const { GenericProtocol } = require('../../dist/protocol'); const { GenericProtocol } = require('../../dist/protocol');
Before(function() { Before(function() {
mockAxios.reset();
mockAxios.onGet( mockAxios.onGet(
`${mockValidConfig.api}/token`, `${mockValidConfig.api}/token`,
...@@ -35,10 +37,17 @@ Given('valid config', function() { ...@@ -35,10 +37,17 @@ Given('valid config', function() {
this.config = mockValidConfig this.config = mockValidConfig
}); });
Then('the adapter credentials are populated', function() { When('the adapter instance is created', function() {
let mockProtocol = new GenericProtocol(this.schema); let mockProtocol = new GenericProtocol(this.schema);
let mockAdapter = new Adapter(mockProtocol, this.config); 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); assert.equal(this.adapter.credentials.token, fixtures.get('response-valid-token').token);
}); });
...@@ -47,15 +56,9 @@ Given('invalid config', function() { ...@@ -47,15 +56,9 @@ Given('invalid config', function() {
this.config = mockInvalidConfig; this.config = mockInvalidConfig;
}); });
Then('the adapter auth fails', function() { Then('the adapter auth fails', async function() {
assert.throws( this.adapter.auth()
function() { .catch((error) => {
let mockProtocol = new GenericProtocol(mockSchema); assert.equal(error.response.status, 403);
let mockAdapter = new Adapter(mockProtocol, mockInvalidConfig); });
this.adapter = mockAdapter;
},
{
name: "AxiosError"
}
);
}); });
\ No newline at end of file
...@@ -10,7 +10,6 @@ export class Adapter { ...@@ -10,7 +10,6 @@ export class Adapter {
this.config = config; this.config = config;
this.axios = axios; this.axios = axios;
this.validator = new Validator(protocol.schema); this.validator = new Validator(protocol.schema);
this.auth();
} }
/** /**
...@@ -79,6 +78,9 @@ export class Adapter { ...@@ -79,6 +78,9 @@ export class Adapter {
.then((response) => { .then((response) => {
this.credentials = response.data; this.credentials = response.data;
return response; return response;
})
.catch((error) => {
return Promise.reject(error);
}); });
} }
......
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