PageRenderTime 51ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/spec/features/admin/admin_users_spec.rb

https://gitlab.com/beverett/gitlab-ce
Ruby | 412 lines | 317 code | 95 blank | 0 comment | 0 complexity | 2c80415b86bfe6a2ddbf3ebf2ea1c9fd MD5 | raw file
  1. require 'spec_helper'
  2. describe "Admin::Users" do
  3. let!(:user) do
  4. create(:omniauth_user, provider: 'twitter', extern_uid: '123456')
  5. end
  6. let!(:current_user) { create(:admin) }
  7. before do
  8. sign_in(current_user)
  9. end
  10. describe "GET /admin/users" do
  11. before do
  12. visit admin_users_path
  13. end
  14. it "is ok" do
  15. expect(current_path).to eq(admin_users_path)
  16. end
  17. it "has users list" do
  18. expect(page).to have_content(current_user.email)
  19. expect(page).to have_content(current_user.name)
  20. expect(page).to have_content(user.email)
  21. expect(page).to have_content(user.name)
  22. expect(page).to have_link('Block', href: block_admin_user_path(user))
  23. expect(page).to have_button('Delete user')
  24. expect(page).to have_button('Delete user and contributions')
  25. end
  26. describe 'Two-factor Authentication filters' do
  27. it 'counts users who have enabled 2FA' do
  28. create(:user, :two_factor)
  29. visit admin_users_path
  30. page.within('.filter-two-factor-enabled small') do
  31. expect(page).to have_content('1')
  32. end
  33. end
  34. it 'filters by users who have enabled 2FA' do
  35. user = create(:user, :two_factor)
  36. visit admin_users_path
  37. click_link '2FA Enabled'
  38. expect(page).to have_content(user.email)
  39. end
  40. it 'counts users who have not enabled 2FA' do
  41. visit admin_users_path
  42. page.within('.filter-two-factor-disabled small') do
  43. expect(page).to have_content('2') # Including admin
  44. end
  45. end
  46. it 'filters by users who have not enabled 2FA' do
  47. visit admin_users_path
  48. click_link '2FA Disabled'
  49. expect(page).to have_content(user.email)
  50. end
  51. end
  52. end
  53. describe "GET /admin/users/new" do
  54. before do
  55. visit new_admin_user_path
  56. fill_in "user_name", with: "Big Bang"
  57. fill_in "user_username", with: "bang"
  58. fill_in "user_email", with: "bigbang@mail.com"
  59. end
  60. it "creates new user" do
  61. expect { click_button "Create user" }.to change {User.count}.by(1)
  62. end
  63. it "applies defaults to user" do
  64. click_button "Create user"
  65. user = User.find_by(username: 'bang')
  66. expect(user.projects_limit)
  67. .to eq(Gitlab.config.gitlab.default_projects_limit)
  68. expect(user.can_create_group)
  69. .to eq(Gitlab.config.gitlab.default_can_create_group)
  70. end
  71. it "creates user with valid data" do
  72. click_button "Create user"
  73. user = User.find_by(username: 'bang')
  74. expect(user.name).to eq('Big Bang')
  75. expect(user.email).to eq('bigbang@mail.com')
  76. end
  77. it "calls send mail" do
  78. expect_any_instance_of(NotificationService).to receive(:new_user)
  79. click_button "Create user"
  80. end
  81. it "sends valid email to user with email & password" do
  82. perform_enqueued_jobs do
  83. click_button "Create user"
  84. end
  85. user = User.find_by(username: 'bang')
  86. email = ActionMailer::Base.deliveries.last
  87. expect(email.subject).to have_content('Account was created')
  88. expect(email.text_part.body).to have_content(user.email)
  89. expect(email.text_part.body).to have_content('password')
  90. end
  91. end
  92. describe "GET /admin/users/:id" do
  93. it "has user info" do
  94. visit admin_users_path
  95. click_link user.name
  96. expect(page).to have_content(user.email)
  97. expect(page).to have_content(user.name)
  98. expect(page).to have_link('Block user', href: block_admin_user_path(user))
  99. expect(page).to have_button('Delete user')
  100. expect(page).to have_button('Delete user and contributions')
  101. end
  102. describe 'Impersonation' do
  103. let(:another_user) { create(:user) }
  104. before do
  105. visit admin_user_path(another_user)
  106. end
  107. context 'before impersonating' do
  108. it 'shows impersonate button for other users' do
  109. expect(page).to have_content('Impersonate')
  110. end
  111. it 'does not show impersonate button for admin itself' do
  112. visit admin_user_path(current_user)
  113. expect(page).not_to have_content('Impersonate')
  114. end
  115. it 'does not show impersonate button for blocked user' do
  116. another_user.block
  117. visit admin_user_path(another_user)
  118. expect(page).not_to have_content('Impersonate')
  119. another_user.activate
  120. end
  121. end
  122. context 'when impersonating' do
  123. before do
  124. click_link 'Impersonate'
  125. end
  126. it 'logs in as the user when impersonate is clicked' do
  127. expect(page.find(:css, '.header-user .profile-link')['data-user']).to eql(another_user.username)
  128. end
  129. it 'sees impersonation log out icon' do
  130. icon = first('.fa.fa-user-secret')
  131. expect(icon).not_to be nil
  132. end
  133. it 'logs out of impersonated user back to original user' do
  134. find(:css, 'li.impersonation a').click
  135. expect(page.find(:css, '.header-user .profile-link')['data-user']).to eq(current_user.username)
  136. end
  137. it 'is redirected back to the impersonated users page in the admin after stopping' do
  138. find(:css, 'li.impersonation a').click
  139. expect(current_path).to eq("/admin/users/#{another_user.username}")
  140. end
  141. end
  142. context 'when impersonating a user with an expired password' do
  143. before do
  144. another_user.update(password_expires_at: Time.now - 5.minutes)
  145. click_link 'Impersonate'
  146. end
  147. it 'does not redirect to password change page' do
  148. expect(current_path).to eq('/')
  149. end
  150. it 'is redirected back to the impersonated users page in the admin after stopping' do
  151. find(:css, 'li.impersonation a').click
  152. expect(current_path).to eq("/admin/users/#{another_user.username}")
  153. end
  154. end
  155. end
  156. describe 'Two-factor Authentication status' do
  157. it 'shows when enabled' do
  158. user.update_attribute(:otp_required_for_login, true)
  159. visit admin_user_path(user)
  160. expect_two_factor_status('Enabled')
  161. end
  162. it 'shows when disabled' do
  163. visit admin_user_path(user)
  164. expect_two_factor_status('Disabled')
  165. end
  166. def expect_two_factor_status(status)
  167. page.within('.two-factor-status') do
  168. expect(page).to have_content(status)
  169. end
  170. end
  171. end
  172. end
  173. describe "GET /admin/users/:id/edit" do
  174. before do
  175. visit admin_users_path
  176. click_link "edit_user_#{user.id}"
  177. end
  178. it "has user edit page" do
  179. expect(page).to have_content('Name')
  180. expect(page).to have_content('Password')
  181. end
  182. describe "Update user" do
  183. before do
  184. fill_in "user_name", with: "Big Bang"
  185. fill_in "user_email", with: "bigbang@mail.com"
  186. fill_in "user_password", with: "AValidPassword1"
  187. fill_in "user_password_confirmation", with: "AValidPassword1"
  188. choose "user_access_level_admin"
  189. click_button "Save changes"
  190. end
  191. it "shows page with new data" do
  192. expect(page).to have_content('bigbang@mail.com')
  193. expect(page).to have_content('Big Bang')
  194. end
  195. it "changes user entry" do
  196. user.reload
  197. expect(user.name).to eq('Big Bang')
  198. expect(user.admin?).to be_truthy
  199. expect(user.password_expires_at).to be <= Time.now
  200. end
  201. end
  202. describe 'update username to non ascii char' do
  203. it do
  204. fill_in 'user_username', with: '\u3042\u3044'
  205. click_button('Save')
  206. page.within '#error_explanation' do
  207. expect(page).to have_content('Username')
  208. end
  209. expect(page).to have_selector(%(form[action="/admin/users/#{user.username}"]))
  210. end
  211. end
  212. end
  213. describe "GET /admin/users/:id/projects" do
  214. let(:group) { create(:group) }
  215. let!(:project) { create(:project, group: group) }
  216. before do
  217. group.add_developer(user)
  218. visit projects_admin_user_path(user)
  219. end
  220. it "lists group projects" do
  221. within(:css, '.append-bottom-default + .panel') do
  222. expect(page).to have_content 'Group projects'
  223. expect(page).to have_link group.name, admin_group_path(group)
  224. end
  225. end
  226. it 'allows navigation to the group details' do
  227. within(:css, '.append-bottom-default + .panel') do
  228. click_link group.name
  229. end
  230. within(:css, 'h3.page-title') do
  231. expect(page).to have_content "Group: #{group.name}"
  232. end
  233. expect(page).to have_content project.name
  234. end
  235. it 'shows the group access level' do
  236. within(:css, '.append-bottom-default + .panel') do
  237. expect(page).to have_content 'Developer'
  238. end
  239. end
  240. it 'allows group membership to be revoked', :js do
  241. page.within(first('.group_member')) do
  242. accept_confirm { find('.btn-remove').click }
  243. end
  244. wait_for_requests
  245. expect(page).not_to have_selector('.group_member')
  246. end
  247. end
  248. describe 'show user attributes' do
  249. it do
  250. visit admin_users_path
  251. click_link user.name
  252. expect(page).to have_content 'Account'
  253. expect(page).to have_content 'Personal projects limit'
  254. end
  255. end
  256. describe 'remove users secondary email', :js do
  257. let!(:secondary_email) do
  258. create :email, email: 'secondary@example.com', user: user
  259. end
  260. it do
  261. visit admin_user_path(user.username)
  262. expect(page).to have_content("Secondary email: #{secondary_email.email}")
  263. accept_confirm { find("#remove_email_#{secondary_email.id}").click }
  264. expect(page).not_to have_content(secondary_email.email)
  265. end
  266. end
  267. describe 'show user keys' do
  268. let!(:key1) do
  269. create(:key, user: user, title: "ssh-rsa Key1", key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4FIEBXGi4bPU8kzxMefudPIJ08/gNprdNTaO9BR/ndy3+58s2HCTw2xCHcsuBmq+TsAqgEidVq4skpqoTMB+Uot5Uzp9z4764rc48dZiI661izoREoKnuRQSsRqUTHg5wrLzwxlQbl1MVfRWQpqiz/5KjBC7yLEb9AbusjnWBk8wvC1bQPQ1uLAauEA7d836tgaIsym9BrLsMVnR4P1boWD3Xp1B1T/ImJwAGHvRmP/ycIqmKdSpMdJXwxcb40efWVj0Ibbe7ii9eeoLdHACqevUZi6fwfbymdow+FeqlkPoHyGg3Cu4vD/D8+8cRc7mE/zGCWcQ15Var83Tczour Key1")
  270. end
  271. let!(:key2) do
  272. create(:key, user: user, title: "ssh-rsa Key2", key: "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQSTWXhJAX/He+nG78MiRRRn7m0Pb0XbcgTxE0etArgoFoh9WtvDf36HG6tOSg/0UUNcp0dICsNAmhBKdncp6cIyPaXJTURPRAGvhI0/VDk4bi27bRnccGbJ/hDaUxZMLhhrzY0r22mjVf8PF6dvv5QUIQVm1/LeaWYsHHvLgiIjwrXirUZPnFrZw6VLREoBKG8uWvfSXw1L5eapmstqfsME8099oi+vWLR8MgEysZQmD28M73fgW4zek6LDQzKQyJx9nB+hJkKUDvcuziZjGmRFlNgSA2mguERwL1OXonD8WYUrBDGKroIvBT39zS5d9tQDnidEJZ9Y8gv5ViYP7x Key2")
  273. end
  274. it do
  275. visit admin_users_path
  276. click_link user.name
  277. click_link 'SSH keys'
  278. expect(page).to have_content(key1.title)
  279. expect(page).to have_content(key2.title)
  280. click_link key2.title
  281. expect(page).to have_content(key2.title)
  282. expect(page).to have_content(key2.key)
  283. click_link 'Remove'
  284. expect(page).not_to have_content(key2.title)
  285. end
  286. end
  287. describe 'show user identities' do
  288. it 'shows user identities' do
  289. visit admin_user_identities_path(user)
  290. expect(page).to have_content(user.name)
  291. expect(page).to have_content('twitter')
  292. end
  293. end
  294. describe 'update user identities' do
  295. before do
  296. allow(Gitlab::Auth::OAuth::Provider).to receive(:providers).and_return([:twitter, :twitter_updated])
  297. end
  298. it 'modifies twitter identity' do
  299. visit admin_user_identities_path(user)
  300. find('.table').find(:link, 'Edit').click
  301. fill_in 'identity_extern_uid', with: '654321'
  302. select 'twitter_updated', from: 'identity_provider'
  303. click_button 'Save changes'
  304. expect(page).to have_content(user.name)
  305. expect(page).to have_content('twitter_updated')
  306. expect(page).to have_content('654321')
  307. end
  308. end
  309. describe 'remove user with identities' do
  310. it 'removes user with twitter identity' do
  311. visit admin_user_identities_path(user)
  312. click_link 'Delete'
  313. expect(page).to have_content(user.name)
  314. expect(page).not_to have_content('twitter')
  315. end
  316. end
  317. end