Manual Reference Source Test

test/unit/services.agreement-template.v3.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  clinical6,
  AgreementTemplate,
} from '../../src';

test.before('start server', () => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
});

test.after('server shut down', () => {});

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';

  nock(client.apiBaseUrl).get('/v3/agreement/templates').reply(200, {
    data: [
      {
        id: 1,
        type: 'agreement__templates',
        attributes: {
          template_name: 'template_name',
          description: 'somedescription',
          expiration: 365,
          message: null,
          redirect_url: null,
          created_at: '2018-02-22T07:04:13Z',
          updated_at: '2018-02-22T07:04:13Z',
          permanent_link: 'template_name',
          reminder_frequency: null,
          archived_at: null
        },
        relationships: {
          approvers: {
            data: []
          }
        }
      },
      {
        id: 2,
        type: 'agreement__templates',
        attributes: {
          template_name: 'dummy_1 1',
          description: 'Sample agreement template',
          expiration: 18,
          message: null,
          redirect_url: 'http://jenkinchuster.net/delpha.kohler',
          created_at: '2018-02-26T19:06:53Z',
          updated_at: '2018-02-26T19:06:53Z',
          permanent_link: 'dummy11',
          reminder_frequency: 'WEEKLY_UNTIL_SIGNED',
          archived_at: null
        },
        relationships: {
          approvers: {
            data: [
              {
                id: 1,
                type: 'consent__approvers'
              },
              {
                id: 2,
                type: 'consent__approvers'
              }
            ]
          }
        },
      },
      {
        id: 3,
        type: 'agreement__templates',
        attributes: {
          template_name: 'dummy_2 2',
          description: 'Sample agreement template',
          expiration: 18,
          message: null,
          redirect_url: 'http://jenkinchuster.net/delpha.kohler',
          created_at: '2018-02-26T19:06:53Z',
          updated_at: '2018-02-26T19:06:53Z',
          permanent_link: 'dummy22',
          reminder_frequency: 'WEEKLY_UNTIL_SIGNED',
          archived_at: null
        },
        relationships: {
          approvers: {
            data: [
              {
                id: 3,
                type: 'consent__approvers'
              },
              {
                id: 4,
                type: 'consent__approvers'
              }
            ]
          }
        }
      },
      {
        id: 4,
        type: 'agreement__templates',
        attributes: {
          template_name: 'dummy_3 3',
          description: 'Sample agreement template',
          expiration: 18,
          message: null,
          redirect_url: 'http://jenkinchuster.net/delpha.kohler',
          created_at: '2018-02-26T19:06:53Z',
          updated_at: '2018-02-26T19:06:53Z',
          permanent_link: 'dummy33',
          reminder_frequency: 'WEEKLY_UNTIL_SIGNED',
          archived_at: null
        },
        relationships: {
          approvers: {
            data: [
              {
                id: 5,
                type: 'consent__approvers'
              },
              {
                id: 6,
                type: 'consent__approvers'
              }
            ]
          }
        }
      },
      {
        id: 5,
        type: 'agreement__templates',
        attributes: {
          template_name: 'dummy_4 4',
          description: 'Sample agreement template',
          expiration: 18,
          message: null,
          redirect_url: 'http://jenkinchuster.net/delpha.kohler',
          created_at: '2018-02-26T19:06:53Z',
          updated_at: '2018-02-26T19:06:53Z',
          permanent_link: 'dummy44',
          reminder_frequency: 'WEEKLY_UNTIL_SIGNED',
          archived_at: null
        },
        relationships: {
          approvers: {
            data: [
              {
                id: 7,
                type: 'consent__approvers'
              },
              {
                id: 8,
                type: 'consent__approvers'
              }
            ]
          }
        }
      },
      {
        id: 6,
        type: 'agreement__templates',
        attributes: {
          template_name: 'dummy_5 5',
          description: 'Sample agreement template',
          expiration: 18,
          message: null,
          redirect_url: 'http://jenkinchuster.net/delpha.kohler',
          created_at: '2018-02-26T19:06:53Z',
          updated_at: '2018-02-26T19:06:53Z',
          permanent_link: 'dummy55',
          reminder_frequency: 'WEEKLY_UNTIL_SIGNED',
          archived_at: null
        },
        relationships: {
          approvers: {
            data: [
              {
                id: 9,
                type: 'consent__approvers'
              },
              {
                id: 10,
                type: 'consent__approvers'
              }
            ]
          }
        }
      }
    ]
  });

  t.context.storage = client.storageUtility;
  t.context.agreementTemplateJsonApi = {
    data: {
      type: 'agreement__templates',
      attributes: {
        description: 'somedescription',
        permanent_link: 'another_template',
      },
      relationships: {}
    }
  };
  t.context.agreementTemplate = new AgreementTemplate(t.context.agreementTemplateJsonApi);
});

