users_controller_test.rb 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. require 'test_helper'
  2. class UsersControllerTest < ActionDispatch::IntegrationTest
  3. def setup
  4. @user = users(:daniel)
  5. @other_user = users(:ben)
  6. @student_user = users(:billy)
  7. @global_admin = users(:admin)
  8. end
  9. test "should display age correctly" do
  10. log_in_as @user
  11. get user_path @user
  12. assert_select ".user-age", /[A-z]+\: [0-9]+/
  13. end
  14. test "should show user" do
  15. log_in_as @user
  16. get user_url(@user)
  17. assert_response :success
  18. end
  19. test "should get edit" do
  20. log_in_as(@user)
  21. get edit_user_url(@user)
  22. assert_response :success
  23. end
  24. test "should update user" do
  25. log_in_as(@user)
  26. patch user_url(@user), params: { user: { email: @user.email, login: @user.login } }
  27. assert_redirected_to user_url(@user)
  28. end
  29. test "should redirect edit when not logged in" do
  30. get edit_user_path(@user)
  31. assert_not flash.empty?
  32. assert_redirected_to login_url
  33. end
  34. test "should redirect update when not logged in" do
  35. patch user_path(@user), params: { user: { name: @user.name,
  36. email: @user.email } }
  37. assert_not flash.empty?
  38. assert_redirected_to login_url
  39. end
  40. test "should not allow the name attribute to be edited by non-admin" do
  41. log_in_as(@user)
  42. assert_not_equal @user.name, "Wrong Name"
  43. patch user_path(@user), params: {
  44. user: { password: 'newpass',
  45. password_confirmation: 'newpass',
  46. name: "Wrong Name" } }
  47. assert_not_equal @user.reload.name, "Wrong Name"
  48. end
  49. test "should redirect edit when unauthorized" do
  50. log_in_as(@other_user)
  51. get edit_user_path(@user)
  52. assert flash.empty?
  53. assert_redirected_to root_url
  54. end
  55. test "should redirect update when unauthorized" do
  56. log_in_as(@other_user)
  57. patch user_path(@user), params: { user: { name: @user.name,
  58. email: @user.email } }
  59. assert flash.empty?
  60. assert_redirected_to root_url
  61. end
  62. test "should update name attribute when logged in as school admin" do
  63. log_in_as(@user)
  64. patch user_url(@student_user), params: { user: { name: "New Name" } }
  65. assert_equal "New Name", @student_user.reload.name
  66. assert_redirected_to user_url(@student_user)
  67. end
  68. test "should update name attribute when logged in as global admin" do
  69. log_in_as(@global_admin)
  70. patch user_url(@student_user), params: { user: { name: "New Name" } }
  71. assert_equal "New Name", @student_user.reload.name
  72. assert_redirected_to user_url(@student_user)
  73. end
  74. end