test_admin_command.rb

  1# frozen_string_literal: true
  2
  3require "test_helper"
  4require "admin_command"
  5
  6BackendSgx::IQ_MANAGER = Minitest::Mock.new
  7AdminAction::LaunchSnikket::IQ_MANAGER = Minitest::Mock.new
  8Customer::BLATHER = Minitest::Mock.new
  9AdminActionRepo::REDIS = Minitest::Mock.new
 10TrivialBackendSgxRepo::REDIS = Minitest::Mock.new
 11
 12class AdminCommandTest < Minitest::Test
 13	def admin_command(tel="+15556667777")
 14		sgx = Minitest::Mock.new(OpenStruct.new(
 15			registered?: OpenStruct.new(phone: tel)
 16		))
 17		[
 18			sgx,
 19			AdminCommand.new(
 20				customer(sgx: sgx),
 21				CustomerRepo.new(db: FakeDB.new),
 22				AdminActionRepo.new,
 23				Snikket::Repo.new(db: FakeDB.new)
 24			)
 25		]
 26	end
 27
 28	def test_no_user
 29		q_form = Blather::Stanza::Iq::Command.new
 30		q_form.action = :complete
 31		q_form.form.fields = [
 32			{ var: "q", value: "testuser" }
 33		]
 34
 35		customer_repo = Minitest::Mock.new
 36
 37		result = execute_command {
 38			customer_repo.expect(
 39				:find_by_format,
 40				EMPromise.resolve(OpenStruct.new(
 41					customer_id: "testuser",
 42					billing_customer_id: "testuser",
 43					balance: 0.to_d,
 44					jid: Blather::JID.new("test@example.com"),
 45					tndetails: {}
 46				)),
 47				["testuser"]
 48			)
 49
 50			customer_repo.expect(
 51				:find_by_format,
 52				EMPromise.resolve(nil),
 53				[Blather::JID]
 54			)
 55
 56			customer_repo.expect(
 57				:find_by_format,
 58				EMPromise.resolve(nil),
 59				[ProxiedJID]
 60			)
 61
 62			TrivialBackendSgxRepo::REDIS.expect(
 63				:get,
 64				EMPromise.resolve(nil),
 65				["jmp_customer_backend_sgx-testuser"]
 66			)
 67
 68			TrustLevelRepo::REDIS.expect(
 69				:get,
 70				EMPromise.resolve("Customer"),
 71				["jmp_customer_trust_level-testuser"]
 72			)
 73
 74			TrustLevelRepo::DB.expect(
 75				:query_one,
 76				EMPromise.resolve({ settled_amount: 0 }),
 77				[String, "testuser"], default: {}
 78			)
 79
 80			Subaccount::DB.expect(
 81				:query_defer,
 82				EMPromise.resolve([]),
 83				[String, ["testuser"]]
 84			)
 85
 86			Command::COMMAND_MANAGER.expect(
 87				:write,
 88				EMPromise.resolve(q_form),
 89				[Matching.new do |iq|
 90					 assert_equal :form, iq.form.type
 91					 assert iq.form.field("q")
 92				 end]
 93			)
 94
 95			AdminCommand.for(nil, customer_repo).start.catch { |e| e }
 96		}
 97
 98		assert result.stanza.completed?
 99		assert_mock customer_repo
