users_spec.rb 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. require 'rails_helper'
  2. RSpec.describe 'Manage > Users', type: :system do
  3. describe 'switching to an alternative user', authenticated_as: -> { original_user } do
  4. let(:original_user) { create(:admin) }
  5. let(:alternative_one_user) { create(:admin) }
  6. let(:alternative_two_user) { create(:admin) }
  7. before do
  8. alternative_one_user
  9. alternative_two_user
  10. end
  11. it 'starts as original user' do
  12. expect(current_user).to eq original_user
  13. end
  14. it 'switches to alternative user' do
  15. switch_to(alternative_one_user)
  16. expect(current_user).to eq alternative_one_user
  17. end
  18. it 'switches to another alternative user' do
  19. switch_to(alternative_one_user)
  20. switch_to(alternative_two_user)
  21. expect(current_user).to eq alternative_two_user
  22. end
  23. it 'switches back to original user' do
  24. switch_to(alternative_one_user)
  25. switch_to(alternative_two_user)
  26. click '.switchBackToUser-close'
  27. expect(current_user).to eq original_user
  28. end
  29. def switch_to(user)
  30. visit 'manage/users'
  31. within(:active_content) do
  32. row = find("tr[data-id=\"#{user.id}\"]")
  33. row.find('.js-action').click
  34. row.find('.js-switchTo').click
  35. end
  36. await_empty_ajax_queue
  37. end
  38. end
  39. describe 'check user edit permissions', authenticated_as: -> { user } do
  40. shared_examples 'user permission' do |allow|
  41. it(allow ? 'allows editing' : 'forbids editing') do
  42. visit "#user/profile/#{record.id}"
  43. find('.js-action .icon-arrow-down').click
  44. selector = '.js-action [data-type="edit"]'
  45. expect(page).to(allow ? have_css(selector) : have_no_css(selector))
  46. end
  47. end
  48. context 'when admin tries to change admin' do
  49. let(:user) { create(:admin) }
  50. let(:record) { create(:admin) }
  51. include_examples 'user permission', true
  52. end
  53. context 'when admin tries to change agent' do
  54. let(:user) { create(:admin) }
  55. let(:record) { create(:agent) }
  56. include_examples 'user permission', true
  57. end
  58. context 'when admin tries to change customer' do
  59. let(:user) { create(:admin) }
  60. let(:record) { create(:customer) }
  61. include_examples 'user permission', true
  62. end
  63. context 'when agent tries to change admin' do
  64. let(:user) { create(:agent) }
  65. let(:record) { create(:admin) }
  66. include_examples 'user permission', false
  67. end
  68. context 'when agent tries to change agent' do
  69. let(:user) { create(:agent) }
  70. let(:record) { create(:agent) }
  71. include_examples 'user permission', false
  72. end
  73. context 'when agent tries to change customer' do
  74. let(:user) { create(:agent) }
  75. let(:record) { create(:customer) }
  76. include_examples 'user permission', true
  77. end
  78. context 'when agent tries to change customer who is also admin' do
  79. let(:user) { create(:agent) }
  80. let(:record) { create(:customer, role_ids: Role.signup_role_ids.push(Role.find_by(name: 'Admin').id)) }
  81. include_examples 'user permission', false
  82. end
  83. context 'when agent tries to change customer who is also agent' do
  84. let(:user) { create(:agent) }
  85. let(:record) { create(:customer, role_ids: Role.signup_role_ids.push(Role.find_by(name: 'Agent').id)) }
  86. include_examples 'user permission', false
  87. end
  88. end
  89. end