test/unit/helpers.consent-form-version.js
import test from 'ava';
import nock from 'nock';
import {
client,
ApproverGroupAssignment,
ConsentFormVersion,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
t.context.getResponseAssignments = {
data: [
{
id: 10,
type: 'consent__approver_group_assignments',
relationships: {
form_version: {
data: {
id: 22,
type: 'consent__form_versions'
}
},
approver_group: {
data: {
id: 15,
type: 'consent__approver_groups'
}
}
}
},
{
id: 11,
type: 'consent__approver_group_assignments',
relationships: {
form_version: {
data: {
id: 22,
type: 'consent__form_versions'
}
},
approver_group: {
data: {
id: 16,
type: 'consent__approver_groups'
}
}
}
}
],
included: [
{
id: 22,
type: 'consent__form_versions',
attributes: {
archived_at: '2018-03-09T16:44:08Z'
},
relationships: {
site: {
data: {
id: 70,
type: 'trials__sites'
}
},
templatable: {
data: {
id: 56,
type: 'agreement__templates'
}
},
language: {
data: {
id: 41,
type: 'languages'
}
},
consent_form: {
data: {
id: 28,
type: 'consent__forms'
}
},
approver_groups: {
data: [
{
id: 15,
type: 'consent__approver_groups'
},
{
id: 16,
type: 'consent__approver_groups'
}
]
},
approvers: {
data: [
{
id: 19,
type: 'consent__approvers'
},
{
id: 20,
type: 'consent__approvers'
},
{
id: 21,
type: 'consent__approvers'
},
{
id: 22,
type: 'consent__approvers'
}
]
}
}
},
{
id: 15,
type: 'consent__approver_groups',
attributes: {
name: 'Approver Group 15',
created_at: '2018-05-04T16:44:27Z',
updated_at: '2018-05-04T16:44:27Z'
},
relationships: {
approver_assignments: {
data: [
{
id: 18,
type: 'consent__approver_assignments'
},
{
id: 19,
type: 'consent__approver_assignments'
}
]
},
group_assignments: {
data: [
{
id: 10,
type: 'consent__approver_group_assignments'
}
]
},
approvers: {
data: [
{
id: 19,
type: 'consent__approvers'
},
{
id: 20,
type: 'consent__approvers'
}
]
}
}
},
{
id: '18',
type: 'consent__approver_assignments',
relationships: {
approver_group: {
data: {
id: 15,
type: 'consent__approver_groups'
}
},
approver: {
data: {
id: 19,
type: 'consent__approvers'
}
}
}
},
{
id: 19,
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
approver_groups: {
data: [
{
id: 15,
type: 'consent__approver_groups'
}
]
},
consent_form_versions: {
data: [
{
id: 22,
type: 'consent__form_versions'
}
]
}
}
},
{
id: 19,
type: 'consent__approver_assignments',
relationships: {
approver_group: {
data: {
id: 15,
type: 'consent__approver_groups'
}
},
approver: {
data: {
id: 20,
type: 'consent__approvers'
}
}
}
},
{
id: 20,
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
approver_groups: {
data: [
{
id: 15,
type: 'consent__approver_groups'
}
]
},
consent_form_versions: {
data: [
{
id: 22,
type: 'consent__form_versions'
}
]
}
}
},
{
id: 16,
type: 'consent__approver_groups',
attributes: {
name: 'Approver Group 16',
created_at: '2018-05-04T16:44:27Z',
updated_at: '2018-05-04T16:44:27Z'
},
relationships: {
approver_assignments: {
data: [
{
id: 20,
type: 'consent__approver_assignments'
},
{
id: 21,
type: 'consent__approver_assignments'
}
]
},
group_assignments: {
data: [
{
id: 11,
type: 'consent__approver_group_assignments'
}
]
},
approvers: {
data: [
{
id: 21,
type: 'consent__approvers'
},
{
id: 22,
type: 'consent__approvers'
}
]
}
}
},
{
id: 20,
type: 'consent__approver_assignments',
relationships: {
approver_group: {
data: {
id: 16,
type: 'consent__approver_groups'
}
},
approver: {
data: {
id: 21,
type: 'consent__approvers'
}
}
}
},
{
id: 21,
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
approver_groups: {
data: [
{
id: 16,
type: 'consent__approver_groups'
}
]
},
consent_form_versions: {
data: [
{
id: 22,
type: 'consent__form_versions'
}
]
}
}
},
{
id: 21,
type: 'consent__approver_assignments',
relationships: {
approver_group: {
data: {
id: 16,
type: 'consent__approver_groups'
}
},
approver: {
data: {
id: 22,
type: 'consent__approvers'
}
}
}
},
{
id: 22,
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
approver_groups: {
data: [
{
id: 16,
type: 'consent__approver_groups'
}
]
},
consent_form_versions: {
data: [
{
id: 22,
type: 'consent__form_versions'
}
]
}
}
}
]
};
t.context.getResponseAssignment9 = {
data: {
id: 9,
type: 'consent__approver_group_assignments',
relationships: {
form_version: {
data: {
id: 20,
type: 'consent__form_versions'
}
},
approver_group: {
data: {
id: 13,
type: 'consent__approver_groups'
}
}
}
},
included: [
{
id: 20,
type: 'consent__form_versions',
attributes: {
archived_at: '2018-03-09T16:44:08Z'
},
relationships: {
site: {
data: {
id: 67,
type: 'trials__sites'
}
},
templatable: {
data: {
id: 53,
type: 'agreement__templates'
}
},
language: {
data: {
id: 38,
type: 'languages'
}
},
consent_form: {
data: {
id: 25,
type: 'consent__forms'
}
},
approver_groups: {
data: [
{
id: 13,
type: 'consent__approver_groups'
}
]
},
approvers: {
data: [
{
id: 17,
type: 'consent__approvers'
},
{
id: 18,
type: 'consent__approvers'
}
]
}
}
},
{
id: 13,
type: 'consent__approver_groups',
attributes: {
name: 'Approver Group 13',
created_at: '2018-05-04T16:44:26Z',
updated_at: '2018-05-04T16:44:26Z'
},
relationships: {
approver_assignments: {
data: [
{
id: 16,
type: 'consent__approver_assignments'
},
{
id: 17,
type: 'consent__approver_assignments'
}
]
},
group_assignments: {
data: [
{
id: 9,
type: 'consent__approver_group_assignments'
}
]
},
approvers: {
data: [
{
id: 17,
type: 'consent__approvers'
},
{
id: 18,
type: 'consent__approvers'
}
]
}
}
},
{
id: 16,
type: 'consent__approver_assignments',
relationships: {
approver_group: {
data: {
id: 13,
type: 'consent__approver_groups'
}
},
approver: {
data: {
id: 17,
type: 'consent__approvers'
}
}
}
},
{
id: 17,
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
approver_groups: {
data: [
{
id: 13,
type: 'consent__approver_groups'
}
]
},
consent_form_versions: {
data: [
{
id: 20,
type: 'consent__form_versions'
}
]
}
}
},
{
id: 17,
type: 'consent__approver_assignments',
relationships: {
approver_group: {
data: {
id: 13,
type: 'consent__approver_groups'
}
},
approver: {
data: {
id: 18,
type: 'consent__approvers'
}
}
}
},
{
id: 18,
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
approver_groups: {
data: [
{
id: 13,
type: 'consent__approver_groups'
}
]
},
consent_form_versions: {
data: [
{
id: 20,
type: 'consent__form_versions'
}
]
}
}
}
]
};
t.context.updateResponse = {
data: {
id: 3,
type: 'consent__form_versions',
attributes: {
archived_at: '2018-03-09T16:44:08Z'
},
relationships: {
site: {
data: {
id: 4,
type: 'trials__sites'
}
},
templatable: {
data: {
id: 36,
type: 'agreement__templates'
}
},
language: {
data: {
id: 5,
type: 'languages'
}
},
consent_form: {
data: {
id: 6,
type: 'consent__forms'
}
},
approvers: {
data: [
{
id: 1,
type: 'consent__approvers'
},
{
id: 2,
type: 'consent__approvers'
}
]
}
}
},
included: [
{
id: 4,
type: 'trials__sites',
attributes: {
site_id: '628037',
name: 'Leopold Heller',
email: 'selena@mclaughlinlegros.com',
phone_number: null,
fax_number: null,
contact_name: null,
contact_email: null,
contact_phone: null,
contact_fax: null,
created_at: '2018-04-24T01:36:07Z',
updated_at: '2018-04-24T01:36:07Z'
},
relationships: {
location: {
data: {
id: 47,
type: 'locations'
}
},
templatables: {
data: []
}
}
},
{
id: 36,
type: 'agreement__templates',
attributes: {
template_name: 'dummy_1465 39',
description: 'Sample agreement template',
document_url: '/uploads/test/agreement/template/document/36/agreement-with-counter-signer.pdf',
expiration: 24,
message: null,
redirect_url: 'http://padberg.co/vernon',
created_at: '2018-04-24T01:36:07Z',
updated_at: '2018-04-24T01:36:07Z',
permanent_link: 'dummy146539',
reminder_frequency: 'WEEKLY_UNTIL_SIGNED',
archived_at: null
},
relationships: {
consent_form_versions: {
data: [
{
id: 3,
type: 'consent__form_versions'
}
]
}
}
},
{
id: 5,
type: 'languages',
attributes: {
iso: 'dummy_261',
name: 'molestiae',
is_default: false
}
},
{
id: 6,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'Form 8',
created_at: '2018-04-24T01:36:07Z',
updated_at: '2018-04-24T01:36:07Z'
},
relationships: {
consent_form_versions: {
data: [
{
id: 3,
type: 'consent__form_versions'
}
]
}
}
},
{
id: 1,
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
consent_form_version: {
data: {
id: 3,
type: 'consent__form_versions'
}
}
}
},
{
id: 2,
type: 'consent__approvers',
attributes: {
first_name: 'John',
last_name: 'Doe',
email: 'john@example.com',
title: null
},
relationships: {
consent_form_version: {
data: {
id: 3,
type: 'consent__form_versions'
}
}
}
}
]
};
t.context.insertResponse = {
data: {
id: 6,
type: 'consent__form_versions',
attributes: {
archived_at: null
},
relationships: {
site: {
data: {
id: 9,
type: 'trials__sites'
}
},
templatable: {
data: {
id: 8,
type: 'agreement__templates'
}
},
language: {
data: {
id: 2,
type: 'languages'
}
},
approvers: {
data: []
}
}
}
};
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.consentFormVersionJsonApi = {
id: 3,
type: 'consent__form_versions',
attributes: {},
relationships: {
templatable: {
data: {
id: 8,
type: 'agreement__templates'
}
},
language: {
data: {
id: 2,
type: 'languages',
},
},
site: {
data: {
id: 9,
type: 'trials__sites'
}
}
}
};
t.context.consentFormVersionJson = {
id: 3,
type: 'consent__form_versions',
relationships: {
templatable: {
data: {
id: 8,
type: 'agreement__templates'
}
},
site: {
data: {
id: 9,
type: 'trials__sites'
}
}
}
};
t.context.consentFormVersion = new ConsentFormVersion(t.context.consentFormJsonApi);
});
/**
* @test {ConsentFormVersion}
*/
test('[unit] ConsentFormVersion should handle consentFormVersion data with a normal json format', (t) => {
const { consentFormVersionJson } = t.context;
const consentFormVersion = new ConsentFormVersion(consentFormVersionJson);
t.is(consentFormVersion.type, 'consent__form_versions');
t.is(consentFormVersion.site.id, 9);
t.is(consentFormVersion.site.type, 'trials__sites');
t.is(consentFormVersion.templatable.id, 8);
t.is(consentFormVersion.templatable.type, 'agreement__templates');
});
/**
* @test {ConsentFormVersion}
*/
test('[unit] ConsentFormVersion should handle consentFormVersion data with json api format', (t) => {
const { consentFormVersionJsonApi } = t.context;
const consentFormVersion = new ConsentFormVersion({ data: consentFormVersionJsonApi });
t.is(consentFormVersion.type, 'consent__form_versions');
t.is(consentFormVersion.site.id, 9);
t.is(consentFormVersion.site.type, 'trials__sites');
t.is(consentFormVersion.templatable.id, 8);
t.is(consentFormVersion.templatable.type, 'agreement__templates');
});
/**
* @test {ConsentFormVersion}
*/
test('[unit] ConsentFormVersion should generate json api format when converted to string', (t) => {
const { consentFormVersionJsonApi } = t.context;
let consentFormVersion = new ConsentFormVersion({ data: consentFormVersionJsonApi });
let json = consentFormVersion.toJSON();
t.deepEqual(json, consentFormVersionJsonApi);
consentFormVersion = new ConsentFormVersion({ data: consentFormVersionJsonApi });
json = consentFormVersion.toJSON();
t.deepEqual(json, consentFormVersionJsonApi);
});
// ConsentFormVersion.getApproverGroupAssignments method
/**
* @test {ConsentFormVersion.getApproverGroupAssignments}
*/
test.serial('[unit] ConsentFormVersion.getApproverGroupAssignments should throw an error when there is no authToken or invalid params', async (t) => {
const { consentFormVersionJsonApi } = t.context;
const formVersion = new ConsentFormVersion({ data: consentFormVersionJsonApi });
client.authToken = undefined;
const title = `ConsentFormVersionService.getChildren error`;
await t.throwsAsync(formVersion.getApproverGroupAssignments(formVersion, { type: 'consent__approver_group_assignments' }), `${title}: requires authToken`);
});
/**
* @test {ConsentFormVersion.getApproverGroupAssignments}
*/
test(`[unit] ConsentFormVersion.getApproverGroupAssignments should receive a valid request and response for a get request without an id`, async (t) => {
const { consentFormVersionJsonApi, getResponseAssignments } = t.context;
const formVersion = new ConsentFormVersion({ data: consentFormVersionJsonApi });
let request = {};
nock(client.apiBaseUrl).get(`/v3/consent/form_versions/${formVersion.id}/consent/approver_group_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAssignments];
});
const response = await formVersion.getApproverGroupAssignments();
t.is(request.path, `/v3/consent/form_versions/${formVersion.id}/consent/approver_group_assignments`);
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, 2);
t.truthy(response[0] instanceof ApproverGroupAssignment);
t.is(response[0].id, 10);
t.is(response[0].type, 'consent__approver_group_assignments');
t.is(response[0].formVersion.id, 22);
t.is(response[0].formVersion.type, 'consent__form_versions');
t.is(response[0].formVersion.site.id, 70);
t.is(response[0].formVersion.site.type, 'trials__sites');
t.is(response[0].formVersion.templatable.id, 56);
t.is(response[0].formVersion.templatable.type, 'agreement__templates');
t.is(response[0].approverGroup.id, 15);
t.is(response[0].approverGroup.type, 'consent__approver_groups');
t.is(response[0].approverGroup.name, 'Approver Group 15');
t.is(response[0].approverGroup.createdAt, '2018-05-04T16:44:27Z');
t.is(response[0].approverGroup.updatedAt, '2018-05-04T16:44:27Z');
t.truthy(response[1] instanceof ApproverGroupAssignment);
t.is(response[1].id, 11);
t.is(response[1].type, 'consent__approver_group_assignments');
t.is(response[1].formVersion.id, 22);
t.is(response[1].formVersion.type, 'consent__form_versions');
t.is(response[1].formVersion.site.id, 70);
t.is(response[1].formVersion.site.type, 'trials__sites');
t.is(response[1].formVersion.templatable.id, 56);
t.is(response[1].formVersion.templatable.type, 'agreement__templates');
t.is(response[1].approverGroup.id, 16);
t.is(response[1].approverGroup.type, 'consent__approver_groups');
t.is(response[1].approverGroup.name, 'Approver Group 16');
t.is(response[1].approverGroup.createdAt, '2018-05-04T16:44:27Z');
t.is(response[1].approverGroup.updatedAt, '2018-05-04T16:44:27Z');
});
/**
* @test {ConsentFormVersion.getApproverGroupAssignments}
*/
test('[unit] ConsentFormVersion.getApproverGroupAssignments should receive a valid response for a get request with an id', async (t) => {
const { consentFormVersionJsonApi, getResponseAssignment9 } = t.context;
const formVersion = new ConsentFormVersion({ data: consentFormVersionJsonApi });
const assignment = new ApproverGroupAssignment({ id: 9 });
let request = {};
nock(client.apiBaseUrl).get(`/v3/consent/form_versions/${formVersion.id}/consent/approver_group_assignments/${assignment.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAssignment9];
});
const response = await formVersion.getApproverGroupAssignments(assignment);
t.is(request.path, `/v3/consent/form_versions/${formVersion.id}/consent/approver_group_assignments/${assignment.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.truthy(response);
t.truthy(response instanceof ApproverGroupAssignment);
t.is(response.id, 9);
t.is(response.type, 'consent__approver_group_assignments');
t.is(response.formVersion.id, 20);
t.is(response.formVersion.type, 'consent__form_versions');
t.is(response.formVersion.site.id, 67);
t.is(response.formVersion.site.type, 'trials__sites');
t.is(response.formVersion.templatable.id, 53);
t.is(response.formVersion.templatable.type, 'agreement__templates');
t.is(response.approverGroup.id, 13);
t.is(response.approverGroup.type, 'consent__approver_groups');
t.is(response.approverGroup.name, 'Approver Group 13');
t.is(response.approverGroup.createdAt, '2018-05-04T16:44:26Z');
t.is(response.approverGroup.updatedAt, '2018-05-04T16:44:26Z');
});
// ConsentFormVersion.save
/**
* @test {ConsentFormVersion.save}
*/
test('[unit] ConsentFormVersion.save should successfully insert a consentFormVersion with a consentFormVersion object when id does not exist', async (t) => {
const { consentFormVersionJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(consentFormVersionJsonApi));
delete json.id;
const consentFormVersion = new ConsentFormVersion({ data: json });
consentFormVersion.language = {
id: 2,
type: 'languages'
};
let request = {};
nock(client.apiBaseUrl).post(`/v3/consent/form_versions`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await consentFormVersion.save();
t.is(request.path, `/v3/consent/form_versions`);
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');
// Verify that language is sent in request
t.is(request.requestBody.data.relationships.language.data.id, 2);
t.is(request.requestBody.data.relationships.language.data.type, 'languages');
t.is(response.id, 6);
t.is(response.type, 'consent__form_versions');
t.is(response.templatable.id, 8);
t.is(response.templatable.type, 'agreement__templates');
t.is(response.site.id, 9);
t.is(response.site.type, 'trials__sites');
t.is(response.language.id, 2);
t.is(response.language.type, 'languages');
t.is(response.approvers.length, 0);
t.is(response.site.id, 9);
t.is(response.site.type, 'trials__sites');
t.is(response.templatable.id, 8);
t.is(response.templatable.type, 'agreement__templates');
t.is(response.approvers.length, 0);
t.is(response.language.id, 2);
t.is(response.language.type, 'languages');
});
/**
* @test {ConsentFormVersion.save}
*/
test('[unit] ConsentFormVersion.save should successfully update a consentFormVersion with a consentFormVersion object when id exists', async (t) => {
const { consentFormVersionJsonApi, updateResponse } = t.context;
const consentFormVersion = new ConsentFormVersion({ data: consentFormVersionJsonApi });
let request = {};
nock(client.apiBaseUrl).patch(`/v3/consent/form_versions/${consentFormVersion.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
const response = await consentFormVersion.save();
t.is(request.path, `/v3/consent/form_versions/${consentFormVersion.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: consentFormVersionJsonApi });
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__form_versions');
t.is(response.archivedAt, '2018-03-09T16:44:08Z');
t.is(response.site.id, 4);
t.is(response.site.type, 'trials__sites');
t.is(response.site.siteId, '628037');
t.is(response.site.name, 'Leopold Heller');
t.is(response.site.location.id, 47);
t.is(response.site.location.type, 'locations');
t.is(response.site.templatables.length, 0);
t.is(response.templatable.id, 36);
t.is(response.templatable.type, 'agreement__templates');
t.is(response.templatable.templateName, 'dummy_1465 39');
t.is(response.templatable.description, 'Sample agreement template');
t.is(response.templatable.consentFormVersions.length, 1);
t.is(response.templatable.consentFormVersions[0].id, 3);
t.is(response.language.id, 5);
t.is(response.language.type, 'languages');
t.is(response.language.iso, 'dummy_261');
t.is(response.language.name, 'molestiae');
t.is(response.consentForm.id, 6);
t.is(response.consentForm.type, 'consent__forms');
t.is(response.consentForm.enabled, true);
t.is(response.consentForm.name, 'Form 8');
t.is(response.consentForm.consentFormVersions.length, 1);
t.is(response.consentForm.consentFormVersions[0].id, 3);
t.is(response.approvers.length, 2);
t.is(response.approvers[0].id, 1);
t.is(response.approvers[0].type, 'consent__approvers');
t.is(response.approvers[0].firstName, 'John');
t.is(response.approvers[0].lastName, 'Doe');
t.is(response.approvers[0].consentFormVersion.id, 3);
t.is(response.approvers[1].id, 2);
t.is(response.approvers[1].type, 'consent__approvers');
t.is(response.approvers[1].firstName, 'John');
t.is(response.approvers[1].lastName, 'Doe');
t.is(response.approvers[1].consentFormVersion.id, 3);
});