|
| 1 | +defmodule ElixirBenchWeb.Github.WebHooksControllerTest do |
| 2 | + use ElixirBenchWeb.ConnCase, async: false |
| 3 | + |
| 4 | + import ElixirBenchWeb.TestHelpers |
| 5 | + |
| 6 | + alias ElixirBench.{Repos.Repo, Benchmarks.Job} |
| 7 | + |
| 8 | + @github_repo_attrs %{name: "public-repo", owner: "baxterthehacker"} |
| 9 | + |
| 10 | + describe "handle/2" do |
| 11 | + test "create job given pull request event", context do |
| 12 | + insert(:repo, @github_repo_attrs) |
| 13 | + params = %{"payload" => pull_request_payload()} |
| 14 | + |
| 15 | + assert_difference(Repo, 0) do |
| 16 | + assert_difference(Job, 1) do |
| 17 | + {:ok, %{"data" => data}} = |
| 18 | + context.conn |
| 19 | + |> set_headers("pull_request") |
| 20 | + |> post("/hooks/handle", params) |
| 21 | + |> decode_response_body |
| 22 | + end |
| 23 | + end |
| 24 | + |
| 25 | + assert %{"branch_name" => "changes", "repo_slug" => "baxterthehacker/public-repo"} = data |
| 26 | + end |
| 27 | + |
| 28 | + test "create job given push event", context do |
| 29 | + insert(:repo, @github_repo_attrs) |
| 30 | + params = %{"payload" => push_payload()} |
| 31 | + |
| 32 | + assert_difference(Repo, 0) do |
| 33 | + assert_difference(Job, 1) do |
| 34 | + {:ok, %{"data" => data}} = |
| 35 | + context.conn |
| 36 | + |> set_headers("push") |
| 37 | + |> post("/hooks/handle", params) |
| 38 | + |> decode_response_body |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + assert %{"branch_name" => "changes", "repo_slug" => "baxterthehacker/public-repo"} = data |
| 43 | + end |
| 44 | + |
| 45 | + test "respond to ping event", context do |
| 46 | + response = |
| 47 | + context.conn |
| 48 | + |> set_headers("ping") |
| 49 | + |> post("/hooks/handle", %{"payload" => ~s({"data": "some data"})}) |
| 50 | + |
| 51 | + assert {:ok, %{"message" => "pong"}} = decode_response_body(response) |
| 52 | + assert 200 = response.status |
| 53 | + end |
| 54 | + |
| 55 | + test "not break given unexpected pull request event payload scheme", context do |
| 56 | + insert(:repo, @github_repo_attrs) |
| 57 | + params = %{"payload" => ~s({"data": "some data"})} |
| 58 | + |
| 59 | + assert_difference(Repo, 0) do |
| 60 | + assert_difference(Job, 0) do |
| 61 | + {:ok, %{"errors" => errors}} = |
| 62 | + context.conn |
| 63 | + |> set_headers("pull_request") |
| 64 | + |> post("/hooks/handle", params) |
| 65 | + |> decode_response_body |
| 66 | + end |
| 67 | + end |
| 68 | + |
| 69 | + assert %{"detail" => "Bad request"} = errors |
| 70 | + end |
| 71 | + |
| 72 | + test "not break given unexpected push event payload scheme", context do |
| 73 | + insert(:repo, @github_repo_attrs) |
| 74 | + params = %{"payload" => ~s({"data": "some data"})} |
| 75 | + |
| 76 | + assert_difference(Repo, 0) do |
| 77 | + assert_difference(Job, 0) do |
| 78 | + {:ok, %{"errors" => errors}} = |
| 79 | + context.conn |
| 80 | + |> set_headers("push") |
| 81 | + |> post("/hooks/handle", params) |
| 82 | + |> decode_response_body |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + assert %{"detail" => "Bad request"} = errors |
| 87 | + end |
| 88 | + |
| 89 | + test "return error given unprocessable event in header", context do |
| 90 | + insert(:repo, @github_repo_attrs) |
| 91 | + params = %{"payload" => push_payload()} |
| 92 | + |
| 93 | + {:ok, %{"errors" => errors}} = |
| 94 | + context.conn |
| 95 | + |> set_headers("randomevent") |
| 96 | + |> post("/hooks/handle", params) |
| 97 | + |> decode_response_body |
| 98 | + |
| 99 | + assert %{"detail" => "Unprocessable entity"} = errors |
| 100 | + end |
| 101 | + |
| 102 | + test "return error when event headers not present", context do |
| 103 | + insert(:repo, @github_repo_attrs) |
| 104 | + params = %{"payload" => push_payload()} |
| 105 | + |
| 106 | + {:ok, %{"errors" => errors}} = |
| 107 | + context.conn |
| 108 | + |> post("/hooks/handle", params) |
| 109 | + |> decode_response_body |
| 110 | + |
| 111 | + assert %{"detail" => "Unprocessable entity"} = errors |
| 112 | + end |
| 113 | + |
| 114 | + test "return error when payload missing", context do |
| 115 | + insert(:repo, @github_repo_attrs) |
| 116 | + |
| 117 | + {:ok, %{"errors" => errors}} = |
| 118 | + context.conn |
| 119 | + |> set_headers("pull_request") |
| 120 | + |> post("/hooks/handle", %{"payload" => ""}) |
| 121 | + |> decode_response_body |
| 122 | + |
| 123 | + assert %{"detail" => "Bad request"} = errors |
| 124 | + |
| 125 | + {:ok, %{"errors" => errors}} = |
| 126 | + context.conn |
| 127 | + |> set_headers("pull_request") |
| 128 | + |> post("/hooks/handle") |
| 129 | + |> decode_response_body |
| 130 | + |
| 131 | + assert %{"detail" => "Bad request"} = errors |
| 132 | + end |
| 133 | + end |
| 134 | + |
| 135 | + defp set_headers(conn, event) do |
| 136 | + conn |
| 137 | + |> put_req_header("x-github-event", event) |
| 138 | + end |
| 139 | +end |
0 commit comments