oa-jira
Version:
Octet Agile's JIRA connectivity project.
71 lines (59 loc) • 3.38 kB
JavaScript
const Missing = require('../../../src/commons/errors/missing.error');
const index = require('../../../');
const Connection = require('../../../src/connection/classes/connection.class');
const constants = require('../../../src/connection/constants/connection.constants');
describe('Check [Connection] index', () => {
describe('Check [create Bearer] service', () => {
it('should reject error when given nothing.', () => {
expect(index.connection.createBearer()).rejects.toThrow(new Missing('URL Base'));
});
it.each([
[new Missing('URL Base'), undefined, 'token'],
[new Missing('token'), 'baseURL', undefined]
])('should reject [%p] when given [%p] as base URL and [%p] as token.', (error, baseUrl, token) => {
expect(index.connection.createBearer({ baseUrl, token })).rejects.toThrow(error);
});
it('should resolve a new connection when base URL and token are valid.', async () => {
const connection = await index.connection.createBearer({ baseUrl: ' baseURL ', token: ' token ' });
expect(connection).toBeInstanceOf(Connection);
expect(connection.getMode()).toEqual(constants.mode.BEARER);
expect(connection.getBaseUrl()).toEqual('baseURL');
expect(connection.getUser()).toBeNull();
expect(connection.getToken()).toEqual('token');
expect(connection.buildAuthorization()).toEqual(`Bearer token`);
});
});
describe('Check [create Basic] service', () => {
it('should reject error when given nothing.', () => {
expect(index.connection.createBasic()).rejects.toThrow(new Missing('URL Base'));
});
it.each([
[new Missing('URL Base'), undefined, 'user', 'token'],
[new Missing('user'), 'baseURL', undefined, 'token'],
[new Missing('token'), 'baseURL', 'user', undefined]
])('should reject [%p] when given [%p] as base URL and [%p] as token.', (error, baseUrl, user, token) => {
expect(index.connection.createBasic({ baseUrl, user, token })).rejects.toThrow(error);
});
it('should resolve a new connection when base URL and token are valid.', async () => {
const connection = await index.connection.createBasic({ baseUrl: ' baseURL ', user: ' user ', token: ' token ' });
expect(connection).toBeInstanceOf(Connection);
expect(connection.getMode()).toEqual(constants.mode.BASIC);
expect(connection.getBaseUrl()).toEqual('baseURL');
expect(connection.getUser()).toEqual('user');
expect(connection.getToken()).toEqual('token');
expect(connection.buildAuthorization()).toEqual(`Basic ${Buffer.from(`user:token`).toString('base64')}`);
});
});
describe('Check [resolve] utility', () => {
it('should reject when cache is falsy.', async () => {
expect(Connection.resolve()).rejects.toThrow('A [connection] is mandatory and cannot be falsy.');
});
it('should reject when cache does not implements expected one.', async () => {
expect(Connection.resolve(new Error())).rejects.toThrow('A [connection] MUST implements [Connection].');
});
it('should resolve given when valid.', async () => {
const connection = await index.connection.createBasic({ baseUrl: 'url', user: 'u', token: 't' });
expect(Connection.resolve(connection)).resolves.toBe(connection);
});
});
});