Manual Reference Source Test

test/unit/services.user.v2.js

import test from 'ava';
import nock from 'nock';
import { client, Device, UserService } from '../../src';

const userService = new UserService('mobile_users');
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';
  client.device = new Device({
    app_version: undefined,
    push_id: 'FAKE_ID',
    technology: undefined,
    udid: undefined,
  });

  nock(client.apiBaseUrl).put(`/api/mobile_users/accept_invitation`).reply(200, { status: 'ok' });

  nock(client.apiBaseUrl).delete(`/api/mobile_users`).reply(200, { status: 'ok' });

  nock(client.apiBaseUrl).delete(`/api/mobile_users/disable`).reply(200, { status: 'ok' });

  nock(client.apiBaseUrl).post(`/api/mobile_users/forgot_account_name`).reply(200, { message: 'Account name sent!' });

  nock(client.apiBaseUrl).post(`/api/mobile_users/forgot_password`).reply(200, { message: 'Password reset instructions has been sent' });

  nock(client.apiBaseUrl).put(`/api/mobile_users/reset_password`).reply(200, {
    status: 'ok',
    message: 'Password has been successfully reset'
  });

  nock(client.apiBaseUrl).put(`/api/mobile_users/set_pin`).reply(200, { status: 'ok' });

  nock(client.apiBaseUrl).post(`/api/mobile_users/sign_in_guest`).reply(200, {
    status: 'ok',
    auth_token: 'valid_token',
    encryption_key: 'thisisaterribleencryptionkey',
    verification_status: 'unverified'
  });

  nock(client.apiBaseUrl).post(`/api/mobile_users/sign_in`).reply(200, {
    status: 'ok',
    auth_token: 'valid_token',
    encryption_key: 'thisisaterribleencryptionkey',
    verification_status: 'unverified'
  });

  nock(client.apiBaseUrl).delete(`/v3/mobile_users/sessions`).reply(200, { message: 'Signed out successfully.' });

  t.context.acceptInvitationPinV2Json = {
    pin: 123456,
    pin_confirmation: 123456,
    invitation_token: 'b0dcdf85785490bc47c782e4a87443dab489833fe2a7dc1a486d6c4da66bd490',
  };
  t.context.setPinJson = {
    pin: 123456,
    pin_confirmation: 123456,
  };
});

// UserService.acceptInvitationPinV2 method
/**
 * @test {UserService.acceptInvitationPinV2}
 */
test.serial('[unit] UserService.acceptInvitationPinV2 should throw an error when there is no authToken', (t) => {
  client.authToken = undefined;
  const expectedError = 'UserService.acceptInvitationPinV2 error: requires authToken and json is not defined';
  t.throws(() => userService.acceptInvitationPinV2(), expectedError);
});

/**
 * @test {UserService.acceptInvitationPinV2}
 */
test('[unit] UserService.acceptInvitationPinV2 should throw an error when a JSON value is not provided', (t) => {
  const expectedError = 'UserService.acceptInvitationPinV2 error: json is not defined';
  t.throws(() => userService.acceptInvitationPinV2(), expectedError);
});

// /**
//  * @test {UserService.acceptInvitationPinV2}
//  */
// test('[unit] UserService.acceptInvitationPinV2 should make a properly formatted PUT request', async (t) => {
//   const { acceptInvitationPinV2Json } = t.context;
//   await userService.acceptInvitationPinV2(acceptInvitationPinV2Json);
//   t.is(request.method, 'PUT');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/accept_invitation`);
//   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(request.requestBody, JSON.stringify({
//     pin: 123456,
//     pin_confirmation: 123456,
//     invitation_token: 'b0dcdf85785490bc47c782e4a87443dab489833fe2a7dc1a486d6c4da66bd490',
//   }));
// });

/**
 * @test {UserService.acceptInvitationPinV2}
 */
test('[unit] UserService.acceptInvitationPinV2 should successfully submit the JSON to the server', async (t) => {
  const { acceptInvitationPinV2Json } = t.context;
  const response = await userService.acceptInvitationPinV2(acceptInvitationPinV2Json);
  // t.is(request.method, 'PUT');
  // t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/accept_invitation`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify({
  //   pin: 123456,
  //   pin_confirmation: 123456,
  //   invitation_token: 'b0dcdf85785490bc47c782e4a87443dab489833fe2a7dc1a486d6c4da66bd490',
  // }));
  // 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');
});

