Manual Reference Source Test

test/unit/services.version.js

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

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

  t.context.getResponseAll = {
    data: [
      {
        id: 1,
        type: 'versions',
        attributes: {
          category: 'Module',
          created_at: '2018-08-02T15:54:17Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            access_class: [
              null,
              'Version'
            ]
          },
          reason_for_changes: {},
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 133,
              type: 'sections'
            }
          }
        }
      },
      {
        id: 2,
        type: 'versions',
        attributes: {
          category: 'User Personal Information',
          created_at: '2018-08-02T15:54:17Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            first_name: [
              null,
              'Maryam'
            ],
            last_name: [
              null,
              'Grimes'
            ],
            gender: [
              null,
              'male'
            ],
            age: [
              null,
              32
            ],
            ethnicity: [
              null,
              0
            ],
            avatar: [
              '{url=\\u003enil}',
              '{url=\\u003e/uploads/tmp/1533225257-2953-0335-3841/1.png, small=\\u003e{url=\\u003e/uploads/tmp/1533225257-2953-0335-3841/small_1.png}, main=\\u003e{url=\\u003e/uploads/tmp/1533225257-2953-0335-3841/main_1.png}, thumb=\\u003e{url=\\u003e/uploads/tmp/1533225257-2953-0335-3841/thumb_1.png}}'
            ],
            language_id: [
              null,
              1
            ],
            timezone: [
              null,
              'UTC'
            ]
          },
          reason_for_changes: {},
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 272,
              type: 'profiles'
            }
          }
        }
      },
      {
        id: 3,
        type: 'versions',
        attributes: {
          category: 'User Roles',
          created_at: '2018-08-02T15:54:17Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            is_default: [
              false,
              true
            ],
            name: [
              null,
              'Standard'
            ],
            permanent_link: [
              null,
              'standard'
            ]
          },
          reason_for_changes: {},
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 113,
              type: 'user_roles'
            }
          }
        }
      },
      {
        id: 4,
        type: 'versions',
        attributes: {
          category: 'Brand',
          created_at: '2018-08-02T15:54:17Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            name: [
              null,
              'sixappeal'
            ]
          },
          reason_for_changes: {
            name: "Participant's spelling error",
          },
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 96,
              name: 'sixappeal',
              created_at: '2018-08-02T15:54:17Z',
              updated_at: '2018-08-02T15:54:17Z',
              logo: {
                url: '/default/default.png',
                small: {
                  url: '/default/small_default.png'
                },
                small_hd: {
                  url: '/default/small_hd_default.png'
                },
                fullscreen: {
                  url: '/default/fullscreen_default.png'
                },
                main: {
                  url: '/default/main_default.png'
                },
                fullscreen_hd: {
                  url: '/default/fullscreen_hd_default.png'
                },
                main_hd: {
                  url: '/default/main_hd_default.png'
                },
                thumb: {
                  url: '/default/thumb_default.png'
                }
              },
              description: null,
              position: 1,
              ancestry: null
            }
          }
        }
      },
      {
        id: 5,
        type: 'versions',
        attributes: {
          category: 'User Account Details',
          created_at: '2018-08-02T15:54:17Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            email: [
              null,
              'user258@fake.com'
            ],
            username: [
              null,
              'dummy_863'
            ],
            encrypted_password: [
              null,
              '$2a$04$5zQgzt/XwuxeksGiHEgrGOmpS/DzP32ddluOEKjZOdltcOIgcigfq'
            ],
            pin: [
              null,
              123456
            ],
            uuid: [
              null,
              null
            ],
            authentication_token: [
              null,
              '40f2bed4c78ef7c7cc144b9f747f966e3b9fa2f1df402a24213bbf351821977e'
            ],
            authentication_token_expired_at: [
              null,
              '2018-08-02T16:09:17Z'
            ],
            allow_authentication_token_refresh_until: [
              null,
              '2018-08-02T21:54:17Z'
            ],
            user_role_id: [
              null,
              113
            ],
            brand_id: [
              null,
              96
            ],
            password_changed_at: [
              null,
              '2018-08-02T15:54:17Z'
            ],
            confirmation_token: [
              null,
              76009529
            ],
            confirmation_sent_at: [
              null,
              '2018-08-02T15:54:17Z'
            ]
          },
          reason_for_changes: {},
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 73,
              type: 'users'
            }
          }
        }
      },
      {
        id: 6,
        type: 'versions',
        attributes: {
          category: 'User Role Permissions',
          created_at: '2018-08-02T15:54:17Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            user_role_id: [
              null,
              113
            ],
            authorizable_id: [
              null,
              133
            ],
            authorizable_type: [
              null,
              'Section'
            ]
          },
          reason_for_changes: {},
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 139,
              type: 'permissions'
            }
          }
        }
      },
      {
        id: 7,
        type: 'versions',
        attributes: {
          category: 'User Account Details',
          created_at: '2018-08-02T15:54:17Z',
          event: 'update',
          ip: null,
          original_object: {
            email: 'user258@fake.com',
            encrypted_password: '$2a$04$5zQgzt/XwuxeksGiHEgrGOmpS/DzP32ddluOEKjZOdltcOIgcigfq',
            reset_password_token: null,
            reset_password_sent_at: null,
            remember_created_at: null,
            confirmation_token: 76009529,
            confirmed_at: null,
            confirmation_sent_at: '2018-08-02 15:54:17',
            unconfirmed_email: null,
            failed_attempts: 0,
            unlock_token: null,
            locked_at: null,
            username: 'dummy_863',
            pin: 123456,
            invitation_token: null,
            invitation_sent_at: null,
            invitation_accepted_at: null,
            invitation_limit: null,
            invited_by_id: null,
            invited_by_type: null,
            brand_id: 96,
            password_changed_at: '2018-08-02 15:54:17',
            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: 113,
            invitation_created_at: null,
            uuid: {
              value: '946fb318-8c4f-4efd-9279-4a840a62c4cc'
            },
            authentication_token: '40f2bed4c78ef7c7cc144b9f747f966e3b9fa2f1df402a24213bbf351821977e',
            disabled_at: null,
            authentication_token_expired_at: '2018-08-02 16:09:17',
            allow_authentication_token_refresh_until: '2018-08-02 21:54:17',
            sso_identifier: null
          },
          object_changes: {
            authentication_token_expired_at: [
              '2018-08-02T16:09:17Z',
              '2018-08-02T16:09:17Z'
            ]
          },
          reason_for_changes: {},
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 73,
              type: 'users'
            }
          }
        }
      },
      {
        id: 8,
        type: 'versions',
        attributes: {
          category: 'User token authentication event',
          created_at: '2018-08-02T15:54:17Z',
          event: 'create',
          ip: null,
          original_object: {},
          object_changes: {
            type: [
              null,
              'UserTokenAuthenticationEvent'
            ],
            identifier: [
              null,
              'user258@fake.com'
            ],
            result: [
              null,
              'success'
            ],
            ip: [
              null,
              '127.0.0.1'
            ]
          },
          reason_for_changes: {},
          user_agent: null
        },
        relationships: {
          whodunnitable: {
            data: null
          },
          item: {
            data: {
              id: 66,
              identifier: 'user258@fake.com',
              result: 'success',
              created_at: '2018-08-02T15:54:17Z',
              updated_at: '2018-08-02T15:54:17Z',
              ip: '127.0.0.1'
            }
          }
        }
      }
    ],
    included: [
      {
        id: 133,
        type: 'sections',
        attributes: {
          access_class: 'Version',
          name: 'Version'
        }
      },
      {
        id: 272,
        type: 'profiles',
        attributes: {
          created_at: '2018-08-02T15:54:17Z',
          updated_at: '2018-08-02T15:54:17Z',
          first_name: 'Maryam',
          middle_initial: null,
          last_name: 'Grimes',
          prefix: null,
          suffix: null,
          gender: 'male',
          ethnicity: 0,
          occupation: null,
          avatar: {
            url: '/uploads/test/profile/avatar/272/1.png',
            small: {
              url: '/uploads/test/profile/avatar/272/small_1.png'
            },
            main: {
              url: '/uploads/test/profile/avatar/272/main_1.png'
            },
            thumb: {
              url: '/uploads/test/profile/avatar/272/thumb_1.png'
            }
          },
          username: null,
          dob: null,
          timezone: 'UTC',
          phone: null,
          email: 'user258@fake.com',
          age: 32
        },
        relationships: {
          profileable: {
            data: {
              id: 73,
              type: 'users'
            }
          },
          language: {
            data: {
              id: 1,
              type: 'languages'
            }
          }
        }
      },
      {
        id: 113,
        type: 'user_roles',
        attributes: {
          permanent_link: 'standard',
          name: 'Standard',
          is_super: false,
          is_admin: false,
          is_mobile: false,
          is_default: true,
          description: null
        }
      },
      {
        id: 73,
        type: 'users',
        attributes: {
          uuid: '946fb318-8c4f-4efd-9279-4a840a62c4cc',
          email: 'user258@fake.com',
          created_at: '2018-08-02T15:54:17Z',
          updated_at: '2018-08-02T15:54:17Z',
          invitation_sent_at: null,
          invitation_accepted_at: null,
          disabled_at: null
        },
        relationships: {
          user_role: {
            data: {
              id: 113,
              type: 'user_roles'
            }
          },
          devices: {
            data: []
          },
          profile: {
            data: {
              id: 272,
              type: 'profiles'
            }
          }
        }
      },
      {
        id: 139,
        type: 'permissions',
        attributes: {
          enabled: true,
          scope_name: null
        },
        relationships: {
          allowed_actions: {
            data: []
          },
          authorizable: {
            data: {
              id: 133,
              type: 'sections'
            }
          }
        }
      }
    ]
  };

  /*
  t.context.versionJsonApi = {
    data: {
      type: 'consent__strategies',
      attributes: {
        name: 'A form name',
        strategy_type: 'paper'
      },
      relationships: {
        cohort: {
          data: {
            id: 21,
            type: 'cohorts'
          }
        }
      }
    }
  }; */
});

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

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  t.context.version = new Version(t.context.versionJsonApi);
});

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

