@@ -12,6 +12,7 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
12
12
alias Algora.Bounties.Claim
13
13
alias Algora.Bounties.Jobs.NotifyBounty
14
14
alias Algora.Github.Webhook
15
+ alias Algora.Payments.Transaction
15
16
alias Algora.Repo
16
17
alias AlgoraWeb.Webhooks.GithubController
17
18
@@ -452,6 +453,133 @@ defmodule AlgoraWeb.Webhooks.GithubControllerTest do
452
453
453
454
assert Repo . one ( Claim ) . status == :approved
454
455
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
455
583
end
456
584
457
585
defp mock_body ( body \\ "" ) , do: "Lorem\r \n ipsum\r \n dolor #{ body } sit\r \n amet"
0 commit comments