Manual Reference Source Test

test/unit/services.user.web.js

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

test.before('start server', (t) => {
  client.apiBaseUrl = 'https://somesite.Clinical6.com';
  nock.disableNetConnect();
  t.context.signInResponse = {
    data: {
      id: '180',
      type: 'user_sessions',
      attributes: {
        sign_in_count: 1,
        last_sign_in_at: '2018-01-02T21:53:39Z',
        failed_attempts: 0,
        locked_at: null,
        username: 'dummy_2634',
        email: 'user539@fake.com',
        authentication_token: '82735948',
        confirmed_at: null,
        invitation_sent_at: null,
        invitation_accepted_at: null,
        password_changed_at: '2018-01-02T21:53:39Z',
        disabled_at: null
      },
      relationships: {
        user: {
          data: {
            id: '180',
            type: 'users'
          }
        }
      }
    },
    included: [
      {
        id: '180',
        type: 'users',
        attributes: {
          uuid: '6267059b-1f53-46ff-a00c-d4f007d5a5d7',
          email: 'user539@fake.com',
          created_at: '2018-01-02T21:53:39Z',
          updated_at: '2018-01-02T21:53:39Z',
          invitation_sent_at: null,
          invitation_accepted_at: null,
          disabled_at: null
        },
        relationships: {
          user_role: {
            data: {
              id: '244',
              type: 'user_roles'
            }
          },
          devices: {
            data: []
          },
          profile: {
            data: {
              id: '545',
              type: 'profiles'
            }
          }
        }
      },
      {
        id: '244',
        type: 'user_roles',
        attributes: {
          permanent_link: 'standard',
          name: 'Standard',
          is_super: false,
          is_admin: false,
          is_mobile: false,
          is_default: true
        }
      }
    ]
  };
});

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

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

  t.context.signOut = nock(client.apiBaseUrl)
    .delete('/v3/users/sessions')
    .reply(200, { message: 'Signed out successfully.' });
  t.context.storage = client.storageUtility;
  // t.context.storage.clear();
});

// test.afterEach.always(t => t.context.server.restore());

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

/**
 * @test {UserService.signIn}
 */
test.serial('[unit] UserService.signIn should successfully signIn', async (t) => {
  // http://clinical6-docs.s3-website-us-east-1.amazonaws.com/apidoc/v3userssessions/create.html

  const requestJsonApi = {
    data: {
      type: 'sessions',
      attributes: {
        email: 'user539@fake.com',
        password: 'My_s3kret_password!'
      }
    }
  };

  let requestBody = {};
  const server = nock(client.apiBaseUrl)
    .post('/v3/users/sessions', (body) => {
      requestBody = body;
      return requestJsonApi;
    })
    .reply(201, t.context.signInResponse);

  const response = await userService.signIn({ email: 'user539@fake.com', password: 'My_s3kret_password!' });
  t.deepEqual(requestBody, requestJsonApi);
  server.done();

  t.is(response.username, 'dummy_2634');
  t.is(response.id, 180);
  t.is(response.type, 'user_sessions');
  t.is(response.signInCount, 1);
  t.is(response.lastSignInAt, '2018-01-02T21:53:39Z');
  t.is(response.failedAttempts, 0);
  t.is(response.email, 'user539@fake.com');
  t.is(response.authenticationToken, '82735948');
  t.is(response.passwordChangedAt, '2018-01-02T21:53:39Z');

  t.is(response.user.id, 180);
  t.is(response.user.type, 'users');
  t.is(response.user.createdAt, '2018-01-02T21:53:39Z');
  t.is(response.user.email, 'user539@fake.com');
  t.is(response.user.updatedAt, '2018-01-02T21:53:39Z');
  t.is(response.user.uuid, '6267059b-1f53-46ff-a00c-d4f007d5a5d7');

  t.is(response.user.role.type, 'user_roles');
  t.is(response.user.role.id, 244);
  t.is(response.user.role.name, 'Standard');
  t.is(response.user.role.permanentLink, 'standard');
  t.is(response.user.role.isAdmin, false);
  t.is(response.user.role.isDefault, true);
  t.is(response.user.role.isMobile, false);
  t.is(response.user.role.isSuper, false);

  t.is(client.user.type, 'users');
});

// UserService.signOut method
/**
 * @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.');
});