Manual Reference Source Test

test/unit/helpers.sign-account.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  SignAccount
} 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: '1',
      type: 'adobe_sign__accounts',
      attributes: {
        email: 'eguillen+unique4@parallel6.com',
        created_at: '2018-08-16T18:11:58Z',
        updated_at: '2018-08-16T18:11:58Z',
        linked: false,
        password: null,
        first_name: 'CanBe',
        last_name: 'Repeated',
        title: null,
        company_name: 'RepeatableCompany',
        country_code: null,
        phone_number: null,
        locale: null,
        time_zone: null
      }
    }
  };

  t.context.signAccountJsonApi = {
    type: 'adobe_sign__accounts',
    attributes: {
      email: 'eguillen+unique4@parallel6.com',
      first_name: 'CanBe',
      last_name: 'Repeated',
      company_name: 'RepeatableCompany',
      linked: undefined,
      locale: undefined,
      password: undefined,
      phone_number: undefined,
      refresh_token: undefined,
      time_zone: undefined,
      title: undefined,
      updated_at: undefined,
      country_code: undefined,
      created_at: undefined
    }
  };

  t.context.signAccountJson = {
    type: 'adobe_sign__accounts',
    email: 'eguillen+unique4@parallel6.com',
    first_name: 'CanBe',
    last_name: 'Repeated',
    company_name: 'RepeatableCompany',
    linked: undefined,
    locale: undefined,
    password: undefined,
    phone_number: undefined,
    refresh_token: undefined,
    time_zone: undefined,
    title: undefined,
    updated_at: undefined,
    country_code: undefined,
    created_at: undefined
  };

  client.storageUtility.clear();
  t.context.storage = client.storageUtility;
  client.signAccount = new SignAccount({});
  t.context.signAccount = new SignAccount({ data: t.context.signAccountJsonApi });
});

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

  t.is(signAccount.type, 'adobe_sign__accounts');
  t.is(signAccount.email, 'eguillen+unique4@parallel6.com');
  t.is(signAccount.firstName, 'CanBe');
  t.is(signAccount.lastName, 'Repeated');
  t.is(signAccount.companyName, 'RepeatableCompany');
  t.is(signAccount.linked, undefined);
  t.is(signAccount.locale, undefined);
  t.is(signAccount.password, undefined);
  t.is(signAccount.phoneNumber, undefined);
  t.is(signAccount.refreshToken, undefined);
  t.is(signAccount.timeZone, undefined);
  t.is(signAccount.title, undefined);
  t.is(signAccount.updatedAt, undefined);
  t.is(signAccount.countryCode, undefined);
  t.is(signAccount.createdAt, undefined);
});

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

  t.is(signAccount.type, 'adobe_sign__accounts');
  t.is(signAccount.email, 'eguillen+unique4@parallel6.com');
  t.is(signAccount.firstName, 'CanBe');
  t.is(signAccount.lastName, 'Repeated');
  t.is(signAccount.companyName, 'RepeatableCompany');
  t.is(signAccount.linked, undefined);
  t.is(signAccount.locale, undefined);
  t.is(signAccount.password, undefined);
  t.is(signAccount.phoneNumber, undefined);
  t.is(signAccount.refreshToken, undefined);
  t.is(signAccount.timeZone, undefined);
  t.is(signAccount.title, undefined);
  t.is(signAccount.updatedAt, undefined);
  t.is(signAccount.countryCode, undefined);
  t.is(signAccount.createdAt, undefined);
});

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

  signAccount = new SignAccount({ data: signAccountJsonApi });
  t.deepEqual(signAccount.toJSON(), signAccountJsonApi);
});

/**
  * @test {SignAccount.save}
  */

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

  const response = await signAccount.save();

  t.is(request.path, `/v3/adobe_sign/accounts`);
  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, 1);
  t.is(response.type, 'adobe_sign__accounts');
  t.is(response.email, 'eguillen+unique4@parallel6.com');
  t.is(response.createdAt, '2018-08-16T18:11:58Z');
  t.is(response.updatedAt, '2018-08-16T18:11:58Z');
  t.is(response.linked, false);
  t.is(response.password, null);
  t.is(response.firstName, 'CanBe');
  t.is(response.lastName, 'Repeated');
  t.is(response.title, null);
  t.is(response.companyName, 'RepeatableCompany');
  t.is(response.countryCode, null);
  t.is(response.phoneNumber, null);
  t.is(response.locale, null);
  t.is(response.timeZone, null);
});


/**
 * @test {SignAccount.delete}
 */
test('[unit] SignAccount.delete should successfully delete a SignAccount', async (t) => {
  const { signAccountJsonApi } = t.context;
  signAccountJsonApi.id = 1;
  const signAccount = new SignAccount({ data: signAccountJsonApi });
  let request = {};
  nock(client.apiBaseUrl).delete(`/v3/adobe_sign/accounts/${signAccount.id}`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [204, ''];
  });
  const response = await signAccount.delete();
  t.is(request.path, `/v3/adobe_sign/accounts/${signAccount.id}`);
  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.falsy(response);
});

/**
  * @test {SignAccount.delete}
  */
test('[unit] SignAccount.delete should remove the element from local storage', async (t) => {
  const { signAccount, storage } = t.context;
  signAccount.id = 1;
  await storage.set('adobe_sign__accounts', signAccount.toJSON(), { id: signAccount.id });
  await signAccount.delete(signAccount);
  t.is(storage.has('adobe_sign__accounts', { id: signAccount.id }), false);
});