mirror of
https://github.com/elyby/accounts-frontend.git
synced 2024-12-02 11:41:04 +05:30
#355: fix authentication.test.js
This commit is contained in:
parent
d64c2a728d
commit
22ae85c9c6
@ -92,7 +92,7 @@ export async function validateToken(id: number, token: string, refreshToken: ?st
|
|||||||
try {
|
try {
|
||||||
user = await getInfoEndpoint(id, token);
|
user = await getInfoEndpoint(id, token);
|
||||||
} catch (resp) {
|
} catch (resp) {
|
||||||
const token = await handleTokenError(resp, refreshToken);
|
token = await handleTokenError(resp, refreshToken);
|
||||||
user = await getInfoEndpoint(id, token); // TODO: replace with recursive call
|
user = await getInfoEndpoint(id, token); // TODO: replace with recursive call
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable camelcase */
|
||||||
import expect from 'unexpected';
|
import expect from 'unexpected';
|
||||||
import sinon from 'sinon';
|
import sinon from 'sinon';
|
||||||
|
|
||||||
@ -6,6 +7,28 @@ import * as authentication from 'services/api/authentication';
|
|||||||
import * as accounts from 'services/api/accounts';
|
import * as accounts from 'services/api/accounts';
|
||||||
|
|
||||||
describe('authentication api', () => {
|
describe('authentication api', () => {
|
||||||
|
let server;
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
server = sinon.createFakeServer({
|
||||||
|
autoRespond: true
|
||||||
|
});
|
||||||
|
|
||||||
|
['get', 'post'].forEach((method) => {
|
||||||
|
server[method] = (url, resp = {}, status = 200, headers = {}) => {
|
||||||
|
server.respondWith(method, url, [
|
||||||
|
status,
|
||||||
|
{ 'Content-Type': 'application/json', ...headers },
|
||||||
|
JSON.stringify(resp)
|
||||||
|
]);
|
||||||
|
};
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(() => {
|
||||||
|
server.restore();
|
||||||
|
});
|
||||||
|
|
||||||
describe('#login', () => {
|
describe('#login', () => {
|
||||||
const params = {
|
const params = {
|
||||||
login: 'foo',
|
login: 'foo',
|
||||||
@ -113,15 +136,25 @@ describe('authentication api', () => {
|
|||||||
authentication.requestToken.restore();
|
authentication.requestToken.restore();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('resolves with new token and user object', () =>
|
it('resolves with new token and user object', async () => {
|
||||||
expect(authentication.validateToken(...validateTokenArgs),
|
server.post('/api/authentication/refresh-token', {
|
||||||
|
access_token: newToken,
|
||||||
|
refresh_token: validRefreshToken,
|
||||||
|
success: true,
|
||||||
|
expires_in: 50000
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
await expect(authentication.validateToken(...validateTokenArgs),
|
||||||
'to be fulfilled with', {token: newToken, refreshToken: validRefreshToken, user}
|
'to be fulfilled with', {token: newToken, refreshToken: validRefreshToken, user}
|
||||||
)
|
);
|
||||||
);
|
|
||||||
|
expect(server.requests[0].requestBody, 'to equal', `refresh_token=${validRefreshToken}`);
|
||||||
|
});
|
||||||
|
|
||||||
it('rejects if token request failed', () => {
|
it('rejects if token request failed', () => {
|
||||||
const error = 'Something wrong';
|
const error = {error: 'Unexpected error example'};
|
||||||
authentication.requestToken.returns(Promise.reject(error));
|
server.post('/api/authentication/refresh-token', error, 500);
|
||||||
|
|
||||||
return expect(authentication.validateToken(...validateTokenArgs),
|
return expect(authentication.validateToken(...validateTokenArgs),
|
||||||
'to be rejected with', error
|
'to be rejected with', error
|
||||||
@ -140,25 +173,28 @@ describe('authentication api', () => {
|
|||||||
const newToken = 'baz';
|
const newToken = 'baz';
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
sinon.stub(authentication, 'requestToken');
|
|
||||||
|
|
||||||
accounts.getInfo.onCall(0).returns(Promise.reject(expiredResponse));
|
accounts.getInfo.onCall(0).returns(Promise.reject(expiredResponse));
|
||||||
authentication.requestToken.returns(Promise.resolve(newToken));
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterEach(() => {
|
it('resolves with new token and user object', async () => {
|
||||||
authentication.requestToken.restore();
|
server.post('/api/authentication/refresh-token', {
|
||||||
});
|
access_token: newToken,
|
||||||
|
refresh_token: validRefreshToken,
|
||||||
|
success: true,
|
||||||
|
expires_in: 50000
|
||||||
|
});
|
||||||
|
|
||||||
it('resolves with new token and user object', () =>
|
|
||||||
expect(authentication.validateToken(...validateTokenArgs),
|
await expect(authentication.validateToken(...validateTokenArgs),
|
||||||
'to be fulfilled with', {token: newToken, refreshToken: validRefreshToken, user}
|
'to be fulfilled with', {token: newToken, refreshToken: validRefreshToken, user}
|
||||||
)
|
);
|
||||||
);
|
|
||||||
|
expect(server.requests[0].requestBody, 'to equal', `refresh_token=${validRefreshToken}`);
|
||||||
|
});
|
||||||
|
|
||||||
it('rejects if token request failed', () => {
|
it('rejects if token request failed', () => {
|
||||||
const error = 'Something wrong';
|
const error = {error: 'Unexpected error example'};
|
||||||
authentication.requestToken.returns(Promise.reject(error));
|
server.post('/api/authentication/refresh-token', error, 500);
|
||||||
|
|
||||||
return expect(authentication.validateToken(...validateTokenArgs),
|
return expect(authentication.validateToken(...validateTokenArgs),
|
||||||
'to be rejected with', error
|
'to be rejected with', error
|
||||||
|
Loading…
Reference in New Issue
Block a user