API de comptabilité horaire.
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

activities_controller_test.rb 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. #SHOW
  19. test "should access show" do
  20. get api_v1_activity_url(@activity),
  21. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  22. as: :json
  23. assert_response :success
  24. end
  25. test "should not access show" do
  26. get api_v1_activity_url(@activity),
  27. as: :json
  28. assert_response :forbidden
  29. end
  30. #CREATE
  31. test "should create activity" do
  32. assert_difference("Activity.count") do
  33. post api_v1_activities_url,
  34. params: { activity: { name: @activity.name, client: @activity.client, description: @activity.description, author_id: @user }},
  35. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  36. as: :json
  37. end
  38. assert_response :created
  39. end
  40. test "should forbid create activity" do
  41. assert_no_difference "Activity.count" do
  42. post api_v1_activities_url,
  43. params: { activity: { name: @activity.name, client: @activity.client, description: @activity.description, author_id: @user } },
  44. as: :json
  45. end
  46. assert_response :forbidden
  47. end
  48. #UPDATE
  49. test "should update activity" do
  50. patch api_v1_activity_url(@activity),
  51. params: { activity: { name: "Updated name" } },
  52. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  53. as: :json
  54. assert_response :success
  55. end
  56. test "should forbid update activity - not logged in" do
  57. patch api_v1_activity_url(@activity),
  58. params: { activity: { name: "Updated name" } },
  59. as: :json
  60. assert_response :forbidden
  61. end
  62. #DESTROY
  63. test "should destroy activity" do
  64. assert_difference "Activity.count", -1 do
  65. delete api_v1_activity_url(@activity),
  66. headers: { Authorization: JsonWebToken.encode(user_id: @activity.author_id) },
  67. as: :json
  68. end
  69. assert_response :no_content
  70. end
  71. test "should forbid destroy activity - not logged in" do
  72. assert_no_difference('Activity.count') do
  73. delete api_v1_activity_url(@activity), as: :json
  74. end
  75. assert_response :forbidden
  76. end
  77. end