// UserService.deleteCurrent method
/**
 * @test {UserService.deleteCurrent}
 */
test.serial('[unit] UserService.deleteCurrent should throw an error when there is no authToken', (t) => {
  client.authToken = undefined;
  const expectedError = 'UserService.deleteCurrent error: requires authToken';
  t.throws(() => userService.deleteCurrent(), expectedError);
});

// /**
//  * @test {UserService.deleteCurrent}
//  */
// test('[unit] UserService.deleteCurrent should make a properly formatted DELETE request', async (t) => {
//   await userService.deleteCurrent();
//   t.is(request.method, 'DELETE');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// });

/**
 * @test {UserService.deleteCurrent}
 */
test('[unit] UserService.deleteCurrent should delete the current mobile user', async (t) => {
  const response = await userService.deleteCurrent();
  // t.is(request.method, 'DELETE');
  // t.is(request.url, `${client.apiBaseUrl}/api/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');
  t.is(response.status, 'ok');
});

// UserService.disableCurrent method
/**
 * @test {UserService.disableCurrent}
 */
test.serial('[unit] UserService.disableCurrent should throw an error when there is no authToken', (t) => {
  client.authToken = undefined;
  const expectedError = 'UserService.disableCurrent error: requires authToken';
  t.throws(() => userService.disableCurrent(), expectedError);
});

// /**
//  * @test {UserService.disableCurrent}
//  */
// test('[unit] UserService.disableCurrent should make a properly formatted DELETE request', async (t) => {
//   await userService.disableCurrent();
//   t.is(request.method, 'DELETE');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/disable`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// });

/**
 * @test {UserService.disableCurrent}
 */
test('[unit] UserService.disableCurrent should disable the current mobile user', async (t) => {
  const response = await userService.disableCurrent();
  // t.is(request.method, 'DELETE');
  // t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/disable`);
  // 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.status, 'ok');
});

// UserService.emailAccountName method
/**
 * @test {UserService.emailAccountName}
 */
test('[unit] UserService.emailAccountName should throw an error when the required parameters are missing', (t) => {
  const expectedError = 'UserService.emailAccountName error: emailAddress is not defined';
  t.throws(() => userService.emailAccountName(), expectedError);
});

// /**
//  * @test {UserService.emailAccountName}
//  */
// test('[unit] UserService.emailAccountName should make a properly formatted POST request with the emailAddress parameter', async (t) => {
//   await userService.emailAccountName('email@parallel6.com');
//   t.is(request.method, 'POST');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/forgot_account_name`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestBody, JSON.stringify({ email: 'email@parallel6.com' }));
// });

/**
 * @test {UserService.emailAccountName}
 */
