require "test_helper" class Api::V1::MembershipsControllerTest < ActionDispatch::IntegrationTest setup do @membership = memberships(:one) @user = users(:one) @user2 = users(:two) @team = teams(:one) @team2 = teams(:two) end # INDEX test "should access Membership index" do get api_v1_memberships_url, headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json assert_response :success end test "should forbid Membership index" do get api_v1_memberships_url, as: :json assert_response :forbidden end # SHOW test "should show Membership" do get api_v1_memberships_url(@membership), headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json assert_response :success end test "should forbid show Membership" do get api_v1_memberships_url(@membership), as: :json assert_response :forbidden end # CREATE # test "should create membership" do # assert_difference("Membership.count") do # post api_v1_memberships_url, # params: { membership: { team_id: @team.id, member_id: @user.id, can_edit: false } }, # headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, # as: :json # end # assert_response :created # end test "should forbid create membership" do assert_no_difference("Membership.count") do post api_v1_memberships_url, params: { membership: { team_id: @team.id, member_id: @user.id, can_edit: false } }, as: :json end assert_response :forbidden end # UPDATE test "should update membership" do patch api_v1_membership_url(@membership), headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, params: { membership: { can_edit: true } }, as: :json assert_response :success end test "should forbid update membership" do patch api_v1_membership_url(@membership), params: { membership: { can_edit: true } }, as: :json assert_response :forbidden end test "should forbid update read_only attributes - team_id" do patch api_v1_membership_url(@membership), headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, params: { membership: { team_id: @team2.id, member_id: @user2.id } }, as: :json assert_response :unprocessable_entity end # DESTROY test "should destroy membership" do assert_difference('Membership.count', -1) do delete api_v1_membership_url(@membership), headers: { Authorization: JsonWebToken.encode(user_id: @user.id) }, as: :json end assert_response :no_content end test "should forbid destroy membership" do assert_no_difference('Membership.count') do delete api_v1_membership_url(@membership), as: :json end assert_response :forbidden end end