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.

teams_controller_test.rb 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. require "test_helper"
  2. class Api::V1::TeamsControllerTest < ActionDispatch::IntegrationTest
  3. setup do
  4. @team = teams(:one)
  5. @user = users(:one)
  6. end
  7. # INDEX
  8. test "should access team index" do
  9. get api_v1_teams_url,
  10. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  11. as: :json
  12. assert_response :success
  13. end
  14. test "should forbid team index" do
  15. get api_v1_teams_url, as: :json
  16. assert_response :forbidden
  17. end
  18. # SHOW
  19. test "should show team" do
  20. get api_v1_team_url(@team),
  21. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  22. as: :json
  23. assert_response :success
  24. end
  25. test "should forbid show team" do
  26. get api_v1_team_url(@team),
  27. as: :json
  28. assert_response :forbidden
  29. end
  30. # CREATE
  31. test "should create team" do
  32. assert_difference('Team.count') do
  33. post api_v1_teams_url,
  34. params: { team: { name: "Random name", description: "Random description" } },
  35. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  36. as: :json
  37. end
  38. assert_response :created
  39. end
  40. test "should forbid create team" do
  41. assert_no_difference('Team.count') do
  42. post api_v1_teams_url,
  43. params: { team: { name: "Random name", description: "Random description" } },
  44. as: :json
  45. end
  46. assert_response :forbidden
  47. end
  48. test "should forbid create team with taken name" do
  49. assert_no_difference('Team.count') do
  50. post api_v1_teams_url,
  51. params: { team: { name: @team.name, description: "Random description" } },
  52. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  53. as: :json
  54. end
  55. assert_response :unprocessable_entity
  56. end
  57. test "should forbid create a team without name" do
  58. assert_no_difference('Team.count') do
  59. post api_v1_teams_url,
  60. params: { team: { description: "Random description"} },
  61. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  62. as: :json
  63. end
  64. assert_response :unprocessable_entity
  65. end
  66. # UPDATE
  67. test "should update team" do
  68. patch api_v1_team_url(@team),
  69. params: { team: { name: "New name", description: "New description" } },
  70. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  71. as: :json
  72. assert_response :success
  73. end
  74. test "should not update team " do
  75. patch api_v1_team_url(@team),
  76. params: { team: { name: "New name", description: "New description" } },
  77. as: :json
  78. assert_response :forbidden
  79. end
  80. # Ajouter un test pour vérifier le statut de "current_user.can_edit" dans le modèle Membership
  81. # DESTROY
  82. test "should destroy team" do
  83. assert_difference('Team.count', -1) do
  84. delete api_v1_team_url(@team),
  85. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  86. as: :json
  87. end
  88. assert_response :no_content
  89. end
  90. test "should forbid destroy team" do
  91. assert_no_difference('Team.count') do
  92. delete api_v1_team_url(@team), as: :json
  93. end
  94. assert_response :forbidden
  95. end
  96. # Ajouter un test pour vérifier le statut de "current_user.can_edit" dans le modèle Membership
  97. end