1# frozen_string_literal: true
2
3require "test_helper"
4require "registration"
5
6class RegistrationTest < Minitest::Test
7 Customer::IQ_MANAGER = Minitest::Mock.new
8
9 def test_for_activated
10 skip "Registration#for activated not implemented yet"
11 iq = Blather::Stanza::Iq::Command.new
12 Registration.for(iq, Customer.new("test"), Minitest::Mock.new).sync
13 end
14 em :test_for_activated
15
16 def test_for_not_activated_with_customer_id
17 Customer::IQ_MANAGER.expect(
18 :write,
19 EMPromise.resolve(nil),
20 [Blather::Stanza::Iq]
21 )
22 web_manager = WebRegisterManager.new
23 web_manager["test@example.com"] = "+15555550000"
24 iq = Blather::Stanza::Iq::Command.new
25 iq.from = "test@example.com"
26 result = Registration.for(
27 iq,
28 Customer.new("test"),
29 web_manager
30 ).sync
31 assert_kind_of Registration::Activation, result
32 end
33 em :test_for_not_activated_with_customer_id
34
35 def test_for_not_activated_without_customer_id
36 skip "customer_id creation not implemented yet"
37 iq = Blather::Stanza::Iq::Command.new
38 Registration.for(iq, nil, Minitest::Mock.new).sync
39 end
40 em :test_for_not_activated_without_customer_id
41
42 class ActivationTest < Minitest::Test
43 Registration::Activation::COMMAND_MANAGER = Minitest::Mock.new
44 def setup
45 iq = Blather::Stanza::Iq::Command.new
46 @activation = Registration::Activation.new(iq, "test", "+15555550000")
47 end
48
49 def test_write
50 result = Minitest::Mock.new
51 result.expect(:then, result)
52 result.expect(:then, EMPromise.resolve(:test_result))
53 Registration::Activation::COMMAND_MANAGER.expect(
54 :write,
55 result,
56 [Blather::Stanza::Iq::Command]
57 )
58 assert_equal :test_result, @activation.write.sync
59 end
60 em :test_write
61 end
62
63 class PaymentTest < Minitest::Test
64 Registration::Payment::Bitcoin::ELECTRUM = Minitest::Mock.new
65
66 def test_for_bitcoin
67 Registration::Payment::Bitcoin::ELECTRUM.expect(:createnewaddress, "addr")
68 iq = Blather::Stanza::Iq::Command.new
69 iq.form.fields = [
70 { var: "activation_method", value: "bitcoin" },
71 { var: "plan_name", value: "test_usd" }
72 ]
73 result = Registration::Payment.for(
74 iq,
75 Customer.new("test"),
76 "+15555550000"
77 )
78 assert_kind_of Registration::Payment::Bitcoin, result
79 end
80
81 def test_for_credit_card
82 skip "CreditCard not implemented yet"
83 iq = Blather::Stanza::Iq::Command.new
84 iq.form.fields = [
85 { var: "activation_method", value: "credit_card" },
86 { var: "plan_name", value: "test_usd" }
87 ]
88 result = Registration::Payment.for(iq, "test", "+15555550000")
89 assert_kind_of Registration::Payment::CreditCard, result
90 end
91
92 def test_for_code
93 skip "Code not implemented yet"
94 iq = Blather::Stanza::Iq::Command.new
95 iq.form.fields = [
96 { var: "activation_method", value: "code" },
97 { var: "plan_name", value: "test_usd" }
98 ]
99 result = Registration::Payment.for(iq, "test", "+15555550000")
100 assert_kind_of Registration::Payment::Code, result
101 end
102
103 class BitcoinTest < Minitest::Test
104 Registration::Payment::Bitcoin::ELECTRUM = Minitest::Mock.new
105 Registration::Payment::Bitcoin::REDIS = Minitest::Mock.new
106 Registration::Payment::Bitcoin::BTC_SELL_PRICES = Minitest::Mock.new
107 Registration::Payment::Bitcoin::BLATHER = Minitest::Mock.new
108
109 def setup
110 Registration::Payment::Bitcoin::ELECTRUM.expect(
111 :createnewaddress,
112 EMPromise.resolve("testaddr")
113 )
114 iq = Blather::Stanza::Iq::Command.new
115 iq.form.fields = [
116 { var: "plan_name", value: "test_usd" }
117 ]
118 @bitcoin = Registration::Payment::Bitcoin.new(
119 iq,
120 Customer.new("test"),
121 "+15555550000"
122 )
123 end
124
125 def test_write
126 reply_text = <<~NOTE
127 Activate your account by sending at least 1.000000 BTC to
128 testaddr
129
130 You will receive a notification when your payment is complete.
131 NOTE
132 Registration::Payment::Bitcoin::BLATHER.expect(
133 :<<,
134 nil,
135 [Matching.new do |reply|
136 assert_equal :completed, reply.status
137 assert_equal :info, reply.note_type
138 assert_equal reply_text, reply.note.content
139 true
140 end]
141 )
142 Registration::Payment::Bitcoin::BTC_SELL_PRICES.expect(
143 :usd,
144 EMPromise.resolve(BigDecimal.new(1))
145 )
146 @bitcoin.stub(:save, EMPromise.resolve(nil)) do
147 @bitcoin.write.sync
148 end
149 Registration::Payment::Bitcoin::BLATHER.verify
150 end
151 em :test_write
152 end
153 end
154end