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

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