// test.afterEach(t => t.context.server.restore());

// AgreementTemplateService.delete method

// AgreementTemplateService.get method
/**
 * @test {Clinical6.get}
 */
test.serial('[unit] AgreementTemplateService.get should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const title = 'AgreementTemplateService.get error';
  await t.throwsAsync(clinical6.get(AgreementTemplate), `${title}: requires authToken`);
});

// /**
//  * @test {Clinical6.get}
//  */
// test('[unit] AgreementTemplateService.get should make a properly formatted get request', async (t) => {
//   await clinical6.get(AgreementTemplate);
//   const request = t.context.server.requests[0];
//   t.is(request.method, 'GET');
//   t.is(request.url, `${client.apiBaseUrl}/v3/agreement/templates`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
// });


/**
 * @test {SiteMemberService.get}
 */

test('[unit] AgreementTemplateService.get should receive a valid response for a get request without an id', async (t) => {
  const response = await clinical6.get(AgreementTemplate);
  t.truthy(response);
  t.is(Object.keys(response).length, 6);
  t.is(response[0].id, 1);
  t.is(response[0].type, 'agreement__templates');
  t.is(response[0].templateName, 'template_name');
  t.is(response[0].description, 'somedescription');
  t.is(response[0].expiration, 365);
  t.is(response[0].message, null);
  t.is(response[0].redirectUrl, null);
  t.is(response[0].createdAt, '2018-02-22T07:04:13Z');
  t.is(response[0].updatedAt, '2018-02-22T07:04:13Z');
  t.is(response[0].permanentLink, 'template_name');
  t.is(response[0].reminderFrequency, null);
  t.is(response[0].archivedAt, null);
  t.is(response[0].approvers.length, 0);
  t.is(response[0].relationships.relationships.approvers.data.length, 0);

  t.is(response[1].id, 2);
  t.is(response[1].type, 'agreement__templates');
  t.is(response[1].templateName, 'dummy_1 1');
  t.is(response[1].description, 'Sample agreement template');
  t.is(response[1].expiration, 18);
  t.is(response[1].message, null);
  t.is(response[1].redirectUrl, 'http://jenkinchuster.net/delpha.kohler');
  t.is(response[1].createdAt, '2018-02-26T19:06:53Z');
  t.is(response[1].updatedAt, '2018-02-26T19:06:53Z');
  t.is(response[1].permanentLink, 'dummy11');
  t.is(response[1].reminderFrequency, 'WEEKLY_UNTIL_SIGNED');
  t.is(response[1].archivedAt, null);
  t.is(response[1].approvers.length, 2);
  t.is(response[1].relationships.relationships.approvers.data.length, 2);
  t.is(response[1].relationships.relationships.approvers.data[0].id, 1);
  t.is(response[1].relationships.relationships.approvers.data[0].type, 'consent__approvers');
  t.is(response[1].relationships.relationships.approvers.data[1].id, 2);
  t.is(response[1].relationships.relationships.approvers.data[1].type, 'consent__approvers');
  t.is(response[1].approvers[0].id, 1);
  t.is(response[1].approvers[0].type, 'consent__approvers');
  t.is(response[1].approvers[1].id, 2);
  t.is(response[1].approvers[1].type, 'consent__approvers');

  t.is(response[2].id, 3);
  t.is(response[2].type, 'agreement__templates');
  t.is(response[2].templateName, 'dummy_2 2');
  t.is(response[2].description, 'Sample agreement template');
  t.is(response[2].expiration, 18);
  t.is(response[2].message, null);
  t.is(response[2].redirectUrl, 'http://jenkinchuster.net/delpha.kohler');
  t.is(response[2].createdAt, '2018-02-26T19:06:53Z');
  t.is(response[2].updatedAt, '2018-02-26T19:06:53Z');
  t.is(response[2].permanentLink, 'dummy22');
  t.is(response[2].reminderFrequency, 'WEEKLY_UNTIL_SIGNED');
  t.is(response[2].archivedAt, null);
  t.is(response[2].approvers.length, 2);
  t.is(response[2].relationships.relationships.approvers.data.length, 2);
  t.is(response[2].relationships.relationships.approvers.data[0].id, 3);
  t.is(response[2].relationships.relationships.approvers.data[0].type, 'consent__approvers');
  t.is(response[2].relationships.relationships.approvers.data[1].id, 4);
  t.is(response[2].relationships.relationships.approvers.data[1].type, 'consent__approvers');
  t.is(response[2].approvers[0].id, 3);
  t.is(response[2].approvers[0].type, 'consent__approvers');
  t.is(response[2].approvers[1].id, 4);
  t.is(response[2].approvers[1].type, 'consent__approvers');

  t.is(response[3].id, 4);
  t.is(response[3].type, 'agreement__templates');
  t.is(response[3].templateName, 'dummy_3 3');
  t.is(response[3].description, 'Sample agreement template');
  t.is(response[3].expiration, 18);
  t.is(response[3].message, null);
  t.is(response[3].redirectUrl, 'http://jenkinchuster.net/delpha.kohler');
  t.is(response[3].createdAt, '2018-02-26T19:06:53Z');
  t.is(response[3].updatedAt, '2018-02-26T19:06:53Z');
  t.is(response[3].permanentLink, 'dummy33');
  t.is(response[3].reminderFrequency, 'WEEKLY_UNTIL_SIGNED');
  t.is(response[3].archivedAt, null);
  t.is(response[3].approvers.length, 2);
  t.is(response[3].relationships.relationships.approvers.data.length, 2);
  t.is(response[3].relationships.relationships.approvers.data[0].id, 5);
  t.is(response[3].relationships.relationships.approvers.data[0].type, 'consent__approvers');
  t.is(response[3].relationships.relationships.approvers.data[1].id, 6);
  t.is(response[3].relationships.relationships.approvers.data[1].type, 'consent__approvers');
  t.is(response[3].approvers[0].id, 5);
  t.is(response[3].approvers[0].type, 'consent__approvers');
  t.is(response[3].approvers[1].id, 6);
  t.is(response[3].approvers[1].type, 'consent__approvers');

  t.is(response[4].id, 5);
  t.is(response[4].type, 'agreement__templates');
  t.is(response[4].templateName, 'dummy_4 4');
  t.is(response[4].description, 'Sample agreement template');
  t.is(response[4].expiration, 18);
  t.is(response[4].message, null);
  t.is(response[4].redirectUrl, 'http://jenkinchuster.net/delpha.kohler');
  t.is(response[4].createdAt, '2018-02-26T19:06:53Z');
  t.is(response[4].updatedAt, '2018-02-26T19:06:53Z');
  t.is(response[4].permanentLink, 'dummy44');
  t.is(response[4].reminderFrequency, 'WEEKLY_UNTIL_SIGNED');
  t.is(response[4].archivedAt, null);
  t.is(response[4].approvers.length, 2);
  t.is(response[4].relationships.relationships.approvers.data.length, 2);
  t.is(response[4].relationships.relationships.approvers.data[0].id, 7);
  t.is(response[4].relationships.relationships.approvers.data[0].type, 'consent__approvers');
  t.is(response[4].relationships.relationships.approvers.data[1].id, 8);
  t.is(response[4].relationships.relationships.approvers.data[1].type, 'consent__approvers');
  t.is(response[4].approvers[0].id, 7);
  t.is(response[4].approvers[0].type, 'consent__approvers');
  t.is(response[4].approvers[1].id, 8);
  t.is(response[4].approvers[1].type, 'consent__approvers');

  t.is(response[5].id, 6);
  t.is(response[5].type, 'agreement__templates');
  t.is(response[5].templateName, 'dummy_5 5');
  t.is(response[5].description, 'Sample agreement template');
  t.is(response[5].expiration, 18);
  t.is(response[5].message, null);
  t.is(response[5].redirectUrl, 'http://jenkinchuster.net/delpha.kohler');
  t.is(response[5].createdAt, '2018-02-26T19:06:53Z');
  t.is(response[5].updatedAt, '2018-02-26T19:06:53Z');
  t.is(response[5].permanentLink, 'dummy55');
  t.is(response[5].reminderFrequency, 'WEEKLY_UNTIL_SIGNED');
  t.is(response[5].archivedAt, null);
  t.is(response[5].approvers.length, 2);
  t.is(response[5].relationships.relationships.approvers.data.length, 2);
  t.is(response[5].relationships.relationships.approvers.data[0].id, 9);
  t.is(response[5].relationships.relationships.approvers.data[0].type, 'consent__approvers');
  t.is(response[5].relationships.relationships.approvers.data[1].id, 10);
  t.is(response[5].relationships.relationships.approvers.data[1].type, 'consent__approvers');
  t.is(response[5].approvers[0].id, 9);
  t.is(response[5].approvers[0].type, 'consent__approvers');
  t.is(response[5].approvers[1].id, 10);
  t.is(response[5].approvers[1].type, 'consent__approvers');
});

