Manual Reference Source Test

test/unit/services.sso-option.js

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

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

  t.context.getResponseAll = {
    data: [
      {
        id: 15,
        type: 'sso_options',
        attributes: {
          name: 'Dummy SSO',
          user_type: 'User',
          permanent_link: 'dummy_sso'
        }
      }
    ]
  };

  t.context.ssoOptionJsonApi = {
    data: {
      type: 'sso_options',
      attributes: {
      }
    }
  };
});

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

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

// SsoOptionService.get strategy
/**
 * @test {Clinical6.get}
 */
test.serial('[unit] SsoOptionService.get should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const expectedError = 'SsoOptionService.get error: requires authToken';
  await t.throwsAsync(clinical6.get(SsoOption), expectedError);
});

/**
* @test {Clinical6.get}
*/
test('[unit] SsoOptionService.get should make a properly formatted get request', async (t) => {
  const { getResponseAll } = t.context;
  let request = {};
  nock(client.apiBaseUrl).get(`/v3/sso_options`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, getResponseAll];
  });
  await clinical6.get(SsoOption);
  t.is(request.path, `/v3/sso_options`);
  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 {Clinical6.get}
*/
test('[unit] SsoOptionService.get should receive a valid response for a get request without an id', async (t) => {
  const { getResponseAll } = t.context;
  nock(client.apiBaseUrl).get(`/v3/sso_options`).reply(200, getResponseAll);
  const response = await clinical6.get(SsoOption);
  t.truthy(response);
  t.is(Object.keys(response).length, 1);

  t.is(response[0].id, 15);
  t.is(response[0].type, 'sso_options');
  t.is(response[0].name, 'Dummy SSO');
  t.is(response[0].userType, 'User');
  t.is(response[0].permanentLink, 'dummy_sso');
});