accounts-frontend/src/components/user/middlewares/bearerHeaderMiddleware.test.js

94 lines
2.3 KiB
JavaScript
Raw Normal View History

import expect from 'test/unexpected';
2016-08-10 00:47:49 +05:30
import bearerHeaderMiddleware from 'components/user/middlewares/bearerHeaderMiddleware';
describe('bearerHeaderMiddleware', () => {
2016-11-05 15:41:41 +05:30
const emptyState = {
user: {},
accounts: {
active: null,
available: [],
},
2016-11-05 15:41:41 +05:30
};
describe('when token available', () => {
2016-08-10 00:47:49 +05:30
const token = 'foo';
const middleware = bearerHeaderMiddleware({
getState: () => ({
2016-11-05 15:41:41 +05:30
...emptyState,
accounts: {
active: 1,
available: [{
id: 1,
token,
}],
2016-11-05 15:41:41 +05:30
}
2016-08-10 00:47:49 +05:30
})
});
it('should set Authorization header', () => {
let data = {
options: {
headers: {}
}
};
2016-08-10 00:47:49 +05:30
data = middleware.before(data);
2016-11-05 15:41:41 +05:30
expectBearerHeader(data, token);
});
it('overrides user.token with options.token if available', () => {
const tokenOverride = 'tokenOverride';
let data = {
options: {
headers: {},
token: tokenOverride
}
};
data = middleware.before(data);
2016-08-10 00:47:49 +05:30
2016-11-05 15:41:41 +05:30
expectBearerHeader(data, tokenOverride);
});
it('disables token if options.token is null', () => {
const tokenOverride = null;
let data = {
options: {
headers: {},
token: tokenOverride
}
};
data = middleware.before(data);
expect(data.options.headers.Authorization, 'to be undefined');
});
2016-11-05 15:41:41 +05:30
});
2016-08-10 00:47:49 +05:30
it('should not set Authorization header if no token', () => {
const middleware = bearerHeaderMiddleware({
getState: () => ({
2016-11-05 15:41:41 +05:30
...emptyState
2016-08-10 00:47:49 +05:30
})
});
let data = {
2016-08-10 00:47:49 +05:30
options: {
headers: {}
}
};
data = middleware.before(data);
2016-08-10 00:47:49 +05:30
expect(data.options.headers.Authorization, 'to be undefined');
});
2016-11-05 15:41:41 +05:30
function expectBearerHeader(data, token) {
expect(data.options.headers, 'to satisfy', {
Authorization: `Bearer ${token}`
});
}
2016-08-10 00:47:49 +05:30
});