2019-06-30 19:02:50 +05:30
|
|
|
import expect from 'test/unexpected';
|
2019-12-07 16:58:52 +05:30
|
|
|
import { RootState } from 'reducers';
|
2016-08-10 00:47:49 +05:30
|
|
|
|
|
|
|
import bearerHeaderMiddleware from 'components/user/middlewares/bearerHeaderMiddleware';
|
|
|
|
|
|
|
|
describe('bearerHeaderMiddleware', () => {
|
2019-12-07 16:58:52 +05:30
|
|
|
const emptyState: RootState = {
|
2019-11-27 14:33:32 +05:30
|
|
|
user: {},
|
|
|
|
accounts: {
|
|
|
|
active: null,
|
|
|
|
available: [],
|
|
|
|
},
|
2019-12-07 16:58:52 +05:30
|
|
|
} as any;
|
2019-11-27 14:33:32 +05:30
|
|
|
|
|
|
|
describe('when token available', () => {
|
|
|
|
const token = 'foo';
|
|
|
|
const middleware = bearerHeaderMiddleware({
|
|
|
|
getState: () => ({
|
|
|
|
...emptyState,
|
2016-11-05 15:41:41 +05:30
|
|
|
accounts: {
|
2019-11-27 14:33:32 +05:30
|
|
|
active: 1,
|
|
|
|
available: [
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
token,
|
2019-12-07 16:58:52 +05:30
|
|
|
username: 'username',
|
|
|
|
email: 'email',
|
|
|
|
refreshToken: null,
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
],
|
2017-12-31 00:34:31 +05:30
|
|
|
},
|
2019-11-27 14:33:32 +05:30
|
|
|
}),
|
2019-12-07 16:58:52 +05:30
|
|
|
} as any);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
it('should set Authorization header', async () => {
|
|
|
|
let data: any = {
|
2019-11-27 14:33:32 +05:30
|
|
|
options: {
|
|
|
|
headers: {},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
data = middleware.before && (await middleware.before(data));
|
2016-11-05 15:41:41 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expectBearerHeader(data, token);
|
2016-11-05 15:41:41 +05:30
|
|
|
});
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
it('overrides user.token with options.token if available', async () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const tokenOverride = 'tokenOverride';
|
2019-12-07 16:58:52 +05:30
|
|
|
let data: any = {
|
2019-11-27 14:33:32 +05:30
|
|
|
options: {
|
|
|
|
headers: {},
|
|
|
|
token: tokenOverride,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
data = middleware.before && (await middleware.before(data));
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2019-11-27 14:33:32 +05:30
|
|
|
expectBearerHeader(data, tokenOverride);
|
|
|
|
});
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
it('disables token if options.token is null', async () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const tokenOverride = null;
|
2019-12-07 16:58:52 +05:30
|
|
|
const data: any = {
|
2019-11-27 14:33:32 +05:30
|
|
|
options: {
|
2019-12-07 16:58:52 +05:30
|
|
|
headers: {} as { [key: string]: any },
|
2019-11-27 14:33:32 +05:30
|
|
|
token: tokenOverride,
|
|
|
|
},
|
|
|
|
};
|
2016-08-10 00:47:49 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
if (!middleware.before) {
|
|
|
|
throw new Error('No middleware.before');
|
|
|
|
}
|
|
|
|
|
|
|
|
const resp = await middleware.before(data);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
expect(resp.options.headers.Authorization, 'to be undefined');
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
it('should not set Authorization header if no token', async () => {
|
2019-11-27 14:33:32 +05:30
|
|
|
const middleware = bearerHeaderMiddleware({
|
|
|
|
getState: () => ({
|
|
|
|
...emptyState,
|
|
|
|
}),
|
2019-12-07 16:58:52 +05:30
|
|
|
} as any);
|
2016-11-05 15:41:41 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
const data: any = {
|
2019-11-27 14:33:32 +05:30
|
|
|
options: {
|
2019-12-07 16:58:52 +05:30
|
|
|
headers: {} as { [key: string]: any },
|
2019-11-27 14:33:32 +05:30
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
if (!middleware.before) {
|
|
|
|
throw new Error('No middleware.before');
|
|
|
|
}
|
|
|
|
|
|
|
|
const resp = await middleware.before(data);
|
2019-11-27 14:33:32 +05:30
|
|
|
|
2019-12-07 16:58:52 +05:30
|
|
|
expect(resp.options.headers.Authorization, 'to be undefined');
|
2019-11-27 14:33:32 +05:30
|
|
|
});
|
|
|
|
|
|
|
|
function expectBearerHeader(data, token) {
|
|
|
|
expect(data.options.headers, 'to satisfy', {
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
});
|
|
|
|
}
|
2016-08-10 00:47:49 +05:30
|
|
|
});
|