Skip to content

Commit 0d74d23

Browse files
committed
add invoice payment test for bounties
1 parent abd04ef commit 0d74d23

File tree

1 file changed

+94
-10
lines changed

1 file changed

+94
-10
lines changed

test/algora/bounties_test.exs

Lines changed: 94 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ defmodule Algora.BountiesTest do
77

88
alias Algora.Activities.Notifier
99
alias Algora.Activities.SendEmail
10+
alias Algora.Bounties
11+
alias Algora.Payments.Transaction
12+
alias Bounties.Tip
1013

1114
describe "bounties" do
1215
test "create" do
@@ -35,10 +38,10 @@ defmodule Algora.BountiesTest do
3538
amount: amount
3639
}
3740

38-
assert {:ok, bounty} = Algora.Bounties.create_bounty(bounty_params, [])
41+
assert {:ok, bounty} = Bounties.create_bounty(bounty_params, [])
3942

4043
assert {:ok, claims} =
41-
Algora.Bounties.claim_bounty(
44+
Bounties.claim_bounty(
4245
%{
4346
user: recipient,
4447
coauthor_provider_logins: [],
@@ -53,7 +56,7 @@ defmodule Algora.BountiesTest do
5356
claims = Repo.preload(claims, :user)
5457

5558
assert {:ok, _bounty} =
56-
Algora.Bounties.reward_bounty(
59+
Bounties.reward_bounty(
5760
%{
5861
owner: owner,
5962
amount: ~M[4000]usd,
@@ -64,7 +67,7 @@ defmodule Algora.BountiesTest do
6467
)
6568

6669
assert {:ok, _stripe_session_url} =
67-
Algora.Bounties.create_tip(
70+
Bounties.create_tip(
6871
%{
6972
amount: amount,
7073
owner: owner,
@@ -83,7 +86,7 @@ defmodule Algora.BountiesTest do
8386
assert "tip_activities" == tip.assoc_name
8487
assert tip.notify_users == [recipient.id]
8588
assert activity = Algora.Activities.get_with_preloaded_assoc(tip.assoc_name, tip.id)
86-
assert activity.assoc.__meta__.schema == Algora.Bounties.Tip
89+
assert activity.assoc.__meta__.schema == Tip
8790
assert activity.assoc.creator.id == creator.id
8891

8992
assert_enqueued(worker: Notifier, args: %{"activity_id" => bounty.id})
@@ -115,18 +118,99 @@ defmodule Algora.BountiesTest do
115118
amount: amount
116119
}
117120

118-
Algora.Bounties.create_bounty(bounty_params, [])
121+
Bounties.create_bounty(bounty_params, [])
119122
end)
120123

121-
assert Algora.Bounties.list_bounties(
124+
assert Bounties.list_bounties(
122125
owner_id: bounty.owner_id,
123126
tech_stack: ["elixir"],
124127
status: :open
125128
)
126129

127-
# assert Algora.Bounties.fetch_stats(bounty.owner_id)
128-
# assert Algora.Bounties.fetch_stats()
129-
assert Algora.Bounties.PrizePool.list()
130+
# assert Bounties.fetch_stats(bounty.owner_id)
131+
# assert Bounties.fetch_stats()
132+
assert Bounties.PrizePool.list()
133+
end
134+
135+
test "successfully creates and pays invoice for bounty claim" do
136+
creator = insert!(:user)
137+
owner = insert!(:organization)
138+
customer = insert!(:customer, user: owner)
139+
payment_method = insert!(:payment_method, customer: customer)
140+
recipient = insert!(:user)
141+
installation = insert!(:installation, owner: creator, connected_user: owner)
142+
_identity = insert!(:identity, user: creator, provider_email: creator.email)
143+
repo = insert!(:repository, %{user: owner})
144+
ticket = insert!(:ticket, %{repository: repo})
145+
amount = ~M[4000]usd
146+
147+
ticket_ref = %{
148+
owner: owner.handle,
149+
repo: repo.name,
150+
number: ticket.number
151+
}
152+
153+
assert {:ok, bounty} =
154+
Bounties.create_bounty(
155+
%{
156+
ticket_ref: ticket_ref,
157+
owner: owner,
158+
creator: creator,
159+
amount: amount
160+
},
161+
installation_id: installation.id
162+
)
163+
164+
assert {:ok, [claim]} =
165+
Bounties.claim_bounty(
166+
%{
167+
user: recipient,
168+
coauthor_provider_logins: [],
169+
target_ticket_ref: ticket_ref,
170+
source_ticket_ref: ticket_ref,
171+
status: :pending,
172+
type: :pull_request
173+
},
174+
installation_id: installation.id
175+
)
176+
177+
claim = Repo.preload(claim, :user)
178+
179+
assert {:ok, invoice} =
180+
Bounties.create_invoice(
181+
%{owner: owner, amount: amount},
182+
ticket_ref: ticket_ref,
183+
bounty_id: bounty.id,
184+
claims: [claim]
185+
)
186+
187+
assert {:ok, _invoice} =
188+
Algora.Stripe.Invoice.pay(invoice, %{
189+
payment_method: payment_method.provider_id,
190+
off_session: true
191+
})
192+
193+
charge = Repo.one!(from t in Transaction, where: t.type == :charge)
194+
assert Money.equal?(charge.net_amount, amount)
195+
assert charge.status == :initialized
196+
assert charge.user_id == owner.id
197+
198+
debit = Repo.one!(from t in Transaction, where: t.type == :debit)
199+
assert Money.equal?(debit.net_amount, amount)
200+
assert debit.status == :initialized
201+
assert debit.user_id == owner.id
202+
assert debit.bounty_id == bounty.id
203+
assert debit.claim_id == claim.id
204+
205+
credit = Repo.one!(from t in Transaction, where: t.type == :credit)
206+
assert Money.equal?(credit.net_amount, amount)
207+
assert credit.status == :initialized
208+
assert credit.user_id == recipient.id
209+
assert credit.bounty_id == bounty.id
210+
assert credit.claim_id == claim.id
211+
212+
transfer = Repo.one(from t in Transaction, where: t.type == :transfer)
213+
assert is_nil(transfer)
130214
end
131215
end
132216
end

0 commit comments

Comments
 (0)