     @tag :authenticated
     test "creates and renders resource when data is valid", %{conn: conn, current_user: member} do
       organization = insert(:organization)
-
-      payload = build_payload |> put_relationships_with_member(organization, member)
-
-      path = conn |> organization_membership_path(:create)
-      response =
-        conn
-        |> post(path, payload)
-        |> json_response(201)
-        |> assert_jsonapi_relationship("organization", organization.id)
-        |> assert_jsonapi_relationship("member", member.id)
-
-      data = response |> Map.get("data") |> assert_role("pending")
-
-      membership = OrganizationMembership |> Repo.get(data["id"])
-      assert membership
-      assert membership.role == "pending"
-      assert membership.organization_id == organization.id
-      assert membership.member_id == member.id
+      attrs = @valid_attrs |> Map.merge(%{organization: organization, member: member})
+      assert conn |> request_create(attrs) |> json_response(201)
     end
 
     @tag :authenticated

      membership = insert(:organization_membership, organization: organization, role: "pending")
      insert(:organization_membership, organization: organization, member: current_user, role: "owner")

      payload = build_payload |> put_id(membership.id) |> put_attributes(@valid_attrs)

      path = conn |> organization_membership_path(:update, membership)

      response =
        conn
        |> put(path, payload)
        |> json_response(200)
        |> assert_jsonapi_relationship("organization", membership.organization.id)
        |> assert_jsonapi_relationship("member", membership.member.id)

      data = response |> Map.get("data") |> assert_role("contributor")

      membership = OrganizationMembership |> Repo.get(data["id"])
      assert membership
      assert membership.role == "contributor"
      assert membership.organization_id == membership.organization_id
      assert membership.member_id == membership.member_id
    end

    test "doesn't update and renders 401 when unauthenticated", %{conn: conn} do
      path = conn |> organization_membership_path(:update, "id doesn't matter")
      conn = conn |> put(path)

      assert conn |> json_response(401)
    end

    @tag :authenticated
    test "doesn't update and renders 403 when not authorized", %{conn: conn} do
      membership = insert(:organization_membership)

      payload =
        build_payload
        |> put_id(membership.id)
        |> put_attributes(@valid_attrs)

      path = conn |> organization_membership_path(:update, membership)
      conn = conn |> put(path, payload)

      assert conn |> json_response(403)
    end

    @tag :authenticated
    test "renders page not found when id is nonexistent on update", %{conn: conn} do
      payload = build_payload |> put_id(-1) |> put_attributes(@valid_attrs)

      path = conn |> organization_membership_path(:update, -1)
      assert conn |> put(path, payload) |> json_response(:not_found)
    end
  end


defmodule CodeCorps.OrganizationMembershipControllerTest do
  use CodeCorps.ApiCase, resource_name: :organization_membership

  @valid_attrs %{role: "contributor"}

  describe "index" do
    test "lists all resources", %{conn: conn} do
      [membership_1, membership_2] = insert_pair(:organization_membership)

      conn
      |> request_index
      |> json_response(200)
      |> assert_ids_from_response([membership_1.id, membership_2.id])
    end

    test "filters resources by membership id", %{conn: conn} do
      [membership_1, membership_2 | _] = insert_list(3, :organization_membership)

      path = "organization-memberships/?filter[id]=#{membership_1.id},#{membership_2.id}"

      conn
      |> get(path)
      |> json_response(200)
      |> assert_ids_from_response([membership_1.id, membership_2.id])
    end
  end

  describe "show" do
    test "shows chosen resource", %{conn: conn} do
      membership = insert(:organization_membership)
      conn
      |> request_show(membership)
      |> json_response(200)
      |> Map.get("data")
      |> assert_result_id(membership.id)
    end

    test "renders 404 when id is nonexistent", %{conn: conn} do
      assert conn |> request_show(:not_found) |> json_response(404)
    end
  end

