test/unit/helpers.user.mobile.js
import test from 'ava';
import nock from 'nock';
import {
client,
AvailableStrategy,
Cohort,
CohortAssignment,
ConsentGrant,
Language,
Profile,
Role,
Site,
User,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
t.context.getResponseGrants454 = {
data: [
{
id: 3,
type: 'consent__grants',
attributes: {
document: {
url: '/uploads/test/consent/grant/document/3/1.pdf'
},
progress: 'all_signed',
created_at: '2018-07-20T13:32:51Z',
updated_at: '2018-07-20T13:32:51Z'
},
relationships: {
granted_for: {
data: {
id: 454,
type: 'mobile_users'
}
},
granted_by: {
data: {
id: 454,
type: 'mobile_users'
}
},
strategy: {
data: {
id: 62,
type: 'consent__strategies'
}
},
form_version: {
data: null
},
agreement: {
data: null
}
}
},
{
id: 4,
type: 'consent__grants',
attributes: {
sign_url: null,
progress: 'all_signed',
created_at: '2018-07-20T13:32:51Z',
updated_at: '2018-07-20T13:32:51Z'
},
relationships: {
granted_for: {
data: {
id: 454,
type: 'mobile_users'
}
},
granted_by: {
data: {
id: 454,
type: 'mobile_users'
}
},
strategy: {
data: {
id: 63,
type: 'consent__strategies'
}
},
form_version: {
data: {
id: 37,
type: 'consent__form_versions'
}
},
agreement: {
data: {
id: 18,
type: 'agreement__agreements'
}
}
}
}
],
included: [
{
id: 62,
type: 'consent__strategies',
attributes: {
name: 'Strategy-67',
strategy_type: 'paper',
created_at: '2018-07-20T13:32:51Z',
updated_at: '2018-07-20T13:32:51Z'
},
relationships: {
cohort: {
data: {
id: 105,
type: 'cohorts'
}
},
forms: {
data: []
}
}
},
{
id: 63,
type: 'consent__strategies',
attributes: {
name: 'Strategy-68',
strategy_type: 'electronic',
created_at: '2018-07-20T13:32:51Z',
updated_at: '2018-07-20T13:32:51Z'
},
relationships: {
cohort: {
data: {
id: 106,
type: 'cohorts'
}
},
forms: {
data: [
{
id: 51,
type: 'consent__forms'
}
]
}
}
},
{
id: 37,
type: 'consent__form_versions',
attributes: {
archived_at: null
},
relationships: {
site: {
data: {
id: 115,
type: 'trials__sites'
}
},
agreement_template: {
data: {
id: 102,
type: 'agreement__templates'
}
},
language: {
data: {
id: 63,
type: 'languages'
}
},
consent_form: {
data: {
id: 51,
type: 'consent__forms'
}
},
approver_groups: {
data: []
},
approvers: {
data: []
}
}
},
{
id: 51,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'Form 54',
created_at: '2018-07-20T13:32:51Z',
updated_at: '2018-07-20T13:32:51Z'
},
relationships: {
consent_form_versions: {
data: [
{
id: 37,
type: 'consent__form_versions'
}
]
},
strategy: {
data: {
id: 63,
type: 'consent__strategies'
}
}
}
}
],
links: {},
meta: {
total_records: 2,
total_pages: 1
}
};
t.context.getResponseStrategies77 = {
data: [
{
id: 7,
type: 'consent__available_strategies',
relationships: {
strategy: {
data: {
id: 7,
type: 'consent__strategies'
}
},
forms: {
data: [
{
id: 5,
type: 'consent__forms'
},
{
id: 6,
type: 'consent__forms'
}
]
}
}
},
{
id: 8,
type: 'consent__available_strategies',
relationships: {
strategy: {
data: {
id: 8,
type: 'consent__strategies'
}
},
forms: {
data: null
}
}
}
],
included: [
{
id: 7,
type: 'consent__strategies',
attributes: {
name: 'Strategy-8',
strategy_type: 'electronic',
created_at: '2018-07-16T23:08:26Z',
updated_at: '2018-07-16T23:08:26Z'
},
relationships: {
cohort: {
data: {
id: 9,
type: 'cohorts'
}
},
forms: {
data: [
{
id: 5,
type: 'consent__forms'
},
{
id: 6,
type: 'consent__forms'
}
]
}
}
},
{
id: 5,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'Form 5',
created_at: '2018-07-16T23:08:26Z',
updated_at: '2018-07-16T23:08:26Z'
},
relationships: {
consent_form_versions: {
data: [
{
id: 5,
type: 'consent__form_versions'
},
{
id: 6,
type: 'consent__form_versions'
}
]
},
strategy: {
data: {
id: 7,
type: 'consent__strategies'
}
}
}
},
{
id: 6,
type: 'consent__forms',
attributes: {
enabled: true,
name: 'Form 6',
created_at: '2018-07-16T23:08:26Z',
updated_at: '2018-07-16T23:08:26Z'
},
relationships: {
consent_form_versions: {
data: [
{
id: 7,
type: 'consent__form_versions'
}
]
},
strategy: {
data: {
id: 7,
type: 'consent__strategies'
}
}
}
},
{
id: 8,
type: 'consent__strategies',
attributes: {
name: 'Strategy-9',
strategy_type: 'waived',
created_at: '2018-07-16T23:08:26Z',
updated_at: '2018-07-16T23:08:26Z'
},
relationships: {
cohort: {
data: {
id: 9,
type: 'cohorts'
}
},
forms: {
data: []
}
}
}
]
};
t.context.getResponseAssignments80 = {
data: [
{
id: '6',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-25T17:44:57Z',
updated_at: '2018-04-25T17:44:57Z'
},
relationships: {
user: {
data: {
id: '80',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '22',
type: 'cohorts'
}
}
}
},
{
id: '7',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-25T17:44:57Z',
updated_at: '2018-04-25T17:44:57Z'
},
relationships: {
user: {
data: {
id: '80',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '23',
type: 'cohorts'
}
}
}
},
{
id: '8',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-25T17:44:57Z',
updated_at: '2018-04-25T17:44:57Z'
},
relationships: {
user: {
data: {
id: '80',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '24',
type: 'cohorts'
}
}
}
}
],
included: [
{
id: '22',
type: 'cohorts',
attributes: {
name: 'name-22',
cohort_type: 'static',
created_at: '2018-04-25T17:44:57Z',
updated_at: '2018-04-25T17:44:57Z'
}
},
{
id: '23',
type: 'cohorts',
attributes: {
name: 'name-23',
cohort_type: 'static',
created_at: '2018-04-25T17:44:57Z',
updated_at: '2018-04-25T17:44:57Z'
}
},
{
id: '24',
type: 'cohorts',
attributes: {
name: 'name-24',
cohort_type: 'static',
created_at: '2018-04-25T17:44:57Z',
updated_at: '2018-04-25T17:44:57Z'
}
}
]
};
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
// let request = {};
// nock(client.apiBaseUrl).get(`/api/related_mobile_users/1797/profile`).reply(function (uri, requestBody) {
// request = this.req;
// request.requestBody = requestBody;
// return [200, { 'content-type': 'application/json' }, JSON.stringify({})];
// });
t.context.mobileUser = new User({
id: 1797,
first_name: 'Barack',
last_name: 'Obama',
phone: '555-555-5555',
email: 'bobama@parallel6.com',
account_name: '78473136',
avatar: null,
disabled: false,
invitation_status: 'pending',
user_role: 'Care Circle',
unread: 0,
conversation_id: null,
disabled_at: '2016-12-22T16:57:12.127Z'
});
});
// test.afterEach(t => t.context.server.restore());
// MobileUser helper class
// Add Comment (comments not implemented yet on server for mobile users)
// /**
// * @test {MobileUser.addComment}
// */
// test('[unit] MobileUser.addComment should return a comment object', (t) => {
// const { mobileUser } = t.context;
// const comment = await mobileUser.addComment('Some Comment');
// t.is(comment.id, 1);
// utils.tick(() => {
// request.respondWith({
// status: 200,
// statusText: 'OK',
// response: `{
// "data": {
// "type": "comments",
// "id": 1,
// "attributes": {
// "body": "Some Comment",
// "created_at": "2016-12-22T16:57:12.127Z"
// },
// "relations": {
// "author": {
// "email": "developer+8@parallel6.com",
// "username": null,
// "user_role": "Physician"
// }
// }
// }
// }`,
// headers: {
// 'content-type': 'application/json',
// },
// });
// });
// });
// MobileUser.delete
/**
* @test {MobileUser.delete}
*/
test('[unit] MobileUser.delete method should remove a related mobile user with an id parameter', async (t) => {
const { mobileUser } = t.context;
let request = {};
nock(client.apiBaseUrl).delete('/v3/mobile_users/1797').reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, { invited: [] }];
});
const response = await mobileUser.delete();
t.is(request.path, `/v3/mobile_users/1797`);
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.is(response.invited.length, 0);
});
// Disable
/**
* @test {MobileUser.disable}
*/
test('[unit] MobileUser.disable should disable a related mobile user with an id parameter', async (t) => {
const { mobileUser } = t.context;
let request = {};
nock(client.apiBaseUrl).post(`/api/related_mobile_users/1797/disable`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, { invited: [] }];
});
const response = await mobileUser.disable();
t.is(request.path, `/api/related_mobile_users/1797/disable`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, {});
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.invited.length, 0);
});
// Enable
/**
* @test {MobileUser.enable}
*/
test('[unit] MobileUser.enable should enable a related mobile user with an id parameter', async (t) => {
const { mobileUser } = t.context;
let request = {};
nock(client.apiBaseUrl).post(`/api/related_mobile_users/1797/enable`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, {}];
});
const response = await mobileUser.enable();
t.is(request.path, `/api/related_mobile_users/1797/enable`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, {});
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.truthy(response);
});
// User.getCohortAssignments method
/**
* @test {User.getCohortAssignments}
*/
test.serial('[unit] User.getCohortAssignments should throw an error when there is no authToken or invalid params', async (t) => {
const user = new User({ id: 80 });
client.authToken = undefined;
const title = `UserService.getChildren error`;
await t.throwsAsync(user.getCohortAssignments(user, { type: 'cohort_assignments' }), `${title}: requires authToken`);
});
/**
* @test {User.getCohortAssignments}
*/
test.serial(`[unit] User.getCohortAssignments should receive a valid request and response for a get request without an id`, async (t) => {
const { getResponseAssignments80 } = t.context;
const user = new User({ id: 80 });
user.type = 'mobile_users';
user.email = 'myEmail@email.com';
await user.store();
let request = {};
nock(client.apiBaseUrl).get(`/v3/${user.type}/${user.id}/cohort_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAssignments80];
});
const response = await user.getCohortAssignments();
t.is(request.path, `/v3/${user.type}/${user.id}/cohort_assignments`);
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, 3);
t.truthy(response[0] instanceof CohortAssignment);
t.is(response[0].id, 6);
t.is(response[0].type, 'cohort_assignments');
t.is(response[0].createdAt, '2018-04-25T17:44:57Z');
t.is(response[0].updatedAt, '2018-04-25T17:44:57Z');
t.truthy(response[0].user);
t.is(response[0].user.id, 80);
t.is(response[0].user.email, 'myEmail@email.com');
t.truthy(response[0].cohort);
t.is(response[0].cohort.id, 22);
t.is(response[0].cohort.cohortType, 'static');
t.is(response[0].cohort.createdAt, '2018-04-25T17:44:57Z');
t.is(response[0].cohort.name, 'name-22');
t.is(response[0].cohort.updatedAt, '2018-04-25T17:44:57Z');
t.truthy(response[1] instanceof CohortAssignment);
t.is(response[1].id, 7);
t.is(response[1].type, 'cohort_assignments');
t.is(response[1].createdAt, '2018-04-25T17:44:57Z');
t.is(response[1].updatedAt, '2018-04-25T17:44:57Z');
t.truthy(response[2] instanceof CohortAssignment);
t.is(response[2].id, 8);
t.is(response[2].type, 'cohort_assignments');
t.is(response[2].createdAt, '2018-04-25T17:44:57Z');
t.is(response[2].updatedAt, '2018-04-25T17:44:57Z');
t.is(user.cohortAssignments[0].id, 6);
t.is(user.cohortAssignments[0].type, 'cohort_assignments');
t.is(user.cohortAssignments[0].createdAt, '2018-04-25T17:44:57Z');
t.is(user.cohortAssignments[0].updatedAt, '2018-04-25T17:44:57Z');
t.truthy(user.cohorts[0]);
t.is(user.cohorts[0].id, 22);
t.is(user.cohorts[0].cohortType, 'static');
t.is(user.cohorts[0].createdAt, '2018-04-25T17:44:57Z');
t.is(user.cohorts[0].name, 'name-22');
t.is(user.cohorts[0].updatedAt, '2018-04-25T17:44:57Z');
});
// User.getConsentGrants method
/**
* @test {User.getConsentGrants}
*/
test.serial('[unit] User.getConsentGrants should throw an error when there is no authToken or invalid params', async (t) => {
const user = new User({ id: 80 });
client.authToken = undefined;
const title = `UserService.getChildren error`;
await t.throwsAsync(user.getConsentGrants(user, { type: 'consent__grants' }), `${title}: requires authToken`);
});
/**
* @test {User.getConsentGrants}
*/
test.serial(`[unit] User.getConsentGrants should receive a valid request and response for a get request without an id`, async (t) => {
const { getResponseGrants454 } = t.context;
const user = new User({ id: 454 });
user.type = 'mobile_users';
user.email = 'myEmail@email.com';
await user.store();
let request = {};
nock(client.apiBaseUrl).get(`/v3/${user.type}/${user.id}/consent/grants`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseGrants454];
});
const response = await user.getConsentGrants();
t.is(request.path, `/v3/${user.type}/${user.id}/consent/grants`);
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, 2);
t.truthy(response[0] instanceof ConsentGrant);
t.is(response[0].id, 3);
t.is(response[0].type, 'consent__grants');
t.is(response[0].documentUrl, '/uploads/test/consent/grant/document/3/1.pdf');
t.is(response[0].progress, 'all_signed');
t.is(response[0].createdAt, '2018-07-20T13:32:51Z');
t.is(response[0].updatedAt, '2018-07-20T13:32:51Z');
t.truthy(response[0].grantedFor);
t.is(response[0].grantedFor.id, 454);
t.is(response[0].grantedFor.type, 'mobile_users');
t.truthy(response[0].grantedBy);
t.is(response[0].grantedBy.id, 454);
t.is(response[0].grantedBy.type, 'mobile_users');
t.truthy(response[0].strategy);
t.is(response[0].strategy.id, 62);
t.is(response[0].strategy.type, 'consent__strategies');
t.is(response[0].strategy.name, 'Strategy-67');
t.is(response[0].strategy.strategyType, 'paper');
t.is(response[0].strategy.createdAt, '2018-07-20T13:32:51Z');
t.is(response[0].strategy.updatedAt, '2018-07-20T13:32:51Z');
t.is(response[0].strategy.cohort.id, 105);
t.is(response[0].strategy.cohort.type, 'cohorts');
t.is(response[0].strategy.forms.length, 0);
t.is(response[0].formVersion, null);
t.is(response[0].agreement, null);
t.truthy(response[1] instanceof ConsentGrant);
t.is(response[1].id, 4);
t.is(response[1].type, 'consent__grants');
t.is(response[1].signUrl, null);
t.is(response[1].progress, 'all_signed');
t.is(response[1].createdAt, '2018-07-20T13:32:51Z');
t.is(response[1].updatedAt, '2018-07-20T13:32:51Z');
t.truthy(response[1].grantedFor);
t.is(response[1].grantedFor.id, 454);
t.is(response[1].grantedFor.type, 'mobile_users');
t.truthy(response[1].grantedBy);
t.is(response[1].grantedBy.id, 454);
t.is(response[1].grantedBy.type, 'mobile_users');
t.truthy(response[1].strategy);
t.is(response[1].strategy.id, 63);
t.is(response[1].strategy.type, 'consent__strategies');
t.is(response[1].strategy.name, 'Strategy-68');
t.is(response[1].strategy.strategyType, 'electronic');
t.is(response[1].strategy.createdAt, '2018-07-20T13:32:51Z');
t.is(response[1].strategy.updatedAt, '2018-07-20T13:32:51Z');
t.is(response[1].strategy.cohort.id, 106);
t.is(response[1].strategy.cohort.type, 'cohorts');
t.is(response[1].strategy.forms.length, 1);
t.is(response[1].strategy.forms[0].id, 51);
t.is(response[1].strategy.forms[0].type, 'consent__forms');
t.is(response[1].strategy.forms[0].enabled, true);
t.is(response[1].strategy.forms[0].name, 'Form 54');
t.is(response[1].strategy.forms[0].createdAt, '2018-07-20T13:32:51Z');
t.is(response[1].strategy.forms[0].updatedAt, '2018-07-20T13:32:51Z');
t.is(response[1].strategy.forms[0].consentFormVersions.length, 1);
t.is(response[1].strategy.forms[0].consentFormVersions[0].id, 37);
t.is(response[1].strategy.forms[0].consentFormVersions[0].type, 'consent__form_versions');
t.is(response[1].strategy.forms[0].consentStrategy.id, 63);
t.is(response[1].strategy.forms[0].consentStrategy.type, 'consent__strategies');
t.truthy(response[1].formVersion);
t.is(response[1].formVersion.id, 37);
t.is(response[1].formVersion.type, 'consent__form_versions');
t.is(response[1].formVersion.archivedAt, undefined);
t.is(response[1].formVersion.site.id, 115);
t.is(response[1].formVersion.site.type, 'trials__sites');
t.is(response[1].formVersion.agreementTemplate.id, 102);
t.is(response[1].formVersion.agreementTemplate.type, 'agreement__templates');
t.is(response[1].formVersion.language.id, 63);
t.is(response[1].formVersion.language.type, 'languages');
t.is(response[1].formVersion.consentForm.id, 51);
t.is(response[1].formVersion.consentForm.type, 'consent__forms');
t.is(response[1].formVersion.consentForm.type, 'consent__forms');
t.is(response[1].formVersion.consentForm.enabled, true);
t.is(response[1].formVersion.consentForm.name, 'Form 54');
t.is(response[1].formVersion.consentForm.createdAt, '2018-07-20T13:32:51Z');
t.is(response[1].formVersion.consentForm.updatedAt, '2018-07-20T13:32:51Z');
t.is(response[1].formVersion.consentForm.consentFormVersions.length, 1);
t.is(response[1].formVersion.consentForm.consentFormVersions[0].id, 37);
t.is(response[1].formVersion.consentForm.consentFormVersions[0].type, 'consent__form_versions');
t.is(response[1].formVersion.consentForm.consentStrategy.id, 63);
t.is(response[1].formVersion.consentForm.consentStrategy.type, 'consent__strategies');
t.is(response[1].formVersion.approverGroups.length, 0);
t.is(response[1].formVersion.approvers.length, 0);
t.truthy(response[1].agreement);
t.is(response[1].agreement.id, 18);
t.is(response[1].agreement.type, 'agreement__agreements');
});
// /**
// * @test {User.getCohortAssignments}
// */
// test('[unit] User.getCohortAssignments should receive a valid response for a get request with an id', async (t) => {
// const { getResponseAssignment20 } = t.context;
// const user = new User({ id: 80 });
// const cohortAssignment = new CohortAssignment({ id: 20 });
// let request = {};
// nock(client.apiBaseUrl).get(`/v3/${user.type}/${user.id}/cohort_assignments/${cohortAssignment.id}`).reply(function (uri, requestBody) {
// request = this.req;
// request.requestBody = requestBody;
// return [200, getResponseAssignment20];
// });
// const response = await user.getCohortAssignments({ id: 20 });
// t.is(request.path, `/v3/${user.type}/${user.id}/cohort_assignments/${cohortAssignment.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.truthy(response);
// t.truthy(response instanceof CohortAssignment);
// t.is(response.id, 20);
// t.is(response.type, 'cohort_assignments');
// t.is(response.createdAt, '2018-04-17T19:59:53Z');
// t.is(response.updatedAt, '2018-04-17T19:59:53Z');
// t.truthy(response.user);
// t.is(response.user.id, 278);
// t.is(response.user.createdAt, '2018-04-17T19:59:54Z');
// t.is(response.user.email, 'user418@fake.com');
// t.is(response.user.updatedAt, '2018-04-17T19:59:54Z');
// t.is(response.user.uuid, '7731e5db-3c94-4596-8a52-cb753b9d23bd');
// t.truthy(response.cohort);
// t.is(response.cohort.id, 2);
// t.is(response.cohort.type, 'cohorts');
// });
// Cohort.getCohorts method
/**
* @test {Cohort.getCohorts}
*/
test.serial('[unit] Cohort.getCohorts should throw an error when there is no authToken or invalid params', async (t) => {
const user = new User({ id: 80 });
client.authToken = undefined;
const title = `UserService.getChildren error`;
await t.throwsAsync(user.getCohorts(user, { type: 'cohort_assignments' }), `${title}: requires authToken`);
});
/**
* @test {Cohort.getCohorts}
*/
test(`[unit] Cohort.getCohorts should receive a valid request and response for a get request without an id`, async (t) => {
const { getResponseAssignments80 } = t.context;
const user = new User({ id: 80 });
let request = {};
nock(client.apiBaseUrl).get(`/v3/${user.type}/${user.id}/cohort_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAssignments80];
});
const response = await user.getCohorts();
t.is(request.path, `/v3/${user.type}/${user.id}/cohort_assignments`);
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, 3);
t.truthy(response[0] instanceof Cohort);
t.is(response[0].id, 22);
t.is(response[0].type, 'cohorts');
t.is(response[0].cohortType, 'static');
t.is(response[0].createdAt, '2018-04-25T17:44:57Z');
t.is(response[0].updatedAt, '2018-04-25T17:44:57Z');
t.is(response[0].name, 'name-22');
t.truthy(response[1] instanceof Cohort);
t.is(response[1].id, 23);
t.is(response[1].type, 'cohorts');
t.is(response[1].cohortType, 'static');
t.is(response[1].createdAt, '2018-04-25T17:44:57Z');
t.is(response[1].updatedAt, '2018-04-25T17:44:57Z');
t.is(response[1].name, 'name-23');
t.truthy(response[2] instanceof Cohort);
t.is(response[2].id, 24);
t.is(response[2].type, 'cohorts');
t.is(response[2].cohortType, 'static');
t.is(response[2].createdAt, '2018-04-25T17:44:57Z');
t.is(response[2].updatedAt, '2018-04-25T17:44:57Z');
t.is(response[2].name, 'name-24');
});
// User.getAvailableStrategies method
/**
* @test {User.getAvailableStrategies}
*/
test.serial('[unit] User.getAvailableStrategies should throw an error when there is no authToken', async (t) => {
const user = new User({ id: 80 });
client.authToken = undefined;
const title = `UserService.getChildren error`;
await t.throwsAsync(user.getAvailableStrategies(user, { type: 'consent__available_strategies' }), `${title}: requires authToken`);
});
/**
* @test {User.getAvailableStrategies}
*/
test.serial(`[unit] User.getAvailableStrategies should receive a valid request and response for a get request`, async (t) => {
const { getResponseStrategies77 } = t.context;
const user = new User({ id: 77 });
user.type = 'mobile_users';
user.email = 'myEmail@email.com';
await user.store();
let request = {};
nock(client.apiBaseUrl).get(`/v3/${user.type}/${user.id}/consent/available_strategies`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseStrategies77];
});
const response = await user.getAvailableStrategies();
t.is(request.path, `/v3/${user.type}/${user.id}/consent/available_strategies`);
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, 2);
t.truthy(response[0] instanceof AvailableStrategy);
t.is(response[0].id, 7);
t.is(response[0].type, 'consent__available_strategies');
t.is(response[0].strategy.id, 7);
t.is(response[0].strategy.type, 'consent__strategies');
t.is(response[0].strategy.name, 'Strategy-8');
t.is(response[0].strategy.cohort.id, 9);
t.is(response[0].strategy.cohort.type, 'cohorts');
t.is(response[0].strategy.forms.length, 2);
t.is(response[0].strategy.forms[0].id, 5);
t.is(response[0].strategy.forms[0].type, 'consent__forms');
t.is(response[0].strategy.forms[1].id, 6);
t.is(response[0].strategy.forms[1].type, 'consent__forms');
t.is(response[0].forms.length, 2);
t.is(response[0].forms[0].id, 5);
t.is(response[0].forms[0].type, 'consent__forms');
t.is(response[0].forms[0].enabled, true);
t.is(response[0].forms[0].name, 'Form 5');
t.is(response[0].forms[0].consentFormVersions.length, 2);
t.is(response[0].forms[0].consentFormVersions[0].id, 5);
t.is(response[0].forms[0].consentFormVersions[0].type, 'consent__form_versions');
t.is(response[0].forms[0].consentFormVersions[1].id, 6);
t.is(response[0].forms[0].consentFormVersions[1].type, 'consent__form_versions');
t.is(response[0].forms[0].consentStrategy.id, 7);
t.is(response[0].forms[0].consentStrategy.type, 'consent__strategies');
t.is(response[0].forms[1].id, 6);
t.is(response[0].forms[1].type, 'consent__forms');
t.is(response[0].forms[1].consentFormVersions.length, 1);
t.is(response[0].forms[1].consentFormVersions[0].id, 7);
t.is(response[0].forms[1].consentFormVersions[0].type, 'consent__form_versions');
t.is(response[0].forms[1].consentStrategy.id, 7);
t.is(response[0].forms[1].consentStrategy.type, 'consent__strategies');
t.truthy(response[1] instanceof AvailableStrategy);
t.is(response[1].id, 8);
t.is(response[1].strategy.id, 8);
t.is(response[1].strategy.type, 'consent__strategies');
t.is(response[1].strategy.name, 'Strategy-9');
t.is(response[1].strategy.cohort.id, 9);
t.is(response[1].strategy.forms.length, 0);
t.is(response[1].forms, undefined);
});
// Get Comments (comments not implemented yet on server for mobile users)
// /**
// * @test {MobileUser.getComments}
// */
// test('[unit] MobileUser.getComments should return an array of comment objects', (t) => {
// mobileUser.getComments().then((comments) => {
// t.is(comments[0].id, 1);
// });
// utils.tick(() => {
// request.respondWith({
// status: 200,
// statusText: 'OK',
// response: `{
// "data": [{
// "type": "comments",
// "id": 1,
// "attributes": {
// "body": "Some Comment",
// "created_at": "2016-12-22T16:57:12.127Z"
// },
// "relations": {
// "author": {
// "email": "developer+8@parallel6.com",
// "username": null,
// "user_role": "Physician"
// }
// }
// }]
// }`,
// headers: {
// 'content-type': 'application/json',
// },
// });
// });
// });
// Get Profile
// Resend Invite
/**
* @test {MobileUser.resendInvite}
*/
test('[unit] MobileUser.resendInvite should successfully resend an invite to a related mobile user with an id parameter', async (t) => {
const { mobileUser } = t.context;
let request = {};
nock(client.apiBaseUrl).post(`/api/related_mobile_users/1797/reinvite`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, { status: 'ok' }];
});
const response = await mobileUser.resendInvite();
t.is(request.path, `/api/related_mobile_users/1797/reinvite`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, {});
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.status, 'ok');
});
// UserService.revokeConsent method
/**
* @test {MobileUser.revokeConsent}
*/
test.serial('[unit] UserService.revokeConsent should throw an error when there is no authToken', async (t) => {
const { mobileUser } = t.context;
client.authToken = undefined;
const title = `UserService.revokeConsent error`;
await t.throwsAsync(mobileUser.revokeConsent(), `${title}: requires authToken`);
});
/**
* @test {MobileUser.revokeConsent}
*/
test('[unit] MobileUser.revokeConsent method should revoke consent for a mobile user with and id value and type', async (t) => {
const { mobileUser } = t.context;
let request = {};
nock(client.apiBaseUrl).delete(`/v3/${mobileUser.type}/${mobileUser.id}/consent/consent`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await mobileUser.revokeConsent();
t.is(request.path, `/v3/mobile_users/1797/consent/consent`);
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);
});
// MobileUser.getRegistrationStatus method
// /**
// * @test {MobileUser.getRegistrationStatus}
// */
// test('[unit] MobileUser.getRegistrationStatus should return a promise', async (t) => {
// t.context.server.restore();
// t.truthy(await mobileUserService.getRegistrationStatus({ account_name: 'a' }).then);
// });
// /**
// * @test {MobileUser.getRegistrationStatus}
// */
// test('[unit] MobileUser.getRegistrationStatus should throw errors for invalid parameters', (t) => {
// const undefinedError = 'MobileUser.getRegistrationStatus error: attributes is not defined';
// const validError = 'MobileUser.getRegistrationStatus error: attributes must be valid';
// // undefined mobile user
// t.throws(() => mobileUserService.getRegistrationStatus(), undefinedError);
// t.throws(() => mobileUserService.getRegistrationStatus({}), validError);
// });
/**
* @test {MobileUser.getRegistrationStatus}
*/
test('[unit] MobileUser.getRegistrationStatus should successfully get the registration status of a mobile user', async (t) => {
const { mobileUser } = t.context;
const requestJsonApi = {
data: {
type: 'registration_validations',
attributes: {
id: 1797,
type: 'mobile_users',
attributes: {
account_name: '78473136',
email: 'bobama@parallel6.com',
disabled_at: '2016-12-22T16:57:12.127Z',
disabled: false
}
}
}
};
client.authToken = undefined;
let request = {};
nock(client.apiBaseUrl).post(`/v3/mobile_users/registration_validation`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, {
data: {
id: '1373',
type: 'statuses',
attributes: {
value: 'new'
},
relationships: {
owner: {
data: {
id: '34',
type: 'mobile_users'
}
},
statusable: {
data: {
id: '34',
type: 'mobile_users'
}
}
}
},
meta: {
password_set: true
}
}];
});
const response = await mobileUser.getRegistrationStatus();
t.is(request.path, `/v3/mobile_users/registration_validation`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, requestJsonApi);
t.is(request.headers.authorization, undefined);
t.is(request.headers['content-type'], 'application/json');
t.is(response.value, 'new');
t.truthy(mobileUser.isPasswordSet());
});
/**
* @test {MobileUser.getRegistrationStatus}
*/
test('[unit] MobileUser.getRegistrationStatus should successfully get the registration status of an account name', async (t) => {
const mobileUser = new User({ account_name: '78473136', type: 'mobile_users' });
const requestJsonApi = {
data: {
type: 'registration_validations',
attributes: {
type: 'mobile_users',
attributes: {
account_name: '78473136',
}
}
}
};
client.authToken = undefined;
let request = {};
nock(client.apiBaseUrl).post(`/v3/mobile_users/registration_validation`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, {
data: {
id: '1373',
type: 'statuses',
attributes: {
value: 'new'
},
relationships: {
owner: {
data: {
id: '34',
type: 'mobile_users'
}
},
statusable: {
data: {
id: '34',
type: 'mobile_users'
}
}
}
},
meta: {
password_set: true
}
}];
});
const response = await mobileUser.getRegistrationStatus();
t.is(request.path, `/v3/mobile_users/registration_validation`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, requestJsonApi);
t.is(request.headers.authorization, undefined);
t.is(request.headers['content-type'], 'application/json');
t.is(response.value, 'new');
t.truthy(mobileUser.isPasswordSet());
});
/**
* @test {MobileUser.toInvitation}
*/
test('[unit] MobileUser.toInvitation should generate an Invitation when using implicit user relationships', (t) => {
const { mobileUser } = t.context;
const french = new Language({ id: 54, iso: 'fr', name: 'French' });
const pi = new Role({ id: 5, name: 'pi' });
mobileUser.profile.firstName = 'Barack';
mobileUser.profile.lastName = 'Obama';
mobileUser.profile.language = french;
mobileUser.role = pi;
const invitation = mobileUser.toInvitation();
t.is(invitation.email, 'bobama@parallel6.com');
t.is(invitation.firstName, 'Barack');
t.is(invitation.lastName, 'Obama');
t.is(invitation.language.name, 'French');
t.is(invitation.role.name, 'pi');
});
/**
* @test {MobileUser.toInvitation}
*/
test('[unit] MobileUser.toInvitation should generate an Invitation when using attributes and relationships', (t) => {
const { mobileUser } = t.context;
const french = new Language({ id: 54, iso: 'fr', name: 'French' });
const pi = new Role({ id: 5, name: 'pi' });
mobileUser.profile.firstName = 'Barack';
mobileUser.profile.lastName = 'Obama';
const invitation = mobileUser.toInvitation({}, { language: french, user_role: pi });
t.is(invitation.email, 'bobama@parallel6.com');
t.is(invitation.firstName, 'Barack');
t.is(invitation.lastName, 'Obama');
// the getter "relationships" return a json having {relationships} for request purposes
// const relationships = invitation.relationships.relationships;
t.is(invitation.language.id, french.id);
t.is(invitation.role.id, pi.id);
});
/**
* @test {MobileUser.toInvitation}
*/
test('[unit] MobileUser.toInvitation should generate the correct json api request format when converted toJson', (t) => {
// JSON pulled from Pompe, Carecircle, and CDiff
const jsonApiRequest = {
data: {
type: 'invitations',
attributes: {
account_name: undefined,
dob: undefined,
// role: 'patient', Can't have role as it's used as a relationship
email: 'myemail@email.com',
prefix: 'mrs',
suffix: undefined,
first_name: 'Jane',
last_name: 'Doe',
language_id: 23,
phone: '820-203-4032',
relationship: 'self',
dynamic_profile: {
is_primary_guardian: 'no',
}
},
relationships: {
followed: {
data: {
type: 'mobile_users',
id: 23
}
},
site: {
data: {
type: 'trials__sites',
id: 57
}
}
}
}
};
const user = new User({ email: 'myemail@email.com' });
user.profile = new Profile({
prefix: 'mrs',
first_name: 'Jane',
last_name: 'Doe',
phone: '820-203-4032'
});
const site = new Site({ id: 57 });
const followed = new User({ id: 23 });
const invitation = user.toInvitation({
relationship: 'self',
language_id: 23,
dynamic_profile: {
is_primary_guardian: 'no',
}
}, { followed, site });
t.deepEqual({ data: invitation.toJSON() }, jsonApiRequest);
});