test/unit/helpers.role.js
import test from 'ava';
import sinon from 'sinon';
import {
client,
AllowedAction,
Role,
Section,
} from '../../src';
// See tests.js for testing client creation
const roleJsonApi = {
id: 1,
type: 'user_roles',
attributes: {
permanent_link: 'dummy_459',
name: 'aut',
is_super: false,
is_admin: false,
is_mobile: false,
is_default: false,
description: 'Super Role Level1',
can_view_pii: false
}
};
const roleJson = {
id: '1',
permanent_link: 'dummy_459',
name: 'aut',
is_super: false,
is_admin: false,
is_mobile: false,
is_default: false,
description: 'Super Role Level1',
can_view_pii: false
};
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.server = sinon.fakeServer.create();
t.context.server.autoRespond = true;
t.context.server.respondWith('PATCH', `${client.apiBaseUrl}/v3/user_roles/*`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: '126',
type: 'user_roles',
attributes: {
permanent_link: 'dummy_1559',
name: 'assumenda',
is_super: false,
is_admin: false,
is_mobile: false,
is_default: false,
description: 'Super Role Level1'
}
}
})]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/user_roles/259/invitable_user_roles`,
[201, { 'Content-Type': 'application/json' }, JSON.stringify({
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,
can_view_pii: 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
}
}
]
})]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/user_roles/17/permissions`,
[201, { 'Content-Type': 'application/json' }, JSON.stringify({
data: [
{
id: 21,
type: 'permissions',
attributes: {
enabled: true,
scope_name: null
},
relationships: {
allowed_actions: {
data: [
{
id: 3,
type: 'allowed_actions'
},
{
id: 4,
type: 'allowed_actions'
}
]
},
authorizable: {
data: {
id: 16,
type: 'sections'
}
}
}
}
],
included: [
{
id: 3,
type: 'allowed_actions',
attributes: {
name: 'read'
}
},
{
id: 4,
type: 'allowed_actions',
attributes: {
name: 'create'
}
},
{
id: 16,
type: 'sections',
attributes: {
access_class: 'MobileUser'
}
}
]
})]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/v3/authorizables`,
[201, { 'Content-Type': 'application/json' }, JSON.stringify({
data: [
{
id: 1,
type: 'data_collection__flow_processes',
attributes: {
name: 'occaecati',
permanent_link: 'dummy_9',
consent_credentials: null,
created_at: '2018-02-23T01:07:09Z',
updated_at: '2018-02-23T01:07:09Z',
owner_type: 'MobileUser',
published_at: null,
conditional_paths: null
},
relationships: {
linked_steps: {
data: []
},
published: {
data: null
},
draft: {
data: null
},
initial_step: {
data: null
}
}
},
{
id: 2,
type: 'data_collection__flow_processes',
attributes: {
name: 'occaecati',
permanent_link: 'dummy_10',
consent_credentials: null,
created_at: '2018-02-23T01:07:10Z',
updated_at: '2018-02-23T01:07:10Z',
owner_type: 'MobileUser',
published_at: null,
conditional_paths: null
},
relationships: {
linked_steps: {
data: []
},
published: {
data: null
},
draft: {
data: null
},
initial_step: {
data: null
}
}
},
{
id: 3,
type: 'data_collection__flow_processes',
attributes: {
name: 'occaecati',
permanent_link: 'dummy_11',
consent_credentials: null,
created_at: '2018-02-23T01:07:10Z',
updated_at: '2018-02-23T01:07:10Z',
owner_type: 'MobileUser',
published_at: null,
conditional_paths: null
},
relationships: {
linked_steps: {
data: []
},
published: {
data: null
},
draft: {
data: null
},
initial_step: {
data: null
}
}
},
{
id: 1,
type: 'dynamic_content__content_types',
attributes: {
name: 'dummy_3',
permanent_link: 'dummy_4',
description: null,
created_at: '2018-02-23T01:07:09Z',
updated_at: '2018-02-23T01:07:09Z'
},
relationships: {
dynamic_attributes: {
data: []
}
}
},
{
id: 2,
type: 'dynamic_content__content_types',
attributes: {
name: 'dummy_5',
permanent_link: 'dummy_6',
description: null,
created_at: '2018-02-23T01:07:09Z',
updated_at: '2018-02-23T01:07:09Z'
},
relationships: {
dynamic_attributes: {
data: []
}
}
},
{
id: 3,
type: 'dynamic_content__content_types',
attributes: {
name: 'dummy_7',
permanent_link: 'dummy_8',
description: null,
created_at: '2018-02-23T01:07:09Z',
updated_at: '2018-02-23T01:07:09Z'
},
relationships: {
dynamic_attributes: {
data: []
}
}
},
{
id: 1,
type: 'sections',
attributes: {
access_class: 'dummy_1'
}
},
{
id: 2,
type: 'sections',
attributes: {
access_class: 'dummy_2'
}
}
]
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/user_roles`,
[201, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: '133',
type: 'user_roles',
attributes: {
permanent_link: 'dummy_1567',
name: 'assumenda',
is_super: false,
is_admin: false,
is_mobile: false,
is_default: false,
description: 'Super Role Level1'
}
}
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/v3/permissions`,
[201, { 'Content-Type': 'application/json' }, JSON.stringify({
data: {
id: '914',
type: 'permissions',
attributes: {
enabled: false,
scope_name: null
},
relationships: {
allowed_actions: {
data: []
},
authorizable: {
data: {
id: '117',
type: 'sections'
}
}
}
}
})]);
t.context.storage = client.storageUtility;
client.role = new Role({});
t.context.roleJsonApi = {
data: {
type: 'user_roles',
attributes: {
permanent_link: 'dummy_1567',
name: 'assumenda',
is_super: true
}
}
};
t.context.role = new Role(t.context.roleJsonApi);
});
test.afterEach(t => t.context.server.restore());
/**
* @test {Role}
*/
test('[unit] Role should handle role data with a normal json format', (t) => {
const role = new Role(roleJson);
t.is(role.id, 1);
t.is(role.type, 'user_roles');
t.is(role.isSuper, false);
t.is(role.isAdmin, false);
t.is(role.isMobile, false);
t.is(role.isDefault, false);
t.is(role.description, 'Super Role Level1');
});
/**
* @test {Role}
*/
test('[unit] Role should handle device data with json api format', (t) => {
const role = new Role({ data: roleJsonApi });
t.is(role.id, 1);
t.is(role.type, 'user_roles');
t.is(role.isSuper, false);
t.is(role.isAdmin, false);
t.is(role.isMobile, false);
t.is(role.isDefault, false);
t.is(role.description, 'Super Role Level1');
});
/**
* @test {Role}
*/
test('[unit] Role should generate json api format when converted to string', (t) => {
let role = new Role({ data: roleJsonApi });
t.deepEqual(role.toJSON(), roleJsonApi);
role = new Role({ data: roleJsonApi });
t.deepEqual(role.toJSON(), roleJsonApi);
});
/**
* @test {Role.getInvitableRoles}
*/
test('[unit] Role.getInvitableRoles should successfully get the invitable user roles', async (t) => {
const json = JSON.parse(JSON.stringify(roleJsonApi));
json.id = 259;
const role = new Role({ data: json });
const response = await role.getInvitableRoles();
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/v3/user_roles/259/invitable_user_roles`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response[0].id, 260);
t.is(response[0].type, 'user_roles');
t.is(response[0].canViewPii, false);
});
/**
* @test {Role.getPermissions}
*/
test('[unit] Role.getPermissions should successfully get the role permissions', async (t) => {
const json = JSON.parse(JSON.stringify(roleJsonApi));
json.id = 17;
const role = new Role({ data: json });
const response = await role.getPermissions();
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/v3/user_roles/17/permissions`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response[0].id, 21);
t.is(response[0].enabled, true);
t.is(response[0].scopeName, null);
t.is(response[0].allowedActions.length, 2);
t.is(response[0].authorizable.id, 16);
t.is(response[0].authorizable.type, 'sections');
t.true(response[0].allowedActions[0] instanceof AllowedAction);
t.is(response[0].allowedActions[0].id, 3);
t.is(response[0].allowedActions[0].name, 'read');
t.true(response[0].allowedActions[1] instanceof AllowedAction);
t.is(response[0].allowedActions[1].id, 4);
t.is(response[0].allowedActions[1].name, 'create');
t.true(response[0].authorizable instanceof Section);
t.is(response[0].authorizable.id, 16);
t.is(response[0].authorizable.type, 'sections');
});
/**
* @test {Role.save}
*/
test('[unit] Role.save should successfully insert a role with a role object when id does not exist', async (t) => {
const json = JSON.parse(JSON.stringify(roleJsonApi));
delete json.id;
const role = new Role({ data: json });
const response = await role.save();
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/v3/user_roles`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.deepEqual(JSON.parse(request.requestBody), { data: json });
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.id, 133);
t.is(response.type, 'user_roles');
t.is(role.description, 'Super Role Level1');
});
/**
* @test {Role.save}
*/
test('[unit] Role.save should successfully update a role with a role object when id exists', async (t) => {
const role = new Role({ data: roleJsonApi });
const response = await role.save();
// const request = t.context.server.requests[0];
// t.is(request.method, 'PATCH');
// t.is(request.url, `${client.apiBaseUrl}/v3/user_roles/${role.id}`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.deepEqual(JSON.parse(request.requestBody), { data: roleJsonApi });
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.id, 126);
t.is(response.type, 'user_roles');
t.is(role.description, 'Super Role Level1');
});