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 {
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);
});
}
......
......@@ -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);
});
}
......
Feature: Does the adapter work?
The adapter behaves as expected
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
......@@ -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
......@@ -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);
});
}
......
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