@@ -14,6 +14,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
1414 alias Algora.Github.Webhook
1515 alias Algora.Payments.Transaction
1616 alias Algora.Repo
17+ alias Algora.Workspace.Ticket
1718 alias AlgoraWeb.Webhooks.GithubController
1819
1920 setup do
@@ -220,7 +221,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
220221
221222 test "does not allow multiple claims in a single PR" , ctx do
222223 issue_number1 = :rand . uniform ( 1000 )
223- issue_number2 = :rand . uniform ( 1000 )
224+ issue_number2 = issue_number1 + 1
224225 pr_number = :rand . uniform ( 1000 )
225226
226227 process_scenario! ( ctx , [
@@ -252,7 +253,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
252253
253254 test "cancels existing claim when attempting to claim a different bounty in the same PR" , ctx do
254255 issue_number1 = :rand . uniform ( 1000 )
255- issue_number2 = :rand . uniform ( 1000 )
256+ issue_number2 = issue_number1 + 1
256257 pr_number = :rand . uniform ( 1000 )
257258
258259 process_scenario! ( ctx , [
@@ -510,6 +511,71 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
510511 assert is_nil ( transfer )
511512 end
512513
514+ test "handles autopay when claim is changed to a different bounty and PR is merged" , ctx do
515+ issue_number1 = :rand . uniform ( 1000 )
516+ issue_number2 = issue_number1 + :rand . uniform ( 1000 )
517+ pr_number = issue_number1 + :rand . uniform ( 1000 )
518+
519+ customer = insert! ( :customer , user: ctx [ :org ] )
520+ _payment_method = insert! ( :payment_method , is_default: true , customer: customer )
521+
522+ process_scenario! ( ctx , [
523+ % {
524+ event_action: "issue_comment.created" ,
525+ user_type: :admin ,
526+ body: "/bounty $100" ,
527+ params: % { "issue" => % { "number" => issue_number1 } }
528+ } ,
529+ % {
530+ event_action: "pull_request.opened" ,
531+ user_type: :unauthorized ,
532+ body: "/claim #{ issue_number1 } " ,
533+ params: % { "pull_request" => % { "number" => pr_number } }
534+ } ,
535+ % {
536+ event_action: "pull_request.edited" ,
537+ user_type: :unauthorized ,
538+ body: "/claim #{ issue_number2 } " ,
539+ params: % { "pull_request" => % { "number" => pr_number } }
540+ } ,
541+ % {
542+ event_action: "pull_request.closed" ,
543+ user_type: :unauthorized ,
544+ body: "/claim #{ issue_number2 } " ,
545+ params: % { "pull_request" => % { "number" => pr_number , "merged_at" => DateTime . to_iso8601 ( DateTime . utc_now ( ) ) } }
546+ }
547+ ] )
548+
549+ bounty = Repo . one! ( Bounty )
550+
551+ ticket1 = Repo . get_by! ( Ticket , number: issue_number1 )
552+ ticket2 = Repo . get_by! ( Ticket , number: issue_number2 )
553+
554+ active_claim = Repo . get_by! ( Claim , target_id: ticket1 . id )
555+ cancelled_claim = Repo . get_by! ( Claim , target_id: ticket2 . id )
556+ assert active_claim . status == :approved
557+ assert cancelled_claim . status == :cancelled
558+
559+ charge = Repo . one! ( from t in Transaction , where: t . type == :charge )
560+ assert Money . equal? ( charge . net_amount , Money . new ( :USD , 100 ) )
561+ assert charge . status == :initialized
562+ assert charge . user_id == ctx [ :org ] . id
563+
564+ debit = Repo . one! ( from t in Transaction , where: t . type == :debit )
565+ assert Money . equal? ( debit . net_amount , Money . new ( :USD , 100 ) )
566+ assert debit . status == :initialized
567+ assert debit . user_id == ctx [ :org ] . id
568+ assert debit . bounty_id == bounty . id
569+ assert debit . claim_id == active_claim . id
570+
571+ credit = Repo . one! ( from t in Transaction , where: t . type == :credit )
572+ assert Money . equal? ( credit . net_amount , Money . new ( :USD , 100 ) )
573+ assert credit . status == :initialized
574+ assert credit . user_id == ctx [ :unauthorized_user ] . id
575+ assert credit . bounty_id == bounty . id
576+ assert credit . claim_id == active_claim . id
577+ end
578+
513579 test "prevents duplicate transaction creation when receiving multiple PR closed events" , ctx do
514580 issue_number = :rand . uniform ( 1000 )
515581 pr_number = :rand . uniform ( 1000 )
0 commit comments