admin_users_spec.rb 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. require 'rails_helper'
  2. describe Admin::UsersController do
  3. it "requires to be signed in as an admin" do
  4. login_as(users(:bob))
  5. visit admin_users_path
  6. expect(page).to have_text('Admin access required to view that page.')
  7. end
  8. context "as an admin" do
  9. before :each do
  10. login_as(users(:jane))
  11. end
  12. it "lists all users" do
  13. visit admin_users_path
  14. expect(page).to have_text('bob')
  15. expect(page).to have_text('jane')
  16. end
  17. it "allows to delete a user" do
  18. visit admin_users_path
  19. find(:css, "a[href='/admin/users/#{users(:bob).id}']").click
  20. expect(page).to have_text("User 'bob' was deleted.")
  21. expect(page).to have_no_text('bob@example.com')
  22. end
  23. context "creating new users" do
  24. it "follow the 'new user' link" do
  25. visit admin_users_path
  26. click_on('New User')
  27. expect(page).to have_text('Create new User')
  28. end
  29. it "creates a new user" do
  30. visit new_admin_user_path
  31. fill_in 'Email', with: 'test@test.com'
  32. fill_in 'Username', with: 'usertest'
  33. fill_in 'Password', with: '12345678'
  34. fill_in 'Password confirmation', with: '12345678'
  35. click_on 'Create User'
  36. expect(page).to have_text("User 'usertest' was successfully created.")
  37. expect(page).to have_text('test@test.com')
  38. end
  39. it "requires the passwords to match" do
  40. visit new_admin_user_path
  41. fill_in 'Email', with: 'test@test.com'
  42. fill_in 'Username', with: 'usertest'
  43. fill_in 'Password', with: '12345678'
  44. fill_in 'Password confirmation', with: 'no_match'
  45. click_on 'Create User'
  46. expect(page).to have_text("Password confirmation doesn't match")
  47. end
  48. end
  49. context "updating existing users" do
  50. it "follows the edit link" do
  51. visit admin_users_path
  52. click_on('bob')
  53. expect(page).to have_text('Edit User')
  54. end
  55. it "updates an existing user" do
  56. visit edit_admin_user_path(users(:bob))
  57. check 'Admin'
  58. click_on 'Update User'
  59. expect(page).to have_text("User 'bob' was successfully updated.")
  60. visit edit_admin_user_path(users(:bob))
  61. expect(page).to have_checked_field('Admin')
  62. end
  63. it "requires the passwords to match when changing them" do
  64. visit edit_admin_user_path(users(:bob))
  65. fill_in 'Password', with: '12345678'
  66. fill_in 'Password confirmation', with: 'no_match'
  67. click_on 'Update User'
  68. expect(page).to have_text("Password confirmation doesn't match")
  69. end
  70. end
  71. context "(de)activating users" do
  72. it "does not show deactivation buttons for the current user" do
  73. visit admin_users_path
  74. expect(page).to have_no_css("a[href='/admin/users/#{users(:jane).id}/deactivate']")
  75. end
  76. it "deactivates an existing user" do
  77. visit admin_users_path
  78. expect(page).to have_no_text('inactive')
  79. find(:css, "a[href='/admin/users/#{users(:bob).id}/deactivate']").click
  80. expect(page).to have_text('inactive')
  81. users(:bob).reload
  82. expect(users(:bob)).not_to be_active
  83. end
  84. it "activates an existing user" do
  85. users(:bob).deactivate!
  86. visit admin_users_path
  87. find(:css, "a[href='/admin/users/#{users(:bob).id}/activate']").click
  88. expect(page).to have_no_text('inactive')
  89. users(:bob).reload
  90. expect(users(:bob)).to be_active
  91. end
  92. end
  93. end
  94. end