test('[unit] UserService.emailAccountName should successfully send an email with the forgotten user account name with all required parameters', async (t) => {
  const response = await userService.emailAccountName('email@parallel6.com');
  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/forgot_account_name`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify({ email: 'email@parallel6.com' }));
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  t.is(response.message, 'Account name sent!');
});

// UserService.emailPassword method
/**
 * @test {UserService.emailPassword}
 */
test('[unit] UserService.emailPassword should throw an error when there is no email', (t) => {
  const expectedError = 'UserService.emailPassword error: emailAddress is not defined';
  t.throws(() => userService.emailPassword(), expectedError);
});

/**
 * @test {UserService.emailPassword}
 */
// test('[unit] UserService.emailPassword should reply with a friendly error message for an invalid email address', async (t) => {
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.1'
//   });
//   const response = await userService.signIn();
//   request.respondWith({
//     status: 404,
//     statusText: 'fail',
//     response: `{
//               "error_detail": {
//                   "MobileUser": ["not_found"]
//               },
//               "friendly_error": "No user found with the specified email",
//               "internal_code": 50364,
//               "more_info": "",
//               "status": "fail",
//               "message": "No user found with the specified email"
//           }`,
//     headers: {
//       'Content-Type': 'application/json',
//     },
//   });
//   t.is(response.status, 'fail');

//   t.is(response.friendly, 'No user found with the specified email');

//   t.is(response.code, 50364);
// });

// /**
//  * @test {UserService.emailPassword}
//  */
// test('[unit] UserService.emailPassword should make a properly formatted post request', async (t) => {
//   await userService.emailPassword('testEmail@parallel6.com');
//   t.is(request.method, 'POST');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/forgot_password`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestBody, JSON.stringify({ email: 'testEmail@parallel6.com' }));
// });

/**
 * @test {UserService.emailPassword}
 */
test('[unit] UserService.emailPassword should receive valid response for a post request', async (t) => {
  const response = await userService.emailPassword('testEmail@parallel6.com');
  t.is(response.message, 'Password reset instructions has been sent');
});

// UserService.resetPasswordSimple method
/**
 * @test {UserService.resetPasswordSimple}
 */
test.serial('[unit] UserService.resetPasswordSimple UserService.resetPasswordSimple should throw an error when there is no authToken', (t) => {
  client.authToken = undefined;
  const expectedError = 'UserService.resetPasswordSimple error: requires authToken'
  + ' and newPassword is not defined and newPasswordConf is not defined and oldPassword is not defined';
  t.throws(() => userService.resetPasswordSimple(), expectedError);
});

/**
 * @test {UserService.resetPasswordSimple}
 */
test('[unit] UserService.resetPasswordSimple UserService.resetPasswordSimple should throw errors for invalid parameters', (t) => {
  const expectedError = 'UserService.resetPasswordSimple error: newPassword is not defined'
  + ' and newPasswordConf is not defined and oldPassword is not defined';
  t.throws(() => userService.resetPasswordSimple(), expectedError);
});

// /**
//  * @test {UserService.resetPasswordSimple}
//  */
// test('[unit] UserService.resetPasswordSimple UserService.resetPasswordSimple should make a properly formatted PUT request with the required parameters', async (t) => {
//   await userService.resetPasswordSimple('password', 'password', 'yHSSug');
//   t.is(request.method, 'PUT');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/reset_password`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestBody, JSON.stringify({
//     old_password: 'yHSSug',
//     new_password: 'password',
//     new_password_confirmation: 'password',
//   }));
// });

/**
 * @test {UserService.resetPasswordSimple}
 */
test('[unit] UserService.resetPasswordSimple UserService.resetPasswordSimple should reset a password with the required parameters', async (t) => {
  const response = await userService.resetPasswordSimple('password', 'password', 'yHSSug');
  // t.is(request.method, 'PUT');
  // t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/reset_password`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify({
  //   old_password: 'yHSSug',
  //   new_password: 'password',
  //   new_password_confirmation: 'password',
  // }));
  // 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');
  t.is(response.message, 'Password has been successfully reset');
});

// UserService.setPin method
/**
 * @test {UserService.setPin}
 */
test.serial('[unit] UserService.setPin should throw an error when there is no authToken', (t) => {
  client.authToken = undefined;
  const expectedError = 'UserService.setPin error: requires authToken and json is not defined';
  t.throws(() => userService.setPin(), expectedError);
});

/**
 * @test {UserService.setPin}
 */
test('[unit] UserService.setPin should throw an error when a JSON value is not provided', (t) => {
  const expectedError = 'UserService.setPin error: json is not defined';
  t.throws(() => userService.setPin(), expectedError);
});

// /**
//  * @test {UserService.setPin}
//  */
// test('[unit] UserService.setPin should make a properly formatted PUT request', async (t) => {
//   const { setPinJson } = t.context;
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     platform: 'ios',
//     push_id: 'abc',
//     app_version: '0.0.1'
//   });
//   await userService.setPin(setPinJson);
//   t.is(request.method, 'PUT');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/set_pin`);
//   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(request.requestBody, JSON.stringify({
//     pin: 123456,
//     pin_confirmation: 123456,
//   }));
// });

/**
 * @test {UserService.setPin}
 */
test('[unit] UserService.setPin should successfully submit the JSON to the server', async (t) => {
  const { setPinJson } = t.context;
  client.device = new Device({
    udid: 'rbiasvolh6yval9ofqff',
    platform: 'ios',
    push_id: 'abc',
    app_version: '0.0.1'
  });
  const response = await userService.setPin(setPinJson);
  // t.is(request.method, 'PUT');
  // t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/set_pin`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify({
  //   pin: 123456,
  //   pin_confirmation: 123456,
  // }));
  // 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');
});

// // UserService.signInGuest method
// /**
//  * @test {UserService.signInGuest}
//  */
// test('[unit] UserService.signInGuest should throw an error when invalid arguments are passed', (t) => {
//   const pre = 'UserService.signInGuest error';

//   client.device = new Device({ udid: undefined, technology: undefined, app_version: undefined, push_id: 'FAKE_ID' });
//   t.throws(() => userService.signInGuest(), `${pre}: udid is not defined and technology is not defined and version is not defined`);

//   client.device.udid = 'aaabb';
//   t.throws(() => userService.signInGuest(), `${pre}: technology is not defined and version is not defined`);

//   client.device.technology = 'android';
//   t.throws(() => userService.signInGuest(), `${pre}: version is not defined`);
// });

// /**
//  * @test {UserService.signInGuest}
//  */
// test('[unit] UserService.signInGuest should make a properly formatted post request', async (t) => {
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     push_id: 'FAKE_ID',
//     app_version: '0.0.1'
//   });
//   await userService.signInGuest();
//   t.is(request.method, 'POST');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/sign_in_guest`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestBody, JSON.stringify({
//     device: {
//       udid: 'rbiasvolh6yval9ofqff',
//       technology: 'ios',
//       push_id: 'FAKE_ID',
//       app_version: '0.0.1',
//     },
//   }));
// });

// /**
//  * @test {UserService.signInGuest}
//  */
// test('[unit] UserService.signInGuest should receive valid response for a post request', async (t) => {
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'android',
//     app_version: '0.0.1'
//   });
//   const response = await userService.signInGuest();
//   t.is(response.status, 'ok');
//   t.is(response.auth_token, 'valid_token');
//   t.is(response.encryption_key, 'thisisaterribleencryptionkey');
//   t.is(response.verification_status, 'unverified');
// });

// /**
//  * @test {UserService.signInGuest}
//  */
// test('[unit] UserService.signInGuest should pass on an error message for a post request', async (t) => {
//   nock(client.apiBaseUrl).post(`/api/mobile_users/sign_in_guest`).reply(     [400,{
//       error_detail: { technology: 'invalid_value' },
//       friendly_error: 'One or more of the given parameters is invalid',
//       internal_code: 50362,
//       status: 'fail',
//       message: 'One or more of the given parameters is invalid'
//     })]
//   );
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'windows',
//     push_id: 'abc',
//     app_version: '0.0.1'
//   });
//   let response;
//   await userService.signInGuest().then(null, res => (response = res));
//   t.is(response.status, 'fail');
//   t.is(response.friendly, 'One or more of the given parameters is invalid');
//   t.is(response.details, 'Technology invalid value');
// });

// UserService.signIn method
// /**
//  * @test {UserService.signIn}
//  */
// test('[unit] UserService.signIn should throw an error when invalid arguments are passed', (t) => {
//   const pre = 'UserService.signIn error';

//   t.throws(() => userService.signIn(), `${pre}: udid is not defined and technology is not defined and version is not defined`);

//   client.device.udid = 'aaaaaaaaaaabbbb';
//   t.throws(() => userService.signIn(), `${pre}: technology is not defined and version is not defined`);

//   client.device.technology = 'android';
//   t.throws(() => userService.signIn(), `${pre}: version is not defined`);
// });

// /**
//  * @test {UserService.signIn}
//  */
// test('[unit] UserService.signIn should reply with a friendly error message for missing/invalid credentials', async (t) => {
//   nock(client.apiBaseUrl).post(`/api/mobile_users/sign_in_guest`).reply(     [400,{
//       error_detail: 'Invalid credentials, please review your login and password',
//       friendly_error: 'Invalid credentials, please review your login and password',
//       internal_code: 50361,
//       more_info: '',
//       status: 'fail',
//       message: 'Invalid credentials, please review your login and password'
//     })]
//   );
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   const response = await userService.signIn().catch(r => r);
//   t.is(response.status, 'fail');
//   t.is(response.details, 'Invalid credentials, please review your login and password');
//   t.is(response.friendly, 'Invalid credentials, please review your login and password');
//   t.is(response.code, 50361);
// });

// /**
//  * @test {UserService.signIn}
//  */
// test('[unit] UserService.signIn should pass on an error message for a post request', async (t) => {
//   nock(client.apiBaseUrl).post(`/api/mobile_users/sign_in`).reply(     [400,{
//       error_detail: { technology: 'invalid_value' },
//       friendly_error: 'One or more of the given parameters is invalid',
//       internal_code: 50362,
//       status: 'fail',
//       message: 'One or more of the given parameters is invalid'
//     })]
//   );
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   const response = await userService.signIn('username', 'password').catch(r => r);
//   t.is(response.status, 'fail');
//   t.is(response.details, 'Technology invalid value');
//   t.is(response.friendly, 'One or more of the given parameters is invalid');
//   t.is(response.code, 50362);
// });

// /**
//  * @test {UserService.signIn}
//  */
// test('[unit] UserService.signIn should make a properly formatted post request', async (t) => {
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   await userService.signIn('user', 'password');
//   t.is(request.method, 'POST');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/sign_in`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestBody, JSON.stringify({
//     mobile_user: {
//       account_name: 'user',
//       password: 'password',
//     },
//     device: {
//       udid: 'rbiasvolh6yval9ofqff',
//       technology: 'ios',
//       push_id: 'FAKE_ID',
//       app_version: '0.0.1',
//     },
//   }));
// });

// /**
//  * @test {UserService.signIn}
//  */
// test('[unit] UserService.signIn should receive valid response for a post request', async (t) => {
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   const response = await userService.signIn('username', 'password');
//   t.is(response.status, 'ok');
//   t.is(response.auth_token, 'valid_token');
//   t.is(response.encryption_key, 'thisisaterribleencryptionkey');
//   t.is(response.verification_status, 'unverified');
// });

// /**
//  * @test {UserService.signIn}
//  */
// test('[unit] UserService.signIn when empty should make a properly formatted guest sign in post request', async (t) => {
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   await userService.signIn();
//   t.is(request.method, 'POST');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/sign_in_guest`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestBody, JSON.stringify({
//     device: {
//       udid: 'rbiasvolh6yval9ofqff',
//       technology: 'ios',
//       push_id: 'FAKE_ID',
//       app_version: '0.0.1',
//     },
//   }));
// });

// /**
//  * @test {UserService.signIn}
//  */
// test('[unit] UserService.signIn when empty should receive valid response for a guest sign in post request', async (t) => {
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   const response = await userService.signIn();
//   t.is(response.status, 'ok');
//   t.is(response.auth_token, 'valid_token');
//   t.is(response.encryption_key, 'thisisaterribleencryptionkey');
//   t.is(response.verification_status, 'unverified');
// });

// /**
//  * @test {UserService.signIn}
//  */
// test('[unit] UserService.signIn when empty should pass on an error message for a guest sign in post request', async (t) => {
//   nock(client.apiBaseUrl).post(`/api/mobile_users/sign_in_guest`).reply(     [400,{
//       error_detail: { technology: 'invalid_value' },
//       friendly_error: 'One or more of the given parameters is invalid',
//       internal_code: 50362,
//       status: 'fail',
//       message: 'One or more of the given parameters is invalid'
//     })]
//   );
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'windows',
//     app_version: '0.0.1'
//   });
//   let response;
//   await userService.signIn().then(null, (res) => {
//     response = res;
//   });
//   t.is(response.status, 'fail');
//   t.is(response.friendly, 'One or more of the given parameters is invalid');
//   t.is(response.details, 'Technology invalid value');
// });

// UserService.signOut method
// /**
//  * @test {UserService.signOut}
//  */
// test('[unit] UserService.signOut should make a properly formatted delete request', async (t) => {
//   await userService.signOut();
//   t.is(request.method, 'DELETE');
//   t.is(request.url, `${client.apiBaseUrl}/v3/mobile_users/sessions`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
// });

/**
 * @test {UserService.signOut}
 */
test('[unit] UserService.signOut should clear the client authToken, device, and storage', async (t) => {
  client.storageUtility.set('mobile_users', 4);
  client.authToken = '123456789';
  client.device = new Device({ push_id: 'REAL_ID' });
  await userService.signOut();
  t.is(client.authToken, undefined);
  t.is(client.device.pushId, 'FAKE_ID');
  t.is(client.storage, undefined);
});

/**
 * @test {UserService.signOut}
 */
test('[unit] UserService.signOut should successfully sign out and remove auth token from client', async (t) => {
  const response = await userService.signOut();
  t.is(response.message, 'Signed out successfully.');
});

// UserService.signUp method
/**
 * @test {UserService.signUp}
 */
test('[unit] UserService.signUp should throw an error when invalid arguments are passed', (t) => {
  const errorMessage1 = 'UserService.signUp error: udid is not defined'
      + ' and technology is not defined'
      + ' and version is not defined'
      + ' and username is not defined'
      + ' and password is not defined'
      + ' and profile is not defined';

  const errorMessage2 = 'UserService.signUp error: technology is not defined'
      + ' and version is not defined'
      + ' and username is not defined'
      + ' and password is not defined'
      + ' and profile is not defined';

  const errorMessage3 = 'UserService.signUp error: version is not defined'
      + ' and username is not defined'
      + ' and password is not defined'
      + ' and profile is not defined';

  const errorMessage4 = 'UserService.signUp error: username is not defined'
      + ' and password is not defined'
      + ' and profile is not defined';

  const errorMessage5 = 'UserService.signUp error: password is not defined'
    + ' and profile is not defined';

  t.throws(() => userService.signUp(), errorMessage1);

  client.device.udid = 'aaaaaaaaaaabbbb';

  t.throws(() => userService.signUp(), errorMessage2);

  client.device.technology = 'android';

  t.throws(() => userService.signUp(), errorMessage3);

  client.device.appVersion = '0.0.1';

  t.throws(() => userService.signUp(), errorMessage4);

  t.throws(() => userService.signUp('user'), errorMessage5);
});

/**
 * @test {UserService.signUp}
 */
test('[unit] UserService.signUp should reply with a friendly error message for missing/invalid credentials', async (t) => {
  nock(client.apiBaseUrl).post(`/api/mobile_users/sign_up`).reply(400, {
    error_detail: 'Invalid credentials, please review your login and password',
    friendly_error: 'Invalid credentials, please review your login and password',
    internal_code: 50361,
    more_info: '',
    status: 'fail',
    message: 'Invalid credentials, please review your login and password'
  });
  client.device = new Device({
    udid: 'rbiasvolh6yval9ofqff',
    technology: 'ios',
    app_version: '0.0.1'
  });
  const response = await userService.signUp('user', 'password', {}).catch(r => r);
  t.is(response.status, 'fail');
  t.is(response.details, 'Invalid credentials, please review your login and password');
  t.is(response.friendly, 'Invalid credentials, please review your login and password');
  t.is(response.code, 50361);
});

/**
 * @test {UserService.signUp}
 */
test('[unit] UserService.signUp should pass on an error message for a post request', async (t) => {
  nock(client.apiBaseUrl).post(`/api/mobile_users/sign_up`).reply(400, {
    error_detail: { technology: 'invalid_value' },
    friendly_error: 'One or more of the given parameters is invalid',
    internal_code: 50362,
    status: 'fail',
    message: 'One or more of the given parameters is invalid'
  });
  client.device = new Device({
    udid: 'rbiasvolh6yval9ofqff',
    technology: 'windows',
    app_version: '0.0.1'
  });
  const response = await userService.signUp('username', 'password', {}).catch(r => r);
  t.is(response.status, 'fail');
  t.is(response.details, 'Technology invalid value');
  t.is(response.friendly, 'One or more of the given parameters is invalid');
  t.is(response.code, 50362);
});

// /**
//  * @test {UserService.signUp}
//  */
// test('[unit] UserService.signUp should make a properly formatted post request with an empty profile', async (t) => {
//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   await userService.signUp('username', 'password', {});
//   t.is(request.method, 'POST');
//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/sign_up`);
//   t.is(request.requestHeaders.Accept, 'application/json');
//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
//   t.is(request.requestBody, JSON.stringify({
//     mobile_user: {
//       account_name: 'username',
//       password: 'password',
//     },
//     device: {
//       udid: 'rbiasvolh6yval9ofqff',
//       technology: 'ios',
//       push_id: 'FAKE_ID',
//       app_version: '0.0.1',
//     },
//     profile: {
//     },
//   }));
// });

// /**
//  * @test {UserService.signUp}
//  */
// test('[unit] UserService.signUp should make a properly formatted post request with Profile fields', async (t) => {
//   const profile = {
//     username: 'max',
//     email: 'max@gmail.com',
//   };

//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   await userService.signUp('username', 'password', profile);
//   t.is(request.method, 'POST');

//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/sign_up`);

//   t.is(request.requestHeaders.Accept, 'application/json');

//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');

//   t.is(request.requestBody, JSON.stringify({
//     mobile_user: {
//       account_name: 'username',
//       password: 'password',
//     },
//     device: {
//       udid: 'rbiasvolh6yval9ofqff',
//       technology: 'ios',
//       push_id: 'FAKE_ID',
//       app_version: '0.0.1',
//     },
//     profile: {
//       username: 'max',
//       email: 'max@gmail.com',
//     },
//   }));
// });

// /**
//  * @test {UserService.signUp}
//  */
// test('[unit] UserService.signUp should make a properly formatted post request', async (t) => {
//   const profile = {
//     username: 'max',
//     email: 'max@gmail.com',
//     first_name: 'max',
//     last_name: 'smith',
//     remove_avatar: true,
//     gender: 'male',
//     age: 25,
//     language: 'english',
//     ethnicity: 'caucasian',
//     zip_code: '12345',
//     occupation: 'highwayman',
//     phone: '123-456-7890',
//     custom_attributes: { a: 'b' },
//   };

//   client.device = new Device({
//     udid: 'rbiasvolh6yval9ofqff',
//     technology: 'ios',
//     app_version: '0.0.1'
//   });
//   await userService.signUp('username', 'password', profile);
//   t.is(request.method, 'POST');

//   t.is(request.url, `${client.apiBaseUrl}/api/mobile_users/sign_up`);

//   t.is(request.requestHeaders.Accept, 'application/json');

//   t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');

//   t.is(request.requestBody, JSON.stringify({
//     mobile_user: {
//       account_name: 'username',
//       password: 'password',
//     },
//     device: {
//       udid: 'rbiasvolh6yval9ofqff',
//       technology: 'ios',
//       push_id: 'FAKE_ID',
//       app_version: '0.0.1',
//     },
//     profile: {
//       username: 'max',
//       email: 'max@gmail.com',
//       first_name: 'max',
//       last_name: 'smith',
//       remove_avatar: true,
//       gender: 'male',
//       age: 25,
//       language: 'english',
//       ethnicity: 'caucasian',
//       zip_code: '12345',
//       occupation: 'highwayman',
//       phone: '123-456-7890',
//       custom_attributes: { a: 'b' },
//     },
//   }));
// });

/**
 * @test {UserService.signUp}
 */
test('[unit] UserService.signUp should receive valid response for a post request', async (t) => {
  nock(client.apiBaseUrl).post(`/api/mobile_users/sign_up`).reply(200, {
    status: 'ok',
    auth_token: 'valid_token',
    encryption_key: 'thisisaterribleencryptionkey',
    verification_status: 'unverified'
  });

  client.device = new Device({
    udid: 'rbiasvolh6yval9ofqff',
    technology: 'ios',
    app_version: '0.0.1'
  });
  const response = await userService.signUp('username', 'password', {});
  t.is(response.status, 'ok');
  t.is(response.auth_token, 'valid_token');
  t.is(response.encryption_key, 'thisisaterribleencryptionkey');
  t.is(response.verification_status, 'unverified');
  t.is(client.authToken, 'valid_token');
});