Skip to content

Commit a786837

Browse files
committed
add tests for bounty edits
1 parent c130fa3 commit a786837

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

test/algora_web/controllers/webhooks/github_controller_test.exs

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
defmodule AlgoraWeb.Webhooks.GithubControllerTest do
22
use AlgoraWeb.ConnCase
33
use ExMachina.Ecto, repo: Algora.Repo
4+
use Oban.Testing, repo: Algora.Repo
45

56
import Algora.Factory
6-
import AlgoraWeb.Webhooks.GithubController
77
import Money.Sigil
88
import Mox
99

1010
alias Algora.Bounties.Bounty
1111
alias Algora.Bounties.Claim
12+
alias Algora.Bounties.Jobs.NotifyBounty
1213
alias Algora.Github.Webhook
1314
alias Algora.Repo
15+
alias AlgoraWeb.Webhooks.GithubController
1416

1517
setup :verify_on_exit!
1618

@@ -30,7 +32,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
3032
}
3133
end
3234

33-
describe "bounty command" do
35+
describe "create bounties" do
3436
setup [:setup_github_mocks]
3537

3638
test "handles bounty command with unauthorized user", ctx do
@@ -100,6 +102,68 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
100102
end
101103
end
102104

105+
describe "edit bounties" do
106+
setup [:setup_github_mocks]
107+
108+
test "updates bounty amount when editing the original bounty comment", ctx do
109+
comment_id = :rand.uniform(1000)
110+
111+
process_scenario!(ctx, [
112+
%{
113+
event_action: "issue_comment.created",
114+
user_type: :admin,
115+
body: "/bounty $100",
116+
params: %{"id" => comment_id}
117+
}
118+
])
119+
120+
assert Money.equal?(Repo.one(Bounty).amount, ~M[100]usd)
121+
122+
assert [job] = all_enqueued(worker: NotifyBounty)
123+
assert {:ok, _} = perform_job(NotifyBounty, job.args)
124+
125+
process_scenario!(ctx, [
126+
%{
127+
event_action: "issue_comment.edited",
128+
user_type: :admin,
129+
body: "/bounty $200",
130+
params: %{"id" => comment_id}
131+
}
132+
])
133+
134+
assert Money.equal?(Repo.one(Bounty).amount, ~M[200]usd)
135+
end
136+
137+
test "adds to bounty amount when creating a new bounty comment", ctx do
138+
comment_id = :rand.uniform(1000)
139+
140+
process_scenario!(ctx, [
141+
%{
142+
event_action: "issue_comment.created",
143+
user_type: :admin,
144+
body: "/bounty $100",
145+
params: %{"id" => comment_id}
146+
}
147+
])
148+
149+
assert Money.equal?(Repo.one(Bounty).amount, ~M[100]usd)
150+
151+
assert [job] = all_enqueued(worker: NotifyBounty)
152+
assert {:ok, _} = perform_job(NotifyBounty, job.args)
153+
154+
process_scenario!(ctx, [
155+
%{
156+
event_action: "issue_comment.created",
157+
user_type: :admin,
158+
body: "/bounty $200",
159+
params: %{"id" => comment_id + 1}
160+
}
161+
])
162+
163+
assert Money.equal?(Repo.one(Bounty).amount, ~M[300]usd)
164+
end
165+
end
166+
103167
describe "pull request closed event" do
104168
setup [:setup_github_mocks]
105169

@@ -197,6 +261,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
197261
setup_get_user_by_username()
198262
setup_get_issue()
199263
setup_get_repository()
264+
setup_add_labels()
200265
:ok
201266
end
202267

@@ -273,6 +338,14 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
273338
)
274339
end
275340

341+
defp setup_add_labels do
342+
stub(
343+
Algora.GithubMock,
344+
:add_labels,
345+
fn _token, _owner, _repo, _number, _labels -> {:ok, %{}} end
346+
)
347+
end
348+
276349
defp mock_body(body \\ ""), do: "Lorem\r\nipsum\r\n dolor #{body} sit\r\namet"
277350

278351
defp mock_user(user) do
@@ -388,7 +461,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
388461
scenario,
389462
:ok,
390463
fn opts, :ok ->
391-
case ctx |> Map.merge(opts) |> mock_webhook() |> process_delivery() do
464+
case ctx |> Map.merge(opts) |> mock_webhook() |> GithubController.process_delivery() do
392465
:ok -> {:cont, :ok}
393466
error -> {:halt, error}
394467
end

0 commit comments

Comments
 (0)