Manual Reference Source Test

test/unit/services.import.v3.js

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

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

  t.context.getResponseAll = {
    data: [
      {
        id: '1',
        type: 'csv_example_files',
        attributes: {
          platform_model: 'Trials::Site',
          human_readable_name: 'sites',
          file_url: '/uploads/test/csv_example_file/file/1/sample.xml'
        }
      }
    ]
  };

  t.context.insertResponse = {
    data: {
      id: '5ef4ded5-ab1f-4d73-a842-7366c8efbc9a',
      type: 'import_wizard__bulk_imports',
      attributes: {
        separator: '|',
        platform_model: 'Trials::Site'
      },
      relationships: {
        generic_file: {
          data: {
            id: '1',
            type: 'generic_files'
          }
        },
        status: {
          data: {
            id: 'e08f121d-6918-4038-b752-f447207600cd',
            type: 'job_status'
          }
        }
      }
    },
    included: [
      {
        id: '1',
        type: 'generic_files',
        attributes: {
          name: 'import_wizard.csv',
          public: false,
          visible_until: null,
          created_at: '2018-09-18T21:35:06Z',
          updated_at: '2018-09-18T21:35:06Z'
        }
      },
      {
        id: 'e08f121d-6918-4038-b752-f447207600cd',
        type: 'job_status',
        attributes: {
          status: 'pending',
          started_at: null,
          completed_at: null
        }
      }
    ]
  };
});

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

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


  t.context.storage = client.storageUtility;
  t.context.importJsonApi = {
    data: {
      type: 'import_wizard__bulk_imports',
      attributes: {
        platform_model: 'Trials::Site',
        separator: '|'
      },
      relationships: {
        generic_file: {
          data: {
            id: 1,
            type: 'generic_files'
          }
        }
      }
    }
  };
  t.context.bulkImport = new BulkImport(t.context.importJsonApi);
});


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

/**
 * @test {ImportService.getExampleFiles}
 */
test('[unit] ImportService.getExampleFiles should receive a valid response for a get request without an id', async (t) => {
  const { getResponseAll } = t.context;
  let request = {};
  nock(client.apiBaseUrl).get(`/v3/import_wizard/example_files`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, getResponseAll];
  });
  const response = await importService.getExampleFiles();
  t.is(request.path, `/v3/import_wizard/example_files`);
  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.is(response[0].id, 1);
  t.is(response[0].type, 'csv_example_files');
});


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

/**
 * @test {Clinical6.insert}
 */
test('[unit] ImportService.insert should successfully insert a bulkImport with a bulkImport object', async (t) => {
  const { insertResponse } = t.context;
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/import_wizard/bulk_imports`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [201, insertResponse];
  });

  const requestJsonApi = {
    data: {
      type: 'import_wizard__bulk_imports',
      attributes: {
        platform_model: 'Trials::Site',
        separator: '|'
      },
      relationships: {
        generic_file: {
          data: {
            id: 1,
            type: 'generic_files'
          }
        }
      }
    }
  };

  const bulkImport = new BulkImport(requestJsonApi);
  const response = await clinical6.insert(bulkImport);

  t.is(request.path, `/v3/import_wizard/bulk_imports`);
  t.is(request.headers.accept, 'application/json');
  t.deepEqual(request.requestBody, requestJsonApi);
  t.is(request.headers['content-type'], 'application/json');
  t.is(request.headers.authorization, 'Token token=valid_token');
  t.is(response.id, 5);
  t.is(response.type, 'import_wizard__bulk_imports');
});

/**
 * @test {Clinical6.insert}
 */
test('[unit] ImportService.insert should throw an error with an invalid token', async (t) => {
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/import_wizard/bulk_imports`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [422, {
      errors: [
        {
          source: {
            pointer: '/data/attributes/name'
          },
          detail: 'can\'t be blank'
        }
      ]
    }];
  });

  const requestJsonApi = {
    data: {
      type: 'import_wizard__bulk_imports',
      attributes: {
        platform_model: 'Trials::Site',
        separator: '|'
      },
      relationships: {
        generic_file: {
          data: {
            id: 1,
            type: 'generic_files'
          }
        }
      }
    }
  };

  const bulkImport = new BulkImport(requestJsonApi);
  const response = await clinical6.insert(bulkImport).catch(err => err);

  t.is(request.path, `/v3/import_wizard/bulk_imports`);
  t.is(request.headers.accept, 'application/json');
  t.deepEqual(request.requestBody, requestJsonApi);
  t.is(request.headers['content-type'], 'application/json');
  t.is(request.headers.authorization, 'Token token=valid_token');
  t.is(response.message, `"/data/attributes/name": can't be blank`);
});