API de comptabilité horaire.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

users_controller_test.rb 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. require "test_helper"
  2. class Api::V1::UsersControllerTest < ActionDispatch::IntegrationTest
  3. setup do
  4. @user = users(:one)
  5. @admin = users(:admin)
  6. end
  7. # INDEX
  8. test "should access index user" do
  9. get api_v1_users_url,
  10. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  11. as: :json
  12. assert_response :success
  13. end
  14. test "should forbid index user" do
  15. get api_v1_users_url, as: :json
  16. assert_response :forbidden
  17. end
  18. #SHOW
  19. test "should show user" do
  20. get api_v1_user_url(@user),
  21. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  22. as: :json
  23. assert_response :success
  24. # Test to ensure response contains the correct email
  25. json_response = JSON.parse(self.response.body, symbolize_names: true)
  26. assert_equal @user.email, json_response.dig(:data, :attributes, :email)
  27. assert_equal @user.username, json_response.dig(:data, :attributes, :username)
  28. end
  29. test "should not show user" do
  30. get api_v1_user_url(@user),
  31. as: :json
  32. assert_response :forbidden
  33. end
  34. #CREATE
  35. test "should create user" do
  36. assert_difference('User.count') do
  37. post api_v1_users_url, params: { user: { email: 'test@test.org', username: 'new_user_name', password: '123456' } }, as: :json
  38. end
  39. assert_response :created
  40. end
  41. test "should not create user with taken email" do
  42. assert_no_difference('User.count') do
  43. post api_v1_users_url, params: { user: { email: @user.email, username: 'username_test', password: '123456' } }, as: :json
  44. end
  45. assert_response :unprocessable_entity
  46. end
  47. test "should not create user with taken username" do
  48. assert_no_difference('User.count') do
  49. post api_v1_users_url, params: { user: { email: "test@email.com", username: @user.username, password: '123456' } }, as: :json
  50. end
  51. assert_response :unprocessable_entity
  52. end
  53. #UPDATE
  54. test "should update user" do
  55. patch api_v1_user_url(@user),
  56. params: { user: { email: @user.email, password: '123456' } },
  57. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  58. as: :json
  59. assert_response :success
  60. end
  61. test "should forbid update user" do
  62. patch api_v1_user_url(@user),
  63. params: { user: { email: @user.email, password: '123456' } },
  64. as: :json
  65. assert_response :forbidden
  66. end
  67. #DESTROY
  68. test "should destroy user" do
  69. assert_difference('User.count', -1) do
  70. delete api_v1_user_url(@user),
  71. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  72. as: :json
  73. end
  74. assert_response :no_content
  75. end
  76. test "should forbid destroy user" do
  77. assert_no_difference('User.count') do
  78. delete api_v1_user_url(@user), as: :json
  79. end
  80. assert_response :forbidden
  81. end
  82. end