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.

tasks_controller_test.rb 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. require "test_helper"
  2. class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest
  3. setup do
  4. @task = tasks(:one)
  5. @activity = activities(:one)
  6. @user = users(:one)
  7. end
  8. # INDEX
  9. test "should access index" do
  10. get api_v1_activity_tasks_url(@activity),
  11. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  12. as: :json
  13. assert_response :success
  14. end
  15. test "should not access index" do
  16. get api_v1_activity_tasks_url(@activity),
  17. as: :json
  18. assert_response :forbidden
  19. end
  20. # SHOW
  21. test "should access show" do
  22. get api_v1_activity_task_url(@activity, @task),
  23. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  24. as: :json
  25. assert_response :success
  26. json_response = JSON.parse(self.response.body)
  27. assert_equal @task.name, json_response['data']['attributes']['name']
  28. assert_equal @task.owner_id, json_response['data']['attributes']['owner_id']
  29. assert_equal @task.activity_id, json_response['data']['attributes']['activity_id']
  30. end
  31. test "should not access show" do
  32. get api_v1_activity_task_url(@activity, @task),
  33. as: :json
  34. assert_response :forbidden
  35. end
  36. # CREATE
  37. test "should create task" do
  38. assert_difference("Task.count") do
  39. post api_v1_activity_tasks_url(@activity),
  40. params: { task: { name: @task.name, description: @task.description, owner_id: @user, start_time: DateTime.now } },
  41. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  42. as: :json
  43. end
  44. assert_response :created
  45. end
  46. test "should not create task - not logged in" do
  47. assert_no_difference("Task.count") do
  48. post api_v1_activity_tasks_url(@activity),
  49. params: { task: { name: @task.name, description: @task.description, owner_id: @user } },
  50. as: :json
  51. end
  52. assert_response :forbidden
  53. end
  54. # UPDATE
  55. test "should update task" do
  56. patch api_v1_activity_task_url(@activity, @task),
  57. params: { task: { name: "New name", description: "New description" } },
  58. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  59. as: :json
  60. assert_response :success
  61. end
  62. test "should not update task" do
  63. patch api_v1_activity_task_url(@activity, @task),
  64. params: { task: { name: "New name", description: "New description" } },
  65. as: :json
  66. assert_response :forbidden
  67. end
  68. # DESTROY
  69. test "should destroy task" do
  70. assert_difference "Task.count", -1 do
  71. delete api_v1_activity_task_url(@activity, @task),
  72. headers: { Authorization: JsonWebToken.encode(user_id: @user.id) },
  73. as: :json
  74. end
  75. assert_response :no_content
  76. end
  77. test "should not destroy task" do
  78. assert_no_difference('Task.count') do
  79. delete api_v1_activity_task_url(@activity, @task),
  80. as: :json
  81. end
  82. assert_response :forbidden
  83. end
  84. end