test/unit/helpers.consent-form.js
import test from 'ava';
import nock from 'nock';
import {
client,
ConsentForm,
ConsentFormVersion,
} from '../../src';
// See tests.js for testing client creation
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.insertResponse = {
data: {
id: 3,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'A form name',
created_at: '2018-04-20T16:42:53Z',
updated_at: '2018-04-20T16:42:53Z'
},
relationships: {
consent_form_versions: {
data: []
},
strategy: {
data: {
id: 2,
type: 'consent__strategies'
}
}
}
}
};
t.context.getResponseConsentFormVersions = {
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.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 = {
id: 2,
type: 'consent__forms',
attributes: {
name: 'A new form name',
enabled: false,
created_at: '2018-04-19T21:50:38Z',
updated_at: '2018-04-19T21:50:38Z'
},
relationships: {
strategy: {
data: {
id: 2,
type: 'consent__strategies'
}
}
}
};
t.context.consentFormJson = {
id: 2,
type: 'consent__forms',
name: 'A new form name',
enabled: false,
created_at: '2018-04-19T21:50:38Z',
updated_at: '2018-04-19T21:50:38Z',
relationships: {
strategy: {
data: {
id: 2,
type: 'consent__strategies'
}
}
}
};
client.storageUtility.clear();
t.context.storage = client.storageUtility;
client.consentForm = new ConsentForm({});
t.context.consentForm = new ConsentForm({ data: t.context.consentFormJsonApi });
});
/**
* @test {ConsentForm}
*/
test('[unit] ConsentForm should handle consentForm data with a normal json format', (t) => {
const { consentFormJson } = t.context;
const consentForm = new ConsentForm(consentFormJson);
t.is(consentForm.id, 2);
t.is(consentForm.type, 'consent__forms');
t.is(consentForm.name, 'A new form name');
t.is(consentForm.enabled, false);
});
/**
* @test {ConsentForm}
*/
test('[unit] ConsentForm should handle consentForm data with json api format', (t) => {
const { consentFormJsonApi } = t.context;
const consentForm = new ConsentForm({ data: consentFormJsonApi });
t.is(consentForm.id, 2);
t.is(consentForm.type, 'consent__forms');
t.is(consentForm.name, 'A new form name');
t.is(consentForm.enabled, false);
});
/**
* @test {ConsentForm}
*/
test('[unit] ConsentForm should generate json api format when converted to string', (t) => {
const { consentFormJsonApi } = t.context;
let consentForm = new ConsentForm({ data: consentFormJsonApi });
t.deepEqual(consentForm.toJSON(), consentFormJsonApi);
consentForm = new ConsentForm({ data: consentFormJsonApi });
t.deepEqual(consentForm.toJSON(), consentFormJsonApi);
});
/**
* @test {ConsentForm.save}
*/
test('[unit] ConsentForm.save should successfully save a new consentForm when id does not exist', async (t) => {
const { consentFormJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(consentFormJsonApi));
delete json.id;
const consentForm = new ConsentForm({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/consent/forms`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await consentForm.save();
t.is(request.path, `/v3/consent/forms`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: json });
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 3);
t.is(response.type, 'consent__forms');
t.is(response.enabled, true);
t.is(response.name, 'A 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, 0);
t.is(response.strategy.id, 2);
t.is(response.strategy.type, 'consent__strategies');
});
/**
* @test {ConsentForm.save}
*/
test('[unit] ConsentForm.save should successfully update a consentForm with a consentForm object when an id exists', async (t) => {
const { consentFormJsonApi } = t.context;
let requestBody = {};
const server = nock(client.apiBaseUrl)
.patch('/v3/consent/forms/2', (body) => {
requestBody = body;
return { data: json };
})
.reply(201, {
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'
}
}
}
}
});
const json = JSON.parse(JSON.stringify(consentFormJsonApi));
const consentForm = new ConsentForm({ data: json });
const response = await consentForm.save();
t.deepEqual(requestBody, { data: json });
server.done();
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.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.consentStrategy.id, 2);
t.is(response.consentStrategy.type, 'consent__strategies');
});
// ConsentForm.getVersions method
/**
* @test {ConsentForm.getVersions}
*/
test.serial('[unit] ConsentForm.getVersions should throw an error when there is no authToken or invalid params', async (t) => {
const { consentFormJsonApi } = t.context;
const consentForm = new ConsentForm({ data: consentFormJsonApi });
client.authToken = undefined;
const title = `ConsentFormService.getChildren error`;
await t.throwsAsync(consentForm.getVersions(consentForm, { type: 'consent__form_versions' }), `${title}: requires authToken`);
});
/**
* @test {ConsentForm.getVersions}
*/
test(`[unit] ConsentForm.getVersions should receive a valid request and response for a get request without an id`, async (t) => {
const { consentFormJsonApi, getResponseConsentFormVersions } = t.context;
consentFormJsonApi.id = 9;
const consentForm = new ConsentForm({ data: consentFormJsonApi });
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 consentForm.getVersions();
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);
});