test/unit/helpers.site.js
import test from 'ava';
import nock from 'nock';
import {
client,
Language,
Site,
SiteContact,
SiteMember,
SiteLanguage,
} from '../../src';
// See tests.js for testing client creation
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
t.context.getResponseContacts = {
data: [
{
id: 1,
type: 'trials__site_contacts',
attributes: {
first_name: 'Ollie',
last_name: 'Abshire',
email: 'olaf.rath@johns.io',
phone: null,
fax: null,
content_type: 'referring_doctor',
primary_contact: true,
created_at: '2018-06-06T17:45:43Z',
updated_at: '2018-06-06T17:45:43Z'
},
relationships: {
site: {
data: {
id: 1,
type: 'trials__sites'
}
}
}
},
{
id: 2,
type: 'trials__site_contacts',
attributes: {
first_name: 'Rowena',
last_name: 'Pollich',
email: 'malika@watsicatowne.com',
phone: null,
fax: null,
primary_contact: false,
created_at: '2018-06-06T17:45:43Z',
updated_at: '2018-06-06T17:45:43Z'
},
relationships: {
site: {
data: {
id: 1,
type: 'trials__sites'
}
}
}
}
]
};
t.context.getResponseLanguages = {
data: [
{
id: 3,
type: 'trials__site_supported_languages',
attributes: {
created_at: '2018-05-18T02:35:20Z',
updated_at: '2018-05-18T02:35:20Z'
},
relationships: {
language: {
data: {
id: 33,
type: 'languages'
}
},
site: {
data: {
id: 59,
type: 'trials__sites'
}
}
}
},
{
id: 4,
type: 'trials__site_supported_languages',
attributes: {
created_at: '2018-05-18T02:35:20Z',
updated_at: '2018-05-18T02:35:20Z'
},
relationships: {
language: {
data: {
id: 34,
type: 'languages'
}
},
site: {
data: {
id: 59,
type: 'trials__sites'
}
}
}
}
],
included: [
{
id: 33,
type: 'languages',
attributes: {
iso: 'dummy_1808',
name: 'maiores',
is_default: false
}
},
{
id: 34,
type: 'languages',
attributes: {
iso: 'dummy_1809',
name: 'maiores',
is_default: false
}
}
]
};
t.context.getResponseMembersAll = {
data: [
{
id: '21',
type: 'trials__site_members',
attributes: {
member_type: 'pi',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
site: {
data: {
id: '24',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '47',
type: 'mobile_users'
}
}
}
},
{
id: '22',
type: 'trials__site_members',
attributes: {
member_type: 'pi',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
site: {
data: {
id: '24',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '48',
type: 'mobile_users'
}
}
}
},
{
id: '23',
type: 'trials__site_members',
attributes: {
member_type: 'pi',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
site: {
data: {
id: '24',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '49',
type: 'mobile_users'
}
}
}
},
{
id: '24',
type: 'trials__site_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
site: {
data: {
id: '24',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '50',
type: 'mobile_users'
}
}
}
},
{
id: '25',
type: 'trials__site_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
site: {
data: {
id: '24',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '51',
type: 'mobile_users'
}
}
}
},
{
id: '26',
type: 'trials__site_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
site: {
data: {
id: '24',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '52',
type: 'mobile_users'
}
}
}
},
{
id: '27',
type: 'trials__site_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
site: {
data: {
id: '24',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '53',
type: 'mobile_users'
}
}
}
},
{
id: '28',
type: 'trials__site_members',
attributes: {
member_type: 'patient',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z'
},
relationships: {
site: {
data: {
id: '24',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '54',
type: 'mobile_users'
}
}
}
}
],
included: [
{
id: '47',
type: 'mobile_users',
attributes: {
uuid: 'd140c877-5414-46ca-b96f-60fd74461e83',
account_name: 'dummy_47',
email: 'user61@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '61',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '21',
type: 'trials__site_members'
}
}
}
},
{
id: '61',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Stephany',
middle_initial: null,
last_name: 'Lubowitz',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/61/1.png',
small: {
url: '/uploads/test/profile/avatar/61/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/61/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/61/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user61@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '47',
type: 'mobile_users'
}
}
}
},
{
id: '48',
type: 'mobile_users',
attributes: {
uuid: '08e956a6-929c-48ce-b913-0386bd5f0220',
account_name: 'dummy_48',
email: 'user62@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '62',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '22',
type: 'trials__site_members'
}
}
}
},
{
id: '62',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Rosalyn',
middle_initial: null,
last_name: 'Conn',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/62/1.png',
small: {
url: '/uploads/test/profile/avatar/62/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/62/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/62/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user62@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '48',
type: 'mobile_users'
}
}
}
},
{
id: '49',
type: 'mobile_users',
attributes: {
uuid: 'ce178761-cb55-4d59-9f8a-7d797c71290f',
account_name: 'dummy_49',
email: 'user63@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '63',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '23',
type: 'trials__site_members'
}
}
}
},
{
id: '63',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Triston',
middle_initial: null,
last_name: 'Gorczany',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/63/1.png',
small: {
url: '/uploads/test/profile/avatar/63/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/63/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/63/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user63@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '49',
type: 'mobile_users'
}
}
}
},
{
id: '50',
type: 'mobile_users',
attributes: {
uuid: '901741a7-2b6d-4f9c-9d00-d470b86d2f66',
account_name: 'dummy_50',
email: 'user64@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '64',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '24',
type: 'trials__site_members'
}
}
}
},
{
id: '64',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Gregg',
middle_initial: null,
last_name: 'Rodriguez',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/64/1.png',
small: {
url: '/uploads/test/profile/avatar/64/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/64/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/64/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user64@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '50',
type: 'mobile_users'
}
}
}
},
{
id: '51',
type: 'mobile_users',
attributes: {
uuid: 'ec259c81-a4a4-466c-be64-adfc7eb71e03',
account_name: 'dummy_51',
email: 'user65@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '65',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '25',
type: 'trials__site_members'
}
}
}
},
{
id: '65',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Alfonso',
middle_initial: null,
last_name: 'Rippin',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/65/1.png',
small: {
url: '/uploads/test/profile/avatar/65/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/65/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/65/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user65@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '51',
type: 'mobile_users'
}
}
}
},
{
id: '52',
type: 'mobile_users',
attributes: {
uuid: 'a4b0974a-6e1b-432d-8087-6121b925dffb',
account_name: 'dummy_52',
email: 'user66@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '66',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '26',
type: 'trials__site_members'
}
}
}
},
{
id: '66',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Robbie',
middle_initial: null,
last_name: 'Schinner',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/66/1.png',
small: {
url: '/uploads/test/profile/avatar/66/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/66/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/66/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user66@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '52',
type: 'mobile_users'
}
}
}
},
{
id: '53',
type: 'mobile_users',
attributes: {
uuid: '49c35ac6-4483-4ff4-b7e7-43aa3b7500c6',
account_name: 'dummy_53',
email: 'user67@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '67',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '27',
type: 'trials__site_members'
}
}
}
},
{
id: '67',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Justyn',
middle_initial: null,
last_name: 'Jaskolski',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/67/1.png',
small: {
url: '/uploads/test/profile/avatar/67/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/67/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/67/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user67@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '53',
type: 'mobile_users'
}
}
}
},
{
id: '54',
type: 'mobile_users',
attributes: {
uuid: 'f7eeac76-0057-444e-9bc8-feaf29018dc3',
account_name: 'dummy_54',
email: 'user68@fake.com',
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '68',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '28',
type: 'trials__site_members'
}
}
}
},
{
id: '68',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:12Z',
updated_at: '2018-02-07T17:48:12Z',
first_name: 'Verlie',
middle_initial: null,
last_name: 'Treutel',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/68/1.png',
small: {
url: '/uploads/test/profile/avatar/68/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/68/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/68/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user68@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '54',
type: 'mobile_users'
}
}
}
}
]
};
t.context.getResponseMembersId = {
data: {
id: '20',
type: 'trials__site_members',
attributes: {
member_type: 'pi',
created_at: '2018-02-07T17:48:11Z',
updated_at: '2018-02-07T17:48:11Z'
},
relationships: {
site: {
data: {
id: '22',
type: 'trials__sites'
}
},
mobile_user: {
data: {
id: '46',
type: 'mobile_users'
}
}
}
},
included: [
{
id: '46',
type: 'mobile_users',
attributes: {
uuid: '21660223-8d6f-4a5e-ba1a-581656e65b4e',
account_name: 'dummy_46',
email: 'user58@fake.com',
created_at: '2018-02-07T17:48:11Z',
updated_at: '2018-02-07T17:48:11Z',
verified_at: null,
invitation_sent_at: null,
invitation_accepted_at: null,
disabled: false,
disabled_at: null
},
relationships: {
devices: {
data: []
},
user_role: {
data: null
},
profile: {
data: {
id: '58',
type: 'profiles'
}
},
language: {
data: {
id: '1',
type: 'languages'
}
},
patient: {
data: {
id: '20',
type: 'trials__site_members'
}
}
}
},
{
id: '58',
type: 'profiles',
attributes: {
created_at: '2018-02-07T17:48:11Z',
updated_at: '2018-02-07T17:48:11Z',
first_name: 'Ellis',
middle_initial: null,
last_name: 'Rempel',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/58/1.png',
small: {
url: '/uploads/test/profile/avatar/58/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/58/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/58/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user58@fake.com',
age: 33
},
relationships: {
profileable: {
data: {
id: '46',
type: 'mobile_users'
}
}
}
}
]
};
t.context.updateResponse = {
data: {
id: '4',
type: 'trials__sites',
attributes: {
site_id: '922176',
name: 'Thomas Doughton',
email: 'thomas@kulas.info',
phone_number: '0003330000',
fax_number: '0004440000',
content_type: undefined,
},
relationships: {
contact: {
data: null
},
location: {
data: {
id: 98,
type: 'locations'
}
},
templatables: {
data: []
}
}
}
};
t.context.insertResponse = {
data: {
id: '16',
type: 'trials__sites',
attributes: {
site_id: '922176',
external_identifier: '922176',
name: 'Tony Smith',
email: 'tony@kulas.info',
phone_number: '0005550000',
fax_number: '0006660000'
},
relationships: {
contact: {
data: null
},
location: {
data: {
id: 98,
type: 'locations'
}
},
templatables: {
data: []
}
}
}
};
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
client.storageUtility.clear();
t.context.storage = client.storageUtility;
t.context.siteJsonApi = {
id: 15,
type: 'trials__sites',
attributes: {
site_id: '922176',
external_identifier: '922176',
randomization_identifier: '8675309',
name: 'Frankie Hermann',
email: 'lisa@kulas.info',
phone_number: '0001110000',
fax_number: '0002220000',
content_type: 'referring_doctor',
created_at: '2018-09-12T19:36:41.396Z',
updated_at: '2018-09-12T19:36:41.396Z'
},
relationships: {
contact: {
data: null
},
location: {
data: {
id: 98,
type: 'locations'
}
},
templatables: {
data: []
}
}
};
t.context.siteJson = {
id: 15,
type: 'trials__sites',
site_id: '922176',
external_identifier: '922176',
randomization_identifier: '8675309',
name: 'Frankie Hermann',
email: 'lisa@kulas.info',
phone_number: '0001110000',
fax_number: '0002220000',
created_at: '2018-09-12T19:36:41.396Z',
updated_at: '2018-09-12T19:36:41.396Z'
};
t.context.siteJsonApiRaw = {
data: {
id: 15,
type: 'trials__sites',
attributes: {
site_id: '922176',
external_identifier: '922176',
randomization_identifier: '8675309',
name: 'Frankie Hermann',
email: 'lisa@kulas.info',
created_at: '2018-09-12T19:36:41.396Z',
updated_at: '2018-09-12T19:36:41.396Z'
}
}
};
t.context.site = new Site(t.context.siteJsonApiRaw);
});
/**
* @test {Site}
*/
test('[unit] Site should handle site data with a normal json format', (t) => {
const { siteJson } = t.context;
const site = new Site(siteJson);
t.is(site.id, 15);
t.is(site.type, 'trials__sites');
t.is(site.siteId, '922176');
t.is(site.externalIdentifier, '922176');
t.is(site.randomizationIdentifier, '8675309');
t.is(site.email, 'lisa@kulas.info');
t.is(site.name, 'Frankie Hermann');
t.is(site.phoneNumber, '0001110000');
t.is(site.faxNumber, '0002220000');
});
/**
* @test {Site}
*/
test('[unit] Site should handle site data with json api format', (t) => {
const { siteJsonApi } = t.context;
const site = new Site({ data: siteJsonApi });
t.is(site.id, 15);
t.is(site.type, 'trials__sites');
t.is(site.siteId, '922176');
t.is(site.email, 'lisa@kulas.info');
t.is(site.name, 'Frankie Hermann');
t.is(site.phoneNumber, '0001110000');
t.is(site.faxNumber, '0002220000');
});
/**
* @test {Site}
*/
test('[unit] Site should generate json api format when converted to string', (t) => {
const { siteJsonApi } = t.context;
let site = new Site({ data: siteJsonApi });
t.deepEqual(site.toJSON(), siteJsonApi);
site = new Site({ data: siteJsonApi });
t.deepEqual(site.toJSON(), siteJsonApi);
});
/**
* @test {Site.delete}
*/
test('[unit] Site.delete should successfully delete a site', async (t) => {
const { siteJsonApi } = t.context;
const site = new Site({ data: siteJsonApi });
let request = {};
nock(client.apiBaseUrl).delete(`/v3/trials/sites/${site.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await site.delete();
t.is(request.path, `/v3/trials/sites/${site.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.falsy(response);
});
// Site.getContacts method
/**
* @test {Site.getContacts}
*/
test.serial('[unit] Site.getContacts should throw an error when there is no authToken or invalid params', async (t) => {
const { siteJsonApi } = t.context;
const site = new Site({ data: siteJsonApi });
client.authToken = undefined;
const title = `SiteService.getChildren error`;
await t.throwsAsync(site.getContacts(site, { type: 'trials__site_contacts' }), `${title}: requires authToken`);
});
/**
* @test {Site.getContacts}
*/
test(`[unit] Site.getContacts should receive a valid request and response for a get request without an id`, async (t) => {
const { siteJsonApi, getResponseContacts } = t.context;
const site = new Site({ data: siteJsonApi });
site.id = 59;
let request = {};
nock(client.apiBaseUrl).get(`/v3/trials/sites/${site.id}/trials/site_contacts`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseContacts];
});
const response = await site.getContacts();
t.is(request.path, `/v3/trials/sites/${site.id}/trials/site_contacts`);
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 SiteContact);
t.is(response[0].id, 1);
t.is(response[0].type, 'trials__site_contacts');
t.is(response[0].firstName, 'Ollie');
t.is(response[0].lastName, 'Abshire');
t.is(response[0].email, 'olaf.rath@johns.io');
t.is(response[0].phone, null);
t.is(response[0].fax, null);
t.is(response[0].contentType, 'referring_doctor');
t.is(response[0].primaryContact, true);
t.is(response[0].createdAt, '2018-06-06T17:45:43Z');
t.is(response[0].updatedAt, '2018-06-06T17:45:43Z');
t.truthy(response[0].site);
t.truthy(response[0].site instanceof Site);
t.is(response[0].site.id, 1);
t.is(response[0].site.type, 'trials__sites');
t.truthy(response[1] instanceof SiteContact);
t.is(response[1].id, 2);
t.is(response[1].type, 'trials__site_contacts');
t.is(response[1].firstName, 'Rowena');
t.is(response[1].lastName, 'Pollich');
t.is(response[1].email, 'malika@watsicatowne.com');
t.is(response[1].phone, null);
t.is(response[1].fax, null);
t.is(response[1].primaryContact, false);
t.is(response[1].createdAt, '2018-06-06T17:45:43Z');
t.is(response[1].updatedAt, '2018-06-06T17:45:43Z');
t.truthy(response[1].site);
t.truthy(response[1].site instanceof Site);
t.is(response[1].site.id, 1);
t.is(response[1].site.type, 'trials__sites');
});
// Site.getSiteLanguages method
/**
* @test {Site.getSiteLanguages}
*/
test.serial('[unit] Site.getSiteLanguages should throw an error when there is no authToken or invalid params', async (t) => {
const { siteJsonApi } = t.context;
const site = new Site({ data: siteJsonApi });
client.authToken = undefined;
const title = `SiteService.getChildren error`;
await t.throwsAsync(site.getSiteLanguages(site, { type: 'trials__site_supported_language' }), `${title}: requires authToken`);
});
/**
* @test {Site.getSiteLanguages}
*/
test(`[unit] Site.getSiteLanguages should receive a valid request and response for a get request without an id`, async (t) => {
const { siteJsonApi, getResponseLanguages } = t.context;
const site = new Site({ data: siteJsonApi });
site.id = 59;
let request = {};
nock(client.apiBaseUrl).get(`/v3/trials/sites/${site.id}/trials/site_supported_languages`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseLanguages];
});
const response = await site.getSiteLanguages();
t.is(request.path, `/v3/trials/sites/${site.id}/trials/site_supported_languages`);
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 SiteLanguage);
t.is(response[0].id, 3);
t.is(response[0].type, 'trials__site_supported_languages');
t.is(response[0].createdAt, '2018-05-18T02:35:20Z');
t.is(response[0].updatedAt, '2018-05-18T02:35:20Z');
t.truthy(response[0].language);
t.truthy(response[0].language instanceof Language);
t.is(response[0].language.id, 33);
t.is(response[0].language.type, 'languages');
t.is(response[0].language.iso, 'dummy_1808');
t.is(response[0].language.name, 'maiores');
t.is(response[0].language.isDefault, false);
t.is(response[0].site.id, 59);
t.is(response[0].site.type, 'trials__sites');
t.truthy(response[1] instanceof SiteLanguage);
t.is(response[1].id, 4);
t.is(response[1].type, 'trials__site_supported_languages');
t.is(response[1].createdAt, '2018-05-18T02:35:20Z');
t.is(response[1].updatedAt, '2018-05-18T02:35:20Z');
t.truthy(response[1].language);
t.truthy(response[1].language instanceof Language);
t.is(response[1].language.id, 34);
t.is(response[1].language.type, 'languages');
t.is(response[1].language.iso, 'dummy_1809');
t.is(response[1].language.name, 'maiores');
t.is(response[1].language.isDefault, false);
t.is(response[1].site.id, 59);
t.is(response[1].site.type, 'trials__sites');
t.is(site.siteLanguages[0].id, 3);
t.is(site.siteLanguages[0].type, 'trials__site_supported_languages');
t.is(site.siteLanguages[0].createdAt, '2018-05-18T02:35:20Z');
t.is(site.siteLanguages[0].updatedAt, '2018-05-18T02:35:20Z');
t.is(site.siteLanguages[1].id, 4);
t.is(site.siteLanguages[1].type, 'trials__site_supported_languages');
t.is(site.siteLanguages[1].createdAt, '2018-05-18T02:35:20Z');
t.is(site.siteLanguages[1].updatedAt, '2018-05-18T02:35:20Z');
});
// Site.getMembers method
/**
* @test {Site.getMembers}
*/
test.serial('[unit] Site.getMembers should throw an error when there is no authToken or invalid params', async (t) => {
const { siteJsonApi } = t.context;
const site = new Site({ data: siteJsonApi });
client.authToken = undefined;
const title = `SiteService.getChildren error`;
await site.getMembers(site, { type: 'trials__site_members' })
.then(() => t.fail())
.catch(e => t.is(e.message, `${title}: requires authToken`));
});
/**
* @test {Site.getMembers}
*/
test(`[unit] Site.getMembers should receive a valid request and response for a get request without an id`, async (t) => {
const { siteJsonApi, getResponseMembersAll } = t.context;
const site = new Site({ data: siteJsonApi });
let request = {};
nock(client.apiBaseUrl).get(`/v3/trials/sites/${site.id}/trials/site_members`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseMembersAll];
});
const response = await site.getMembers();
t.is(request.path, `/v3/trials/sites/15/trials/site_members`);
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, 8);
t.truthy(response[0] instanceof SiteMember);
t.is(response[0].id, 21);
t.is(response[0].type, 'trials__site_members');
t.is(response[0].memberType, 'pi');
t.is(response[0].createdAt, '2018-02-07T17:48:12Z');
t.is(response[0].updatedAt, '2018-02-07T17:48:12Z');
t.truthy(response[0].mobileUser);
t.is(response[0].mobileUser.id, 47);
t.is(response[0].mobileUser.accountName, 'dummy_47');
t.is(response[0].mobileUser.createdAt, '2018-02-07T17:48:12Z');
t.is(response[0].mobileUser.email, 'user61@fake.com');
t.is(response[0].mobileUser.updatedAt, '2018-02-07T17:48:12Z');
t.is(response[0].mobileUser.uuid, 'd140c877-5414-46ca-b96f-60fd74461e83');
t.truthy(response[1] instanceof SiteMember);
t.is(response[1].id, 22);
t.is(response[1].type, 'trials__site_members');
t.is(response[1].memberType, 'pi');
t.is(response[1].createdAt, '2018-02-07T17:48:12Z');
t.is(response[1].updatedAt, '2018-02-07T17:48:12Z');
t.truthy(response[2] instanceof SiteMember);
t.is(response[2].id, 23);
t.is(response[2].type, 'trials__site_members');
t.is(response[2].memberType, 'pi');
t.is(response[2].createdAt, '2018-02-07T17:48:12Z');
t.is(response[2].updatedAt, '2018-02-07T17:48:12Z');
});
/**
* @test {Site.getMembers}
*/
test('[unit] Site.getMembers should receive a valid response for a get request with an id', async (t) => {
const { siteJsonApi, getResponseMembersId } = t.context;
const site = new Site({ data: siteJsonApi });
let request = {};
nock(client.apiBaseUrl).get(`/v3/trials/sites/${site.id}/trials/site_members/20`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseMembersId];
});
const response = await site.getMembers({ id: 20 });
t.is(request.path, `/v3/trials/sites/15/trials/site_members/20`);
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 SiteMember);
t.is(response.id, 20);
t.is(response.type, 'trials__site_members');
t.is(response.memberType, 'pi');
t.is(response.createdAt, '2018-02-07T17:48:11Z');
t.is(response.updatedAt, '2018-02-07T17:48:11Z');
t.truthy(response.mobileUser);
t.is(response.mobileUser.id, 46);
t.is(response.mobileUser.accountName, 'dummy_46');
t.is(response.mobileUser.createdAt, '2018-02-07T17:48:11Z');
t.is(response.mobileUser.email, 'user58@fake.com');
t.is(response.mobileUser.updatedAt, '2018-02-07T17:48:11Z');
t.is(response.mobileUser.uuid, '21660223-8d6f-4a5e-ba1a-581656e65b4e');
t.is(response.site.type, 'trials__sites');
});
// Site.save
/**
* @test {Site.save}
*/
test('[unit] Site.save should successfully insert a site with a site object when id does not exist', async (t) => {
const { siteJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(siteJsonApi));
delete json.id;
const site = new Site({ data: json });
let request = {};
// nock(client.apiBaseUrl).post(`/v3/trials/sites`).reply(201, t.context.insertResponse);
nock(client.apiBaseUrl).post(`/v3/trials/sites`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await site.save();
t.is(request.path, `/v3/trials/sites`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: json });
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, 'trials__sites');
});
/**
* @test {Site.save}
*/
test('[unit] Site.save should successfully update a site with a site object when id exists', async (t) => {
const { siteJsonApi, updateResponse } = t.context;
const site = new Site({ data: siteJsonApi });
let request = {};
nock(client.apiBaseUrl).patch(`/v3/trials/sites/${site.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
const response = await site.save();
t.is(request.path, `/v3/trials/sites/${site.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: siteJsonApi });
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 4);
t.is(response.type, 'trials__sites');
});