test_registration.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "registration"
  5
  6class RegistrationTest < Minitest::Test
  7	def test_for_activated
  8		skip "Registration#for activated not implemented yet"
  9		iq = Blather::Stanza::Iq::Command.new
 10		Registration.for(iq, Customer.new("test"), Minitest::Mock.new).sync
 11	end
 12	em :test_for_activated
 13
 14	def test_for_not_activated_with_customer_id
 15		BACKEND_SGX.expect(
 16			:registered?,
 17			EMPromise.resolve(nil),
 18			["test"]
 19		)
 20		web_manager = WebRegisterManager.new
 21		web_manager["test@example.com"] = "+15555550000"
 22		iq = Blather::Stanza::Iq::Command.new
 23		iq.from = "test@example.com"
 24		result = Registration.for(
 25			iq,
 26			Customer.new("test"),
 27			web_manager
 28		).sync
 29		assert_kind_of Registration::Activation, result
 30	end
 31	em :test_for_not_activated_with_customer_id
 32
 33	def test_for_not_activated_without_customer_id
 34		skip "customer_id creation not implemented yet"
 35		iq = Blather::Stanza::Iq::Command.new
 36		Registration.for(iq, nil, Minitest::Mock.new).sync
 37	end
 38	em :test_for_not_activated_without_customer_id
 39
 40	class ActivationTest < Minitest::Test
 41		Registration::Activation::COMMAND_MANAGER = Minitest::Mock.new
 42		def setup
 43			iq = Blather::Stanza::Iq::Command.new
 44			@activation = Registration::Activation.new(iq, "test", "+15555550000")
 45		end
 46
 47		def test_write
 48			result = Minitest::Mock.new
 49			result.expect(:then, result)
 50			result.expect(:then, EMPromise.resolve(:test_result))
 51			Registration::Activation::COMMAND_MANAGER.expect(
 52				:write,
 53				result,
 54				[Blather::Stanza::Iq::Command]
 55			)
 56			assert_equal :test_result, @activation.write.sync
 57		end
 58		em :test_write
 59	end
 60
 61	class PaymentTest < Minitest::Test
 62		Customer::BRAINTREE = Minitest::Mock.new
 63		Registration::Payment::Bitcoin::ELECTRUM = Minitest::Mock.new
 64
 65		def test_for_bitcoin
 66			Registration::Payment::Bitcoin::ELECTRUM.expect(:createnewaddress, "addr")
 67			iq = Blather::Stanza::Iq::Command.new
 68			iq.form.fields = [
 69				{ var: "activation_method", value: "bitcoin" },
 70				{ var: "plan_name", value: "test_usd" }
 71			]
 72			result = Registration::Payment.for(
 73				iq,
 74				Customer.new("test"),
 75				"+15555550000"
 76			)
 77			assert_kind_of Registration::Payment::Bitcoin, result
 78		end
 79
 80		def test_for_credit_card
 81			braintree_customer = Minitest::Mock.new
 82			Customer::BRAINTREE.expect(
 83				:customer,
 84				braintree_customer
 85			)
 86			braintree_customer.expect(
 87				:find,
 88				EMPromise.resolve(OpenStruct.new(payment_methods: [])),
 89				["test"]
 90			)
 91			iq = Blather::Stanza::Iq::Command.new
 92			iq.from = "test@example.com"
 93			iq.form.fields = [
 94				{ var: "activation_method", value: "credit_card" },
 95				{ var: "plan_name", value: "test_usd" }
 96			]
 97			result = Registration::Payment.for(
 98				iq,
 99				Customer.new("test"),
100				"+15555550000"
101			).sync
102			assert_kind_of Registration::Payment::CreditCard, result
103		end
104		em :test_for_credit_card
105
106		def test_for_code
107			skip "Code not implemented yet"
108			iq = Blather::Stanza::Iq::Command.new
109			iq.form.fields = [
110				{ var: "activation_method", value: "code" },
111				{ var: "plan_name", value: "test_usd" }
112			]
113			result = Registration::Payment.for(iq, "test", "+15555550000")
114			assert_kind_of Registration::Payment::Code, result
115		end
116
117		class BitcoinTest < Minitest::Test
118			Registration::Payment::Bitcoin::ELECTRUM = Minitest::Mock.new
119			Registration::Payment::Bitcoin::REDIS = Minitest::Mock.new
120			Registration::Payment::Bitcoin::BTC_SELL_PRICES = Minitest::Mock.new
121			Registration::Payment::Bitcoin::BLATHER = Minitest::Mock.new
122
123			def setup
124				Registration::Payment::Bitcoin::ELECTRUM.expect(
125					:createnewaddress,
126					EMPromise.resolve("testaddr")
127				)
128				iq = Blather::Stanza::Iq::Command.new
129				@bitcoin = Registration::Payment::Bitcoin.new(
130					iq,
131					Customer.new("test", plan_name: "test_usd"),
132					"+15555550000"
133				)
134			end
135
136			def test_write
137				reply_text = <<~NOTE
138					Activate your account by sending at least 1.000000 BTC to
139					testaddr
140
141					You will receive a notification when your payment is complete.
142				NOTE
143				Registration::Payment::Bitcoin::BLATHER.expect(
144					:<<,
145					nil,
146					[Matching.new do |reply|
147						assert_equal :completed, reply.status
148						assert_equal :info, reply.note_type
149						assert_equal reply_text, reply.note.content
150						true
151					end]
152				)
153				Registration::Payment::Bitcoin::BTC_SELL_PRICES.expect(
154					:usd,
155					EMPromise.resolve(BigDecimal.new(1))
156				)
157				@bitcoin.stub(:save, EMPromise.resolve(nil)) do
158					@bitcoin.write.sync
159				end
160				Registration::Payment::Bitcoin::BLATHER.verify
161			end
162			em :test_write
163		end
164
165		class CreditCardTest < Minitest::Test
166			def setup
167				@iq = Blather::Stanza::Iq::Command.new
168				@iq.from = "test@example.com"
169				@credit_card = Registration::Payment::CreditCard.new(
170					@iq,
171					Customer.new("test"),
172					"+15555550000"
173				)
174			end
175
176			def test_for
177				customer = Minitest::Mock.new(Customer.new("test"))
178				customer.expect(
179					:payment_methods,
180					EMPromise.resolve(OpenStruct.new(default_payment_method: :test))
181				)
182				assert_kind_of(
183					Registration::Payment::CreditCard::Activate,
184					Registration::Payment::CreditCard.for(
185						@iq,
186						customer,
187						"+15555550000"
188					).sync
189				)
190			end
191			em :test_for
192
193			def test_reply
194				assert_equal [:execute, :next], @credit_card.reply.allowed_actions
195				assert_equal(
196					"Add credit card, then return here and choose next: " \
197					"http://creditcard.example.com",
198					@credit_card.reply.note.content
199				)
200			end
201		end
202
203		class ActivateTest < Minitest::Test
204			Registration::Payment::CreditCard::Activate::Finish =
205				Minitest::Mock.new
206			Registration::Payment::CreditCard::Activate::Transaction =
207				Minitest::Mock.new
208
209			def test_write
210				transaction = PromiseMock.new
211				transaction.expect(
212					:insert,
213					EMPromise.resolve(nil)
214				)
215				Registration::Payment::CreditCard::Activate::Transaction.expect(
216					:sale,
217					transaction,
218					[
219						"merchant_usd",
220						:test_default_method,
221						CONFIG[:activation_amount]
222					]
223				)
224				iq = Blather::Stanza::Iq::Command.new
225				customer = Minitest::Mock.new(
226					Customer.new("test", plan_name: "test_usd")
227				)
228				customer.expect(:bill_plan, nil)
229				Registration::Payment::CreditCard::Activate::Finish.expect(
230					:new,
231					OpenStruct.new(write: nil),
232					[Blather::Stanza::Iq, customer, "+15555550000"]
233				)
234				Registration::Payment::CreditCard::Activate.new(
235					iq,
236					customer,
237					:test_default_method,
238					"+15555550000"
239				).write.sync
240				Registration::Payment::CreditCard::Activate::Transaction.verify
241				transaction.verify
242				customer.verify
243			end
244			em :test_write
245		end
246	end
247
248	class FinishTest < Minitest::Test
249		Registration::Finish::BLATHER = Minitest::Mock.new
250
251		def setup
252			@finish = Registration::Finish.new(
253				Blather::Stanza::Iq::Command.new,
254				Customer.new("test"),
255				"+15555550000"
256			)
257		end
258
259		def test_write
260			create_order = stub_request(
261				:post,
262				"https://dashboard.bandwidth.com/v1.0/accounts//orders"
263			).to_return(status: 201, body: <<~RESPONSE)
264				<OrderResponse>
265					<Order>
266						<id>test_order</id>
267					</Order>
268				</OrderResponse>
269			RESPONSE
270			stub_request(
271				:get,
272				"https://dashboard.bandwidth.com/v1.0/accounts//orders/test_order"
273			).to_return(status: 201, body: <<~RESPONSE)
274				<OrderResponse>
275					<OrderStatus>COMPLETE</OrderStatus>
276					<CompletedNumbers>
277						<TelephoneNumber>
278							<FullNumber>5555550000</FullNumber>
279						</TelephoneNumber>
280					</CompletedNumbers>
281				</OrderResponse>
282			RESPONSE
283			stub_request(
284				:post,
285				"https://api.catapult.inetwork.com/v1/users/catapult_user/phoneNumbers"
286			).with(
287				body: open(__dir__ + "/data/catapult_import_body.json").read.chomp,
288				headers: {
289					"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0",
290					"Content-Type" => "application/json"
291				}
292			).to_return(status: 200)
293			BACKEND_SGX.expect(
294				:register!,
295				EMPromise.resolve(OpenStruct.new(error?: false)),
296				["test", "+15555550000"]
297			)
298			Registration::Finish::BLATHER.expect(
299				:<<,
300				nil,
301				[Matching.new do |reply|
302					assert_equal :completed, reply.status
303					assert_equal :info, reply.note_type
304					assert_equal(
305						"Your JMP account has been activated as +15555550000",
306						reply.note.content
307					)
308				end]
309			)
310			@finish.write.sync
311			assert_requested create_order
312			BACKEND_SGX.verify
313			Registration::Finish::BLATHER.verify
314		end
315		em :test_write
316
317		def test_write_tn_fail
318			create_order = stub_request(
319				:post,
320				"https://dashboard.bandwidth.com/v1.0/accounts//orders"
321			).to_return(status: 201, body: <<~RESPONSE)
322				<OrderResponse>
323					<Order>
324						<id>test_order</id>
325					</Order>
326				</OrderResponse>
327			RESPONSE
328			stub_request(
329				:get,
330				"https://dashboard.bandwidth.com/v1.0/accounts//orders/test_order"
331			).to_return(status: 201, body: <<~RESPONSE)
332				<OrderResponse>
333					<OrderStatus>FAILED</OrderStatus>
334				</OrderResponse>
335			RESPONSE
336			Registration::Finish::BLATHER.expect(
337				:<<,
338				nil,
339				[Matching.new do |reply|
340					assert_equal :completed, reply.status
341					assert_equal :error, reply.note_type
342					assert_equal(
343						"The JMP number +15555550000 is no longer available, " \
344						"please visit https://jmp.chat and choose another.",
345						reply.note.content
346					)
347				end]
348			)
349			@finish.write.sync
350			assert_requested create_order
351			Registration::Finish::BLATHER.verify
352		end
353		em :test_write_tn_fail
354	end
355end