Unverified Commit 73605335 authored by Dan Jones's avatar Dan Jones
Browse files

test: add tests for token handling

+ getAuthorizationHeader
+ tokenValid
parent bb722d6b
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
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
const assert = require('assert');
const { When, Then } = require('@cucumber/cucumber');
const { When } = require('@cucumber/cucumber');
const { fixtures } = require('../../fixtures/server');
......
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
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
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