|
| 1 | +defmodule CodeCorpsWeb.GithubEventControllerTest do |
| 2 | + @moduledoc false |
| 3 | + |
| 4 | + use CodeCorpsWeb.ApiCase, resource_name: :github_event |
| 5 | + use CodeCorps.BackgroundProcessingCase |
| 6 | + |
| 7 | + import CodeCorps.GitHub.TestHelpers |
| 8 | + |
| 9 | + alias CodeCorps.GithubEvent |
| 10 | + |
| 11 | + defp for_event(conn, type, id) do |
| 12 | + conn |
| 13 | + |> put_req_header("x-github-event", type) |
| 14 | + |> put_req_header("x-github-delivery", id) |
| 15 | + end |
| 16 | + |
| 17 | + describe "index" do |
| 18 | + @tag authenticated: :admin |
| 19 | + test "paginates on index", %{conn: conn} do |
| 20 | + [_github_event_1, github_event_2] = insert_pair(:github_event) |
| 21 | + |
| 22 | + path = "github-events/?page[page]=2&page[page-size]=1" |
| 23 | + |
| 24 | + conn |
| 25 | + |> get(path) |
| 26 | + |> json_response(200) |
| 27 | + |> assert_ids_from_response([github_event_2.id]) |
| 28 | + end |
| 29 | + |
| 30 | + @tag authenticated: :admin |
| 31 | + test "lists all entries on index", %{conn: conn} do |
| 32 | + [github_event_1, github_event_2] = insert_pair(:github_event) |
| 33 | + |
| 34 | + conn |
| 35 | + |> request_index |
| 36 | + |> json_response(200) |
| 37 | + |> assert_ids_from_response([github_event_1.id, github_event_2.id]) |
| 38 | + end |
| 39 | + |
| 40 | + @tag authenticated: :admin |
| 41 | + test "filters resources on index", %{conn: conn} do |
| 42 | + [github_event_1, github_event_2 | _] = insert_list(3, :github_event) |
| 43 | + |
| 44 | + path = "github-events/?filter[id]=#{github_event_1.id},#{github_event_2.id}" |
| 45 | + |
| 46 | + conn |
| 47 | + |> get(path) |
| 48 | + |> json_response(200) |
| 49 | + |> assert_ids_from_response([github_event_1.id, github_event_2.id]) |
| 50 | + end |
| 51 | + |
| 52 | + test "renders 401 when unauthenticated", %{conn: conn} do |
| 53 | + assert conn |> request_index() |> json_response(401) |
| 54 | + end |
| 55 | + |
| 56 | + @tag :authenticated |
| 57 | + test "renders 403 when unauthorized", %{conn: conn} do |
| 58 | + assert conn |> request_index() |> json_response(403) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + describe "show" do |
| 63 | + @tag authenticated: :admin |
| 64 | + test "shows chosen resource", %{conn: conn} do |
| 65 | + github_event = insert(:github_event) |
| 66 | + |
| 67 | + conn |
| 68 | + |> request_show(github_event) |
| 69 | + |> json_response(200) |
| 70 | + |> assert_id_from_response(github_event.id) |
| 71 | + end |
| 72 | + |
| 73 | + test "renders 401 when unauthenticated", %{conn: conn} do |
| 74 | + github_event = insert(:github_event) |
| 75 | + assert conn |> request_show(github_event) |> json_response(401) |
| 76 | + end |
| 77 | + |
| 78 | + @tag :authenticated |
| 79 | + test "renders 403 when unauthorized", %{conn: conn} do |
| 80 | + github_event = insert(:github_event) |
| 81 | + assert conn |> request_show(github_event) |> json_response(403) |
| 82 | + end |
| 83 | + end |
| 84 | + |
| 85 | + describe "create" do |
| 86 | + @tag :github_webhook |
| 87 | + test "responds with 200 for a supported event", %{conn: conn} do |
| 88 | + path = conn |> github_events_path(:create) |
| 89 | + |
| 90 | + payload = load_event_fixture("installation_created") |
| 91 | + assert conn |> for_event("installation", "foo") |> post(path, payload) |> response(200) |
| 92 | + |
| 93 | + wait_for_supervisor() |
| 94 | + |
| 95 | + assert Repo.get_by(GithubEvent, github_delivery_id: "foo") |
| 96 | + end |
| 97 | + |
| 98 | + @tag :github_webhook |
| 99 | + test "responds with 202 for an unsupported event", %{conn: conn} do |
| 100 | + path = conn |> github_events_path(:create) |
| 101 | + assert conn |> for_event("gollum", "foo") |> post(path, %{}) |> response(202) |
| 102 | + |
| 103 | + wait_for_supervisor() |
| 104 | + |
| 105 | + refute Repo.get_by(GithubEvent, github_delivery_id: "foo") |
| 106 | + end |
| 107 | + |
| 108 | + @tag :github_webhook |
| 109 | + test "responds with 202 for an unknown event", %{conn: conn} do |
| 110 | + path = conn |> github_events_path(:create) |
| 111 | + assert conn |> for_event("unknown", "foo") |> post(path, %{}) |> response(202) |
| 112 | + |
| 113 | + wait_for_supervisor() |
| 114 | + |
| 115 | + refute Repo.get_by(GithubEvent, github_delivery_id: "foo") |
| 116 | + end |
| 117 | + end |
| 118 | +end |
0 commit comments