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		sgx, admin = admin_command
183
184		Customer::BLATHER.expect(
185			:<<,
186			EMPromise.resolve(nil),
187			[
188				Matching.new do |m|
189					assert_equal "Your JMP account has been cancelled.", m.body
190					assert_equal "test@example.net", m.to.to_s
191					assert_equal "notify_from@component", m.from.to_s
192				end
193			]
194		)
195
196		Customer::BLATHER.expect(
197			:<<,
198			EMPromise.resolve(nil),
199			[
200				Matching.new do |iq|
201					assert iq.remove?
202					assert_equal "test@example.net", iq.to.to_s
203					assert_equal "component", iq.from.to_s
204				end
205			]
206		)
207
208		sgx.expect(:deregister!, EMPromise.resolve(nil))
209
210		stub_request(
211			:post,
212			"https://dashboard.bandwidth.com/v1.0/accounts//disconnects"
213		).with(
214			body: {
215				name: "test",
216				DisconnectTelephoneNumberOrderType: {
217					TelephoneNumberList: {
218						TelephoneNumber: "5556667777"
219					}
220				}
221			}.to_xml(indent: 0, root: "DisconnectTelephoneNumberOrder")
222		).to_return(status: 200, body: "")
223
224		admin.action_cancel_account.sync
225
226		assert_mock sgx
227		assert_mock BackendSgx::IQ_MANAGER
228		assert_mock Customer::BLATHER
229	end
230	em :test_action_cancel_account
231
232	def test_action_cancel_account_keep_number
233		sgx, admin = admin_command("+15566667777")
234
235		Customer::BLATHER.expect(
236			:<<,
237			EMPromise.resolve(nil),
238			[
239				Matching.new do |m|
240					assert_equal "Your JMP account has been cancelled.", m.body
241					assert_equal "test@example.net", m.to.to_s
242					assert_equal "notify_from@component", m.from.to_s
243				end
244			]
245		)
246
247		Customer::BLATHER.expect(
248			:<<,
249			EMPromise.resolve(nil),
250			[
251				Matching.new do |iq|
252					assert iq.remove?
253					assert_equal "test@example.net", iq.to.to_s
254					assert_equal "component", iq.from.to_s
255				end
256			]
257		)
258
259		sgx.expect(:deregister!, EMPromise.resolve(nil))
260
261		stub_request(
262			:post,
263			"https://dashboard.bandwidth.com/v1.0/accounts/moveto/moveTns"
264		).with(
265			body: {
266				CustomerOrderId: "test",
267				SourceAccountId: "test_bw_account",
268				SiteId: "movetosite",
269				SipPeerId: "movetopeer",
270				TelephoneNumbers: { TelephoneNumber: "5566667777" }
271			}.to_xml(indent: 0, root: "MoveTnsOrder")
272		).to_return(status: 200, body: "")
273
274		admin.action_cancel_account.sync
275
276		assert_mock sgx
277		assert_mock BackendSgx::IQ_MANAGER
278		assert_mock Customer::BLATHER
279	end
280	em :test_action_cancel_account_keep_number
281end