require "test_helper" class Api::V1::TasksControllerTest < ActionDispatch::IntegrationTest setup do @task = tasks(:one) @activity = activities(:one) @user = users(:one) end # INDEX test "should access index" do get api_v1_activity_tasks_url(@activity), headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json assert_response :success end test "should not access index" do get api_v1_activity_tasks_url(@activity), as: :json assert_response :forbidden end # SHOW test "should access show" do get api_v1_activity_task_url(@activity, @task), headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json assert_response :success end test "should not access show" do get api_v1_activity_task_url(@activity, @task), as: :json assert_response :forbidden end # CREATE test "should create task" do assert_difference("Task.count") do post api_v1_activity_tasks_url(@activity), params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } }, headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json end assert_response :created end test "should forbid create task - not logged in" do assert_no_difference("Task.count") do post api_v1_activity_tasks_url(@activity), params: { task: { name: @task.name, description: @task.description, user_id: @user, activity_id: @activity } }, as: :json end assert_response :forbidden end test "should forbid create task - missing user or activity id" do assert_no_difference("Task.count") do post api_v1_activity_tasks_url(@activity), params: { task: { name: @task.name, description: @task.description } }, headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json end assert_response :forbidden end end