Manual Reference Source Test

test/unit/services.temporary-id.js

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

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

  t.context.getResponseId = {
    data: {
      id: 194,
      type: 'temporary_identifiers',
      attributes: {
        token: '2d8190bb1cb38d5192d850c20f4c51312c959d5c9b455916332727f3e506e18a',
        expires_at: '2018-07-10T16:25:00Z'
      },
      relationships: {
        user: {
          data: {
            id: '194',
            type: 'users'
          }
        }
      }
    },
    included: [
      {
        id: 194,
        type: 'users',
        attributes: {
          uuid: '74fe146e-c8ce-4c80-936a-0e71a04208bd',
          email: 'user531@fake.com',
          created_at: '2018-07-10T16:10:00Z',
          updated_at: '2018-07-10T16:10:00Z',
          invitation_sent_at: null,
          invitation_accepted_at: null,
          disabled_at: null
        },
        relationships: {
          user_role: {
            data: {
              id: 239,
              type: 'user_roles'
            }
          },
          devices: {
            data: []
          },
          profile: {
            data: {
              id: 543,
              type: 'profiles'
            }
          }
        }
      }
    ]
  };

  t.context.tempIdentifierJsonApi = {
    data: {
      type: 'temporary_identifiers',
      attributes: {
        token: undefined,
        expires_at: undefined
      }
    }
  };
});

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

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.temporaryId = new TemporaryId(t.context.tempIdentifierJsonApi);
});

// TemporaryIdService.get strategy
/**
 * @test {TemporaryIdService.get}
 */
test.serial('[unit] TemporaryIdService.get should throw an error when there is no authToken or temporary identifier token', async (t) => {
  client.authToken = undefined;
  const expectedError = 'TemporaryIdService.get error: requires authToken and temporaryId does not have token';
  await t.throwsAsync(clinical6.get(TemporaryId), expectedError);
});

/**
* @test {TemporaryIdService.get}
*/
test('[unit] TemporaryIdService.get should make a properly formatted get request', async (t) => {
  const { getResponseId, tempIdentifierJsonApi } = t.context;
  let request = {};
  const tempIdentifier = new TemporaryId(tempIdentifierJsonApi);
  tempIdentifier.token = '2d8190bb1cb38d5192d850c20f4c51312c959d5c9b455916332727f3e506e18a';
  nock(client.apiBaseUrl).get(`/v3/temporary_identifiers/${tempIdentifier.token}`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, getResponseId];
  });
  await temporaryIdService.get(tempIdentifier);
  t.is(request.path, `/v3/temporary_identifiers/2d8190bb1cb38d5192d850c20f4c51312c959d5c9b455916332727f3e506e18a`);
  t.is(request.headers.accept, 'application/json');
  t.is(request.headers['content-type'], 'application/json');
  t.is(request.headers.authorization, 'Token token=valid_token');
});

/**
 * @test {TemporaryIdService.get}
 */
test('[unit] TemporaryIdService.get should receive a valid response for a get request with an id', async (t) => {
  const { getResponseId, tempIdentifierJsonApi } = t.context;
  // let request = {};
  const tempIdentifier = new TemporaryId(tempIdentifierJsonApi);
  tempIdentifier.token = '2d8190bb1cb38d5192d850c20f4c51312c959d5c9b455916332727f3e506e18a';
  nock(client.apiBaseUrl).get('/v3/temporary_identifiers/2d8190bb1cb38d5192d850c20f4c51312c959d5c9b455916332727f3e506e18a').reply(200, getResponseId);
  const response = await temporaryIdService.get(tempIdentifier);
  t.truthy(response);
  t.is(response.id, 194);
  t.is(response.type, 'temporary_identifiers');
  t.is(response.token, '2d8190bb1cb38d5192d850c20f4c51312c959d5c9b455916332727f3e506e18a');
  t.is(response.expiresAt, '2018-07-10T16:25:00Z');
  t.is(response.user.id, 194);
  t.is(response.user.type, 'users');

  t.is(response.user.uuid, '74fe146e-c8ce-4c80-936a-0e71a04208bd');
  t.is(response.user.email, 'user531@fake.com');
  t.is(response.user.createdAt, '2018-07-10T16:10:00Z');
  t.is(response.user.updatedAt, '2018-07-10T16:10:00Z');
  t.is(response.user.invitationSentAt, null);

  t.is(response.user.userRole.id, 239);
  t.is(response.user.userRole.type, 'user_roles');
  t.is(response.user.devices.length, 0);
  t.is(response.user.profile.id, 543);
  t.is(response.user.profile.type, 'profiles');
});

// TemporaryIdService.insert strategy
/**
 * @test {TemporaryIdService.insert}
 */
test.serial('[unit] TemporaryIdService.insert should throw an error when there is no authToken and tempIdentifier does not have type', async (t) => {
  client.authToken = undefined;
  const expectedError = 'TemporaryIdService.insert error: requires authToken';
  await t.throwsAsync(temporaryIdService.insert(new TemporaryId()), expectedError);
});

/**
 * @test {TemporaryIdService.insert}
 */
test('[unit] TemporaryIdService.insert should successfully insert a temporaryId with a temporaryId object', async (t) => {
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/temporary_identifiers`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [201, {
      data: {
        id: 266,
        type: 'temporary_identifiers',
        attributes: {
          token: '83887ba0992e4de17ba8ead9e075162dedebad6c02890d08d5f17f86b48c0937',
          expires_at: '2018-07-10T16:25:00Z'
        },
        relationships: {
          user: {
            data: {
              id: 266,
              type: 'users'
            }
          }
        }
      }
    }];
  });

  const requestJsonApi = {
    data: {
      type: 'temporary_identifiers',
      attributes: {
      }
    }
  };

  const temporaryId = new TemporaryId(requestJsonApi);
  const response = await temporaryIdService.insert(temporaryId);

  t.is(request.path, `/v3/temporary_identifiers`);
  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, 266);
  t.is(response.type, 'temporary_identifiers');
  t.is(response.token, '83887ba0992e4de17ba8ead9e075162dedebad6c02890d08d5f17f86b48c0937');
  t.is(response.expiresAt, '2018-07-10T16:25:00Z');
  t.is(response.user.id, 266);
  t.is(response.user.type, 'users');
});