100		assert_mock Command::COMMAND_MANAGER
101		assert_mock TrustLevelRepo::REDIS
102		assert_mock Subaccount::DB
103		assert_mock TrivialBackendSgxRepo::REDIS
104	end
105	em :test_no_user
106
107	def test_action_launch_snikket
108		sgx, admin = admin_command
109		domain_form = Blather::Stanza::Iq::Command.new
110		domain_form.form.fields = [
111			{ var: "domain", value: "test.snikket.chat" }
112		]
113
114		launched = Snikket::Launched.new
115		launched << Niceogiri::XML::Node.new(
116			:launched, launched.document, "xmpp:snikket.org/hosting/v1"
117		).tap { |inner|
118			inner << Niceogiri::XML::Node.new(
119				:'instance-id', launched.document, "xmpp:snikket.org/hosting/v1"
120			).tap { |id|
121				id.content = "si-1234"
122			}
123			inner << Niceogiri::XML::Node.new(
124				:bootstrap, launched.document, "xmpp:snikket.org/hosting/v1"
125			).tap { |bootstrap|
126				bootstrap << Niceogiri::XML::Node.new(
127					:token, launched.document, "xmpp:snikket.org/hosting/v1"
128				).tap { |token|
129					token.content = "TOKEN"
130				}
131			}
132		}
133
134		result = execute_command {
135			Command::COMMAND_MANAGER.expect(
136				:write,
137				EMPromise.resolve(domain_form),
138				[Matching.new do |iq|
139					 assert_equal :form, iq.form.type
140					 assert iq.form.field("domain")
141				 end]
142			)
143			Command::COMMAND_MANAGER.expect(
144				:write,
145				EMPromise.reject(:test_result),
146				[Matching.new do |iq|
147					 assert :result, iq.type
148					 assert(
149						 "https://test.snikket.chat/invites_bootstrap?token=TOKEN",
150						 iq.form.field("bootstrap-uri").value
151					 )
152				 end]
153			)
154
155			AdminAction::LaunchSnikket::IQ_MANAGER.expect(
156				:write,
157				EMPromise.resolve(launched),
158				[Matching.new do |iq|
159					 assert_equal :set, iq.type
160					 assert_equal CONFIG[:snikket_hosting_api], iq.to.to_s
161					 assert_equal(
162						 "test.snikket.chat",
163						 iq.xpath(
164							 "./ns:launch/ns:domain",
165							 ns: "xmpp:snikket.org/hosting/v1"
166						 ).text
167					 )
168				 end]
169			)
170
171			admin.action_launch_snikket.catch { |e| e }
172		}
173
174		assert_equal :test_result, result
175		assert_mock sgx
176		assert_mock AdminAction::LaunchSnikket::IQ_MANAGER
177		assert_mock Customer::BLATHER
178	end
179	em :test_action_launch_snikket
180
181	def test_action_cancel_account
182		req = stub_request(:post, "https://api.churnbuster.io/v1/cancellations")
183			.with(
184				body: {
185					customer: {
186						source: "braintree",
187						source_id: "test",
188						email: "test@smtp.cheogram.com",
189						properties: {}
190					},
191					subscription: {
192						source: "braintree",
193						source_id: "test"
194					}
195				}.to_json
196			)
197			.to_return(status: 200, body: "", headers: {})
198
199		sgx, admin = admin_command
200
201		Customer::BLATHER.expect(
202			:<<,
203			EMPromise.resolve(nil),
204			[
205				Matching.new do |m|
206					assert_equal "Your JMP account has been cancelled.", m.body
207					assert_equal "test@example.net", m.to.to_s
208					assert_equal "notify_from@component", m.from.to_s
209				end
210			]
211		)
212
213		Customer::BLATHER.expect(
214			:<<,
215			EMPromise.resolve(nil),
216			[
217				Matching.new do |iq|
218					assert iq.remove?
219					assert_equal "test@example.net", iq.to.to_s
220					assert_equal "component", iq.from.to_s
221				end
222			]
223		)
224
225		sgx.expect(:deregister!, EMPromise.resolve(nil))
226
227		stub_request(
228			:post,
229			"https://dashboard.bandwidth.com/v1.0/accounts//disconnects"
230		).with(
231			body: {
232				name: "test",
233				DisconnectTelephoneNumberOrderType: {
234					TelephoneNumberList: {
235						TelephoneNumber: "5556667777"
236					}
237				}
238			}.to_xml(indent: 0, root: "DisconnectTelephoneNumberOrder")
239		).to_return(status: 200, body: "")
240
241		admin.action_cancel_account.sync
242
243		assert_mock sgx
244		assert_mock BackendSgx::IQ_MANAGER
245		assert_mock Customer::BLATHER
246		assert_requested req
247	end
248	em :test_action_cancel_account
249
250	def test_action_cancel_account_keep_number
251		req = stub_request(:post, "https://api.churnbuster.io/v1/cancellations")
252			.with(
253				body: {
254					customer: {
255						source: "braintree",
256						source_id: "test",
257						email: "test@smtp.cheogram.com",
258						properties: {}
259					},
260					subscription: {
261						source: "braintree",
262						source_id: "test"
263					}
264				}.to_json
265			)
266			.to_return(status: 200, body: "", headers: {})
267
268		sgx, admin = admin_command("+15566667777")
269
270		Customer::BLATHER.expect(
271			:<<,
272			EMPromise.resolve(nil),
273			[
274				Matching.new do |m|
275					assert_equal "Your JMP account has been cancelled.", m.body
276					assert_equal "test@example.net", m.to.to_s
277					assert_equal "notify_from@component", m.from.to_s
278				end
279			]
280		)
281
282		Customer::BLATHER.expect(
283			:<<,
284			EMPromise.resolve(nil),
285			[
286				Matching.new do |iq|
287					assert iq.remove?
288					assert_equal "test@example.net", iq.to.to_s
289					assert_equal "component", iq.from.to_s
290				end
291			]
292		)
293
294		sgx.expect(:deregister!, EMPromise.resolve(nil))
295
296		stub_request(
297			:post,
298			"https://dashboard.bandwidth.com/v1.0/accounts/moveto/moveTns"
299		).with(
300			body: {
301				CustomerOrderId: "test",
302				SourceAccountId: "test_bw_account",
303				SiteId: "movetosite",
304				SipPeerId: "movetopeer",
305				TelephoneNumbers: { TelephoneNumber: "5566667777" }
306			}.to_xml(indent: 0, root: "MoveTnsOrder")
307		).to_return(status: 200, body: "")
308
309		admin.action_cancel_account.sync
310
311		assert_mock sgx
312		assert_mock BackendSgx::IQ_MANAGER
313		assert_mock Customer::BLATHER
314		assert_requested req
315	end
316	em :test_action_cancel_account_keep_number
317end