~singpolyma/sgx-jmp

ref: c69a6218704ba1d09a24425d3ee43c47c2ba7b5d sgx-jmp/test/test_registration.rb -rw-r--r-- 9.9 KiB
c69a6218Stephen Paul Weber Merge branch 'new-signup-credit-card-decline' 1 year, 4 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
# frozen_string_literal: true

require "test_helper"
require "registration"

class RegistrationTest < Minitest::Test
	def test_for_activated
		skip "Registration#for activated not implemented yet"
		iq = Blather::Stanza::Iq::Command.new
		Registration.for(iq, Customer.new("test"), Minitest::Mock.new).sync
	end
	em :test_for_activated

	def test_for_not_activated_with_customer_id
		BACKEND_SGX.expect(
			:registered?,
			EMPromise.resolve(nil),
			["test"]
		)
		web_manager = WebRegisterManager.new
		web_manager["test@example.com"] = "+15555550000"
		iq = Blather::Stanza::Iq::Command.new
		iq.from = "test@example.com"
		result = Registration.for(
			iq,
			Customer.new("test"),
			web_manager
		).sync
		assert_kind_of Registration::Activation, result
	end
	em :test_for_not_activated_with_customer_id

	def test_for_not_activated_without_customer_id
		skip "customer_id creation not implemented yet"
		iq = Blather::Stanza::Iq::Command.new
		Registration.for(iq, nil, Minitest::Mock.new).sync
	end
	em :test_for_not_activated_without_customer_id

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

		def test_write
			result = Minitest::Mock.new
			result.expect(:then, result)
			result.expect(:then, EMPromise.resolve(:test_result))
			Registration::Activation::COMMAND_MANAGER.expect(
				:write,
				result,
				[Blather::Stanza::Iq::Command]
			)
			assert_equal :test_result, @activation.write.sync
		end
		em :test_write
	end

	class PaymentTest < Minitest::Test
		Customer::BRAINTREE = Minitest::Mock.new
		Registration::Payment::Bitcoin::ELECTRUM = Minitest::Mock.new

		def test_for_bitcoin
			Registration::Payment::Bitcoin::ELECTRUM.expect(:createnewaddress, "addr")
			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,
				Customer.new("test"),
				"+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.new("test"),
				"+15555550000"
			).sync
			assert_kind_of Registration::Payment::CreditCard, result
		end
		em :test_for_credit_card

		def test_for_code
			skip "Code not implemented yet"
			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, "test", "+15555550000")
			assert_kind_of Registration::Payment::Code, result
		end

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

			def setup
				Registration::Payment::Bitcoin::ELECTRUM.expect(
					:createnewaddress,
					EMPromise.resolve("testaddr")
				)
				iq = Blather::Stanza::Iq::Command.new
				@bitcoin = Registration::Payment::Bitcoin.new(
					iq,
					Customer.new("test", plan_name: "test_usd"),
					"+15555550000"
				)
			end

			def test_write
				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
				Registration::Payment::Bitcoin::BLATHER.expect(
					:<<,
					nil,
					[Matching.new do |reply|
						assert_equal :completed, 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.new(1))
				)
				@bitcoin.stub(:save, EMPromise.resolve(nil)) do
					@bitcoin.write.sync
				end
				Registration::Payment::Bitcoin::BLATHER.verify
			end
			em :test_write
		end

		class CreditCardTest < Minitest::Test
			def setup
				@iq = Blather::Stanza::Iq::Command.new
				@iq.from = "test@example.com"
				@credit_card = Registration::Payment::CreditCard.new(
					@iq,
					Customer.new("test"),
					"+15555550000"
				)
			end

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

			def test_reply
				assert_equal [:execute, :next], @credit_card.reply.allowed_actions
				assert_equal(
					"Add credit card, then return here and choose next: " \
					"http://creditcard.example.com",
					@credit_card.reply.note.content
				)
			end
		end

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

			def test_write
				transaction = PromiseMock.new
				transaction.expect(
					:insert,
					EMPromise.resolve(nil)
				)
				customer = Minitest::Mock.new(
					Customer.new("test", plan_name: "test_usd")
				)
				Registration::Payment::CreditCard::Activate::Transaction.expect(
					:sale,
					transaction,
					[
						customer,
						CONFIG[:activation_amount],
						:test_default_method
					]
				)
				iq = Blather::Stanza::Iq::Command.new
				customer.expect(:bill_plan, nil)
				Registration::Payment::CreditCard::Activate::Finish.expect(
					:new,
					OpenStruct.new(write: nil),
					[Blather::Stanza::Iq, customer, "+15555550000"]
				)
				Registration::Payment::CreditCard::Activate.new(
					iq,
					customer,
					:test_default_method,
					"+15555550000"
				).write.sync
				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.new("test", plan_name: "test_usd")
				)
				Registration::Payment::CreditCard::Activate::Transaction.expect(
					:sale,
					EMPromise.reject("declined"),
					[
						customer,
						CONFIG[:activation_amount],
						:test_default_method
					]
				)
				iq = Blather::Stanza::Iq::Command.new
				iq.from = "test@example.com"
				result = Minitest::Mock.new
				result.expect(:then, nil)
				Registration::Payment::CreditCard::Activate::COMMAND_MANAGER.expect(
					:write,
					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]
				)
				Registration::Payment::CreditCard::Activate.new(
					iq,
					customer,
					:test_default_method,
					"+15555550000"
				).write.sync
				Registration::Payment::CreditCard::Activate::Transaction.verify
			end
			em :test_write_declines
		end
	end

	class FinishTest < Minitest::Test
		Registration::Finish::BLATHER = Minitest::Mock.new

		def setup
			@finish = Registration::Finish.new(
				Blather::Stanza::Iq::Command.new,
				Customer.new("test"),
				"+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>
				</OrderResponse>
			RESPONSE
			BACKEND_SGX.expect(
				:register!,
				EMPromise.resolve(OpenStruct.new(error?: false)),
				["test", "+15555550000"]
			)
			Registration::Finish::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]
			)
			@finish.write.sync
			assert_requested create_order
			BACKEND_SGX.verify
			Registration::Finish::BLATHER.verify
		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
			Registration::Finish::BLATHER.expect(
				:<<,
				nil,
				[Matching.new do |reply|
					assert_equal :completed, reply.status
					assert_equal :error, reply.note_type
					assert_equal(
						"The JMP number +15555550000 is no longer available, " \
						"please visit https://jmp.chat and choose another.",
						reply.note.content
					)
				end]
			)
			@finish.write.sync
			assert_requested create_order
			Registration::Finish::BLATHER.verify
		end
		em :test_write_tn_fail
	end
end