2019-10-04 19:56:20 +07:00
|
|
|
import {
|
|
|
|
generateTokenWithTimeRange,
|
|
|
|
generateTokenWithExpirationAsString,
|
|
|
|
generateTokenWithOutExpiration,
|
|
|
|
generateInvalidToken,
|
|
|
|
} from '../../jest/unit/components/__mocks__/token';
|
2019-10-08 03:19:18 +07:00
|
|
|
|
|
|
|
import { isTokenExpire, makeLogin } from './login';
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
/* eslint-disable no-console */
|
2019-04-05 02:23:40 +07:00
|
|
|
console.error = jest.fn();
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
jest.mock('./api', () => ({
|
|
|
|
request: require('../../jest/unit/components/__mocks__/api').default.request,
|
2019-04-05 02:23:40 +07:00
|
|
|
}));
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
describe('isTokenExpire', (): void => {
|
|
|
|
test('isTokenExpire - token is not a valid payload', (): void => {
|
2019-04-05 02:23:40 +07:00
|
|
|
expect(isTokenExpire('not_a_valid_token')).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
test('isTokenExpire - token should not expire in 24 hrs range', (): void => {
|
2019-04-05 02:23:40 +07:00
|
|
|
const token = generateTokenWithTimeRange(24);
|
|
|
|
expect(isTokenExpire(token)).toBeFalsy();
|
|
|
|
});
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
test('isTokenExpire - token should expire for current time', (): void => {
|
2019-04-05 02:23:40 +07:00
|
|
|
const token = generateTokenWithTimeRange();
|
|
|
|
expect(isTokenExpire(token)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
test('isTokenExpire - token expiration is not available', (): void => {
|
2019-04-05 02:23:40 +07:00
|
|
|
const token = generateTokenWithOutExpiration();
|
|
|
|
expect(isTokenExpire(token)).toBeTruthy();
|
|
|
|
});
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
test('isTokenExpire - token is not a valid json token', (): void => {
|
2019-10-04 19:56:20 +07:00
|
|
|
const token = generateInvalidToken();
|
|
|
|
const result = ['Invalid token:', new SyntaxError('Unexpected token i in JSON at position 0'), 'xxxxxx.aW52YWxpZHRva2Vu.xxxxxx'];
|
2019-04-05 02:23:40 +07:00
|
|
|
expect(isTokenExpire(token)).toBeTruthy();
|
|
|
|
expect(console.error).toHaveBeenCalledWith(...result);
|
|
|
|
});
|
2019-10-04 19:56:20 +07:00
|
|
|
|
|
|
|
test('isTokenExpire - token expiration is not a number', (): void => {
|
|
|
|
const token = generateTokenWithExpirationAsString();
|
|
|
|
expect(isTokenExpire(token)).toBeTruthy();
|
|
|
|
});
|
2019-04-05 02:23:40 +07:00
|
|
|
});
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
describe('makeLogin', (): void => {
|
|
|
|
test('makeLogin - should give error for blank username and password', async (): Promise<void> => {
|
2019-04-05 02:23:40 +07:00
|
|
|
const result = {
|
|
|
|
error: {
|
|
|
|
description: "Username or password can't be empty!",
|
|
|
|
title: 'Unable to login',
|
2019-06-20 19:37:28 +07:00
|
|
|
type: 'error',
|
|
|
|
},
|
2019-04-05 02:23:40 +07:00
|
|
|
};
|
|
|
|
const login = await makeLogin();
|
|
|
|
expect(login).toEqual(result);
|
|
|
|
});
|
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
test('makeLogin - should login successfully', async (): Promise<void> => {
|
2019-04-05 02:23:40 +07:00
|
|
|
const { username, password } = { username: 'sam', password: '1234' };
|
|
|
|
const result = { token: 'TEST_TOKEN', username: 'sam' };
|
|
|
|
const login = await makeLogin(username, password);
|
|
|
|
expect(login).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('makeLogin - login should failed with 401', async () => {
|
|
|
|
const result = {
|
|
|
|
error: {
|
|
|
|
description: 'bad username/password, access denied',
|
|
|
|
title: 'Unable to login',
|
2019-06-20 19:37:28 +07:00
|
|
|
type: 'error',
|
|
|
|
},
|
2019-04-05 02:23:40 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
const { username, password } = { username: 'sam', password: '123456' };
|
|
|
|
const login = await makeLogin(username, password);
|
|
|
|
expect(login).toEqual(result);
|
|
|
|
});
|
|
|
|
|
|
|
|
test('makeLogin - login should failed with when no data is sent', async () => {
|
|
|
|
const result = {
|
|
|
|
error: {
|
|
|
|
title: 'Unable to login',
|
|
|
|
type: 'error',
|
2019-06-20 19:37:28 +07:00
|
|
|
description: "Username or password can't be empty!",
|
|
|
|
},
|
2019-04-05 02:23:40 +07:00
|
|
|
};
|
|
|
|
|
|
|
|
const { username, password } = { username: '', password: '' };
|
|
|
|
const login = await makeLogin(username, password);
|
|
|
|
expect(login).toEqual(result);
|
|
|
|
});
|
|
|
|
});
|