~singpolyma/sgx-jmp

ref: 43ac00160445859bfed198c781ecb1f0ee55ce4d sgx-jmp/test/test_registration.rb -rw-r--r-- 17.0 KiB
43ac0016Stephen Paul Weber Move more persistence into the repo layer 1 year, 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
# frozen_string_literal: true

require "test_helper"
require "customer"
require "registration"

def execute_command(
	iq=Blather::Stanza::Iq::Command.new.tap { |i| i.from = "test@example.com" },
	blather: BLATHER,
	&blk
)
	Command::Execution.new(
		Minitest::Mock.new,
		blather,
		:to_s.to_proc,
		iq
	).execute(&blk).sync
end

class RegistrationTest < Minitest::Test
	def test_for_registered
		sgx = OpenStruct.new(
			registered?: OpenStruct.new(phone: "+15555550000")
		)
		iq = Blather::Stanza::Iq::Command.new
		iq.from = "test@example.com"
		result = execute_command(iq) do
			Registration.for(
				customer(sgx: sgx),
				Minitest::Mock.new
			)
		end
		assert_kind_of Registration::Registered, result
	end
	em :test_for_registered

	def test_for_activated
		web_manager = TelSelections.new(redis: FakeRedis.new)
		web_manager.set("test@example.net", "+15555550000")
		result = execute_command do
			sgx = OpenStruct.new(registered?: false)
			Registration.for(
				customer(
					plan_name: "test_usd",
					expires_at: Time.now + 999,
					sgx: sgx
				),
				web_manager
			)
		end
		assert_kind_of Registration::Finish, result
	end
	em :test_for_activated

	def test_for_not_activated_with_customer_id
		sgx = OpenStruct.new(registered?: false)
		web_manager = TelSelections.new(redis: FakeRedis.new)
		web_manager.set("test@example.net", "+15555550000")
		iq = Blather::Stanza::Iq::Command.new
		iq.from = "test@example.com"
		result = execute_command(iq) do
			Registration.for(
				customer(sgx: sgx),
				web_manager
			)
		end
		assert_kind_of Registration::Activation, result
	end
	em :test_for_not_activated_with_customer_id

	class ActivationTest < Minitest::Test
		Command::COMMAND_MANAGER = Minitest::Mock.new
		def setup
			@activation = Registration::Activation.new("test", "+15555550000")
		end

		def test_write
			stub_request(
				:get,
				"https://dashboard.bandwidth.com/v1.0/tns/+15555550000"
			).to_return(status: 201, body: <<~RESPONSE)
				<TelephoneNumberResponse>
					<TelephoneNumber>5555550000</TelephoneNumber>
				</TelephoneNumberResponse>
			RESPONSE
			stub_request(
				:get,
				"https://dashboard.bandwidth.com/v1.0/tns/5555550000/ratecenter"
			).to_return(status: 201, body: <<~RESPONSE)
				<TelephoneNumberResponse>
					<TelephoneNumberDetails>
						<State>KE</State>
						<RateCenter>FA</RateCenter>
					</TelephoneNumberDetails>
				</TelephoneNumberResponse>
			RESPONSE
			Command::COMMAND_MANAGER.expect(
				:write,
				EMPromise.reject(:test_result),
				[Matching.new do |iq|
					assert_equal :form, iq.form.type
					assert_equal(
						"You've selected +15555550000 (FA, KE) as your JMP number",
						iq.form.instructions
					)
				end]
			)
			assert_equal(
				:test_result,
				execute_command { @activation.write.catch { |e| e } }
			)
			assert_mock Command::COMMAND_MANAGER
		end
		em :test_write
	end

	class PaymentTest < Minitest::Test
		Customer::BRAINTREE = Minitest::Mock.new

		def test_for_bitcoin
			cust = Minitest::Mock.new(customer)
			cust.expect(
				:add_btc_address,
				EMPromise.resolve("testaddr")
			)
			iq = Blather::Stanza::Iq::Command.new
			iq.form.fields = [
				{ var: "activation_method", value: "bitcoin" },
				{ var: "plan_name", value: "test_usd" }
			]
			result = Registration::Payment.for(iq, cust, "+15555550000")
			assert_kind_of Registration::Payment::Bitcoin, result
		end

		def test_for_credit_card
			braintree_customer = Minitest::Mock.new
			Customer::BRAINTREE.expect(
				:customer,
				braintree_customer
			)
			braintree_customer.expect(
				:find,
				EMPromise.resolve(OpenStruct.new(payment_methods: [])),
				["test"]
			)
			iq = Blather::Stanza::Iq::Command.new
			iq.from = "test@example.com"
			iq.form.fields = [
				{ var: "activation_method", value: "credit_card" },
				{ var: "plan_name", value: "test_usd" }
			]
			result = Registration::Payment.for(
				iq,
				customer,
				"+15555550000"
			).sync
			assert_kind_of Registration::Payment::CreditCard, result
		end
		em :test_for_credit_card

		def test_for_code
			iq = Blather::Stanza::Iq::Command.new
			iq.form.fields = [
				{ var: "activation_method", value: "code" },
				{ var: "plan_name", value: "test_usd" }
			]
			result = Registration::Payment.for(
				iq,
				customer,
				"+15555550000"
			)
			assert_kind_of Registration::Payment::InviteCode, result
		end

		class BitcoinTest < Minitest::Test
			Registration::Payment::Bitcoin::BTC_SELL_PRICES = Minitest::Mock.new
			Customer::REDIS = Minitest::Mock.new

			def setup
				@customer = Minitest::Mock.new(
					customer(plan_name: "test_usd")
				)
				@customer.expect(
					:add_btc_address,
					EMPromise.resolve("testaddr")
				)
				@bitcoin = Registration::Payment::Bitcoin.new(
					@customer,
					"+15555550000"
				)
			end

			def test_write
				Customer::REDIS.expect(
					:smembers,
					EMPromise.resolve([]),
					["jmp_customer_btc_addresses-test"]
				)
				reply_text = <<~NOTE
					Activate your account by sending at least 1.000000 BTC to
					testaddr

					You will receive a notification when your payment is complete.
				NOTE
				blather = Minitest::Mock.new
				blather.expect(
					:<<,
					nil,
					[Matching.new do |reply|
						assert_equal :canceled, reply.status
						assert_equal :info, reply.note_type
						assert_equal reply_text, reply.note.content
						true
					end]
				)
				Registration::Payment::Bitcoin::BTC_SELL_PRICES.expect(
					:usd,
					EMPromise.resolve(BigDecimal(1))
				)
				@bitcoin.stub(:save, EMPromise.resolve(nil)) do
					execute_command(blather: blather) do
						@bitcoin.write
					end
				end
				assert_mock blather
			end
			em :test_write
		end

		class CreditCardTest < Minitest::Test
			def setup
				@credit_card = Registration::Payment::CreditCard.new(
					customer,
					"+15555550000"
				)
			end

			def test_for
				customer = Minitest::Mock.new(customer)
				customer.expect(
					:payment_methods,
					EMPromise.resolve(OpenStruct.new(default_payment_method: :test))
				)
				assert_kind_of(
					Registration::Payment::CreditCard::Activate,
					Registration::Payment::CreditCard.for(
						customer,
						"+15555550000"
					).sync
				)
			end
			em :test_for

			def test_write
				result = execute_command do
					Command::COMMAND_MANAGER.expect(
						:write,
						EMPromise.reject(:test_result),
						[Matching.new do |reply|
							assert_equal [:execute, :next], reply.allowed_actions
							assert_equal(
								"Add credit card, then return here to continue: " \
								"http://creditcard.example.com",
								reply.note.content
							)
						end]
					)

					@credit_card.write.catch { |e| e }
				end

				assert_equal :test_result, result
			end
			em :test_write
		end

		class ActivateTest < Minitest::Test
			Registration::Payment::CreditCard::Activate::Finish =
				Minitest::Mock.new
			Registration::Payment::CreditCard::Activate::Transaction =
				Minitest::Mock.new
			Command::COMMAND_MANAGER = Minitest::Mock.new

			def test_write
				transaction = PromiseMock.new
				transaction.expect(
					:insert,
					EMPromise.resolve(nil)
				)
				customer = Minitest::Mock.new(
					customer(plan_name: "test_usd")
				)
				Registration::Payment::CreditCard::Activate::Transaction.expect(
					:sale,
					transaction
				) do |acustomer, amount:, payment_method:|
					assert_operator customer, :===, acustomer
					assert_equal CONFIG[:activation_amount], amount
					assert_equal :test_default_method, payment_method
				end
				customer.expect(:bill_plan, nil)
				Registration::Payment::CreditCard::Activate::Finish.expect(
					:new,
					OpenStruct.new(write: nil),
					[customer, "+15555550000"]
				)
				execute_command do
					Registration::Payment::CreditCard::Activate.new(
						customer,
						:test_default_method,
						"+15555550000"
					).write
				end
				Registration::Payment::CreditCard::Activate::Transaction.verify
				transaction.verify
				customer.verify
				Registration::Payment::CreditCard::Activate::Finish.verify
			end
			em :test_write

			def test_write_declines
				customer = Minitest::Mock.new(
					customer(plan_name: "test_usd")
				)
				iq = Blather::Stanza::Iq::Command.new
				iq.from = "test@example.com"
				Command::COMMAND_MANAGER.expect(
					:write,
					EMPromise.reject(:test_result),
					[Matching.new do |reply|
						assert_equal :error, reply.note_type
						assert_equal(
							Registration::Payment::CreditCard::Activate::DECLINE_MESSAGE +
							": http://creditcard.example.com",
							reply.note.content
						)
					end]
				)
				result = execute_command do
					Registration::Payment::CreditCard::Activate::Transaction.expect(
						:sale,
						EMPromise.reject("declined")
					) do |acustomer, amount:, payment_method:|
						assert_operator customer, :===, acustomer
						assert_equal CONFIG[:activation_amount], amount
						assert_equal :test_default_method, payment_method
					end

					Registration::Payment::CreditCard::Activate.new(
						customer,
						:test_default_method,
						"+15555550000"
					).write.catch { |e| e }
				end
				assert_equal :test_result, result
				Registration::Payment::CreditCard::Activate::Transaction.verify
			end
			em :test_write_declines
		end

		class InviteCodeTest < Minitest::Test
			Registration::Payment::InviteCode::DB =
				Minitest::Mock.new
			Registration::Payment::InviteCode::REDIS =
				Minitest::Mock.new
			Command::COMMAND_MANAGER = Minitest::Mock.new
			Registration::Payment::InviteCode::Finish =
				Minitest::Mock.new
			def test_write
				customer = customer(plan_name: "test_usd")
				Registration::Payment::InviteCode::DB.expect(:transaction, true, [])
				Registration::Payment::InviteCode::Finish.expect(
					:new,
					OpenStruct.new(write: nil),
					[
						customer,
						"+15555550000"
					]
				)
				execute_command do
					Registration::Payment::InviteCode::REDIS.expect(
						:get,
						EMPromise.resolve(nil),
						["jmp_invite_tries-test"]
					)
					Command::COMMAND_MANAGER.expect(
						:write,
						EMPromise.resolve(
							Blather::Stanza::Iq::Command.new.tap { |iq|
								iq.form.fields = [{ var: "code", value: "abc" }]
							}
						),
						[Matching.new do |reply|
							assert_equal :form, reply.form.type
							assert_nil reply.form.instructions
						end]
					)

					Registration::Payment::InviteCode.new(
						customer,
						"+15555550000"
					).write
				end
				assert_mock Command::COMMAND_MANAGER
				assert_mock Registration::Payment::InviteCode::DB
				assert_mock Registration::Payment::InviteCode::REDIS
				assert_mock Registration::Payment::InviteCode::Finish
			end
			em :test_write

			def test_write_bad_code
				result = execute_command do
					customer = customer(plan_name: "test_usd")
					Registration::Payment::InviteCode::REDIS.expect(
						:get,
						EMPromise.resolve(0),
						["jmp_invite_tries-test"]
					)
					Registration::Payment::InviteCode::DB.expect(:transaction, []) do
						raise Registration::Payment::InviteCode::Invalid, "wut"
					end
					Registration::Payment::InviteCode::REDIS.expect(
						:incr,
						EMPromise.resolve(nil),
						["jmp_invite_tries-test"]
					)
					Registration::Payment::InviteCode::REDIS.expect(
						:expire,
						EMPromise.resolve(nil),
						["jmp_invite_tries-test", 60 * 60]
					)
					Command::COMMAND_MANAGER.expect(
						:write,
						EMPromise.resolve(
							Blather::Stanza::Iq::Command.new.tap { |iq|
								iq.form.fields = [{ var: "code", value: "abc" }]
							}
						),
						[Matching.new do |reply|
							assert_equal :form, reply.form.type
							assert_nil reply.form.instructions
						end]
					)
					Command::COMMAND_MANAGER.expect(
						:write,
						EMPromise.reject(:test_result),
						[Matching.new do |reply|
							assert_equal :form, reply.form.type
							assert_equal "wut", reply.form.instructions
						end]
					)

					Registration::Payment::InviteCode.new(
						customer,
						"+15555550000"
					).write.catch { |e| e }
				end
				assert_equal :test_result, result
				assert_mock Command::COMMAND_MANAGER
				assert_mock Registration::Payment::InviteCode::DB
				assert_mock Registration::Payment::InviteCode::REDIS
			end
			em :test_write_bad_code

			def test_write_bad_code_over_limit
				result = execute_command do
					customer = customer(plan_name: "test_usd")
					Registration::Payment::InviteCode::REDIS.expect(
						:get,
						EMPromise.resolve(11),
						["jmp_invite_tries-test"]
					)
					Command::COMMAND_MANAGER.expect(
						:write,
						EMPromise.resolve(
							Blather::Stanza::Iq::Command.new.tap { |iq|
								iq.form.fields = [{ var: "code", value: "abc" }]
							}
						),
						[Matching.new do |reply|
							assert_equal :form, reply.form.type
							assert_nil reply.form.instructions
						end]
					)
					Registration::Payment::InviteCode::REDIS.expect(
						:incr,
						EMPromise.resolve(nil),
						["jmp_invite_tries-test"]
					)
					Registration::Payment::InviteCode::REDIS.expect(
						:expire,
						EMPromise.resolve(nil),
						["jmp_invite_tries-test", 60 * 60]
					)
					Command::COMMAND_MANAGER.expect(
						:write,
						EMPromise.reject(:test_result),
						[Matching.new do |reply|
							assert_equal :form, reply.form.type
							assert_equal "Too many wrong attempts", reply.form.instructions
						end]
					)
					Registration::Payment::InviteCode.new(
						customer,
						"+15555550000"
					).write.catch { |e| e }
				end
				assert_equal :test_result, result
				assert_mock Command::COMMAND_MANAGER
				assert_mock Registration::Payment::InviteCode::REDIS
			end
			em :test_write_bad_code_over_limit
		end
	end

	class FinishTest < Minitest::Test
		Command::COMMAND_MANAGER = Minitest::Mock.new
		Registration::Finish::TEL_SELECTIONS = FakeTelSelections.new
		Registration::Finish::REDIS = Minitest::Mock.new
		Bwmsgsv2Repo::REDIS = Minitest::Mock.new

		def setup
			@sgx = Minitest::Mock.new(TrivialBackendSgxRepo.new.get("test"))
			iq = Blather::Stanza::Iq::Command.new
			iq.from = "test\\40example.com@cheogram.com"
			@finish = Registration::Finish.new(
				customer(sgx: @sgx),
				"+15555550000"
			)
		end

		def test_write
			create_order = stub_request(
				:post,
				"https://dashboard.bandwidth.com/v1.0/accounts//orders"
			).to_return(status: 201, body: <<~RESPONSE)
				<OrderResponse>
					<Order>
						<id>test_order</id>
					</Order>
				</OrderResponse>
			RESPONSE
			stub_request(
				:get,
				"https://dashboard.bandwidth.com/v1.0/accounts//orders/test_order"
			).to_return(status: 201, body: <<~RESPONSE)
				<OrderResponse>
					<OrderStatus>COMPLETE</OrderStatus>
					<CompletedNumbers>
						<TelephoneNumber>
							<FullNumber>5555550000</FullNumber>
						</TelephoneNumber>
					</CompletedNumbers>
				</OrderResponse>
			RESPONSE
			stub_request(
				:post,
				"https://api.catapult.inetwork.com/v1/users/catapult_user/phoneNumbers"
			).with(
				body: open(__dir__ + "/data/catapult_import_body.json").read.chomp,
				headers: {
					"Authorization" => "Basic Y2F0YXB1bHRfdG9rZW46Y2F0YXB1bHRfc2VjcmV0",
					"Content-Type" => "application/json"
				}
			).to_return(status: 201)
			Registration::Finish::REDIS.expect(
				:del,
				nil,
				["pending_tel_for-test@example.net"]
			)
			Bwmsgsv2Repo::REDIS.expect(
				:set,
				nil,
				[
					"catapult_fwd-+15555550000",
					"xmpp:test@example.net"
				]
			)
			Bwmsgsv2Repo::REDIS.expect(
				:set,
				nil,
				["catapult_fwd_timeout-customer_test@component", 25]
			)
			blather = Minitest::Mock.new
			blather.expect(
				:<<,
				nil,
				[Matching.new do |reply|
					assert_equal :completed, reply.status
					assert_equal :info, reply.note_type
					assert_equal(
						"Your JMP account has been activated as +15555550000",
						reply.note.content
					)
				end]
			)
			execute_command(blather: blather) do
				@sgx.expect(
					:register!,
					EMPromise.resolve(@sgx.with(registered?: IBR.new.tap do |ibr|
						ibr.phone = "+15555550000"
					end)),
					["+15555550000"]
				)

				@finish.write
			end
			assert_requested create_order
			assert_mock @sgx
			assert_mock Registration::Finish::REDIS
			assert_mock Bwmsgsv2Repo::REDIS
			assert_mock blather
		end
		em :test_write

		def test_write_tn_fail
			create_order = stub_request(
				:post,
				"https://dashboard.bandwidth.com/v1.0/accounts//orders"
			).to_return(status: 201, body: <<~RESPONSE)
				<OrderResponse>
					<Order>
						<id>test_order</id>
					</Order>
				</OrderResponse>
			RESPONSE
			stub_request(
				:get,
				"https://dashboard.bandwidth.com/v1.0/accounts//orders/test_order"
			).to_return(status: 201, body: <<~RESPONSE)
				<OrderResponse>
					<OrderStatus>FAILED</OrderStatus>
				</OrderResponse>
			RESPONSE

			result = execute_command do
				Command::COMMAND_MANAGER.expect(
					:write,
					EMPromise.reject(:test_result),
					[Matching.new do |iq|
						assert_equal :form, iq.form.type
						assert_equal(
							"The JMP number +15555550000 is no longer available.",
							iq.form.instructions
						)
					end]
				)

				@finish.write.catch { |e| e }
			end

			assert_equal :test_result, result
			assert_mock Command::COMMAND_MANAGER
			assert_instance_of(
				TelSelections::ChooseTel,
				Registration::Finish::TEL_SELECTIONS["test@example.com"]
			)
			assert_requested create_order
		end
		em :test_write_tn_fail
	end
end