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
 10DB = FakeDB.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(customer(sgx: sgx), CustomerRepo.new, Snikket::Repo.new)
 20		]
 21	end
 22
 23	def test_action_launch_snikket
 24		sgx, admin = admin_command
 25		domain_form = Blather::Stanza::Iq::Command.new
 26		domain_form.form.fields = [
 27			{ var: "domain", value: "test.snikket.chat" }
 28		]
 29
 30		launched = Snikket::Launched.new
 31		launched << Niceogiri::XML::Node.new(
 32			:launched, launched.document, "xmpp:snikket.org/hosting/v1"
 33		).tap { |inner|
 34			inner << Niceogiri::XML::Node.new(
 35				:'instance-id', launched.document, "xmpp:snikket.org/hosting/v1"
 36			).tap { |id|
 37				id.content = "si-1234"
 38			}
 39			inner << Niceogiri::XML::Node.new(
 40				:bootstrap, launched.document, "xmpp:snikket.org/hosting/v1"
 41			).tap { |bootstrap|
 42				bootstrap << Niceogiri::XML::Node.new(
 43					:token, launched.document, "xmpp:snikket.org/hosting/v1"
 44				).tap { |token|
 45					token.content = "TOKEN"
 46				}
 47			}
 48		}
 49
 50		result = execute_command {
 51			Command::COMMAND_MANAGER.expect(
 52				:write,
 53				EMPromise.resolve(domain_form),
 54				[Matching.new do |iq|
 55					 assert_equal :form, iq.form.type
 56					 assert iq.form.field("domain")
 57				 end]
 58			)
 59			Command::COMMAND_MANAGER.expect(
 60				:write,
 61				EMPromise.reject(:test_result),
 62				[Matching.new do |iq|
 63					 assert :result, iq.type
 64					 assert(
 65						 "https://test.snikket.chat/invites_bootstrap?token=TOKEN",
 66						 iq.form.field("bootstrap-uri").value
 67					 )
 68				 end]
 69			)
 70
 71			AdminAction::LaunchSnikket::IQ_MANAGER.expect(
 72				:write,
 73				EMPromise.resolve(launched),
 74				[Matching.new do |iq|
 75					 assert_equal :set, iq.type
 76					 assert_equal CONFIG[:snikket_hosting_api], iq.to.to_s
 77					 assert_equal(
 78						 "test.snikket.chat",
 79						 iq.xpath(
 80							 "./ns:launch/ns:domain",
 81							 ns: "xmpp:snikket.org/hosting/v1"
 82						 ).text
 83					 )
 84				 end]
 85			)
 86
 87			admin.action_launch_snikket.catch { |e| e }
 88		}
 89
 90		assert_equal :test_result, result
 91		assert_mock sgx
 92		assert_mock AdminAction::LaunchSnikket::IQ_MANAGER
 93		assert_mock Customer::BLATHER
 94	end
 95	em :test_action_launch_snikket
 96
 97	def test_action_cancel_account
 98		sgx, admin = admin_command
 99
100		Customer::BLATHER.expect(
101			:<<,
102			EMPromise.resolve(nil),
103			[
104				Matching.new do |m|
105					assert_equal "Your JMP account has been cancelled.", m.body
106					assert_equal "test@example.net", m.to.to_s
107					assert_equal "notify_from@component", m.from.to_s
108				end
109			]
110		)
111
112		Customer::BLATHER.expect(
113			:<<,
114			EMPromise.resolve(nil),
115			[
116				Matching.new do |iq|
117					assert iq.remove?
118					assert_equal "test@example.net", iq.to.to_s
119					assert_equal "component", iq.from.to_s
120				end
121			]
122		)
123
124		sgx.expect(:deregister!, EMPromise.resolve(nil))
125
126		stub_request(
127			:post,
128			"https://dashboard.bandwidth.com/v1.0/accounts//disconnects"
129		).with(
130			body: {
131				name: "test",
132				DisconnectTelephoneNumberOrderType: {
133					TelephoneNumberList: {
134						TelephoneNumber: "5556667777"
135					}
136				}
137			}.to_xml(indent: 0, root: "DisconnectTelephoneNumberOrder")
138		).to_return(status: 200, body: "")
139
140		admin.action_cancel_account.sync
141
142		assert_mock sgx
143		assert_mock BackendSgx::IQ_MANAGER
144		assert_mock Customer::BLATHER
145	end
146	em :test_action_cancel_account
147
148	def test_action_cancel_account_keep_number
149		sgx, admin = admin_command("+15566667777")
150
151		Customer::BLATHER.expect(
152			:<<,
153			EMPromise.resolve(nil),
154			[
155				Matching.new do |m|
156					assert_equal "Your JMP account has been cancelled.", m.body
157					assert_equal "test@example.net", m.to.to_s
158					assert_equal "notify_from@component", m.from.to_s
159				end
160			]
161		)
162
163		Customer::BLATHER.expect(
164			:<<,
165			EMPromise.resolve(nil),
166			[
167				Matching.new do |iq|
168					assert iq.remove?
169					assert_equal "test@example.net", iq.to.to_s
170					assert_equal "component", iq.from.to_s
171				end
172			]
173		)
174
175		sgx.expect(:deregister!, EMPromise.resolve(nil))
176
177		stub_request(
178			:post,
179			"https://dashboard.bandwidth.com/v1.0/accounts/moveto/moveTns"
180		).with(
181			body: {
182				CustomerOrderId: "test",
183				SourceAccountId: "test_bw_account",
184				SiteId: "movetosite",
185				SipPeerId: "movetopeer",
186				TelephoneNumbers: { TelephoneNumber: "5566667777" }
187			}.to_xml(indent: 0, root: "MoveTnsOrder")
188		).to_return(status: 200, body: "")
189
190		admin.action_cancel_account.sync
191
192		assert_mock sgx
193		assert_mock BackendSgx::IQ_MANAGER
194		assert_mock Customer::BLATHER
195	end
196	em :test_action_cancel_account_keep_number
197end