Manual Reference Source Test

test/unit/services.role.js

import test from 'ava';
import nock from 'nock';
import {
  client,
  roleService,
  Permission,
  Role,
} from '../../src';

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

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

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

  nock(client.apiBaseUrl).delete(/\/v3\/permissions\/(\d*)$/).reply(204, {});

  nock(client.apiBaseUrl).get(`/v3/user_roles`).reply(200, {
    data: [
      {
        id: '202',
        type: 'user_roles',
        attributes: {
          permanent_link: 'superuser',
          updated_at: 'SuperUser',
          is_super: true,
          is_admin: false,
          is_mobile: false,
          is_default: false,
          description: 'Super Role Level1'
        }
      },
      {
        id: '203',
        type: 'user_roles',
        attributes: {
          permanent_link: 'dummy_3509',
          updated_at: 'ad',
          is_super: false,
          is_admin: false,
          is_mobile: false,
          is_default: false,
          description: 'Super Role Level1'
        }
      },
      {
        id: '204',
        type: 'user_roles',
        attributes: {
          permanent_link: 'dummy_3510',
          updated_at: 'ad',
          is_super: false,
          is_admin: false,
          is_mobile: false,
          is_default: false,
          description: 'Super Role Level1'
        }
      },
      {
        id: '205',
        type: 'user_roles',
        attributes: {
          permanent_link: 'dummy_3511',
          updated_at: 'ad',
          is_super: false,
          is_admin: false,
          is_mobile: false,
          is_default: false,
          description: 'Super Role Level1'
        }
      },
    ]
  });

  nock(client.apiBaseUrl).get(/\/v3\/user_roles\/(\d*)$/).reply(200, {
    data: {
      id: '218',
      type: 'user_roles',
      attributes: {
        permanent_link: 'dummy_3524',
        name: 'ad',
        is_super: false,
        is_admin: false,
        is_mobile: false,
        is_default: false,
        description: 'Super Role Level1',
        can_view_pii: false
      }
    }
  });

  nock(client.apiBaseUrl).get(`/v3/user_roles/259/invitable_user_roles`).reply(200, {
    data: [
      {
        id: 260,
        type: 'user_roles',
        attributes: {
          permanent_link: 'dummy_3737',
          name: 'Patient',
          is_super: false,
          is_admin: false,
          is_mobile: false,
          is_default: false
        }
      },
      {
        id: 259,
        type: 'user_roles',
        attributes: {
          permanent_link: 'dummy_3736',
          name: 'Doctor',
          is_super: false,
          is_admin: false,
          is_mobile: false,
          is_default: false
        }
      }
    ]
  });

  nock(client.apiBaseUrl).post(`/v3/permissions`).reply(201, {
    data: {
      id: 914,
      type: 'permissions',
      attributes: {
        enabled: false
      },
      relationships: {
        user_role: {
          data: { id: 6, type: 'mobile_users' }
        },
        authorizable: {
          data: {
            id: 177,
            type: 'sections'
          }
        }
      }
    }
  });

  t.context.storage = client.storageUtility;
  client.role = new Role({});
  t.context.roleJsonApi = {
    data: {
      type: 'user_roles',
      attributes: {
        permanent_link: 'dummy_3534',
        name: 'ad'
      }
    }
  };
  t.context.role = new Role(t.context.roleJsonApi);

  t.context.storage = client.storageUtility;
  client.permission = new Permission({});
  /*
  t.context.permissionJsonApi = {
    data: {
      type: 'permissions',
      attributes: {
        enabled: false,
        scope_name: 'scope_name: DataCollection::FlowProcess'
      },
      relationships: {
        user_role: {
          data: {
            id: 180,
            type: 'user_roles'
          }
        },
        authorizable: {
          data: {
            id: 83,
            type: 'DataCollection::FlowProcess'
          }
        }
      }
    }
  };
*/
  t.context.permission = new Permission(t.context.permissionJsonApi);
});

