Manual Reference Source Test

test/unit/services.custom-class.versions.v3.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  clinical6,
  CustomClass,
  HelperFactory,
  ServiceFactory,
} from '../../src';

class Version extends CustomClass {
  static get type() { return 'versions'; }
}

HelperFactory.map.versions = Version;
ServiceFactory.options.versions = { title: 'VersionService' };

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

  t.context.getResponseAll = {
    data: [
      {
        id: '1991',
        type: 'versions',
        attributes: {
          category: 'User token authentication event',
          created_at: '2018-07-08T18:50:01Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            type: [
              null,
              'UserTokenAuthenticationEvent'
            ],
            identifier: [
              null,
              'something@something.com'
            ],
            result: [
              null,
              'success'
            ],
            ip: [
              null,
              '189.202.79.66'
            ]
          },
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 455,
              identifier: 'something@something.com',
              result: 'success',
              created_at: '2018-07-08T18:50:01Z',
              updated_at: '2018-07-08T18:50:01Z',
              ip: '189.202.79.66'
            }
          }
        }
      },
      {
        id: '1992',
        type: 'versions',
        attributes: {
          category: 'User Account Details',
          created_at: '2018-07-08T18:50:31Z',
          event: 'update',
          ip: '189.202.79.66',
          original_object: {
            email: 'something@something.com',
            encrypted_password: 'asdf',
            reset_password_token: 'asdf',
            reset_password_sent_at: '2018-07-07 02:05:59',
            remember_created_at: null,
            confirmation_token: null,
            confirmed_at: '2018-07-07 02:01:38',
            confirmation_sent_at: null,
            unconfirmed_email: null,
            failed_attempts: 0,
            unlock_token: null,
            locked_at: null,
            username: 'superuser',
            pin: null,
            invitation_token: null,
            invitation_sent_at: null,
            invitation_accepted_at: null,
            invitation_limit: null,
            invited_by_id: null,
            invited_by_type: null,
            brand_id: 1,
            password_changed_at: '2018-07-07 02:01:38',
            encrypted_otp_secret: null,
            encrypted_otp_secret_iv: null,
            encrypted_otp_secret_salt: null,
            otp_required_for_login: null,
            provider: null,
            uid: null,
            user_role_id: 1,
            invitation_created_at: null,
            uuid: {
              value: 'asdf'
            },
            authentication_token: 'asdf',
            disabled_at: null,
            authentication_token_expired_at: '2018-07-08 19:05:01',
            allow_authentication_token_refresh_until: '2018-07-09 00:49:51',
            sso_identifier: null
          },
          object_changes: {
            authentication_token_expired_at: [
              '2018-07-08T19:05:01Z',
              '2018-07-08T19:05:31Z'
            ]
          },
          user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: '1',
              type: 'users'
            }
          }
        }
      },
      {
        id: '1993',
        type: 'versions',
        attributes: {
          category: 'User token authentication event',
          created_at: '2018-07-08T18:50:31Z',
          event: 'create',
          ip: '189.202.79.66',
          original_object: {},
          object_changes: {
            type: [
              null,
              'UserTokenAuthenticationEvent'
            ],
            identifier: [
              null,
              'something@something.com'
            ],
            result: [
              null,
              'success'
            ],
            ip: [
              null,
              '189.202.79.66'
            ]
          },
          user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 456,
              identifier: 'something@something.com',
              result: 'success',
              created_at: '2018-07-08T18:50:31Z',
              updated_at: '2018-07-08T18:50:31Z',
              ip: '189.202.79.66'
            }
          }
        }
      },
      {
        id: '1994',
        type: 'versions',
        attributes: {
          category: 'User Account Details',
          created_at: '2018-07-08T18:50:31Z',
          event: 'update',
          ip: '189.202.79.66',
          original_object: {
            email: 'something@something.com',
            encrypted_password: 'asdf',
            reset_password_token: 'asdf',
            reset_password_sent_at: '2018-07-07 02:05:59',
            remember_created_at: null,
            confirmation_token: null,
            confirmed_at: '2018-07-07 02:01:38',
            confirmation_sent_at: null,
            unconfirmed_email: null,
            failed_attempts: 0,
            unlock_token: null,
            locked_at: null,
            username: 'superuser',
            pin: null,
            invitation_token: null,
            invitation_sent_at: null,
            invitation_accepted_at: null,
            invitation_limit: null,
            invited_by_id: null,
            invited_by_type: null,
            brand_id: 1,
            password_changed_at: '2018-07-07 02:01:38',
            encrypted_otp_secret: null,
            encrypted_otp_secret_iv: null,
            encrypted_otp_secret_salt: null,
            otp_required_for_login: null,
            provider: null,
            uid: null,
            user_role_id: 1,
            invitation_created_at: null,
            uuid: {
              value: 'asdf'
            },
            authentication_token: 'asdf',
            disabled_at: null,
            authentication_token_expired_at: '2018-07-08 19:05:31',
            allow_authentication_token_refresh_until: '2018-07-09 00:49:51',
            sso_identifier: null
          },
          object_changes: {
            authentication_token: [
              'asdf',
              'asdf'
            ],
            authentication_token_expired_at: [
              '2018-07-08T19:05:31Z',
              '2018-07-08T19:05:31Z'
            ],
            allow_authentication_token_refresh_until: [
              '2018-07-09T00:49:51Z',
              '2018-07-09T00:50:31Z'
            ]
          },
          user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        },
        relationships: {
          whodunnitable: {
            data: {
              id: '1',
              type: 'users'
            }
          },
          item: {
            data: {
              id: '1',
              type: 'users'
            }
          }
        }
      },
      {
        id: '1995',
        type: 'versions',
        attributes: {
          category: 'User token logout authentication event',
          created_at: '2018-07-08T18:50:31Z',
          event: 'create',
          ip: '189.202.79.66',
          original_object: {},
          object_changes: {
            type: [
              null,
              'UserTokenLogoutAuthenticationEvent'
            ],
            identifier: [
              null,
              'something@something.com'
            ],
            result: [
              null,
              'success'
            ],
            ip: [
              null,
              '189.202.79.66'
            ]
          },
          user_agent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        },
        relationships: {
          whodunnitable: {
            data: {
              id: '1',
              type: 'users'
            }
          },
          item: {
            data: {
              id: 457,
              identifier: 'something@something.com',
              result: 'success',
              created_at: '2018-07-08T18:50:31Z',
              updated_at: '2018-07-08T18:50:31Z',
              ip: '189.202.79.66'
            }
          }
        }
      },
      {
        id: '1996',
        type: 'versions',
        attributes: {
          category: 'User Account Details',
          created_at: '2018-07-08T18:53:53Z',
          event: 'update',
          ip: '76.167.229.103',
          original_object: {
            email: 'somethingelse@something.com',
            encrypted_password: 'asdf',
            reset_password_token: null,
            reset_password_sent_at: null,
            remember_created_at: null,
            confirmation_token: null,
            confirmed_at: '2018-07-08 18:40:59',
            confirmation_sent_at: null,
            unconfirmed_email: null,
            failed_attempts: 0,
            unlock_token: null,
            locked_at: null,
            username: null,
            pin: null,
            invitation_token: null,
            invitation_sent_at: '2018-07-08 18:40:21',
            invitation_accepted_at: '2018-07-08 18:40:59',
            invitation_limit: null,
            invited_by_id: null,
            invited_by_type: null,
            brand_id: 1,
            password_changed_at: '2018-07-08 18:40:59',
            encrypted_otp_secret: null,
            encrypted_otp_secret_iv: null,
            encrypted_otp_secret_salt: null,
            otp_required_for_login: null,
            provider: null,
            uid: null,
            user_role_id: 1,
            invitation_created_at: '2018-07-08 18:40:21',
            uuid: {
              value: 'asdf'
            },
            authentication_token: 'asdf',
            disabled_at: null,
            authentication_token_expired_at: '2018-07-08 19:00:53',
            allow_authentication_token_refresh_until: '2018-07-09 00:41:15',
            sso_identifier: null
          },
          object_changes: {
            authentication_token_expired_at: [
              '2018-07-08T19:00:53Z',
              '2018-07-08T19:08:53Z'
            ]
          },
          user_agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: '4',
              type: 'users'
            }
          }
        }
      },
      {
        id: '1997',
        type: 'versions',
        attributes: {
          category: 'User token authentication event',
          created_at: '2018-07-08T18:53:53Z',
          event: 'create',
          ip: '76.167.229.103',
          original_object: {},
          object_changes: {
            type: [
              null,
              'UserTokenAuthenticationEvent'
            ],
            identifier: [
              null,
              'somethingelse@something.com'
            ],
            result: [
              null,
              'success'
            ],
            ip: [
              null,
              '76.167.229.103'
            ]
          },
          user_agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36'
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 458,
              identifier: 'somethingelse@something.com',
              result: 'success',
              created_at: '2018-07-08T18:53:53Z',
              updated_at: '2018-07-08T18:53:53Z',
              ip: '76.167.229.103'
            }
          }
        }
      },
      {
        id: '1998',
        type: 'versions',
        attributes: {
          category: 'User Account Details',
          created_at: '2018-07-08T18:53:53Z',
          event: 'update',
          ip: null,
          original_object: {
            email: 'somethingelse@something.com',
            encrypted_password: 'asdf',
            reset_password_token: null,
            reset_password_sent_at: null,
            remember_created_at: null,
            confirmation_token: null,
            confirmed_at: '2018-07-08 18:40:59',
            confirmation_sent_at: null,
            unconfirmed_email: null,
            failed_attempts: 0,
            unlock_token: null,
            locked_at: null,
            username: null,
            pin: null,
            invitation_token: null,
            invitation_sent_at: '2018-07-08 18:40:21',
            invitation_accepted_at: '2018-07-08 18:40:59',
            invitation_limit: null,
            invited_by_id: null,
            invited_by_type: null,
            brand_id: 1,
            password_changed_at: '2018-07-08 18:40:59',
            encrypted_otp_secret: null,
            encrypted_otp_secret_iv: null,
            encrypted_otp_secret_salt: null,
            otp_required_for_login: null,
            provider: null,
            uid: null,
            user_role_id: 1,
            invitation_created_at: '2018-07-08 18:40:21',
            uuid: {
              value: 'asdf'
            },
            authentication_token: 'asdf',
            disabled_at: null,
            authentication_token_expired_at: '2018-07-08 19:08:53',
            allow_authentication_token_refresh_until: '2018-07-09 00:41:15',
            sso_identifier: null
          },
          object_changes: {
            authentication_token_expired_at: [
              '2018-07-08T19:08:53Z',
              '2018-07-08T19:08:53Z'
            ]
          },
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: '4',
              type: 'users'
            }
          }
        }
      },
      {
        id: '1999',
        type: 'versions',
        attributes: {
          category: 'User token authentication event',
          created_at: '2018-07-08T18:53:53Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            type: [
              null,
              'UserTokenAuthenticationEvent'
            ],
            identifier: [
              null,
              'somethingelse@something.com'
            ],
            result: [
              null,
              'success'
            ],
            ip: [
              null,
              '76.167.229.103'
            ]
          },
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 459,
              identifier: 'somethingelse@something.com',
              result: 'success',
              created_at: '2018-07-08T18:53:53Z',
              updated_at: '2018-07-08T18:53:53Z',
              ip: '76.167.229.103'
            }
          }
        }
      },
      {
        id: '2000',
        type: 'versions',
        attributes: {
          category: 'User Account Details',
          created_at: '2018-07-08T18:53:54Z',
          event: 'update',
          ip: null,
          original_object: {
            email: 'somethingelse@something.com',
            encrypted_password: 'asdf',
            reset_password_token: null,
            reset_password_sent_at: null,
            remember_created_at: null,
            confirmation_token: null,
            confirmed_at: '2018-07-08 18:40:59',
            confirmation_sent_at: null,
            unconfirmed_email: null,
            failed_attempts: 0,
            unlock_token: null,
            locked_at: null,
            username: null,
            pin: null,
            invitation_token: null,
            invitation_sent_at: '2018-07-08 18:40:21',
            invitation_accepted_at: '2018-07-08 18:40:59',
            invitation_limit: null,
            invited_by_id: null,
            invited_by_type: null,
            brand_id: 1,
            password_changed_at: '2018-07-08 18:40:59',
            encrypted_otp_secret: null,
            encrypted_otp_secret_iv: null,
            encrypted_otp_secret_salt: null,
            otp_required_for_login: null,
            provider: null,
            uid: null,
            user_role_id: 1,
            invitation_created_at: '2018-07-08 18:40:21',
            uuid: {
              value: 'asdf'
            },
            authentication_token: 'asdf',
            disabled_at: null,
            authentication_token_expired_at: '2018-07-08 19:08:53',
            allow_authentication_token_refresh_until: '2018-07-09 00:41:15',
            sso_identifier: null
          },
          object_changes: {
            authentication_token_expired_at: [
              '2018-07-08T19:08:53Z',
              '2018-07-08T19:08:54Z'
            ]
          },
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: '4',
              type: 'users'
            }
          }
        }
      }
    ],
    included: [
      {
        id: '1',
        type: 'users',
        attributes: {
          uuid: 'asdf',
          email: 'something@something.com',
          created_at: '2018-07-07T02:01:38Z',
          updated_at: '2018-07-30T18:49:38Z',
          invitation_sent_at: null,
          invitation_accepted_at: null,
          disabled_at: null
        },
        relationships: {
          user_role: {
            data: {
              id: '1',
              type: 'user_roles'
            }
          },
          devices: {
            data: []
          },
          profile: {
            data: {
              id: '2',
              type: 'profiles'
            }
          }
        }
      },
      {
        id: '4',
        type: 'users',
        attributes: {
          uuid: 'asdf',
          email: 'somethingelse@something.com',
          created_at: '2018-07-08T18:40:21Z',
          updated_at: '2018-07-23T17:32:13Z',
          invitation_sent_at: '2018-07-08T18:40:21Z',
          invitation_accepted_at: '2018-07-08T18:40:59Z',
          disabled_at: null
        },
        relationships: {
          user_role: {
            data: {
              id: '1',
              type: 'user_roles'
            }
          },
          devices: {
            data: []
          },
          profile: {
            data: {
              id: '8',
              type: 'profiles'
            }
          }
        }
      }
    ],
    links: {
      self: 'https://core-development.clinical6.com/v3/versions?filters%5Bcreated_at%5D=2018-07-08&page%5Bnumber%5D=200&page%5Bsize%5D=10',
      first: 'https://core-development.clinical6.com/v3/versions?filters%5Bcreated_at%5D=2018-07-08&page%5Bnumber%5D=1&page%5Bsize%5D=10',
      prev: 'https://core-development.clinical6.com/v3/versions?filters%5Bcreated_at%5D=2018-07-08&page%5Bnumber%5D=199&page%5Bsize%5D=10',
      next: 'https://core-development.clinical6.com/v3/versions?filters%5Bcreated_at%5D=2018-07-08&page%5Bnumber%5D=201&page%5Bsize%5D=10',
      last: 'https://core-development.clinical6.com/v3/versions?filters%5Bcreated_at%5D=2018-07-08&page%5Bnumber%5D=5865&page%5Bsize%5D=10'
    },
    meta: {
      total_records: 58646,
      total_pages: 5865
    }
  };
});

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

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

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

/**
 * @test {Clinical6.get}
 */
test('[unit] VersionService.get 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/versions`).reply(function (uri, requestBody) {
    request = this.req;
    request.requestBody = requestBody;
    return [200, getResponseAll];
  });
  const meta = {};
  const response = await clinical6.get(Version, { meta });
  t.is(request.path, `/v3/versions`);
  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, 10);
  t.is(response[0].id, 1991);
  t.is(response[0].type, 'versions');
  t.is(response[1].id, 1992);
  t.is(response[1].type, 'versions');
  t.is(response[2].id, 1993);
  t.is(response[2].type, 'versions');
  t.is(response.meta.totalRecords, 58646);
  t.is(response.meta.totalPages, 5865);
  t.is(meta.totalRecords, 58646);
  t.is(meta.totalPages, 5865);
});