test/unit/helpers.paper-template.js
import test from 'ava';
import nock from 'nock';
import {
client,
PaperTemplate,
} 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__paper_templates',
attributes: {
template_name: 'dummy_2 2',
description: 'Sample paper template',
document_url: null,
created_at: '2018-03-16T17:44:49Z',
updated_at: '2018-04-20T04:17:17Z',
},
relationships: {
consent_form_versions: {
data: []
}
}
}
};
t.context.insertResponse = {
data: {
id: 7,
type: 'agreement__paper_templates',
attributes: {
template_name: 'another template',
description: 'somedescription',
document_url: null,
created_at: '2018-02-26T19:11:56Z',
updated_at: '2018-02-26T19:11:56Z',
},
relationships: {
consent_form_versions: {
data: []
}
}
}
};
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.paperJson = {
id: 1,
type: 'agreement__paper_templates',
template_name: 'dummy_3157 45',
description: 'Sample paper template',
document_url: '/uploads/test/paper/template/document/45/paper-with-counter-signer.pdf',
created_at: '2018-04-20T21:13:55Z',
updated_at: '2018-04-20T21:13:55Z',
};
t.context.paperJsonApi = {
id: 1,
type: 'agreement__paper_templates',
attributes: {
template_name: 'template_name',
description: 'somedescription',
document_url: 'https://clinical6-platform.s3.amazonaws.com/uploads/development/paper/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',
created_at: '2018-03-07T16:53:07Z',
updated_at: '2018-04-10T05:50:34Z',
},
relationships: {
consent_form_versions: {
data: []
}
}
};
t.context.paperTemplate = new PaperTemplate(t.context.paperJsonApi);
});
/**
* @test {PaperTemplate}
*/
test('[unit] paperTemplate should handle allowedAction data with a normal json format', (t) => {
const { paperJson } = t.context;
const paperTemplate = new PaperTemplate(paperJson);
t.is(paperTemplate.id, 1);
t.is(paperTemplate.type, 'agreement__paper_templates');
t.is(paperTemplate.description, 'Sample paper template');
});
/**
* @test {PaperTemplate}
*/
test('[unit] paperTemplate should handle device data with json api format', (t) => {
const { paperJsonApi } = t.context;
const paperTemplate = new PaperTemplate({ data: paperJsonApi });
t.is(paperTemplate.id, 1);
t.is(paperTemplate.type, 'agreement__paper_templates');
t.is(paperTemplate.description, 'somedescription');
});
/**
* @test {PaperTemplate}
*/
test('[unit] paperTemplate should generate json api format when converted to string', (t) => {
const { paperJsonApi } = t.context;
let paperTemplate = new PaperTemplate({ data: paperJsonApi });
let json = paperTemplate.toJSON();
t.deepEqual(json, paperJsonApi);
paperTemplate = new PaperTemplate({ data: paperJsonApi });
json = paperTemplate.toJSON();
t.deepEqual(json, paperJsonApi);
});
/**
* @test {PaperTemplate.save}
*/
test('[unit] paperTemplate.save should successfully insert a paperTemplate with a paperTemplate object when id does not exist', async (t) => {
const { paperJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(paperJsonApi));
delete json.id;
const paperTemplate = new PaperTemplate({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/agreement/paper_templates`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await paperTemplate.save();
t.is(request.path, `/v3/agreement/paper_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__paper_templates');
t.is(response.description, 'somedescription');
});
/**
* @test {PaperTemplate.save}
*/
test('[unit] paperTemplate.save should successfully update a paperTemplate with a paperTemplate object when id exists', async (t) => {
const { paperJsonApi, updateResponse } = t.context;
const paperTemplate = new PaperTemplate({ data: paperJsonApi });
let request = {};
nock(client.apiBaseUrl).patch(`/v3/agreement/paper_templates/${paperTemplate.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
const response = await paperTemplate.save();
t.is(request.path, `/v3/agreement/paper_templates/${paperTemplate.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: paperJsonApi });
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__paper_templates');
t.is(response.description, 'Sample paper template');
});