// test.afterEach(t => t.context.server.restore());

// RoleService.delete method
/**
 * @test {RoleService.delete}
 */
test('[unit] RoleService.delete should receive a valid response for a delete request', async (t) => {
  const { role } = t.context;
  role.id = 5;
  const expectedError = 'RoleService.delete error:  is not supported';
  await t.throwsAsync(roleService.delete(role), expectedError);
});

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

// /**
//  * @test {RoleService.get}
//  */
// test('[unit] RoleService.get should make a properly formatted get request', async (t) => {
//   await roleService.get();
//   t.is(request.path, `/v3/user_roles`);
//   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 {RoleService.get}
 */
test('[unit] RoleService.get should receive a valid response for a get request without an id', async (t) => {
  const response = await roleService.get();
  t.truthy(response);
  t.is(response.length, 4);
});

/**
 * @test {RoleService.get}
 */
test('[unit] RoleService.get should receive a valid response for a get request with an id', async (t) => {
  const response = await roleService.get({ id: 218 });
  t.truthy(response);
  t.is(response.id, 218);
  t.is(response.type, 'user_roles');
  t.is(response.permanentLink, 'dummy_3524');
  t.is(response.name, 'ad');
  t.is(response.isSuper, false);
  t.is(response.isAdmin, false);
  t.is(response.isMobile, false);
  t.is(response.isDefault, false);
  t.is(response.description, 'Super Role Level1');
  t.is(response.canViewPii, false);
});

// RoleService.getInvitableRoles method
/**
 * @test {RoleService.getInvitableRoles}
 */
test.serial('[unit] RoleService.getInvitableRoles should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const expectedError = 'RoleService.getInvitableRoles error: requires authToken';
  await t.throwsAsync(roleService.getInvitableRoles({ id: 259 }), expectedError);
});

/**
 * @test {RoleService.getInvitableRoles}
 */
test('[unit] RoleService.getInvitableRoles should throw an error when there is no role', async (t) => {
  client.authToken = 'token';
  const expectedError = 'RoleService.getInvitableRoles error: role is not defined';
  await t.throwsAsync(roleService.getInvitableRoles(), expectedError);
});


// /**
//  * @test {RoleService.getInvitableRoles}
//  */
// test('[unit] RoleService.getInvitableRoles should make a properly formatted get request', async (t) => {
//   await roleService.getInvitableRoles({ id: 259 });
//   t.is(request.path, `/v3/user_roles/259/invitable_user_roles`);
//   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 {RoleService.getInvitableRoles}
 */

test('[unit] RoleService.getInvitableRoles should receive a valid response for a get request with a role', async (t) => {
  const response = await roleService.getInvitableRoles({ id: 259 });
  t.truthy(response);
  t.is(response.length, 2);
  t.is(response[0].id, 260);
  t.is(response[0].type, 'user_roles');
  t.is(response[0].permanentLink, 'dummy_3737');
  t.is(response[0].name, 'Patient');
  t.is(response[0].isSuper, false);
  t.is(response[0].isAdmin, false);
  t.is(response[0].isMobile, false);
  t.is(response[0].isDefault, false);
  t.is(response[1].id, 259);
  t.is(response[1].type, 'user_roles');
  t.is(response[1].permanentLink, 'dummy_3736');
  t.is(response[1].name, 'Doctor');
  t.is(response[1].isSuper, false);
  t.is(response[1].isAdmin, false);
  t.is(response[1].isMobile, false);
  t.is(response[1].isDefault, false);
});

// RoleService.insert method
/**
 * @test {RoleService.insert}
 */
test.serial('[unit] RoleService.insert should throw an error when there is no authToken', async (t) => {
  client.authToken = undefined;
  const expectedError = 'RoleService.insert error: requires authToken and role does not have name and role does not have permanentLink';
  await t.throwsAsync(roleService.insert({}), expectedError);
});

/**
 * @test {RoleService.insert}
 */