/**
* @test {Clinical6.get}
*/
test('[unit] VersionService.get should make a properly formatted get request', 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];
  });
  await clinical6.get(Version);
  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');
});

/**
* @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;
  nock(client.apiBaseUrl).get(`/v3/versions`).reply(200, getResponseAll);
  const response = await clinical6.get(Version);
  t.truthy(response);
  t.is(Object.keys(response).length, 8);

  t.is(response[0].id, 1);
  t.is(response[0].type, 'versions');
  t.is(response[0].category, 'Module');
  t.is(response[0].createdAt, '2018-08-02T15:54:17Z');
  t.is(response[0].event, 'create');
  t.is(response[0].ip, null);
  t.deepEqual(response[0].originalObject, {});
  t.is(response[0].objectChanges.accessClass[0], null);
  t.is(response[0].objectChanges.accessClass[1], 'Version');
  t.is(response[0].userAgent, null);
  t.is(response[0].user, null);
  t.is(response[0].item.id, 133);
  t.is(response[0].item.type, 'sections');

  t.is(response[3].id, 4);
  t.is(response[3].type, 'versions');
  t.is(response[3].category, 'Brand');
  t.is(response[3].createdAt, '2018-08-02T15:54:17Z');
  t.is(response[3].event, 'create');
  t.is(response[3].ip, null);
  t.deepEqual(response[3].originalObject, {});
  t.is(response[3].objectChanges.name[0], null);
  t.is(response[3].objectChanges.name[1], 'sixappeal');
  t.is(response[3].userAgent, null);
  t.is(response[3].user, null);
  t.is(response[3].item.id, 96);
  t.is(response[3].item.name, 'sixappeal');
  t.is(response[3].item.createdAt, '2018-08-02T15:54:17Z');
  t.is(response[3].item.updatedAt, '2018-08-02T15:54:17Z');
  t.is(response[3].item.logo.small.url, '/default/small_default.png');

  t.is(response[6].id, 7);
  t.is(response[6].type, 'versions');
  t.is(response[6].category, 'User Account Details');
  t.is(response[6].createdAt, '2018-08-02T15:54:17Z');
  t.is(response[6].event, 'update');
  t.is(response[6].ip, null);
  t.is(response[6].originalObject.email, 'user258@fake.com');
  t.is(response[6].originalObject.encryptedPassword, '$2a$04$5zQgzt/XwuxeksGiHEgrGOmpS/DzP32ddluOEKjZOdltcOIgcigfq');
  t.is(response[6].originalObject.resetPasswordToken, null);
  t.is(response[6].originalObject.uuid.value, '946fb318-8c4f-4efd-9279-4a840a62c4cc');
  t.is(response[6].objectChanges.authenticationTokenExpiredAt[0], '2018-08-02T16:09:17Z');
  t.is(response[6].objectChanges.authenticationTokenExpiredAt[1], '2018-08-02T16:09:17Z');
  t.is(response[6].userAgent, null);
  t.is(response[6].user, null);
  t.is(response[6].item.id, 73);
  t.is(response[6].item.type, 'users');
});