Skip to content

Commit a712326

Browse files
authored
Merge pull request #11 from puretype/json-tests-prepare
Prepare JSON in tests
2 parents 4c92c8b + 81b8bde commit a712326

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

mix.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ defmodule GitHubWebhook.MixProject do
3535

3636
defp deps do
3737
[
38+
{:jason, "~> 1.0", optional: true},
3839
{:mix_test_watch, "~> 1.0", only: [:dev, :test], runtime: false},
3940
{:excoveralls, "~> 0.18", only: :test},
4041
{:credo, "~> 1.0", only: [:dev, :test], runtime: false},

test/github_webhook_test.exs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ defmodule GitHubWebhookTest do
22
use ExUnit.Case, async: true
33
use Plug.Test
44

5+
@test_body %{"hello" => "world"}
6+
@test_body_serialized Jason.encode!(@test_body)
7+
58
# Demo plug with basic auth and a simple index action
69
defmodule DemoPlug do
710
use Plug.Builder
@@ -22,7 +25,7 @@ defmodule GitHubWebhookTest do
2225

2326
test "when verification fails, returns a 403" do
2427
conn =
25-
gh_webhook_request("hello world")
28+
gh_webhook_request()
2629
|> put_req_header("x-hub-signature", "sha1=wrong_hexdigest")
2730
|> DemoPlug.call([])
2831

@@ -33,24 +36,33 @@ defmodule GitHubWebhookTest do
3336

3437
test "when payload is verified, returns a 200" do
3538
conn =
36-
gh_webhook_request("hello world")
39+
gh_webhook_request()
3740
|> DemoPlug.call([])
3841

3942
assert conn.status == 200
40-
assert Process.get(:payload) == "hello world"
43+
assert Process.get(:payload) == @test_body_serialized
4144
assert !Process.get(:next_in_chain_called)
4245
end
4346

4447
test "passes GitHub headers as keyword options" do
4548
conn =
46-
gh_webhook_request("hello world")
49+
gh_webhook_request()
4750
|> put_req_header("x-github-hook-installation-target-id", "123")
4851
|> DemoPlug.call([])
4952

5053
assert conn.status == 200
5154
assert Process.get(:opts) == [{:installation_target_id, "123"}]
5255
end
5356

57+
test "deserializes JSON payload" do
58+
conn =
59+
gh_webhook_request(%{"hello" => "world"})
60+
|> DemoPlug.call([])
61+
62+
assert conn.status == 200
63+
assert Process.get(:payload) == @test_body_serialized
64+
end
65+
5466
test "when path does not match, skips this plug and proceeds to next one" do
5567
conn =
5668
conn(:post, "/hello")
@@ -80,11 +92,11 @@ defmodule GitHubWebhookTest do
8092
Application.put_env(:github_webhook, :secret, "wrong")
8193

8294
conn =
83-
gh_webhook_request("hello world")
95+
gh_webhook_request(@test_body)
8496
|> DemoPlugParamPresendence.call([])
8597

8698
assert conn.status == 200
87-
assert Process.get(:payload) == "hello world"
99+
assert Process.get(:payload) == @test_body_serialized
88100
end
89101

90102
test "when secret is not set in params, it uses application setting" do
@@ -102,11 +114,11 @@ defmodule GitHubWebhookTest do
102114
Application.put_env(:github_webhook, :secret, "1234")
103115

104116
conn =
105-
gh_webhook_request("hello world", "1234")
117+
gh_webhook_request(%{"hello" => "world"}, "1234")
106118
|> DemoPlugApplicationSecret.call([])
107119

108120
assert conn.status == 200
109-
assert Process.get(:payload) == "hello world"
121+
assert Process.get(:payload) == @test_body_serialized
110122
end
111123

112124
test "when secret is not set in params or Application setting, it assumes an empty secret" do
@@ -124,14 +136,16 @@ defmodule GitHubWebhookTest do
124136
Application.delete_env(:github_webhook, :secret)
125137

126138
conn =
127-
gh_webhook_request("hello world", "")
139+
gh_webhook_request(@test_body, "")
128140
|> DemoPlugNoSecret.call([])
129141

130142
assert conn.status == 200
131-
assert Process.get(:payload) == "hello world"
143+
assert Process.get(:payload) == @test_body_serialized
132144
end
133145

134-
defp gh_webhook_request(body, secret \\ "secret") do
146+
defp gh_webhook_request(body \\ %{"hello" => "world"}, secret \\ "secret") do
147+
body = Jason.encode!(body)
148+
135149
hexdigest =
136150
"sha1=" <>
137151
(:crypto.mac(:hmac, :sha, secret, body) |> Base.encode16(case: :lower))

0 commit comments

Comments
 (0)