test/unit/services.user.v2.related-mobile-users.js
import test from 'ava';
import sinon from 'sinon';
import { client, userService } from '../../src';
test.before('start server', () => {
client.apiBaseUrl = 'https://somesite.Clinical6.com';
});
test.after('server shut down', () => {});
test.beforeEach((t) => {
client.authToken = 'valid_token';
client.cache = 'never';
t.context.server = sinon.fakeServer.create();
t.context.server.autoRespond = true;
t.context.server.respondWith('DELETE', `${client.apiBaseUrl}/api/related_mobile_users/1800`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({ invited: [] })]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/related_mobile_users/1800/disable`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({ invited: [] })]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/related_mobile_users/1800/enable`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({ invited: [] })]);
t.context.server.respondWith('GET', `${client.apiBaseUrl}/api/related_mobile_users`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
followed_users: [],
followers: [{
id: 1797,
first_name: 'Barak',
last_name: null,
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
}, {
id: 1800,
first_name: 'MT',
last_name: null,
phone: '555-555-5555',
email: 'gmailsemail@gmail.com',
account_name: '58051208',
avatar: null,
disabled: false,
invitation_status: 'pending',
user_role: 'Care Circle',
unread: 0,
conversation_id: null
}
]
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/related_mobile_users`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({
invited: {
id: 1797,
first_name: 'Barak',
last_name: null,
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
}
})]);
t.context.server.respondWith('POST', `${client.apiBaseUrl}/api/related_mobile_users/1797/reinvite`,
[200, { 'Content-Type': 'application/json' }, JSON.stringify({ status: 'ok' })]);
});
test.afterEach(t => t.context.server.restore());
// UserService.deleteRelated method
/**
* @test {UserService.deleteRelated}
*/
test.serial('[unit] UserService.deleteRelated should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'UserService.deleteRelated error: requires authToken and id is not defined';
t.throws(() => userService.deleteRelated(), expectedError);
});
/**
* @test {UserService.deleteRelated}
*/
test('[unit] UserService.deleteRelated should throw an error when the id paramater is missing', (t) => {
client.authToken = 'valid_token';
const expectedError = 'UserService.deleteRelated error: id is not defined';
t.throws(() => userService.deleteRelated(), expectedError);
});
// /**
// * @test {UserService.deleteRelated}
// */
// test('[unit] UserService.deleteRelated should make a properly formatted DELETE request with the id parameter', async (t) => {
// client.authToken = 'valid_token';
// await userService.deleteRelated(1800);
// const request = t.context.server.requests[0];
// t.is(request.method, 'DELETE');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users/1800`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// });
/**
* @test {UserService.deleteRelated}
*/
test('[unit] UserService.deleteRelated should successfully remove a related mobile user with an id parameter', async (t) => {
client.authToken = 'valid_token';
const response = await userService.deleteRelated(1800);
// const request = t.context.server.requests[0];
// t.is(request.method, 'DELETE');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users/1800`);
// 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.invited.length, 0);
});
// UserService.disable method
/**
* @test {UserService.disable}
*/
test.serial('[unit] UserService.disable should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'UserService.disable error: requires authToken and id is not defined';
t.throws(() => userService.disable(), expectedError);
});
/**
* @test {UserService.disable}
*/
test('[unit] UserService.disable should throw an error when the id paramater is missing', (t) => {
client.authToken = 'valid_token';
const expectedError = 'UserService.disable error: id is not defined';
t.throws(() => userService.disable(), expectedError);
});
// /**
// * @test {UserService.disable}
// */
// test('[unit] UserService.disable should make a properly formatted POST request with the id parameter', async (t) => {
// client.authToken = 'valid_token';
// await userService.disable(1800);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users/1800/disable`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestBody, JSON.stringify({}));
// });
/**
* @test {UserService.disable}
*/
test('[unit] UserService.disable should successfully disable a related mobile user with an id parameter', async (t) => {
client.authToken = 'valid_token';
const response = await userService.disable(1800);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users/1800/disable`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({}));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.invited.length, 0);
});
// UserService.enable method
/**
* @test {UserService.enable}
*/
test.serial('[unit] UserService.enable should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'UserService.enable error: requires authToken and id is not defined';
t.throws(() => userService.enable(), expectedError);
});
/**
* @test {UserService.enable}
*/
test('[unit] UserService.enable should throw an error when the id paramater is missing', (t) => {
client.authToken = 'valid_token';
const expectedError = 'UserService.enable error: id is not defined';
t.throws(() => userService.enable(), expectedError);
});
// /**
// * @test {UserService.enable}
// */
// test('[unit] UserService.enable should make a properly formatted POST request with the id parameter', async (t) => {
// client.authToken = 'valid_token';
// await userService.enable(1800);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users/1800/enable`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestBody, JSON.stringify({}));
// });
/**
* @test {UserService.enable}
*/
test('[unit] UserService.enable should successfully enable a related mobile user with an id parameter', async (t) => {
client.authToken = 'valid_token';
const response = await userService.enable(1800);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users/1800/enable`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({}));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.invited.length, 0);
});
// UserService.getRelatedMobileUsers method
/**
* @test {UserService.getRelatedMobileUsers}
*/
test.serial('[unit] UserService.getRelatedMobileUsers should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'UserService.getRelatedMobileUsers error: requires authToken';
t.throws(() => userService.getRelatedMobileUsers(), expectedError);
});
// /**
// * @test {UserService.getRelatedMobileUsers}
// */
// test('[unit] UserService.getRelatedMobileUsers should make a properly formatted get request', async (t) => {
// // Need to set an AuthToken for this call to work
// client.authToken = 'valid_token';
// await userService.getRelatedMobileUsers();
// const request = t.context.server.requests[0];
// t.is(request.method, 'GET');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users`);
// 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');
// });
/**
* @test {UserService.getRelatedMobileUsers}
*/
test('[unit] UserService.getRelatedMobileUsers should receive a valid response for a get request', async (t) => {
// setup
client.authToken = 'valid_token';
const response = await userService.getRelatedMobileUsers();
t.truthy(response);
t.is(response.followers.length, 2);
t.is(response.followers[0].id, 1797);
// t.is(response.followers[0].firstName, 'Barak');
// t.is(response.followers[0].lastName, null);
// t.is(response.followers[0].phone, '555-555-5555');
t.is(response.followers[0].email, 'bobama@parallel6.com');
t.is(response.followers[0].accountName, '78473136');
// t.is(response.followers[0].avatar, null);
// t.is(response.followers[0].disabled, false);
// t.is(response.followers[0].invitationStatus, 'pending');
// t.is(response.followers[0].userRole, 'Care Circle');
// t.is(response.followers[0].unread, 0);
// t.is(response.followers[0].conversationId, null);
t.is(response.followers[1].id, 1800);
// t.is(response.followers[1].firstName, 'MT');
// t.is(response.followers[1].lastName, null);
// t.is(response.followers[1].phone, '555-555-5555');
t.is(response.followers[1].email, 'gmailsemail@gmail.com');
t.is(response.followers[1].accountName, '58051208');
// t.is(response.followers[1].avatar, null);
// t.is(response.followers[1].disabled, false);
// t.is(response.followers[1].invitationStatus, 'pending');
// t.is(response.followers[1].userRole, 'Care Circle');
// t.is(response.followers[1].unread, 0);
// t.is(response.followers[1].conversationId, null);
});
// UserService.resendInvite method
/**
* @test {UserService.resendInvite}
*/
test.serial('[unit] UserService.resendInvite should throw an error when there is no authToken', (t) => {
client.authToken = undefined;
const expectedError = 'UserService.resendInvite error: requires authToken and id is not defined';
t.throws(() => userService.resendInvite(), expectedError);
});
/**
* @test {UserService.resendInvite}
*/
test('[unit] UserService.resendInvite should throw an error when the id paramater is missing', (t) => {
client.authToken = 'valid_token';
const expectedError = 'UserService.resendInvite error: id is not defined';
t.throws(() => userService.resendInvite(), expectedError);
});
// /**
// * @test {UserService.resendInvite}
// */
// test('[unit] UserService.resendInvite should make a properly formatted POST request with the id parameter', async (t) => {
// client.authToken = 'valid_token';
// await userService.resendInvite(1797);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users/1797/reinvite`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestBody, JSON.stringify({}));
// });
/**
* @test {UserService.resendInvite}
*/
test('[unit] UserService.resendInvite should successfully resend an invite to a related mobile user with an id parameter', async (t) => {
client.authToken = 'valid_token';
const response = await userService.resendInvite(1797);
// const request = t.context.server.requests[0];
// t.is(request.method, 'POST');
// t.is(request.url, `${client.apiBaseUrl}/api/related_mobile_users/1797/reinvite`);
// t.is(request.requestHeaders.Accept, 'application/json');
// t.is(request.requestBody, JSON.stringify({}));
// t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
t.is(response.status, 'ok');
});