Skip to content

Commit cd16439

Browse files
committed
add autopay tests
1 parent 40e8855 commit cd16439

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

test/algora_web/controllers/webhooks/github_controller_test.exs

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
1212
alias Algora.Bounties.Claim
1313
alias Algora.Bounties.Jobs.NotifyBounty
1414
alias Algora.Github.Webhook
15+
alias Algora.Payments.Transaction
1516
alias Algora.Repo
1617
alias AlgoraWeb.Webhooks.GithubController
1718

@@ -452,6 +453,133 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
452453

453454
assert Repo.one(Claim).status == :approved
454455
end
456+
457+
test "handles autopay", ctx do
458+
issue_number = :rand.uniform(1000)
459+
pr_number = :rand.uniform(1000)
460+
461+
customer = insert!(:customer, user: ctx[:org])
462+
_payment_method = insert!(:payment_method, is_default: true, customer: customer)
463+
464+
process_scenario!(ctx, [
465+
%{
466+
event_action: "issue_comment.created",
467+
user_type: :admin,
468+
body: "/bounty $100",
469+
params: %{"issue" => %{"number" => issue_number}}
470+
},
471+
%{
472+
event_action: "pull_request.opened",
473+
user_type: :unauthorized,
474+
body: "/claim #{issue_number}",
475+
params: %{"pull_request" => %{"number" => pr_number}}
476+
},
477+
%{
478+
event_action: "pull_request.closed",
479+
user_type: :unauthorized,
480+
body: "/claim #{issue_number}",
481+
params: %{"pull_request" => %{"number" => pr_number, "merged_at" => DateTime.to_iso8601(DateTime.utc_now())}}
482+
}
483+
])
484+
485+
bounty = Repo.one!(Bounty)
486+
claim = Repo.one!(Claim)
487+
assert claim.target_id == bounty.ticket_id
488+
assert claim.status == :approved
489+
490+
charge = Repo.one!(from t in Transaction, where: t.type == :charge)
491+
assert Money.equal?(charge.net_amount, Money.new(:USD, 100))
492+
assert charge.status == :initialized
493+
assert charge.user_id == ctx[:org].id
494+
495+
debit = Repo.one!(from t in Transaction, where: t.type == :debit)
496+
assert Money.equal?(debit.net_amount, Money.new(:USD, 100))
497+
assert debit.status == :initialized
498+
assert debit.user_id == ctx[:org].id
499+
assert debit.bounty_id == bounty.id
500+
assert debit.claim_id == claim.id
501+
502+
credit = Repo.one!(from t in Transaction, where: t.type == :credit)
503+
assert Money.equal?(credit.net_amount, Money.new(:USD, 100))
504+
assert credit.status == :initialized
505+
assert credit.user_id == ctx[:unauthorized_user].id
506+
assert credit.bounty_id == bounty.id
507+
assert credit.claim_id == claim.id
508+
509+
transfer = Repo.one(from t in Transaction, where: t.type == :transfer)
510+
assert is_nil(transfer)
511+
end
512+
513+
test "handles split bounty payments between two users when PR is merged", ctx do
514+
issue_number = :rand.uniform(1000)
515+
pr_number = :rand.uniform(1000)
516+
517+
customer = insert!(:customer, user: ctx[:org])
518+
_payment_method = insert!(:payment_method, is_default: true, customer: customer)
519+
520+
user1 = ctx[:unauthorized_user]
521+
user2 = insert!(:user)
522+
523+
process_scenario!(ctx, [
524+
%{
525+
event_action: "issue_comment.created",
526+
user_type: :admin,
527+
body: "/bounty $100",
528+
params: %{"issue" => %{"number" => issue_number}}
529+
},
530+
%{
531+
event_action: "pull_request.opened",
532+
user_type: :unauthorized,
533+
body: "/claim #{issue_number} /split @#{user2.provider_login}",
534+
params: %{"pull_request" => %{"number" => pr_number}}
535+
},
536+
%{
537+
event_action: "pull_request.closed",
538+
user_type: :unauthorized,
539+
body: "/claim #{issue_number} /split @#{user2.provider_login}",
540+
params: %{"pull_request" => %{"number" => pr_number, "merged_at" => DateTime.to_iso8601(DateTime.utc_now())}}
541+
}
542+
])
543+
544+
bounty = Repo.one!(Bounty)
545+
claim1 = Repo.one!(from c in Claim, where: c.user_id == ^user1.id)
546+
claim2 = Repo.one!(from c in Claim, where: c.user_id == ^user2.id)
547+
assert claim1.target_id == bounty.ticket_id
548+
assert claim1.status == :approved
549+
assert claim2.status == :approved
550+
551+
charge = Repo.one!(from t in Transaction, where: t.type == :charge)
552+
assert Money.equal?(charge.net_amount, Money.new(:USD, 100))
553+
assert charge.status == :initialized
554+
assert charge.user_id == ctx[:org].id
555+
556+
debit1 = Repo.one!(from t in Transaction, where: t.type == :debit and t.claim_id == ^claim1.id)
557+
assert Money.equal?(debit1.net_amount, Money.new(:USD, 50))
558+
assert debit1.status == :initialized
559+
assert debit1.user_id == ctx[:org].id
560+
assert debit1.bounty_id == bounty.id
561+
562+
debit2 = Repo.one!(from t in Transaction, where: t.type == :debit and t.claim_id == ^claim2.id)
563+
assert Money.equal?(debit2.net_amount, Money.new(:USD, 50))
564+
assert debit2.status == :initialized
565+
assert debit2.user_id == ctx[:org].id
566+
assert debit2.bounty_id == bounty.id
567+
568+
credit1 = Repo.one!(from t in Transaction, where: t.type == :credit and t.claim_id == ^claim1.id)
569+
assert Money.equal?(credit1.net_amount, Money.new(:USD, 50))
570+
assert credit1.status == :initialized
571+
assert credit1.user_id == user1.id
572+
assert credit1.bounty_id == bounty.id
573+
574+
credit2 = Repo.one!(from t in Transaction, where: t.type == :credit and t.claim_id == ^claim2.id)
575+
assert Money.equal?(credit2.net_amount, Money.new(:USD, 50))
576+
assert credit2.status == :initialized
577+
assert credit2.user_id == user2.id
578+
assert credit2.bounty_id == bounty.id
579+
580+
transfer = Repo.one(from t in Transaction, where: t.type == :transfer)
581+
assert is_nil(transfer)
582+
end
455583
end
456584

457585
defp mock_body(body \\ ""), do: "Lorem\r\nipsum\r\n dolor #{body} sit\r\namet"

0 commit comments

Comments
 (0)