test/unit/services.system.js
import test from 'ava';
import sinon from 'sinon';
import { client, systemService as sys } 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('POST', `${client.apiBaseUrl}/api/log`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
status: 'ok',
message: 'Debug Log was successfully created'
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/x_auth/auth`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
status: 'new',
token: 'b0dcdf85785490bc47c782e4a87443dab489833fe2a7dc1a486d6c4da66bd490'
})]);
// t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/related_mobile_users/1797/reinvite`,
// [200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]
// );
// t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/related_mobile_users/1797/reinvite`,
// [200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]
// );
// t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/related_mobile_users/1797/reinvite`,
// [200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]
// );
t.context.json = {
site_id: '12345',
user_id: '21770',
};
});
test.afterEach(t => t.context.server.restore());
// SystemService.log method
/**
* @test {SystemService.log}
*/
test.serial('[unit] SystemService.log should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'Clinical6 log error: requires authToken';
t.truthy(() => sys.log(), expectedError);
});
/**
* @test {SystemService.log}
*/
test('[unit] SystemService.log should throw an error when an event is passed in without a level field', (t) => {
const undefinedError = 'Clinical6 log error: logEntry does not have level';
t.truthy(() => sys.log({ message: 'log message' }), undefinedError);
});
/**
* @test {SystemService.log}
*/
test('[unit] SystemService.log should successfully track a new log event', async (t) => {
const response = await sys.log();
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/log`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({}));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.status, 'ok');
t.is(response.message, 'Debug Log was successfully created');
});
/**
* @test {SystemService.log}
*/
test('[unit] SystemService.log should successfully track a new log event with just the entry parameter', async (t) => {
const entry = {
message: 'log message',
level: 'Fatal',
};
const response = await sys.log(entry);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/log`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({
// entry: {
// message: 'log message',
// level: 'Fatal',
// },
// }));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.status, 'ok');
t.is(response.message, 'Debug Log was successfully created');
});
/**
* @test {SystemService.log}
*/
test('[unit] SystemService.log should successfully track a new log event with the entry and requestInformation parameters', async (t) => {
const entry = {
message: 'log message',
level: 'Fatal',
};
const requestInformation = {
url: 'MyString',
method: 'MyString',
params: 'MyText',
status_code: 'MyString',
};
const response = await sys.log(entry, requestInformation);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/log`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({
// entry: {
// message: 'log message',
// level: 'Fatal',
// },
// request_information: {
// url: 'MyString',
// method: 'MyString',
// params: 'MyText',
// status_code: 'MyString',
// },
// }));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.status, 'ok');
t.is(response.message, 'Debug Log was successfully created');
});
/**
* @test {SystemService.log}
*/
test('[unit] SystemService.log should successfully track a new log event with the entry, requestInformation, and deviceInformation parameters', async (t) => {
const entry = {
message: 'log message',
tag: 'MyString',
level: 'Fatal',
};
const requestInformation = {
url: 'MyString',
method: 'MyString',
params: 'MyText',
status_code: 'MyString',
};
const deviceInformation = {
udid: 'MyString',
token: 'MyString',
push_id: 'MyString',
app_version: 'MyString',
os_version: 'MyString',
network_status: 'MyString',
gps_status: 'MyString',
latitude: '1.5',
longitude: '1.5',
brand: 'MyString',
manufacturer: 'MyString',
carrier: 'MyString',
};
const response = await sys.log(entry, requestInformation, deviceInformation);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/log`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({
// entry: {
// message: 'log message',
// tag: 'MyString',
// level: 'Fatal',
// },
// request_information: {
// url: 'MyString',
// method: 'MyString',
// params: 'MyText',
// status_code: 'MyString',
// },
// device_information: {
// udid: 'MyString',
// token: 'MyString',
// push_id: 'MyString',
// app_version: 'MyString',
// os_version: 'MyString',
// network_status: 'MyString',
// gps_status: 'MyString',
// latitude: '1.5',
// longitude: '1.5',
// brand: 'MyString',
// manufacturer: 'MyString',
// carrier: 'MyString',
// },
// }));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.status, 'ok');
t.is(response.message, 'Debug Log was successfully created');
});
// SystemService.xAuth method
/**
* @test {SystemService.xAuth}
*/
test('[unit] SystemService.xAuth should throw an error when a JSON value is not provided', (t) => {
const expectedError = 'Clinical6 xAuth error: json is not defined';
t.truthy(() => sys.xAuth(), expectedError);
});
// /**
// * @test {SystemService.xAuth}
// */
// test('[unit] SystemService.xAuth should make a properly formatted POST request', async (t) => {
// const { json } = t.context;
// await sys.xAuth(json);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/x_auth/auth`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestBody, JSON.stringify({
// site_id: '12345',
// user_id: '21770',
// }));
// });
/**
* @test {SystemService.xAuth}
*/
test('[unit] SystemService.xAuth should successfully validate the attributes in the JSON', async (t) => {
const { json } = t.context;
const response = await sys.xAuth(json);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/x_auth/auth`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({
// site_id: '12345',
// user_id: '21770',
// }));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
t.is(response.status, 'new');
t.is(response.token, 'b0dcdf85785490bc47c782e4a87443dab489833fe2a7dc1a486d6c4da66bd490');
});