users_spec.rb 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. # Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
  2. require 'rails_helper'
  3. RSpec.describe 'Manage > Users', type: :system do
  4. describe 'switching to an alternative user', authentication_type: :form, authenticated_as: :authenticate do
  5. let(:original_user) { create(:admin) }
  6. let(:alternative_one_user) { create(:admin) }
  7. let(:alternative_two_user) { create(:admin) }
  8. def authenticate
  9. alternative_one_user
  10. alternative_two_user
  11. original_user
  12. end
  13. it 'starts as original user' do
  14. expect(current_user).to eq original_user
  15. end
  16. it 'switches to alternative user' do
  17. switch_to(alternative_one_user)
  18. expect(current_user).to eq alternative_one_user
  19. end
  20. it 'switches to another alternative user' do
  21. switch_to(alternative_one_user)
  22. switch_to(alternative_two_user)
  23. expect(current_user).to eq alternative_two_user
  24. end
  25. it 'switches back to original user' do
  26. switch_to(alternative_one_user)
  27. switch_to(alternative_two_user)
  28. click '.switchBackToUser-close'
  29. expect(current_user).to eq original_user
  30. end
  31. def switch_to(user)
  32. visit 'manage/users'
  33. within(:active_content) do
  34. row = find("tr[data-id=\"#{user.id}\"]")
  35. row.find('.js-action').click
  36. row.find('.js-switchTo').click
  37. end
  38. expect(page).to have_text("Zammad looks like this for \"#{user.firstname} #{user.lastname}\"")
  39. end
  40. end
  41. # Fixes GitHub Issue #3050 - Newly created users are only shown in the admin interface after reload
  42. describe 'adding a new user', authenticated_as: -> { user } do
  43. let(:user) { create(:admin) }
  44. it 'newly added user is visible in the user list' do
  45. visit '#manage/users'
  46. within(:active_content) do
  47. find('[data-type=new]').click
  48. find('[name=firstname]').fill_in with: 'NewTestUserFirstName'
  49. find('[name=lastname]').fill_in with: 'User'
  50. find('span.label-text', text: 'Customer').first(:xpath, './/..').click
  51. click '.js-submit'
  52. expect(page).to have_css('table.user-list td', text: 'NewTestUserFirstName')
  53. end
  54. end
  55. describe 'select an Organization' do
  56. before do
  57. create(:organization, name: 'Example Inc.', active: true)
  58. create(:organization, name: 'Inactive Inc.', active: false)
  59. end
  60. it 'check for inactive Organizations in Organization selection' do
  61. visit '#manage/users'
  62. within(:active_content) do
  63. find('[data-type=new]').click
  64. find('[name=organization_id] ~ .searchableSelect-main').fill_in with: '**'
  65. expect(page).to have_css('ul.js-optionsList > li.js-option', minimum: 2)
  66. expect(page).to have_css('ul.js-optionsList > li.js-option .is-inactive', count: 1)
  67. end
  68. end
  69. end
  70. end
  71. describe 'show/unlock a user', authenticated_as: :authenticate do
  72. let(:user) { create(:admin) }
  73. let(:locked_user) { create(:user, login_failed: 6) }
  74. def authenticate
  75. locked_user
  76. user
  77. end
  78. it 'check marked locked user and execute unlock action' do
  79. visit '#manage/users'
  80. within(:active_content) do
  81. row = find("tr[data-id=\"#{locked_user.id}\"]")
  82. expect(row).to have_css('.icon-lock')
  83. row.find('.js-action').click
  84. row.find('li.unlock').click
  85. expect(row).to have_no_css('.icon-lock')
  86. end
  87. end
  88. end
  89. context 'updating a user' do
  90. let(:user) { create(:admin) }
  91. let(:row) { find "table.user-list tbody tr[data-id='#{user.id}']" }
  92. before do
  93. user
  94. visit '#manage/users'
  95. within(:active_content) do
  96. row.click
  97. end
  98. end
  99. it 'handles permission checkboxes correctly' do
  100. in_modal do
  101. scroll_into_view 'table.settings-list'
  102. within 'table.settings-list tbody tr:first-child' do
  103. click 'input[value="full"]', visible: :all
  104. expect(find('input[value="full"]', visible: :all)).to be_checked
  105. click 'input[value="read"]', visible: :all
  106. expect(find('input[value="full"]', visible: :all)).not_to be_checked
  107. expect(find('input[value="read"]', visible: :all)).to be_checked
  108. click 'input[value="full"]', visible: :all
  109. expect(find('input[value="full"]', visible: :all)).to be_checked
  110. expect(find('input[value="read"]', visible: :all)).not_to be_checked
  111. end
  112. end
  113. end
  114. it 'allows to update a user with no email/first/last/phone if login is present' do
  115. in_modal do
  116. fill_in 'firstname', with: ''
  117. fill_in 'lastname', with: ''
  118. fill_in 'Email', with: ''
  119. fill_in 'Phone', with: ''
  120. click_on 'Submit'
  121. end
  122. within :active_content do
  123. expect(page).to have_no_text(user.firstname)
  124. end
  125. end
  126. context 'when user has auto login' do
  127. let(:user) { create(:admin, login: "auto-#{SecureRandom.uuid}") }
  128. it 'does not allow to update a user with no email/first/last/phone' do
  129. in_modal do
  130. fill_in 'firstname', with: ''
  131. fill_in 'lastname', with: ''
  132. fill_in 'Email', with: ''
  133. fill_in 'Phone', with: ''
  134. click_on 'Submit'
  135. expect(page).to have_text('At least one identifier')
  136. end
  137. end
  138. end
  139. end
  140. describe 'check user edit permissions', authenticated_as: -> { user } do
  141. shared_examples 'user permission' do |allow|
  142. it(allow ? 'allows editing' : 'forbids editing') do
  143. visit "#user/profile/#{record.id}"
  144. find('.js-action .icon-arrow-down').click
  145. selector = '.js-action [data-type="edit"]'
  146. expect(page).to(allow ? have_css(selector) : have_no_css(selector))
  147. end
  148. end
  149. context 'when admin tries to change admin' do
  150. let(:user) { create(:admin) }
  151. let(:record) { create(:admin) }
  152. include_examples 'user permission', true
  153. end
  154. context 'when admin tries to change agent' do
  155. let(:user) { create(:admin) }
  156. let(:record) { create(:agent) }
  157. include_examples 'user permission', true
  158. end
  159. context 'when admin tries to change customer' do
  160. let(:user) { create(:admin) }
  161. let(:record) { create(:customer) }
  162. include_examples 'user permission', true
  163. end
  164. context 'when agent tries to change admin' do
  165. let(:user) { create(:agent) }
  166. let(:record) { create(:admin) }
  167. include_examples 'user permission', false
  168. end
  169. context 'when agent tries to change agent' do
  170. let(:user) { create(:agent) }
  171. let(:record) { create(:agent) }
  172. include_examples 'user permission', false
  173. end
  174. context 'when agent tries to change customer' do
  175. let(:user) { create(:agent) }
  176. let(:record) { create(:customer) }
  177. include_examples 'user permission', true
  178. end
  179. context 'when agent tries to change customer who is also admin' do
  180. let(:user) { create(:agent) }
  181. let(:record) { create(:customer, role_ids: Role.signup_role_ids.push(Role.find_by(name: 'Admin').id)) }
  182. include_examples 'user permission', false
  183. end
  184. context 'when agent tries to change customer who is also agent' do
  185. let(:user) { create(:agent) }
  186. let(:record) { create(:customer, role_ids: Role.signup_role_ids.push(Role.find_by(name: 'Agent').id)) }
  187. include_examples 'user permission', false
  188. end
  189. end
  190. describe 'UI is not updated right after importing users csv file #3919' do
  191. before do
  192. visit '#manage/users'
  193. ensure_websocket
  194. User.csv_import(
  195. string: File.read(Rails.root.join('spec/fixtures/files/csv_import/user/simple.csv')),
  196. parse_params: {
  197. col_sep: ';',
  198. },
  199. try: false,
  200. delete: false,
  201. )
  202. end
  203. it 'does update the user list after import of new users' do
  204. expect(page).to have_text('firstname-simple-import1')
  205. end
  206. end
  207. end