Manual Reference Source Test

test/unit/helpers.temporary-id.js

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

// See tests.js for testing client creation
test.before('start server', () => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
  client.authToken = 'valid_token';
});

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

  t.context.insertResponse = {
    data: {
      id: 266,
      type: 'temporary_identifiers',
      attributes: {
        token: '83887ba0992e4de17ba8ead9e075162dedebad6c02890d08d5f17f86b48c0937',
        expires_at: '2018-07-10T16:25:00Z'
      },
      relationships: {
        user: {
          data: {
            id: 266,
            type: 'users'
          }
        }
      }
    }
  };

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

  t.context.temporaryIdJson = {
    type: 'temporary_identifiers',
    token: undefined,
    expires_at: undefined
  };

  client.storageUtility.clear();
  t.context.storage = client.storageUtility;
  client.temporaryId = new TemporaryId({});
  t.context.temporaryId = new TemporaryId({ data: t.context.temporaryIdJsonApi });
});

/**
 * @test {TemporaryId}
 */
test('[unit] TemporaryId should handle temporaryId data with a normal json format', (t) => {
  const { temporaryIdJson } = t.context;
  const temporaryId = new TemporaryId(temporaryIdJson);

  t.is(temporaryId.type, 'temporary_identifiers');
  t.is(temporaryId.token, undefined);
  t.is(temporaryId.expiresAt, undefined);
});

/**
 * @test {TemporaryId}
 */
test('[unit] TemporaryId should handle temporaryId data with json api format', (t) => {
  const { temporaryIdJsonApi } = t.context;
  const temporaryId = new TemporaryId({ data: temporaryIdJsonApi });

  t.is(temporaryId.type, 'temporary_identifiers');
  t.is(temporaryId.token, undefined);
  t.is(temporaryId.expiresAt, undefined);
});

/**
 * @test {TemporaryId}
 */
test('[unit] TemporaryId should generate json api format when converted to string', (t) => {
  const { temporaryIdJsonApi } = t.context;
  let temporaryId = new TemporaryId({ data: temporaryIdJsonApi });
  t.deepEqual(temporaryId.toJSON(), temporaryIdJsonApi);

  temporaryId = new TemporaryId({ data: temporaryIdJsonApi });
  t.deepEqual(temporaryId.toJSON(), temporaryIdJsonApi);
});

/**
  * @test {TemporaryId.save}
  */
test('[unit] TemporaryId.save should successfully save a new temporaryId when id does not exist', async (t) => {
  const { temporaryIdJsonApi, insertResponse } = t.context;
  const json = JSON.parse(JSON.stringify(temporaryIdJsonApi));
  const temporaryId = new TemporaryId({ data: json });
  let request = {};
  nock(client.apiBaseUrl).post(`/v3/temporary_identifiers`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [201, insertResponse];
  });

  const response = await temporaryId.save();

  t.is(request.path, `/v3/temporary_identifiers`);
  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, 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');
});