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			Subaccount::DB.expect(
 74				:query_one,
 75				EMPromise.resolve({}),
 76				[String, "testuser"]
 77			)
 78
 79			Command::COMMAND_MANAGER.expect(
 80				:write,
 81				EMPromise.resolve(q_form),
 82				[Matching.new do |iq|
 83					 assert_equal :form, iq.form.type
 84					 assert iq.form.field("q")
 85				 end]
 86			)
 87
 88			AdminCommand.for(nil, customer_repo).start.catch { |e| e }
 89		}
 90
 91		assert result.stanza.completed?
 92		assert_mock customer_repo
 93		assert_mock Command::COMMAND_MANAGER
 94		assert_mock TrustLevelRepo::REDIS
 95		assert_mock Subaccount::DB
 96	end
 97	em :test_no_user
 98
 99	def test_action_launch_snikket
100		sgx, admin = admin_command
101		domain_form = Blather::Stanza::Iq::Command.new
102		domain_form.form.fields = [
103			{ var: "domain", value: "test.snikket.chat" }
104		]
105
106		launched = Snikket::Launched.new
107		launched << Niceogiri::XML::Node.new(
108			:launched, launched.document, "xmpp:snikket.org/hosting/v1"
109		).tap { |inner|
110			inner << Niceogiri::XML::Node.new(
111				:'instance-id', launched.document, "xmpp:snikket.org/hosting/v1"
112			).tap { |id|
113				id.content = "si-1234"
114			}
115			inner << Niceogiri::XML::Node.new(
116				:bootstrap, launched.document, "xmpp:snikket.org/hosting/v1"
117			).tap { |bootstrap|
118				bootstrap << Niceogiri::XML::Node.new(
119					:token, launched.document, "xmpp:snikket.org/hosting/v1"
120				).tap { |token|
121					token.content = "TOKEN"
122				}
123			}
124		}
125
126		result = execute_command {
127			Command::COMMAND_MANAGER.expect(
128				:write,
129				EMPromise.resolve(domain_form),
130				[Matching.new do |iq|
131					 assert_equal :form, iq.form.type
132					 assert iq.form.field("domain")
133				 end]
134			)
135			Command::COMMAND_MANAGER.expect(
136				:write,
137				EMPromise.reject(:test_result),
138				[Matching.new do |iq|
139					 assert :result, iq.type
140					 assert(
141						 "https://test.snikket.chat/invites_bootstrap?token=TOKEN",
142						 iq.form.field("bootstrap-uri").value
143					 )
144				 end]
145			)
146
147			AdminAction::LaunchSnikket::IQ_MANAGER.expect(
148				:write,
149				EMPromise.resolve(launched),
150				[Matching.new do |iq|
151					 assert_equal :set, iq.type
152					 assert_equal CONFIG[:snikket_hosting_api], iq.to.to_s
153					 assert_equal(
154						 "test.snikket.chat",
155						 iq.xpath(
156							 "./ns:launch/ns:domain",
157							 ns: "xmpp:snikket.org/hosting/v1"
158						 ).text
159					 )
160				 end]
161			)
162
163			admin.action_launch_snikket.catch { |e| e }
164		}
165
166		assert_equal :test_result, result
167		assert_mock sgx
168		assert_mock AdminAction::LaunchSnikket::IQ_MANAGER
169		assert_mock Customer::BLATHER
170	end
171	em :test_action_launch_snikket
172
173	def test_action_cancel_account
174		sgx, admin = admin_command
175
176		Customer::BLATHER.expect(
177			:<<,
178			EMPromise.resolve(nil),
179			[
180				Matching.new do |m|
181					assert_equal "Your JMP account has been cancelled.", m.body
182					assert_equal "test@example.net", m.to.to_s
183					assert_equal "notify_from@component", m.from.to_s
184				end
185			]
186		)
187
188		Customer::BLATHER.expect(
189			:<<,
190			EMPromise.resolve(nil),
191			[
192				Matching.new do |iq|
193					assert iq.remove?
194					assert_equal "test@example.net", iq.to.to_s
195					assert_equal "component", iq.from.to_s
196				end
197			]
198		)
199
200		sgx.expect(:deregister!, EMPromise.resolve(nil))
201
202		stub_request(
203			:post,
204			"https://dashboard.bandwidth.com/v1.0/accounts//disconnects"
205		).with(
206			body: {
207				name: "test",
208				DisconnectTelephoneNumberOrderType: {
209					TelephoneNumberList: {
210						TelephoneNumber: "5556667777"
211					}
212				}
213			}.to_xml(indent: 0, root: "DisconnectTelephoneNumberOrder")
214		).to_return(status: 200, body: "")
215
216		admin.action_cancel_account.sync
217
218		assert_mock sgx
219		assert_mock BackendSgx::IQ_MANAGER
220		assert_mock Customer::BLATHER
221	end
222	em :test_action_cancel_account
223
224	def test_action_cancel_account_keep_number
225		sgx, admin = admin_command("+15566667777")
226
227		Customer::BLATHER.expect(
228			:<<,
229			EMPromise.resolve(nil),
230			[
231				Matching.new do |m|
232					assert_equal "Your JMP account has been cancelled.", m.body
233					assert_equal "test@example.net", m.to.to_s
234					assert_equal "notify_from@component", m.from.to_s
235				end
236			]
237		)
238
239		Customer::BLATHER.expect(
240			:<<,
241			EMPromise.resolve(nil),
242			[
243				Matching.new do |iq|
244					assert iq.remove?
245					assert_equal "test@example.net", iq.to.to_s
246					assert_equal "component", iq.from.to_s
247				end
248			]
249		)
250
251		sgx.expect(:deregister!, EMPromise.resolve(nil))
252
253		stub_request(
254			:post,
255			"https://dashboard.bandwidth.com/v1.0/accounts/moveto/moveTns"
256		).with(
257			body: {
258				CustomerOrderId: "test",
259				SourceAccountId: "test_bw_account",
260				SiteId: "movetosite",
261				SipPeerId: "movetopeer",
262				TelephoneNumbers: { TelephoneNumber: "5566667777" }
263			}.to_xml(indent: 0, root: "MoveTnsOrder")
264		).to_return(status: 200, body: "")
265
266		admin.action_cancel_account.sync
267
268		assert_mock sgx
269		assert_mock BackendSgx::IQ_MANAGER
270		assert_mock Customer::BLATHER
271	end
272	em :test_action_cancel_account_keep_number
273end