test/unit/helpers.cohort.js
import test from 'ava';
import nock from 'nock';
import {
client,
Cohort,
CohortAssignment,
Profile,
User,
} from '../../src';
test.before('start server', (t) => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
client.authToken = 'valid_token';
t.context.getResponseAssignments = {
data: [
{
id: 14,
type: 'cohort_assignments',
attributes: {
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z'
},
relationships: {
user: {
data: {
id: 261,
type: 'users'
}
},
cohort: {
data: {
id: 73,
type: 'cohorts'
}
}
}
},
{
id: 15,
type: 'cohort_assignments',
attributes: {
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z'
},
relationships: {
user: {
data: {
id: 262,
type: 'users'
}
},
cohort: {
data: {
id: 73,
type: 'cohorts'
}
}
}
},
{
id: 16,
type: 'cohort_assignments',
attributes: {
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z'
},
relationships: {
user: {
data: {
id: 263,
type: 'users'
}
},
cohort: {
data: {
id: 73,
type: 'cohorts'
}
}
}
}
],
included: [
{
id: 261,
type: 'users',
attributes: {
uuid: '05cbf8db-3eda-4269-8761-83496100161b',
email: 'user652@fake.com',
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z',
invitation_sent_at: null,
invitation_accepted_at: null,
disabled_at: null
},
relationships: {
user_role: {
data: {
id: 292,
type: 'user_roles'
}
},
devices: {
data: []
},
profile: {
data: {
id: 665,
type: 'profiles'
}
}
}
},
{
id: 665,
type: 'profiles',
attributes: {
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z',
first_name: 'Nannie',
middle_initial: null,
last_name: 'Wilderman',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/665/1.png',
small: {
url: '/uploads/test/profile/avatar/665/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/665/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/665/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user652@fake.com',
age: 25
},
relationships: {
profileable: {
data: {
id: 261,
type: 'users'
}
},
language: {
data: {
id: 1,
type: 'languages'
}
}
}
},
{
id: 262,
type: 'users',
attributes: {
uuid: 'b886b58b-aeab-4c8f-b03d-7e6b461fdd91',
email: 'user653@fake.com',
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z',
invitation_sent_at: null,
invitation_accepted_at: null,
disabled_at: null
},
relationships: {
user_role: {
data: {
id: 292,
type: 'user_roles'
}
},
devices: {
data: []
},
profile: {
data: {
id: 666,
type: 'profiles'
}
}
}
},
{
id: 666,
type: 'profiles',
attributes: {
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z',
first_name: 'Joseph',
middle_initial: null,
last_name: 'Dietrich',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/666/1.png',
small: {
url: '/uploads/test/profile/avatar/666/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/666/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/666/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user653@fake.com',
age: 25
},
relationships: {
profileable: {
data: {
id: 262,
type: 'users'
}
},
language: {
data: {
id: 1,
type: 'languages'
}
}
}
},
{
id: 263,
type: 'users',
attributes: {
uuid: '2bb4b0ac-fb18-4341-b765-e7bd6188a795',
email: 'user654@fake.com',
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z',
invitation_sent_at: null,
invitation_accepted_at: null,
disabled_at: null
},
relationships: {
user_role: {
data: {
id: 292,
type: 'user_roles'
}
},
devices: {
data: []
},
profile: {
data: {
id: 667,
type: 'profiles'
}
}
}
},
{
id: 667,
type: 'profiles',
attributes: {
created_at: '2018-05-09T19:55:25Z',
updated_at: '2018-05-09T19:55:25Z',
first_name: 'Maurice',
middle_initial: null,
last_name: 'Gibson',
prefix: null,
suffix: null,
gender: 'male',
ethnicity: '',
occupation: null,
avatar: {
url: '/uploads/test/profile/avatar/667/1.png',
small: {
url: '/uploads/test/profile/avatar/667/small_1.png'
},
main: {
url: '/uploads/test/profile/avatar/667/main_1.png'
},
thumb: {
url: '/uploads/test/profile/avatar/667/thumb_1.png'
}
},
username: null,
dob: null,
timezone: 'UTC',
phone: null,
email: 'user654@fake.com',
age: 25
},
relationships: {
profileable: {
data: {
id: 263,
type: 'users'
}
},
language: {
data: {
id: 1,
type: 'languages'
}
}
}
}
]
};
t.context.getResponseAssignment20 = {
data: {
id: '20',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-17T19:59:53Z',
updated_at: '2018-04-17T19:59:53Z'
},
relationships: {
user: {
data: {
id: '278',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '2',
type: 'cohorts'
}
}
}
},
included: [
{
id: '278',
type: 'mobile_users',
attributes: {
uuid: '7731e5db-3c94-4596-8a52-cb753b9d23bd',
email: 'user418@fake.com',
created_at: '2018-04-17T19:59:54Z',
updated_at: '2018-04-17T19:59:54Z',
invitation_sent_at: null,
invitation_accepted_at: null,
disabled_at: null
},
relationships: {
user_role: {
data: {
id: '192',
type: 'user_roles'
}
},
devices: {
data: []
},
profile: {
data: {
id: '431',
type: 'profiles'
}
}
}
}
]
};
t.context.updateResponse = {
data: {
id: '25',
type: 'cohorts',
attributes: {
name: 'new-name',
cohort_type: 'dynamic',
created_at: '2018-04-17T16:14:08Z',
updated_at: '2018-04-17T16:14:08Z'
},
relationships: {
filter_group: {
data: null
}
}
}
};
t.context.insertResponse = {
data: {
id: 1,
type: 'cohorts',
attributes: {
name: 'name',
cohort_type: 'static',
created_at: '2018-04-03T14:16:25Z',
updated_at: '2018-04-03T14:16:25Z'
}
}
};
t.context.insertResponseAssignment = {
data: {
id: '1',
type: 'cohort_assignments',
attributes: {
created_at: '2018-04-17T19:59:53Z',
updated_at: '2018-04-17T19:59:53Z'
},
relationships: {
user: {
data: {
id: '278',
type: 'mobile_users'
}
},
cohort: {
data: {
id: '1',
type: 'cohorts'
}
}
}
}
};
});
test.beforeEach((t) => {
client.cache = 'never';
client.authToken = 'valid_token';
t.context.storage = client.storageUtility;
t.context.cohortJsonApi = {
id: 1,
type: 'cohorts',
attributes: {
name: 'name',
cohort_type: 'static',
created_at: '2018-04-03T14:16:25Z',
updated_at: '2018-04-03T14:16:25Z'
}
};
t.context.cohortJson = {
id: 1,
type: 'cohorts',
name: 'name',
cohort_type: 'static',
created_at: '2018-04-03T14:16:25Z',
updated_at: '2018-04-03T14:16:25Z'
};
t.context.allowedAction = new Cohort(t.context.allowedActionJsonApi);
t.context.cohortAssignmentJsonApi = {
id: 1,
type: 'cohort_assignments',
attributes: {},
relationships: {
user: {
data: {
id: 278,
type: 'mobile_users'
}
},
cohort: {
data: {
id: 1,
type: 'cohorts'
}
}
}
};
});
/**
* @test {Cohort}
*/
test('[unit] Cohort should handle allowedAction data with a normal json format', (t) => {
const { cohortJson } = t.context;
const cohort = new Cohort(cohortJson);
t.is(cohort.id, 1);
t.is(cohort.type, 'cohorts');
t.is(cohort.createdAt, '2018-04-03T14:16:25Z');
t.is(cohort.updatedAt, '2018-04-03T14:16:25Z');
t.is(cohort.name, 'name');
t.is(cohort.cohortType, 'static');
});
/**
* @test {Cohort}
*/
test('[unit] Cohort should handle cohort data with json api format', (t) => {
const { cohortJsonApi } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
t.is(cohort.id, 1);
t.is(cohort.type, 'cohorts');
t.is(cohort.createdAt, '2018-04-03T14:16:25Z');
t.is(cohort.updatedAt, '2018-04-03T14:16:25Z');
t.is(cohort.name, 'name');
t.is(cohort.cohortType, 'static');
});
/**
* @test {Cohort}
*/
test('[unit] Cohort should generate json api format when converted to string', (t) => {
const { cohortJsonApi } = t.context;
let cohort = new Cohort({ data: cohortJsonApi });
let json = cohort.toJSON();
t.deepEqual(json, cohortJsonApi);
cohort = new Cohort({ data: cohortJsonApi });
json = cohort.toJSON();
t.deepEqual(json, cohortJsonApi);
});
// Cohort.addUser
/**
* @test {Cohort.addUser}
*/
test('[unit] Cohort.addUser should successfully insert a cohortAssignment with a user and cohort when cohortAssignment.id does not exist', async (t) => {
const { cohortJsonApi, cohortAssignmentJsonApi, insertResponseAssignment } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
const user = new User({ id: 278 });
let request = {};
nock(client.apiBaseUrl).post(`/v3/cohort_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponseAssignment];
});
const response = await cohort.addUser(user);
t.is(request.path, `/v3/cohort_assignments`);
t.is(request.headers.accept, 'application/json');
const json = JSON.parse(JSON.stringify(cohortAssignmentJsonApi));
delete json.id;
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, 1);
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.is(cohort.assignments[0].id, 1);
t.is(cohort.assignments[0].type, 'cohort_assignments');
t.is(cohort.assignments[0].createdAt, '2018-04-17T19:59:53Z');
t.is(cohort.assignments[0].updatedAt, '2018-04-17T19:59:53Z');
t.is(cohort.users[0].id, 278);
t.is(cohort.users[0].type, 'mobile_users');
});
// Cohort.delete method
/**
* @test {Cohort.delete}
*/
test('[unit] Cohort.delete should successfully delete a cohort', async (t) => {
const { cohortJsonApi } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
let request = {};
nock(client.apiBaseUrl).delete(`/v3/cohorts/${cohort.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, ''];
});
const response = await cohort.delete();
t.is(request.path, `/v3/cohorts/${cohort.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);
});
// Cohort.getAssignments method
/**
* @test {Cohort.getAssignments}
*/
test.serial('[unit] Cohort.getAssignments should throw an error when there is no authToken or invalid params', async (t) => {
const { cohortJsonApi } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
client.authToken = undefined;
const title = `CohortService.getChildren error`;
await t.throwsAsync(cohort.getAssignments(cohort, { type: 'cohort_assignments' }), `${title}: requires authToken`);
});
/**
* @test {Cohort.getAssignments}
*/
test(`[unit] Cohort.getAssignments should receive a valid request and response for a get request without an id`, async (t) => {
const { cohortJsonApi, getResponseAssignments } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
let request = {};
nock(client.apiBaseUrl).get(`/v3/cohorts/${cohort.id}/cohort_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAssignments];
});
const response = await cohort.getAssignments();
t.is(request.path, `/v3/cohorts/${cohort.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, 14);
t.is(response[0].type, 'cohort_assignments');
t.is(response[0].createdAt, '2018-05-09T19:55:25Z');
t.is(response[0].updatedAt, '2018-05-09T19:55:25Z');
t.truthy(response[0].user);
t.truthy(response[0].user instanceof User);
t.is(response[0].user.id, 261);
t.is(response[0].user.type, 'users');
t.is(response[0].user.createdAt, '2018-05-09T19:55:25Z');
t.is(response[0].user.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[0].user.email, 'user652@fake.com');
t.is(response[0].user.uuid, '05cbf8db-3eda-4269-8761-83496100161b');
t.truthy(response[0].user.profile instanceof Profile);
t.is(response[0].user.profile.id, 665);
t.is(response[0].user.profile.type, 'profiles');
t.is(response[0].user.profile.createdAt, '2018-05-09T19:55:25Z');
t.is(response[0].user.profile.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[0].user.profile.firstName, 'Nannie');
t.is(response[0].user.profile.lastName, 'Wilderman');
t.truthy(response[1] instanceof CohortAssignment);
t.is(response[1].id, 15);
t.is(response[1].type, 'cohort_assignments');
t.is(response[1].createdAt, '2018-05-09T19:55:25Z');
t.is(response[1].updatedAt, '2018-05-09T19:55:25Z');
t.truthy(response[1].user);
t.truthy(response[1].user instanceof User);
t.is(response[1].user.id, 262);
t.is(response[1].user.type, 'users');
t.is(response[1].user.createdAt, '2018-05-09T19:55:25Z');
t.is(response[1].user.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[1].user.email, 'user653@fake.com');
t.is(response[1].user.uuid, 'b886b58b-aeab-4c8f-b03d-7e6b461fdd91');
t.truthy(response[1].user.profile instanceof Profile);
t.is(response[1].user.profile.id, 666);
t.is(response[1].user.profile.type, 'profiles');
t.is(response[1].user.profile.createdAt, '2018-05-09T19:55:25Z');
t.is(response[1].user.profile.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[1].user.profile.firstName, 'Joseph');
t.is(response[1].user.profile.lastName, 'Dietrich');
t.truthy(response[2] instanceof CohortAssignment);
t.is(response[2].id, 16);
t.is(response[2].type, 'cohort_assignments');
t.is(response[2].createdAt, '2018-05-09T19:55:25Z');
t.is(response[2].updatedAt, '2018-05-09T19:55:25Z');
t.truthy(response[2].user);
t.truthy(response[2].user instanceof User);
t.is(response[2].user.id, 263);
t.is(response[2].user.type, 'users');
t.is(response[2].user.createdAt, '2018-05-09T19:55:25Z');
t.is(response[2].user.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[2].user.email, 'user654@fake.com');
t.is(response[2].user.uuid, '2bb4b0ac-fb18-4341-b765-e7bd6188a795');
t.truthy(response[2].user.profile instanceof Profile);
t.is(response[2].user.profile.id, 667);
t.is(response[2].user.profile.type, 'profiles');
t.is(response[2].user.profile.createdAt, '2018-05-09T19:55:25Z');
t.is(response[2].user.profile.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[2].user.profile.firstName, 'Maurice');
t.is(response[2].user.profile.lastName, 'Gibson');
t.is(cohort.assignments[0].id, 14);
t.is(cohort.assignments[0].type, 'cohort_assignments');
t.is(cohort.assignments[0].createdAt, '2018-05-09T19:55:25Z');
t.is(cohort.assignments[0].updatedAt, '2018-05-09T19:55:25Z');
t.truthy(cohort.users[0]);
t.is(cohort.users[0].id, 261);
t.is(cohort.users[0].type, 'users');
t.is(cohort.users[0].createdAt, '2018-05-09T19:55:25Z');
t.is(cohort.users[0].updatedAt, '2018-05-09T19:55:25Z');
t.is(cohort.users[0].email, 'user652@fake.com');
t.is(cohort.users[0].uuid, '05cbf8db-3eda-4269-8761-83496100161b');
t.truthy(cohort.users[0].profile instanceof Profile);
t.is(cohort.users[0].profile.id, 665);
t.is(cohort.users[0].profile.type, 'profiles');
t.is(cohort.users[0].profile.createdAt, '2018-05-09T19:55:25Z');
t.is(cohort.users[0].profile.updatedAt, '2018-05-09T19:55:25Z');
t.is(cohort.users[0].profile.firstName, 'Nannie');
t.is(cohort.users[0].profile.lastName, 'Wilderman');
});
/**
* @test {Cohort.getAssignments}
*/
test('[unit] Cohort.getAssignments should receive a valid response for a get request with an id', async (t) => {
const { cohortJsonApi, getResponseAssignment20 } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
const cohortAssignment = new CohortAssignment({ id: 20 });
let request = {};
nock(client.apiBaseUrl).get(`/v3/cohorts/${cohort.id}/cohort_assignments/${cohortAssignment.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAssignment20];
});
const response = await cohort.getAssignments({ id: 20 });
t.is(request.path, `/v3/cohorts/${cohort.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.getUsers method
/**
* @test {Cohort.getUsers}
*/
test.serial('[unit] Cohort.getUsers should throw an error when there is no authToken or invalid params', async (t) => {
const { cohortJsonApi } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
client.authToken = undefined;
const title = `CohortService.getChildren error`;
await t.throwsAsync(cohort.getUsers(cohort, { type: 'cohort_assignments' }), `${title}: requires authToken`);
});
/**
* @test {Cohort.getUsers}
*/
test(`[unit] Cohort.getUsers should receive a valid request and response for a get request without an id`, async (t) => {
const { cohortJsonApi, getResponseAssignments } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
let request = {};
nock(client.apiBaseUrl).get(`/v3/cohorts/${cohort.id}/cohort_assignments`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, getResponseAssignments];
});
const response = await cohort.getUsers();
t.is(request.path, `/v3/cohorts/${cohort.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 User);
t.is(response[0].id, 261);
t.is(response[0].type, 'users');
t.is(response[0].createdAt, '2018-05-09T19:55:25Z');
t.is(response[0].updatedAt, '2018-05-09T19:55:25Z');
t.is(response[0].email, 'user652@fake.com');
t.is(response[0].uuid, '05cbf8db-3eda-4269-8761-83496100161b');
t.truthy(response[0].profile instanceof Profile);
t.is(response[0].profile.id, 665);
t.is(response[0].profile.type, 'profiles');
t.is(response[0].profile.createdAt, '2018-05-09T19:55:25Z');
t.is(response[0].profile.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[0].profile.firstName, 'Nannie');
t.is(response[0].profile.lastName, 'Wilderman');
t.truthy(response[1] instanceof User);
t.is(response[1].id, 262);
t.is(response[1].type, 'users');
t.is(response[1].createdAt, '2018-05-09T19:55:25Z');
t.is(response[1].updatedAt, '2018-05-09T19:55:25Z');
t.is(response[1].email, 'user653@fake.com');
t.is(response[1].uuid, 'b886b58b-aeab-4c8f-b03d-7e6b461fdd91');
t.truthy(response[1].profile instanceof Profile);
t.is(response[1].profile.id, 666);
t.is(response[1].profile.type, 'profiles');
t.is(response[1].profile.createdAt, '2018-05-09T19:55:25Z');
t.is(response[1].profile.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[1].profile.firstName, 'Joseph');
t.is(response[1].profile.lastName, 'Dietrich');
t.truthy(response[2] instanceof User);
t.is(response[2].id, 263);
t.is(response[2].type, 'users');
t.is(response[2].createdAt, '2018-05-09T19:55:25Z');
t.is(response[2].updatedAt, '2018-05-09T19:55:25Z');
t.is(response[2].email, 'user654@fake.com');
t.is(response[2].uuid, '2bb4b0ac-fb18-4341-b765-e7bd6188a795');
t.truthy(response[2].profile instanceof Profile);
t.is(response[2].profile.id, 667);
t.is(response[2].profile.type, 'profiles');
t.is(response[2].profile.createdAt, '2018-05-09T19:55:25Z');
t.is(response[2].profile.updatedAt, '2018-05-09T19:55:25Z');
t.is(response[2].profile.firstName, 'Maurice');
t.is(response[2].profile.lastName, 'Gibson');
});
// Cohort.removeUser
/**
* @test {Cohort.removeUser}
*/
test('[unit] Cohort.removeUser should successfully remove a cohortAssignment with a user and cohort given a user', async (t) => {
const { cohortJsonApi, getResponseAssignments } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
cohort.id = 9;
const user = new User({ id: 261 });
let request = {};
nock(client.apiBaseUrl).get(`/v3/cohorts/${cohort.id}/cohort_assignments`).reply(200, getResponseAssignments);
nock(client.apiBaseUrl).delete(`/v3/cohort_assignments/14`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [204, ''];
});
const response = await cohort.removeUser(user);
t.is(request.path, `/v3/cohort_assignments/14`);
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);
});
// Cohort.removeUser
/**
* @test {Cohort.removeUser}
*/
test('[unit] Cohort.removeUser should successfully remove a cohortAssignment with a user and cohort given an assignment', async (t) => {
const { cohortJsonApi, getResponseAssignments } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
cohort.id = 9;
const assignment = new CohortAssignment({ id: 5 });
let request = {};
nock(client.apiBaseUrl).get(`/v3/cohorts/${cohort.id}/cohort_assignments`).reply(200, getResponseAssignments);
nock(client.apiBaseUrl).delete(`/v3/cohort_assignments/5`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [204, ''];
});
const response = await cohort.removeUser(assignment);
t.is(request.path, `/v3/cohort_assignments/5`);
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);
});
// Cohort.save
/**
* @test {Cohort.save}
*/
test('[unit] Cohort.save should successfully insert a cohort with a cohort object when id does not exist', async (t) => {
const { cohortJsonApi, insertResponse } = t.context;
const json = JSON.parse(JSON.stringify(cohortJsonApi));
delete json.id;
const cohort = new Cohort({ data: json });
let request = {};
nock(client.apiBaseUrl).post(`/v3/cohorts`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [201, insertResponse];
});
const response = await cohort.save();
t.is(request.path, `/v3/cohorts`);
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, 1);
t.is(response.type, 'cohorts');
t.is(response.createdAt, '2018-04-03T14:16:25Z');
t.is(response.updatedAt, '2018-04-03T14:16:25Z');
t.is(response.name, 'name');
t.is(response.cohortType, 'static');
});
/**
* @test {Cohort.save}
*/
test('[unit] Cohort.save should successfully update a cohort with a cohort object when id exists', async (t) => {
const { cohortJsonApi, updateResponse } = t.context;
const cohort = new Cohort({ data: cohortJsonApi });
let request = {};
nock(client.apiBaseUrl).patch(`/v3/cohorts/${cohort.id}`).reply(function (uri, requestBody) {
request = this.req;
request.requestBody = requestBody;
return [200, updateResponse];
});
const response = await cohort.save();
t.is(request.path, `/v3/cohorts/${cohort.id}`);
t.is(request.headers.accept, 'application/json');
t.deepEqual(request.requestBody, { data: cohortJsonApi });
t.is(request.headers['content-type'], 'application/json');
t.is(request.headers.authorization, 'Token token=valid_token');
t.is(response.id, 25);
t.is(response.type, 'cohorts');
t.is(response.createdAt, '2018-04-17T16:14:08Z');
t.is(response.updatedAt, '2018-04-17T16:14:08Z');
t.is(response.name, 'new-name');
t.is(response.cohortType, 'dynamic');
});