<commit_before>import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'hospitalrun/tests/helpers/start-app';
import FakeServer, { stubRequest } from 'ember-cli-fake-server';

function addAllUsers(assert) {
  stubRequest('post', '/allusers', function(request) {
    assert.equal(request.requestBody, '{"name":"hradmin"}', 'All Users request sent to the server');
    request.ok({
      'total_rows': 1,
      'offset': 1,
      'rows': [{
        'id': 'org.couchdb.user:hradmin',
        'key': 'org.couchdb.user:hradmin',
        'value': { 'rev': '1-242f3d5b5eb8596144f8a6300f9f5a2f' },
        'doc': {
          '_id': 'org.couchdb.user:hradmin',
          '_rev': '1-242f3d5b5eb8596144f8a6300f9f5a2f',
          'password_scheme': '<PASSWORD>',
          'iterations': 10,
          'name': 'hradmin',
          'roles': ['System Administrator','admin','user'],
          'type': 'user',
          'userPrefix': 'p',
          'derived_key': 'derivedkeyhere',
          'salt': '<PASSWORD>',
          'displayName': 'HospitalRun Administrator',
          'email': '<EMAIL>'
        }
      }, {
        'id': 'org.couchdb.user:<EMAIL>',
        'key': 'org.couchdb.user:<EMAIL>',
        'value': {
          'rev': '1-ef3d54502f2cc8e8f73d8547881f0836'
        },
        'doc': {
          '_id': 'org.couchdb.user:<EMAIL>',
          '_rev': '1-ef3d54502f2cc8e8f73d8547881f0836',
          'password_scheme': '<PASSWORD>',
          'iterations': 10,
          'displayName': '<NAME>',
          'email': '<EMAIL>',
          'name': '<EMAIL>',
          'roles': ['Hospital Administrator','user'],
          'userPrefix': 'p01',
          'type': 'user',
          'derived_key': 'derivedkeyhere',
          'salt': '<PASSWORD>'
        }
      }]
    });
  });
}

module('Acceptance | users', {
  beforeEach: function() {
    FakeServer.start();
    this.application = startApp();
  },

  afterEach: function() {
    FakeServer.stop();
    Ember.run(this.application, 'destroy');
  }
});

test('visiting /admin/users', function(assert) {
  runWithPouchDump('default', function() {
    authenticateUser();
    addAllUsers(assert);

    visit('/admin/users');
    andThen(function() {
      assert.equal(currentURL(), '/admin/users');
      assert.equal(find('td.user-display-name:first').text(), 'HospitalRun Administrator');
      assert.equal(find('td.user-name:first').text(), 'hradmin');
      assert.equal(find('td.user-email:first').text(), '<EMAIL>');
      assert.equal(find('td.user-role:first').text(), 'System Administrator');
    });
  });
});

test('create new user', function(assert) {
  runWithPouchDump('default', function() {
    authenticateUser();
    addAllUsers(assert);
    visit('/admin/users');

    stubRequest('post', '/updateuser', function(request) {
      var expectedBody = {
        data: {
          _id: 'org.couchdb.user:<EMAIL>',
          displayName: '<NAME>',
          email: '<EMAIL>',
          name: '<EMAIL>',
          password: 'password',
          roles: ['Hospital Administrator', 'user'],
          userPrefix: 'p02',
          type: 'user'
        },
        name: 'hradmin'
      };
      assert.equal(request.requestBody, JSON.stringify(expectedBody), 'New user data sent to the server');
      request.ok({
        'ok': true,
        'id': 'org.couchdb.user:<EMAIL>',
        'rev': '1-ef3d54502f2cc8e8f73d8547881f0836'
      });
    });

    visit('/admin/users/edit/new');
    andThen(function() {
      select('.user-role', 'Hospital Administrator');
      fillIn('.user-display-name input', '<NAME>');
      fillIn('.user-email input', '<EMAIL>');
      fillIn('.user-password input', 'password');
      click('button:contains(Add)');
      waitToAppear('.modal-dialog');
      andThen(() => {
        assert.equal(find('.modal-title').text(), 'User Saved', 'User was saved successfully');
      });
      click('button:contains(Ok)');
    });
  });
});

test('delete user', function(assert) {
  runWithPouchDump('default', function() {
    authenticateUser();
    addAllUsers(assert);

    stubRequest('post', '/deleteuser', function(request) {
      var expectedBody = {
        id: 'org.couchdb.user:<EMAIL>',
        rev: '1-ef3d54502f2cc8e8f73d8547881f0836',
        name: 'hradmin'
      };
      assert.equal(request.requestBody, JSON.stringify(expectedBody), 'Delete user request sent to the server');
      request.ok({
        'ok': true,
        'id': 'org.couchdb.user:<EMAIL>',
        'rev': '1-ef3d54502f2cc8e8f73d8547881f0836'
      });
    });

    visit('/admin/users');
    andThen(function() {
      click('button:contains(Delete):last');
      waitToAppear('.modal-dialog');
      andThen(() => {
        assert.equal(find('.alert').text().trim(), 'Are you sure you wish to delete the user <EMAIL>?', 'User is displayed for deletion');
      });
      click('button:contains(Delete):last');
      andThen(() => {
        assert.equal(find('.user-email:contains(<EMAIL>)').length, 0, 'User disappears from user list');
      });
    });
  });
});<commit_msg>Updated users tests to work with changes to user crud.<commit_after>import Ember from 'ember';
import { module, test } from 'qunit';
import startApp from 'hospitalrun/tests/helpers/start-app';
import FakeServer, { stubRequest } from 'ember-cli-fake-server';

