<commit_before>require 'spec_helper'

describe UsersController do
  let(:admin_role) { Role.find_by_name('Administrator') }
  let(:kate) { User.create({name: 'Kate', email: '<EMAIL>'}) }
  let(:expected_kate) { {email: kate.email, name: kate.name, editable: true, role_ids: []} }
  let(:expected_kate_with_role) { {email: kate.email, name: kate.name, editable: true, role_ids: [admin_role.id]} }

  describe '#index' do
    let(:params) {{}}
    subject(:response) { get :index, params }

    it_behaves_like 'a secured route'

    context 'when listing all users for an admin' do

      before { add_user_to_session('Administrator') }
      let(:expected) { [{name: 'Bob', email: '<EMAIL>', editable: true, role_ids: [admin_role.id]},
                        expected_kate].to_json }

      it { should be_ok }
      its(:body) { should be_json_eql(expected).at_path('users') }
    end

    context 'when filtering for a role' do
      let(:params) {{role_id: admin_role.id }}
      before { add_user_to_session('Administrator') }
      let(:expected) { [{name: 'Bob', email: '<EMAIL>', editable: true, role_ids: [admin_role.id]}].to_json }

      it { should be_ok }
      its(:body) { should be_json_eql(expected).at_path('users') }
    end

    context 'when filtering for a non-existent role' do
      let(:params) {{role_id: 666 }}
      before { add_user_to_session('Administrator') }
      let(:expected) {{error: "Couldn't find Role with 'id'=666"}.to_json}
      its(:body) {should be_json_eql(expected)}
    end

  end

  describe '#show' do
    let(:params) {{ id: kate.id }}
    subject(:response) {get :show, params}
    context 'when an admin user looks at kate' do
      before { add_user_to_session('Administrator') }
      let(:expected) {{roles: [], user: expected_kate}.to_json}
      its(:body) {should be_json_eql(expected)}
    end

    context 'when a recruiter looks at kate' do<commit_msg>Should find user with role even if they have other roles too.<commit_after>require 'spec_helper'

describe UsersController do
  let(:admin_role) { Role.find_by_name('Administrator') }
  let(:assessor_role) { Role.find_by_name('Assessor') }
  let(:kate) { User.create({name: 'Kate', email: '<EMAIL>'}) }
  let(:expected_kate) { {email: kate.email, name: kate.name, editable: true, role_ids: []} }
  let(:expected_kate_with_role) { {email: kate.email, name: kate.name, editable: true, role_ids: [admin_role.id]} }

  describe '#index' do
    let(:params) {{}}
    subject(:response) { get :index, params }

    it_behaves_like 'a secured route'

    context 'when listing all users for an admin' do

      before { add_user_to_session('Administrator') }
      let(:expected) { [{name: 'Bob', email: '<EMAIL>', editable: true, role_ids: [admin_role.id]},
                        expected_kate].to_json }

      it { should be_ok }
      its(:body) { should be_json_eql(expected).at_path('users') }
    end

    context 'when filtering for a role' do
      let(:params) {{role_id: admin_role.id }}
      before { add_user_to_session('Administrator') }
      let(:expected) { [{name: 'Bob', email: '<EMAIL>', editable: true, role_ids: [admin_role.id]}].to_json }

      it { should be_ok }
      its(:body) { should be_json_eql(expected).at_path('users') }
    end

    context 'when filtering for a role show user with role even if they have other roles' do
      let(:params) {{role_id: assessor_role.id }}
      before {
        add_user_to_session('Administrator')
        sue =  User.create({name: 'Sue', email: '<EMAIL>'})
        sue.roles.push(admin_role)
        sue.roles.push(assessor_role)
        sue.save
      }

      let(:expected) { [{name: 'Sue', email: '<EMAIL>', editable: true, role_ids:
          [assessor_role.id, admin_role.id]}].to_json }

      it { should be_ok }
      its(:body) { should be_json_eql(expected).at_path('users') }
    end

    context 'when filtering for a non-existent role' do
      let(:params) {{role_id: 666 }}
      before { add_user_to_session('Administrator') }
      let(:expected) {{error: "Couldn't find Role with 'id'=666"}.to_json}
      its(:body) {should be_json_eql(expected)}
    end

  end

  describe '#show' do
    let(:params) {{ id: kate.id }}
    subject(:response) {get :show, params}
    context 'when an admin user looks at kate' do
      before { add_user_to_session('Administrator') }
      let(:expected) {{roles: [], user: expected_kate}.to_json}
      its(:body) {should be_json_eql(expected)}
    end

    context 'when a recruiter looks at kate' do
{'fraction_non_alphanumeric': 0.14591139519506283, 'fraction_numerical': 0.0026449195503636764, 'mean_word_length': 3.466535433070866, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '1776308', 'n_tokens_mistral': 1574, 'n_tokens_neox': 1511, 'n_words': 377}