test/unit/services.consent-form.js
import test from 'ava';
import nock from 'nock';
import {
client,
clinical6,
ConsentForm,
ConsentFormVersion
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.patchResponse = {
data: {
id: 2,
type: 'consent__forms',
attributes: {
enabled: false,
name: 'A new form name',
created_at: '2018-04-20T16:42:53Z',
updated_at: '2018-04-20T16:42:53Z'
},
relationships: {
consent_form_versions: {
data: [
{
id: 8,
type: 'consent__form_versions'
},
{
id: 9,
type: 'consent__form_versions'
}
]
},
strategy: {
data: {
id: 2,
type: 'consent__strategies'
}
}
}
}
};
t.context.getResponseAll = {
data: [
{
id: 1,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'a consent form name',
created_at: '2018-04-13T00:58:42Z',
updated_at: '2018-04-13T00:58:42Z'
},
relationships: {
consent_form_versions: {
data: []
}
}
},
{
id: 2,
type: 'consent__forms',
attributes: {
enabled: false,
name: 'a consent form name',
created_at: '2018-04-13T01:00:07Z',
updated_at: '2018-04-13T01:00:07Z'
},
relationships: {
consent_form_versions: {
data: []
}
}
},
{
id: 3,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'a consent form name',
created_at: '2018-04-13T01:02:49Z',
updated_at: '2018-04-13T01:02:49Z'
},
relationships: {
consent_form_versions: {
data: []
}
}
}
]
};
t.context.getResponseId = {
data: {
id: 27,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'Form 29',
created_at: '2018-04-27T20:08:19Z',
updated_at: '2018-04-27T20:08:19Z'
},
relationships: {
consent_form_versions: {
data: []
}
}
}
};
t.context.getResponseConsentFormVersions = {
data: [
{
id: 9,
type: 'consent__form_versions',
attributes: {
archived_at: '2018-04-20T16:42:53Z'
},
relationships: {
site: {
data: {
id: 2,
type: 'trials__sites'
}
},
agreement_template: {
data: {
id: 189,
type: 'agreement__templates'
}
},
language: {
data: {
id: 1,
type: 'languages'
}
},
consent_form: {
data: {
id: 9,
type: 'consent__forms'
}
},
approvers: {
data: []
}
}
}
],
included: [
{
id: 2,
type: 'trials__sites',
attributes: {
site_id: '211293',
name: 'Laila Ruecker Jr.',
email: 'eldred@schmitt.biz',
phone_number: null,
fax_number: null,
contact_name: null,
contact_email: null,
contact_phone: null,
contact_fax: null,
created_at: '2018-04-23T19:39:07Z',
updated_at: '2018-04-23T19:39:07Z'
},
relationships: {
location: {
data: null
},
agreement_templates: {
data: []
}
}
},
{
id: 189,
type: 'agreement__templates',
attributes: {
template_name: 'fede',
description: 'fede',
document_url: null,
expiration: null,
message: null,
redirect_url: null,
created_at: '2018-04-18T18:04:42Z',
updated_at: '2018-04-18T18:04:42Z',
permanent_link: 'fede',
reminder_frequency: null,
archived_at: null
},
relationships: {
consent_form_versions: {
data: [
{
id: 9,
type: 'consent__form_versions'
}
]
}
}
},
{
id: 1,
type: 'languages',
attributes: {
iso: 'en',
name: 'English',
is_default: true
}
},
{
id: 9,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'a consent form name he',
created_at: '2018-04-23T19:17:36Z',
updated_at: '2018-04-23T19:17:36Z'
},
relationships: {
consent_form_versions: {
data: [
{
id: 9,
type: 'consent__form_versions'
}
]
}
}
}
]
};
t.context.consentFormJsonApi = {
data: {
id: 2,
type: 'consent__forms',
attributes: {
name: 'A new form name',
enabled: false
},
relationships: {
strategy: {
data: {
id: 2,
type: 'consent__strategies'
}
}
}
}
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.consentForm = new ConsentForm(t.context.consentFormJsonApi);
});
// ConsentFormService.get method
/**
* @test {Clinical6.get}
*/
test.serial('[unit] ConsentFormService.get should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'ConsentFormService.get error: requires authToken';
await t.throwsAsync(clinical6.get(ConsentForm), expectedError);
});
/**
* @test {Clinical6.get}
*/
test('[unit] ConsentFormService.get should make a properly formatted get request', async (t) => {
const { getResponseAll } = t.context;
let request = {};
nock(client.apiBaseUrl).get(`/v3/consent/forms`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAll];
});
await clinical6.get(ConsentForm);
t.is(request.path, `/v3/consent/forms`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
});
/**
* @test {Clinical6.get}
*/
test('[unit] ConsentFormService.get should receive a valid response for a get request without an id', async (t) => {
const { getResponseAll } = t.context;
nock(client.apiBaseUrl).get(`/v3/consent/forms`).reply(200, getResponseAll);
const response = await clinical6.get(ConsentForm);
t.truthy(response);
t.is(Object.keys(response).length, 3);
t.is(response[0].id, 1);
t.is(response[0].type, 'consent__forms');
t.is(response[0].enabled, true);
t.is(response[0].name, 'a consent form name');
t.is(response[0].createdAt, '2018-04-13T00:58:42Z');
t.is(response[0].updatedAt, '2018-04-13T00:58:42Z');
t.is(response[0].consentFormVersions.length, 0);
t.is(response[1].id, 2);
t.is(response[1].type, 'consent__forms');
t.is(response[1].enabled, false);
t.is(response[1].name, 'a consent form name');
t.is(response[1].createdAt, '2018-04-13T01:00:07Z');
t.is(response[1].updatedAt, '2018-04-13T01:00:07Z');
t.is(response[1].consentFormVersions.length, 0);
t.is(response[2].id, 3);
t.is(response[2].type, 'consent__forms');
t.is(response[2].enabled, true);
t.is(response[2].name, 'a consent form name');
t.is(response[2].createdAt, '2018-04-13T01:02:49Z');
t.is(response[2].updatedAt, '2018-04-13T01:02:49Z');
t.is(response[2].consentFormVersions.length, 0);
});
/**
* @test {Clinical6.get}
*/
test('[unit] ConsentFormService.get should receive a valid response for a get request with an id', async (t) => {
const { getResponseId } = t.context;
nock(client.apiBaseUrl).get('/v3/consent/forms/27').reply(200, getResponseId);
const response = await clinical6.get(new ConsentForm({ id: 27 }));
t.truthy(response);
t.is(response.id, 27);
t.is(response.type, 'consent__forms');
t.is(response.enabled, true);
t.is(response.name, 'Form 29');
t.is(response.createdAt, '2018-04-27T20:08:19Z');
t.is(response.updatedAt, '2018-04-27T20:08:19Z');
t.is(response.consentFormVersions.length, 0);
});
// ConsentFormService.getChildren method
/**
* @test {Clinical6.getChildren}
*/
test.serial('[unit] ConsentFormService.getChildren should throw an error when there is no authToken or invalid params', async (t) => {
client.authToken = undefined;
const title = `ConsentFormService.getChildren error`;
const consentForm = new ConsentForm({ id: 9 });
await t.throwsAsync(clinical6.getChildren(consentForm, ConsentFormVersion), `${title}: requires authToken`);
client.authToken = 'valid_authToken';
await t.throwsAsync(clinical6.getChildren(consentForm), `${title}: child does not have type`);
});
/**
* @test {Clinical6.getChildren}
*/
test(`[unit] ConsentFormService.getChildren should receive a valid request and response for a get request without an id`, async (t) => {
const { getResponseConsentFormVersions } = t.context;
const consentForm = new ConsentForm({ id: 9 });
let request = {};
nock(client.apiBaseUrl).get(`/v3/consent/forms/${consentForm.id}/consent/form_versions`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseConsentFormVersions];
});
const response = await clinical6.getChildren(consentForm, ConsentFormVersion);
t.is(request.path, `/v3/consent/forms/${consentForm.id}/consent/form_versions`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.truthy(response);
t.is(Object.keys(response).length, 1);
t.truthy(response[0] instanceof ConsentFormVersion);
t.is(response[0].id, 9);
t.is(response[0].type, 'consent__form_versions');
t.is(response[0].archivedAt, '2018-04-20T16:42:53Z');
t.is(response[0].site.id, 2);
t.is(response[0].site.type, 'trials__sites');
t.is(response[0].site.siteId, '211293');
t.is(response[0].site.name, 'Laila Ruecker Jr.');
t.is(response[0].site.email, 'eldred@schmitt.biz');
t.is(response[0].site.agreementTemplates.length, 0);
t.is(response[0].agreementTemplate.id, 189);
t.is(response[0].agreementTemplate.type, 'agreement__templates');
t.is(response[0].agreementTemplate.type, 'agreement__templates');
t.is(response[0].agreementTemplate.templateName, 'fede');
t.is(response[0].agreementTemplate.description, 'fede');
t.is(response[0].agreementTemplate.consentFormVersions.length, 1);
t.is(response[0].agreementTemplate.consentFormVersions[0].id, 9);
t.is(response[0].language.id, 1);
t.is(response[0].language.type, 'languages');
t.is(response[0].language.iso, 'en');
t.is(response[0].language.name, 'English');
t.is(response[0].consentForm.id, 9);
t.is(response[0].consentForm.type, 'consent__forms');
t.is(response[0].consentForm.enabled, true);
t.is(response[0].consentForm.name, 'a consent form name he');
t.is(response[0].consentForm.consentFormVersions.length, 1);
t.is(response[0].consentForm.consentFormVersions[0].id, 9);
t.is(response[0].approvers.length, 0);
});
// ConsentFormService.insert method
/**
* @test {Clinical6.insert}
*/
test.serial('[unit] ConsentFormService.insert should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'ConsentFormService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new ConsentForm()), expectedError);
});
/**
* @test {Clinical6.insert}
*/
test('[unit] ConsentFormService.insert should successfully insert a consentForm with a consentForm object', async (t) => {
let request = {};
nock(client.apiBaseUrl).post(`/v3/consent/forms`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, {
data: {
id: 1,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'A form name',
created_at: '2018-04-19T21:50:38Z',
updated_at: '2018-04-19T21:50:38Z'
},
relationships: {
consent_form_versions: {
data: []
},
strategy: {
data: {
id: 2,
type: 'consent__strategies'
}
}
}
}
}];
});
const requestJsonApi = {
data: {
type: 'consent__forms',
attributes: {
name: 'A form name'
},
relationships: {
strategy: {
data: {
id: 2,
type: 'consent__strategies'
}
}
}
}
};
const consentForm = new ConsentForm(requestJsonApi);
const response = await clinical6.insert(consentForm);
t.is(request.path, `/v3/consent/forms`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, requestJsonApi);
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 1);
t.is(response.type, 'consent__forms');
t.is(response.enabled, true);
t.is(response.name, 'A form name');
t.is(response.createdAt, '2018-04-19T21:50:38Z');
t.is(response.updatedAt, '2018-04-19T21:50:38Z');
t.is(response.consentFormVersions.length, 0);
t.is(response.consentStrategy.id, 2);
t.is(response.consentStrategy.type, 'consent__strategies');
});
// ConsentFormService.update method
/**
* @test {Clinical6.update}
*/
test.serial('[unit] ConsentFormService.update should throw an error when there is no authToken', async (t) => {
client.authToken = undefined;
const expectedError = 'ConsentFormService.insert error: requires authToken';
await t.throwsAsync(clinical6.insert(new ConsentForm()), expectedError);
});
// ConsentFormService.update method
/**
* @test {Clinical6.update}
*/
test('[unit] ConsentFormService.update should successfully update a consentForm with a consentForm object', async (t) => {
const { consentForm, patchResponse, consentFormJsonApi } = t.context;
let request = {};
nock(client.apiBaseUrl).patch(`/v3/consent/forms/${consentForm.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, patchResponse];
});
const response = await clinical6.update(consentForm);
t.is(request.path, `/v3/consent/forms/${consentForm.id}`);
t.is(request.headers.accept, 'application/json');
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.deepEqual(request.requestBody, consentFormJsonApi);
t.is(response.id, 2);
t.is(response.type, 'consent__forms');
t.is(response.enabled, false);
t.is(response.name, 'A new form name');
t.is(response.createdAt, '2018-04-20T16:42:53Z');
t.is(response.updatedAt, '2018-04-20T16:42:53Z');
t.is(response.consentFormVersions.length, 2);
t.is(response.consentFormVersions[0].id, 8);
t.is(response.consentFormVersions[0].type, 'consent__form_versions');
t.is(response.consentFormVersions[1].id, 9);
t.is(response.consentFormVersions[1].type, 'consent__form_versions');
t.is(response.consentFormVersions[0].id, 8);
t.is(response.consentFormVersions[0].type, 'consent__form_versions');
t.is(response.consentFormVersions[1].id, 9);
t.is(response.consentFormVersions[1].type, 'consent__form_versions');
});