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.

activities_controller_test.rb 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. require "test_helper"
  2. class Api::V1::ActivitiesControllerTest < ActionDispatch::IntegrationTest
  3. setup do
  4. @activity = activities(:one)
  5. @user = users(:one)
  6. end
  7. #INDEX
  8. test "should access index" do
  9. get api_v1_activities_url,
  10. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  11. as: :json
  12. assert_response :success
  13. end
  14. test "should not access index" do
  15. get api_v1_activities_url, as: :json
  16. assert_response :forbidden
  17. end
  18. # test "should filter activities by name" do
  19. # assert_equal 2, Activity.filter_by_name('pyheatpump').count
  20. # end
  21. # test "should filter activities by name and sort them" do
  22. # assert_equal [activities(:one), activities(:three)], Activity.all.select {|n| n.name.include?("pyheat")}.sort {|a,b| a.name <=> b.name}
  23. # end
  24. #SHOW
  25. test "should access show" do
  26. get api_v1_activity_url(@activity),
  27. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  28. as: :json
  29. assert_response :success
  30. json_response = JSON.parse(self.response.body)
  31. assert_equal @activity.name, json_response['data']['attributes']['name']
  32. assert_equal @activity.author_id, json_response['data']['attributes']['author_id']
  33. assert_equal @activity.description, json_response['data']['attributes']['description']
  34. assert_equal @activity.client, json_response['data']['attributes']['client']
  35. end
  36. test "should not access show" do
  37. get api_v1_activity_url(@activity),
  38. as: :json
  39. assert_response :forbidden
  40. end
  41. #CREATE
  42. test "should create activity" do
  43. assert_difference("Activity.count") do
  44. post api_v1_activities_url,
  45. params: { activity: { name: @activity.name, client: @activity.client, description: @activity.description, author_id: @user }},
  46. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  47. as: :json
  48. end
  49. assert_response :created
  50. end
  51. test "should forbid create activity" do
  52. assert_no_difference "Activity.count" do
  53. post api_v1_activities_url,
  54. params: { activity: { name: @activity.name, client: @activity.client, description: @activity.description, author_id: @user } },
  55. as: :json
  56. end
  57. assert_response :forbidden
  58. end
  59. #UPDATE
  60. test "should update activity" do
  61. patch api_v1_activity_url(@activity),
  62. params: { activity: { name: "Updated name" } },
  63. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  64. as: :json
  65. assert_response :success
  66. end
  67. test "should forbid update activity - not logged in" do
  68. patch api_v1_activity_url(@activity),
  69. params: { activity: { name: "Updated name" } },
  70. as: :json
  71. assert_response :forbidden
  72. end
  73. #DESTROY
  74. test "should destroy activity" do
  75. assert_difference "Activity.count", -1 do
  76. delete api_v1_activity_url(@activity),
  77. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  78. as: :json
  79. end
  80. assert_response :no_content
  81. end
  82. test "should forbid destroy activity - not logged in" do
  83. assert_no_difference('Activity.count') do
  84. delete api_v1_activity_url(@activity), as: :json
  85. end
  86. assert_response :forbidden
  87. end
  88. end