// AgreementTemplateService.insert method
// AgreementTemplateService.insert method
/**
 * @test {Clinical6.insert}
 */
test.serial('[unit] AgreementTemplateService.insert should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const title = 'AgreementTemplateService.insert error';
  await t.throwsAsync(clinical6.insert(new AgreementTemplate()), `${title}: requires authToken`);
});

/**
 * @test {Clinical6.insert}
 */
test('[unit] AgreementTemplateService.insert should successfully insert an agreement template with an agreement template object', async (t) => {
  const requestJsonApi = {
    data: {
      type: 'agreement__templates',
      attributes: {
        description: 'somedescription',
        permanent_link: 'another_template',
      }
    }
  };

  let requestBody = {};
  const server = nock(client.apiBaseUrl).post('/v3/agreement/templates', (a) => {
    requestBody = a;
    return requestJsonApi;
  }).reply(201, {
    data: {
      id: 7,
      type: 'agreement__templates',
      attributes: {
        template_name: 'another template',
        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: []
        }
      }
    }
  });

  const agreementTemplate = new AgreementTemplate(requestJsonApi);
  const response = await clinical6.insert(agreementTemplate);
  t.deepEqual(requestBody, requestJsonApi);
  server.done();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/v3/agreement/templates`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.deepEqual(JSON.parse(request.requestBody), requestJsonApi);
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.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');
  t.is(response.templateName, 'another template');
  t.is(response.expiration, 365);
  t.is(response.message, null);
  t.is(response.redirectUrl, null);
  t.is(response.createdAt, '2018-02-26T19:11:56Z');
  t.is(response.updatedAt, '2018-02-26T19:11:56Z');
  t.is(response.reminderFrequency, null);
  t.is(response.archivedAt, null);
  t.is(response.relationships.relationships.approvers.data.length, 0);
  t.is(response.approvers.length, 0);
});

/**
 * @test {Clinical6.insert}
 */
test('[unit] AgreementTemplateService.insert should throw an error without the necessary attributes', async (t) => {
  const requestJsonApi = {
    data: {
      type: 'agreement__templates',
      attributes: {
        permanent_link: 'another_template',
      }
    }
  };

  let requestBody = {};
  const server = nock(client.apiBaseUrl).post('/v3/agreement/templates', (a) => {
    requestBody = a;
    return requestJsonApi;
  }).reply(422, {
    errors: [
      {
        source: {
          pointer: '/data/attributes/description'
        },
        detail: 'can\'t be blank'
      }
    ]
  });

  const agreementTemplate = new AgreementTemplate(requestJsonApi);
  const response = await clinical6.insert(agreementTemplate).catch(err => err);
  t.deepEqual(requestBody, requestJsonApi);
  server.done();

  // const request = t.context.server.requests[0];
  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/v3/agreement/templates`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify(requestJsonApi));
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.is(response.message, `"/data/attributes/description": can't be blank`);
});

// AgreementTemplateService.update method