Manual Reference Source Test

test/unit/services.user.v3.admin.js

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

test.before('start server', () => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
});

test.after('server shut down', () => {});

test.beforeEach((t) => {
  client.cache = 'never';
  client.authToken = 'valid_token';
  userService.cacheMode = 'networkOnly';

  t.context.storage = client.storageUtility;
  // t.context.storage.clear();
});

// UserService.requestPasswordReset method
/**
 * @test {UserService.requestPasswordReset}
 */
test('[unit] UserService.requestPasswordReset should throw errors for invalid parameters', async (t) => {
  const undefinedError = 'UserService.requestPasswordReset error: attributes is not defined';
  const validError = 'UserService.requestPasswordReset error: attributes must be valid';
  await t.throwsAsync(userService.requestPasswordReset(), undefinedError);
  await t.throwsAsync(userService.requestPasswordReset({}), validError);
});

/**
 * @test {UserService.requestPasswordReset}
 */
test('[unit] UserService.requestPasswordReset should successfully requestPasswordReset mobile user with a mobile user object', async (t) => {
  // const requestJson = {
  //   data: {
  //     type: 'password_resets',
  //     attributes: {
  //       email: 'buley@adobe.com'
  //     }
  //   }
  // };
  nock(client.apiBaseUrl).post(`/v3/users/password`).reply(201, '{}');
  const response = await userService.requestPasswordReset({ email: 'buley@adobe.com' });
  // const request = t.context.server.requests[0];
  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/v3/users/password`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify(requestJson));
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.truthy(response);
});

/**
 * @test {UserService.requestPasswordReset}
 */
test('[unit] UserService.requestPasswordReset should throw an error if the email is invalid', async (t) => {
  nock(client.apiBaseUrl).post(`/v3/users/password`).reply(422, {
    errors: [
      {
        source: {
          pointer: '/data/attributes/email'
        },
        detail: 'not found'
      }
    ]
  });
  // const requestJsonInvalid = {
  //   data: {
  //     type: 'password_resets',
  //     attributes: {
  //       email: 'invalidEmail@invalid.com'
  //     }
  //   }
  // };
  let response;
  await userService.requestPasswordReset({ email: 'invalidEmail@invalid.com' }).catch(res => (response = res));
  // const request = t.context.server.requests[0];
  // t.is(request.method, 'POST');
  // t.is(request.url, `${client.apiBaseUrl}/v3/users/password`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify(requestJsonInvalid));
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.truthy(response);
});

// UserService.resetPassword method
/**
 * @test {UserService.resetPassword}
 */
test('[unit] UserService.resetPassword should throw errors for invalid parameters', async (t) => {
  const pre = 'UserService.resetPassword error:';

  await t.throwsAsync(userService.resetPassword(), `${pre} attributes is not defined`);

  const attr = {};
  const device = new Device({ id: 5 });
  await t.throwsAsync(userService.resetPassword(attr, device), `${pre} attributes does not have reset_password_token and attributes does not have password`);

  attr.reset_password_token = 23;
  await t.throwsAsync(userService.resetPassword(attr, device), `${pre} attributes does not have password`);

  attr.password = 23;
  await t.throwsAsync(userService.resetPassword(attr, device), `${pre} reset_password_token is not a string and password is not a string`);

  attr.reset_password_token = 'asdf';
  await t.throwsAsync(userService.resetPassword(attr, device), `${pre} password is not a string`);

  attr.password = 'asdf';
  attr.email = 23;
  await t.throwsAsync(userService.resetPassword(attr, device), `${pre} email is not a string and email must be a valid email`);
});

/**
 * @test {UserService.resetPassword}
 */
test('[unit] UserService.resetPassword should successfully resetPassword mobile user with correct attributes', async (t) => {
  // const requestJson = {
  //   data: {
  //     type: 'password_resets',
  //     attributes: {
  //       reset_password_token: '73169657',
  //       password: 'my_new_sekret',
  //       email: 'test@test.com'
  //     }
  //   }
  // };
  nock(client.apiBaseUrl).patch(`/v3/users/password`).reply(200, '');
  const response = await userService.resetPassword({
    reset_password_token: '73169657',
    password: 'my_new_sekret',
    email: 'test@test.com'
  });
  // const request = t.context.server.requests[0];
  // t.is(request.method, 'PATCH');
  // t.is(request.url, `${client.apiBaseUrl}/v3/users/password`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify(requestJson));
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');

  t.falsy(response);
});

/**
 * @test {UserService.resetPassword}
 */
test('[unit] UserService.resetPassword should throw an error if the password token is invalid', async (t) => {
  nock(client.apiBaseUrl).patch(`/v3/users/password`).reply(422, {
    errors: [
      {
        source: {
          pointer: '/data/attributes/reset_password_token'
        },
        detail: 'is invalid'
      }
    ]
  });
  // const requestJsonInvalid = {
  //   data: {
  //     type: 'password_resets',
  //     attributes: {
  //       reset_password_token: '73169657',
  //       password: 'my_new_sekret',
  //       email: 'test@test.com'
  //     }
  //   }
  // };
  let response;
  await userService.resetPassword({
    reset_password_token: '73169657',
    password: 'my_new_sekret',
    email: 'test@test.com'
  }).catch(res => (response = res));
  // const request = t.context.server.requests[0];
  // t.is(request.method, 'PATCH');
  // t.is(request.url, `${client.apiBaseUrl}/v3/users/password`);
  // t.is(request.requestHeaders.Accept, 'application/json');
  // t.is(request.requestBody, JSON.stringify(requestJsonInvalid));
  // t.is(request.requestHeaders['Content-Type'], 'application/json;charset=utf-8');
  // t.is(request.requestHeaders.Authorization, 'Token token=valid_token');
  t.truthy(response);
});