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