test('[unit] RoleService.insert should successfully insert a role with a role object', async (t) => {
  nock(client.apiBaseUrl).post(`/v3/user_roles`).reply(201, {
    data: {
      id: '16',
      type: 'user_roles',
      attributes: {
        permanent_link: 'dummy_3524',
        name: 'ad',
        is_super: false,
        is_admin: false,
        is_mobile: false,
        is_default: false,
        description: 'Super Role Level1'
      }
    }
  });

  const requestJsonApi = {
    data: {
      type: 'user_roles',
      attributes: {
        permanent_link: 'dummy_3534',
        name: 'ad'
      }
    }
  };

  // client.mobileApplicationKey = '0355b2fb1c8fa7210bec203abb32b916';
  const role = new Role(requestJsonApi);
  const response = await roleService.insert(role);

  // t.is(request.path, `/v3/user_roles`);
  // t.is(request.headers.accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify(requestJsonApi));
  // t.is(request.headers['content-type'], 'application/json');
  // t.is(request.headers.authorization, 'Token token=valid_token');
  t.is(response.id, 16);
  t.is(response.type, 'user_roles');
  t.is(response.name, 'ad');
  t.is(response.description, 'Super Role Level1');
});

// RoleService.update method
/**
 * @test {RoleService.update}
 */
test('[unit] RoleService.update should throw errors for invalid parameters', async (t) => {
  const undefinedError = 'RoleService.update error: role is not defined';
  await t.throwsAsync(roleService.update(), undefinedError);
});

/**
 * @test {RoleService.update}
 */
test('[unit] RoleService.update should successfully update a role with a role object', async (t) => {
  const { role } = t.context; // roleJsonApi
  nock(client.apiBaseUrl).patch(`/v3/user_roles/${role.id}`).reply(200, {
    data: {
      id: '227',
      type: 'user_roles',
      attributes: {
        permanent_link: 'dummy_3534',
        name: 'ad',
        is_super: false,
        is_admin: false,
        is_mobile: false,
        is_default: false,
        description: 'Super Role Level1'
      }
    }
  });
  const response = await roleService.update(role);

  // t.is(request.path, `/v3/user_roles/${role.id}`);
  // t.is(request.headers.accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify(roleJsonApi));
  // t.is(request.headers['content-type'], 'application/json');
  // t.is(request.headers.authorization, 'Token token=valid_token');
  t.is(response.id, 227);
  t.is(response.type, 'user_roles');
  t.is(response.description, 'Super Role Level1');
});

// test.afterEach(t => t.context.server.restore());

// RoleService.deletePermission method
/**
 * @test {RoleService.deletePermission}
 */
test('[unit] RoleService.deletePermission should throw errors for invalid parameters', async (t) => {
  const undefinedError = 'RoleService.deletePermission error: permission is not defined';
  await t.throwsAsync(roleService.deletePermission(), undefinedError);
});

// /**
//  * @test {RoleService.deletePermission}
//  */
// test('[unit] RoleService.deletePermission should make a properly formatted delete request', async (t) => {
//   const permission = t.context.permission;
//   permission.id = 5;
//   await roleService.deletePermission(permission);
//   t.is(request.path, `/v3/permissions/${permission.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');
// });

/**
 * @test {RoleService.deletePermission}
 */
test('[unit] RoleService.deletePermission should receive a valid response for a delete request', async (t) => {
  const { permission } = t.context;
  permission.id = 5;
  const response = await roleService.deletePermission(permission);
  // t.is(request.headers.authorization, 'Token token=valid_token');
  t.truthy(response);
});

/**
 * @test {RoleService.deletePermission}
 */
test('[unit] RoleService.deletePermission should remove the element from local storage', async (t) => {
  const { permission, storage } = t.context;
  permission.id = 5;
  await storage.set('permissions', permission.toJSON(), { id: permission.id });
  await roleService.deletePermission(permission);
  t.is(storage.has('permissions', { id: permission.id }), false);
});