test/unit/services.callback.js
import test from 'ava';
import sinon from 'sinon';
import { client, callbackService } from '../../src';
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.server = sinon.fakeServer.create();
t.context.server.autoRespond = true;
t.context.server.respondWith('GET', `${client.apiBaseUrl}/api/callbacks`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
status: 'ok',
content: [{
action: 'alerts',
action_object: {
title: 'Tracking new system {{caption}}',
message: 'Tracking new system {{caption}}',
acknowledgement: {
required: false,
duration: 5,
duration_type: 'Minute'
}
},
action_owner_id: 57,
action_owner_type: 'DynamicContent::ContentType',
action_subject_id: null,
action_subject_type: null,
callback_type: 'messaging',
created_at: '2016-05-12T21:26:53Z',
email_to_reply: null,
emails_to_notify: '',
id: 106756,
message: null,
reevaluate: false,
title: 'Solar Winds( Create)',
trigger: 'dynamic_contents#create',
updated_at: '2016-05-13T23:15:37Z',
tags: []
}, {
action: 'dynamic_contents',
action_object: 'solar_winds_connector',
action_owner_id: 85,
action_owner_type: 'PushMessage',
action_subject_id: 655936,
action_subject_type: 'DynamicContent::Content',
callback_type: 'redirect',
created_at: '2016-05-14T00:09:48Z',
email_to_reply: null,
emails_to_notify: '',
id: 108858,
message: null,
reevaluate: false,
title: null,
trigger: null,
updated_at: '2016-05-14T00:09:48Z',
tags: []
}, {
action: 'dynamic_contents',
action_object: 'solar_winds_connector',
action_owner_id: 86,
action_owner_type: 'PushMessage',
action_subject_id: 655938,
action_subject_type: 'DynamicContent::Content',
callback_type: 'redirect',
created_at: '2016-05-14T00:09:48Z',
email_to_reply: null,
emails_to_notify: '',
id: 108859,
message: null,
reevaluate: false,
title: null,
trigger: null,
updated_at: '2016-05-14T00:09:48Z',
tags: []
}]
})]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/api/callbacks/108858`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
status: 'ok',
content: {
action: 'dynamic_contents',
action_object: 'solar_winds_connector',
action_owner_id: 85,
action_owner_type: 'PushMessage',
action_subject_id: 655936,
action_subject_type: 'DynamicContent::Content',
callback_type: 'redirect',
created_at: '2016-05-14T00:09:48Z',
email_to_reply: null,
emails_to_notify: '',
id: 108858,
message: null,
reevaluate: false,
title: null,
trigger: null,
updated_at: '2016-05-14T00:09:48Z',
tags: []
}
})]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/api/callbacks/108858/execute`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
status: 'ok',
message: 'The callback action was executed successfully'
})]);
});
test.afterEach(t => t.context.server.restore());
// CallbackService.getCallbacks method
/**
* @test {CallbackService.getCallbacks}
*/
test('[unit] CallbackService.getCallbacks should exist', (t) => {
t.truthy(callbackService.getCallbacks);
});
/**
* @test {CallbackService.getCallbacks}
*/
test.serial('[unit] CallbackService.getCallbacks should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'CallbackService.getCallbacks error: requires authToken';
t.throws(() => callbackService.getCallbacks(), expectedError);
});
/**
* @test {CallbackService.getCallbacks}
*/
test('[unit] CallbackService.getCallbacks should return a promise', async (t) => {
t.truthy(await callbackService.getCallbacks().then);
});
// /**
// * @test {CallbackService.getCallbacks}
// */
// test('[unit] CallbackService.getCallbacks should make a properly formatted get request', async (t) => {
// await callbackService.getCallbacks();
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/api/callbacks`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// });
/**
* @test {CallbackService.getCallbacks}
*/
test('[unit] CallbackService.getCallbacks should receive a valid response for a get request', async (t) => {
const response = await callbackService.getCallbacks();
// const request = t.context.server.requests[0];
t.truthy(response);
t.is(response.status, 'ok');
t.is(response.content.length, 3);
t.is(response.content[0].action, 'alerts');
t.is(response.content[0].action_object.title, 'Tracking new system {{caption}}');
t.is(response.content[0].action_object.message, 'Tracking new system {{caption}}');
t.is(response.content[0].action_object.acknowledgement.required, false);
t.is(response.content[0].action_object.acknowledgement.duration, 5);
t.is(response.content[0].action_object.acknowledgement.duration_type, 'Minute');
t.is(response.content[0].action_owner_id, 57);
t.is(response.content[0].action_owner_type, 'DynamicContent::ContentType');
t.is(response.content[0].action_subject_id, null);
t.is(response.content[0].action_subject_type, null);
t.is(response.content[0].callback_type, 'messaging');
t.is(response.content[0].created_at, '2016-05-12T21:26:53Z');
t.is(response.content[0].email_to_reply, null);
t.is(response.content[0].emails_to_notify, '');
t.is(response.content[0].id, 106756);
t.is(response.content[0].message, null);
t.is(response.content[0].reevaluate, false);
t.is(response.content[0].title, 'Solar Winds( Create)');
t.is(response.content[0].trigger, 'dynamic_contents#create');
t.is(response.content[0].updated_at, '2016-05-13T23:15:37Z');
t.is(response.content[0].tags.length, 0);
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
});
// CallbackService.getCallback method
/**
* @test {CallbackService.getCallback}
*/
test('[unit] CallbackService.getCallback should exist', (t) => {
t.truthy(callbackService.getCallback);
});
/**
* @test {CallbackService.getCallback}
*/
test.serial('[unit] CallbackService.getCallback should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = `CallbackService.getCallback error: requires authToken and id is not defined`;
t.throws(() => callbackService.getCallback(), expectedError);
});
/**
* @test {CallbackService.getCallback}
*/
test('[unit] CallbackService.getCallback should throw an error when there is no id parameter', (t) => {
const expectedError = 'CallbackService.getCallback error: id is not defined';
t.throws(() => callbackService.getCallback(), expectedError);
});
/**
* @test {CallbackService.getCallback}
*/
test('[unit] CallbackService.getCallback should return a promise', async (t) => {
t.truthy(await callbackService.getCallback(108858).then);
});
// /**
// * @test {CallbackService.getCallback}
// */
// test('[unit] CallbackService.getCallback should make a properly formatted get request', async (t) => {
// await callbackService.getCallback(108858);
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/api/callbacks/108858`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// });
/**
* @test {CallbackService.getCallback}
*/
test('[unit] CallbackService.getCallback should receive a valid response for a get request', async (t) => {
const response = await callbackService.getCallback(108858);
// const request = t.context.server.requests[0];
t.truthy(response);
t.is(response.status, 'ok');
t.is(response.content.action, 'dynamic_contents');
t.is(response.content.action_object, 'solar_winds_connector');
t.is(response.content.action_owner_id, 85);
t.is(response.content.action_owner_type, 'PushMessage');
t.is(response.content.action_subject_id, 655936);
t.is(response.content.action_subject_type, 'DynamicContent::Content');
t.is(response.content.callback_type, 'redirect');
t.is(response.content.created_at, '2016-05-14T00:09:48Z');
t.is(response.content.email_to_reply, null);
t.is(response.content.emails_to_notify, '');
t.is(response.content.id, 108858);
t.is(response.content.message, null);
t.is(response.content.reevaluate, false);
t.is(response.content.title, null);
t.is(response.content.trigger, null);
t.is(response.content.updated_at, '2016-05-14T00:09:48Z');
t.is(response.content.tags.length, 0);
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
});
// CallbackService.execute method
/**
* @test {CallbackService.execute}
*/
test('[unit] CallbackService.execute should exist', (t) => {
t.truthy(callbackService.execute);
});
/**
* @test {CallbackService.execute}
*/
test.serial('[unit] CallbackService.execute should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = `CallbackService.execute error: requires authToken and id is not defined`;
t.throws(() => callbackService.execute(), expectedError);
});
/**
* @test {CallbackService.execute}
*/
test('[unit] CallbackService.execute should throw an error when there is no id parameter', (t) => {
const expectedError = 'CallbackService.execute error: id is not defined';
t.throws(() => callbackService.execute(), expectedError);
});
/**
* @test {CallbackService.execute}
*/
test('[unit] CallbackService.execute should return a promise', async (t) => {
t.truthy(await callbackService.execute(108858).then);
});
// /**
// * @test {CallbackService.execute}
// */
// test('[unit] CallbackService.execute should make a properly formatted get request', async (t) => {
// await callbackService.execute(108858);
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/api/callbacks/108858/execute`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// });
/**
* @test {CallbackService.execute}
*/
test('[unit] CallbackService.execute should receive a valid response for a get request', async (t) => {
const response = await callbackService.execute(108858);
// const request = t.context.server.requests[0];
t.truthy(response);
t.is(response.status, 'ok');
t.is(response.message, 'The callback action was executed successfully');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
});