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

111 lines
2.7 KiB
JavaScript
Raw Normal View History

2016-08-09 22:17:49 +03:00
import expect from 'unexpected';
import bearerHeaderMiddleware from 'components/user/middlewares/bearerHeaderMiddleware';
describe('bearerHeaderMiddleware', () => {
2016-11-05 12:11:41 +02:00
const emptyState = {
user: {},
accounts: {
active: null
}
};
describe('when token available', () => {
2016-08-09 22:17:49 +03:00
const token = 'foo';
const middleware = bearerHeaderMiddleware({
getState: () => ({
2016-11-05 12:11:41 +02:00
...emptyState,
accounts: {
active: {token}
}
2016-08-09 22:17:49 +03:00
})
});
it('should set Authorization header', () => {
let data = {
options: {
headers: {}
}
};
2016-08-09 22:17:49 +03:00
data = middleware.before(data);
2016-11-05 12:11:41 +02:00
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-09 22:17:49 +03:00
2016-11-05 12:11:41 +02:00
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 12:11:41 +02:00
});
describe('when legacy token available', () => {
const token = 'foo';
const middleware = bearerHeaderMiddleware({
getState: () => ({
...emptyState,
user: {token}
})
});
it('should set Authorization header', () => {
let data = {
2016-11-05 12:11:41 +02:00
options: {
headers: {}
}
};
data = middleware.before(data);
2016-11-05 12:11:41 +02:00
expectBearerHeader(data, token);
2016-08-09 22:17:49 +03:00
});
});
it('should not set Authorization header if no token', () => {
const middleware = bearerHeaderMiddleware({
getState: () => ({
2016-11-05 12:11:41 +02:00
...emptyState
2016-08-09 22:17:49 +03:00
})
});
let data = {
2016-08-09 22:17:49 +03:00
options: {
headers: {}
}
};
data = middleware.before(data);
2016-08-09 22:17:49 +03:00
expect(data.options.headers.Authorization, 'to be undefined');
});
2016-11-05 12:11:41 +02:00
function expectBearerHeader(data, token) {
expect(data.options.headers, 'to satisfy', {
Authorization: `Bearer ${token}`
});
}
2016-08-09 22:17:49 +03:00
});