|
| 1 | +defmodule CodeCorps.ProjectUserControllerTest do |
| 2 | + use CodeCorps.ApiCase, resource_name: :project_user |
| 3 | + |
| 4 | + @attrs %{role: "contributor"} |
| 5 | + |
| 6 | + describe "index" do |
| 7 | + test "lists all resources", %{conn: conn} do |
| 8 | + [record_1, record_2] = insert_pair(:project_user) |
| 9 | + |
| 10 | + conn |
| 11 | + |> request_index |
| 12 | + |> json_response(200) |
| 13 | + |> assert_ids_from_response([record_1.id, record_2.id]) |
| 14 | + end |
| 15 | + |
| 16 | + test "filters resources by record id", %{conn: conn} do |
| 17 | + [record_1, record_2 | _] = insert_list(3, :project_user) |
| 18 | + |
| 19 | + path = "project-users/?filter[id]=#{record_1.id},#{record_2.id}" |
| 20 | + |
| 21 | + conn |
| 22 | + |> get(path) |
| 23 | + |> json_response(200) |
| 24 | + |> assert_ids_from_response([record_1.id, record_2.id]) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + describe "show" do |
| 29 | + test "shows chosen resource", %{conn: conn} do |
| 30 | + record = insert(:project_user) |
| 31 | + conn |
| 32 | + |> request_show(record) |
| 33 | + |> json_response(200) |
| 34 | + |> Map.get("data") |
| 35 | + |> assert_result_id(record.id) |
| 36 | + end |
| 37 | + |
| 38 | + test "renders 404 when id is nonexistent", %{conn: conn} do |
| 39 | + assert conn |> request_show(:not_found) |> json_response(404) |
| 40 | + end |
| 41 | + end |
| 42 | + |
| 43 | + describe "create" do |
| 44 | + @tag :authenticated |
| 45 | + test "creates and renders resource when data is valid", %{conn: conn, current_user: user} do |
| 46 | + project = insert(:project) |
| 47 | + attrs = @attrs |> Map.merge(%{project: project, user: user}) |
| 48 | + |
| 49 | + assert conn |> request_create(attrs) |> json_response(201) |
| 50 | + |
| 51 | + user_id = user.id |
| 52 | + tracking_properties = %{ |
| 53 | + project: project.title, |
| 54 | + project_id: project.id |
| 55 | + } |
| 56 | + |
| 57 | + assert_received {:track, ^user_id, "Requested Project Membership", ^tracking_properties} |
| 58 | + end |
| 59 | + |
| 60 | + @tag :authenticated |
| 61 | + test "does not create resource and renders 422 when data is invalid", %{conn: conn, current_user: user} do |
| 62 | + # only way to trigger a validation error is to provide a non-existant organization |
| 63 | + # anything else will fail on authorization level |
| 64 | + project = build(:project) |
| 65 | + attrs = @attrs |> Map.merge(%{project: project, user: user}) |
| 66 | + assert conn |> request_create(attrs) |> json_response(422) |
| 67 | + end |
| 68 | + |
| 69 | + test "does not create resource and renders 401 when not authenticated", %{conn: conn} do |
| 70 | + assert conn |> request_create |> json_response(401) |
| 71 | + end |
| 72 | + |
| 73 | + @tag :authenticated |
| 74 | + test "does not create resource and renders 403 when not authorized", %{conn: conn} do |
| 75 | + assert conn |> request_create |> json_response(403) |
| 76 | + end |
| 77 | + end |
| 78 | + |
| 79 | + describe "update" do |
| 80 | + @tag :authenticated |
| 81 | + test "updates and renders resource when data is valid", %{conn: conn, current_user: current_user} do |
| 82 | + project = insert(:project) |
| 83 | + record = insert(:project_user, project: project, role: "pending") |
| 84 | + insert(:project_user, project: project, user: current_user, role: "owner") |
| 85 | + |
| 86 | + assert conn |> request_update(record, @attrs) |> json_response(200) |
| 87 | + |
| 88 | + user_id = current_user.id |
| 89 | + tracking_properties = %{ |
| 90 | + project: project.title, |
| 91 | + project_id: project.id |
| 92 | + } |
| 93 | + |
| 94 | + assert_received {:track, ^user_id, "Approved Project Membership", ^tracking_properties} |
| 95 | + end |
| 96 | + |
| 97 | + test "doesn't update and renders 401 when unauthenticated", %{conn: conn} do |
| 98 | + assert conn |> request_update |> json_response(401) |
| 99 | + end |
| 100 | + |
| 101 | + @tag :authenticated |
| 102 | + test "doesn't update and renders 403 when not authorized", %{conn: conn} do |
| 103 | + assert conn |> request_update |> json_response(403) |
| 104 | + end |
| 105 | + |
| 106 | + @tag :authenticated |
| 107 | + test "renders 404 when id is nonexistent on update", %{conn: conn} do |
| 108 | + assert conn |> request_update(:not_found) |> json_response(404) |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + describe "delete" do |
| 113 | + @tag :authenticated |
| 114 | + test "deletes resource", %{conn: conn, current_user: current_user} do |
| 115 | + project = insert(:project) |
| 116 | + record = insert(:project_user, project: project) |
| 117 | + insert(:project_user, project: project, user: current_user, role: "owner") |
| 118 | + |
| 119 | + assert conn |> request_delete(record) |> response(204) |
| 120 | + end |
| 121 | + |
| 122 | + test "renders 401 when unauthenticated", %{conn: conn} do |
| 123 | + assert conn |> request_delete |> json_response(401) |
| 124 | + end |
| 125 | + |
| 126 | + @tag :authenticated |
| 127 | + test "renders 403 when not authorized", %{conn: conn} do |
| 128 | + assert conn |> request_delete |> json_response(403) |
| 129 | + end |
| 130 | + |
| 131 | + @tag :authenticated |
| 132 | + test "renders 404 when id is nonexistent on delete", %{conn: conn} do |
| 133 | + assert conn |> request_delete(:not_found) |> json_response(404) |
| 134 | + end |
| 135 | + end |
| 136 | +end |
0 commit comments