function addAllUsers(assert) {
  stubRequest('get', '/db/_users/_all_docs', function(request) {
    let expectedQuery = {
      include_docs: 'true',
      startkey: '"org.couchdb.user"'
    };
    assert.equal(JSON.stringify(request.queryParams), JSON.stringify(expectedQuery), 'All Users request sent to the server');
    request.ok({
      'total_rows': 1,
      'offset': 1,
      'rows': [{
        'id': 'org.couchdb.user:hradmin',
        'key': 'org.couchdb.user:hradmin',
        'value': { 'rev': '1-242f3d5b5eb8596144f8a6300f9f5a2f' },
        'doc': {
          '_id': 'org.couchdb.user:hradmin',
          '_rev': '1-242f3d5b5eb8596144f8a6300f9f5a2f',
          'password_scheme': '<PASSWORD>',
          'iterations': 10,
          'name': 'hradmin',
          'roles': ['System Administrator','admin','user'],
          'type': 'user',
          'userPrefix': 'p',
          'derived_key': 'derivedkeyhere',
          'salt': '<PASSWORD>',
          'displayName': 'HospitalRun Administrator',
          'email': '<EMAIL>'
        }
      }, {
        'id': 'org.couchdb.user:<EMAIL>',
        'key': 'org.couchdb.user:<EMAIL>',
        'value': {
          'rev': '1-ef3d54502f2cc8e8f73d8547881f0836'
        },
        'doc': {
          '_id': 'org.couchdb.user:<EMAIL>',
          '_rev': '1-ef3d54502f2cc8e8f73d8547881f0836',
          'password_scheme': 'pbkdf2',
          'iterations': 10,
          'displayName': '<NAME>',
          'email': '<EMAIL>',
          'name': '<EMAIL>',
          'roles': ['Hospital Administrator','user'],
          'userPrefix': 'p01',
          'type': 'user',
          'derived_key': 'derivedkeyhere',
          'salt': '<PASSWORD>'
        }
      }]
    });
  });
}

module('Acceptance | users', {
  beforeEach: function() {
    FakeServer.start();
    this.application = startApp();
  },

  afterEach: function() {
    FakeServer.stop();
    Ember.run(this.application, 'destroy');
  }
});

test('visiting /admin/users', function(assert) {
  runWithPouchDump('default', function() {
    authenticateUser();
    addAllUsers(assert);

    visit('/admin/users');
    andThen(function() {
      assert.equal(currentURL(), '/admin/users');
      assert.equal(find('td.user-display-name:first').text(), 'HospitalRun Administrator');
      assert.equal(find('td.user-name:first').text(), 'hradmin');
      assert.equal(find('td.user-email:first').text(), '<EMAIL>');
      assert.equal(find('td.user-role:first').text(), 'System Administrator');
    });
  });
});

test('create new user', function(assert) {
  runWithPouchDump('default', function() {
    authenticateUser();
    addAllUsers(assert);
    visit('/admin/users');

    stubRequest('put', '/db/_users/org.couchdb.user:<EMAIL>', function(request) {
      var expectedBody = {
        _id: 'org.couchdb.user:<EMAIL>',
        displayName: '<NAME>',
        email: '<EMAIL>',
        name: '<EMAIL>',
        password: 'password',
        roles: ['Hospital Administrator', 'user'],
        userPrefix: 'p02',
        type: 'user'
      };
      assert.equal(request.requestBody, JSON.stringify(expectedBody), 'New user data sent to the server');
      request.ok({
        'ok': true,
        'id': 'org.couchdb.user:<EMAIL>',
        'rev': '1-ef3d54502f2cc8e8f73d8547881f0836'
      });
    });

    visit('/admin/users/edit/new');
    andThen(function() {
      select('.user-role', 'Hospital Administrator');
      fillIn('.user-display-name input', '<NAME>');
      fillIn('.user-email input', '<EMAIL>');
      fillIn('.user-password input', 'password');
      click('button:contains(Add)');
      waitToAppear('.modal-dialog');
      andThen(() => {
        assert.equal(find('.modal-title').text(), 'User Saved', 'User was saved successfully');
      });
      click('button:contains(Ok)');
    });
  });
});

test('delete user', function(assert) {
  runWithPouchDump('default', function() {
    authenticateUser();
    addAllUsers(assert);
    stubRequest('delete', '/db/_users/org.couchdb.user:<EMAIL>', function(request) {
      let expectedQuery = {
        rev: '1-ef3d54502f2cc8e8f73d8547881f0836'
      };
      assert.equal(JSON.stringify(request.queryParams), JSON.stringify(expectedQuery), 'Delete user request sent to the server');
      request.ok({
        'ok': true,
        'id': 'org.couchdb.user:<EMAIL>',
        'rev': '1-ef3d54502f2cc8e8f73d8547881f0836'
      });
    });

    visit('/admin/users');
    andThen(function() {
      click('button:contains(Delete):last');
      waitToAppear('.modal-dialog');
      andThen(() => {
        assert.equal(find('.alert').text().trim(), 'Are you sure you wish to delete the user <EMAIL>?', 'User is displayed for deletion');
      });
      click('button:contains(Delete):last');
      andThen(() => {
        assert.equal(find('.user-email:contains(<EMAIL>)').length, 0, 'User disappears from user list');
      });
    });
  });
});
{'fraction_non_alphanumeric': 0.18837753510140406, 'fraction_numerical': 0.033248829953198125, 'mean_word_length': 2.8059369202226345, 'pattern_counts': {'":': 1, '<': 49, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7196819', 'n_tokens_mistral': 3521, 'n_tokens_neox': 3377, 'n_words': 564}