test/unit/helpers.agreement-template.js
import test from 'ava';
import nock from 'nock';
import {
client,
AgreementTemplate,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
t.context.updateResponse = {
data: {
id: 2,
type: 'agreement__templates',
attributes: {
template_name: 'dummy_2 2',
description: 'Sample agreement template',
document_url: null,
expiration: 26,
message: null,
redirect_url: 'http://sipes.co/cynthia_upton',
created_at: '2018-03-16T17:44:49Z',
updated_at: '2018-04-20T04:17:17Z',
permanent_link: 'dummy22',
reminder_frequency: 'WEEKLY_UNTIL_SIGNED',
archived_at: null
},
relationships: {
consent_form_versions: {
data: []
}
}
}
};
t.context.insertResponse = {
data: {
id: 7,
type: 'agreement__templates',
attributes: {
template_name: 'another template',
approval_required: false,
description: 'somedescription',
expiration: 365,
message: null,
redirect_url: null,
created_at: '2018-02-26T19:11:56Z',
updated_at: '2018-02-26T19:11:56Z',
permanent_link: 'another_template',
reminder_frequency: null,
archived_at: null
},
relationships: {
approvers: {
data: []
}
}
}
};
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.agreementJson = {
id: 1,
type: 'agreement__templates',
template_name: 'dummy_3157 45',
description: 'Sample agreement template',
document_url: '/uploads/test/agreement/template/document/45/agreement-with-counter-signer.pdf',
expiration: 20,
message: null,
redirect_url: 'http://effertz.io/emmy_labadie',
created_at: '2018-04-20T21:13:55Z',
updated_at: '2018-04-20T21:13:55Z',
permanent_link: 'dummy315745',
reminder_frequency: 'WEEKLY_UNTIL_SIGNED',
archived_at: null
};
t.context.agreementJsonApi = {
id: 1,
type: 'agreement__templates',
attributes: {
template_name: 'template_name',
description: 'somedescription',
document_url: 'https://clinical6-platform.s3.amazonaws.com/uploads/development/agreement/template/document/1/230a716d83c5693fd5c006a1b28b91925fda2bf9.pdf?X-Amz-Expires=60&X-Amz-Date=20180420T221551Z&X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAI6VWHWYSENONW2HQ/20180420/us-east-1/s3/aws4_request&X-Amz-SignedHeaders=host&X-Amz-Signature=8f99ede146c5480934b6b7be87567b1f55ea26c8aa16b13532e1fe6e3d71164f',
expiration: 365,
message: null,
redirect_url: null,
created_at: '2018-03-07T16:53:07Z',
updated_at: '2018-04-10T05:50:34Z',
permanent_link: 'template_name',
reminder_frequency: null,
archived_at: null
},
relationships: {
consent_form_versions: {
data: []
}
}
};
t.context.agreementTemplate = new AgreementTemplate(t.context.agreementJsonApi);
});
/**
* @test {AgreementTemplate}
*/
test('[unit] AgreementTemplate should handle allowedAction data with a normal json format', (t) => {
const { agreementJson } = t.context;
const agreementTemplate = new AgreementTemplate(agreementJson);
t.is(agreementTemplate.id, 1);
t.is(agreementTemplate.type, 'agreement__templates');
t.is(agreementTemplate.description, 'Sample agreement template');
t.is(agreementTemplate.permanentLink, 'dummy315745');
});
/**
* @test {AgreementTemplate}
*/
test('[unit] AgreementTemplate should handle device data with json api format', (t) => {
const { agreementJsonApi } = t.context;
const agreementTemplate = new AgreementTemplate({ data: agreementJsonApi });
t.is(agreementTemplate.id, 1);
t.is(agreementTemplate.type, 'agreement__templates');
t.is(agreementTemplate.description, 'somedescription');
t.is(agreementTemplate.permanentLink, 'template_name');
});
/**
* @test {AgreementTemplate}
*/
test('[unit] AgreementTemplate should generate json api format when converted to string', (t) => {
const { agreementJsonApi } = t.context;
let agreementTemplate = new AgreementTemplate({ data: agreementJsonApi });
let json = agreementTemplate.toJSON();
t.deepEqual(json, agreementJsonApi);
agreementTemplate = new AgreementTemplate({ data: agreementJsonApi });
json = agreementTemplate.toJSON();
t.deepEqual(json, agreementJsonApi);
});
/**
* @test {AgreementTemplate.save}
*/
test('[unit] AgreementTemplate.save should successfully insert an agreementTemplate with an agreementTemplate object when id does not exist', async (t) => {
const { agreementJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(agreementJsonApi));
delete json.id;
const agreementTemplate = new AgreementTemplate({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/agreement/templates`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await agreementTemplate.save();
t.is(request.path, `/v3/agreement/templates`);
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, 7);
t.is(response.type, 'agreement__templates');
t.is(response.description, 'somedescription');
t.is(response.permanentLink, 'another_template');
});
/**
* @test {AgreementTemplate.save}
*/
test('[unit] AgreementTemplate.save should successfully update an agreementTemplate with an agreementTemplate object when id exists', async (t) => {
const { agreementJsonApi, updateResponse } = t.context;
const agreementTemplate = new AgreementTemplate({ data: agreementJsonApi });
let request = {};
nock(client.apiBaseUrl).patch(`/v3/agreement/templates/${agreementTemplate.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
const response = await agreementTemplate.save();
t.is(request.path, `/v3/agreement/templates/${agreementTemplate.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: agreementJsonApi });
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 2);
t.is(response.type, 'agreement__templates');
t.is(response.description, 'Sample agreement template');
t.is(response.permanentLink, 'dummy22');
});