1# frozen_string_literal: true
2
3require "test_helper"
4require "bwmsgsv2_repo"
5require "backend_sgx"
6require "trivial_backend_sgx_repo"
7require "customer"
8
9BackendSgx::IQ_MANAGER = Minitest::Mock.new
10IBRRepo::IQ_MANAGER = Minitest::Mock.new
11
12class BackendSgxTest < Minitest::Test
13 def test_registered
14 IBRRepo::IQ_MANAGER.expect(
15 :write,
16 EMPromise.resolve(Blather::Stanza::Iq::IBR.new.tap { |ibr|
17 ibr.registered = true
18 }),
19 [Matching.new do |ibr|
20 assert_equal :get, ibr.type
21 assert_equal "customer_test@component", ibr.from.to_s
22 end]
23 )
24 sgx = Bwmsgsv2Repo.new(redis: FakeRedis.new).get("test").sync
25 assert sgx.registered?
26 end
27 em :test_registered
28
29 def test_registered_not_registered
30 IBRRepo::IQ_MANAGER.expect(
31 :write,
32 EMPromise.resolve(Blather::Stanza::Iq::IBR.new.tap { |ibr|
33 ibr.registered = false
34 }),
35 [Matching.new do |ibr|
36 assert_equal :get, ibr.type
37 assert_equal "customer_test@component", ibr.from.to_s
38 end]
39 )
40 sgx = Bwmsgsv2Repo.new(redis: FakeRedis.new).get("test").sync
41 refute sgx.registered?
42 end
43 em :test_registered_not_registered
44
45 def test_register!
46 BackendSgx::IQ_MANAGER.expect(
47 :write,
48 EMPromise.resolve(OpenStruct.new(error?: false)),
49 [Matching.new do |ibr|
50 assert_equal "customer_test@component", ibr.from.to_s
51 assert_equal "test_bw_account", ibr.nick
52 assert_equal "test_bw_user", ibr.username
53 assert_equal "test_bw_password", ibr.password
54 assert_equal "+15555550000", ibr.phone
55 end]
56 )
57 sgx = TrivialBackendSgxRepo.new(redis: FakeRedis.new).get("test").sync
58 sgx.register!("+15555550000")
59 BackendSgx::IQ_MANAGER.verify
60 end
61 em :test_register!
62
63 def test_set_port_out_pin_happy_path
64 sgx = TrivialBackendSgxRepo.new(redis: FakeRedis.new).get("test").sync
65 cust = customer("test", sgx: sgx)
66
67 port_out_pin = "74hwsn"
68 session_id = "session_yay_awesome"
69
70 BackendSgx::IQ_MANAGER.expect(
71 :write,
72 EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
73 iq.command[:sessionid] = session_id
74 }),
75 [Matching.new do |iq|
76 assert_equal CONFIG[:sgx], iq.to.to_s
77 assert_equal "customer_test@component", iq.from.to_s
78 assert_equal "set-port-out-pin", iq.node
79 assert_equal :execute, iq.action
80 end]
81 )
82
83 BackendSgx::IQ_MANAGER.expect(
84 :write,
85 EMPromise.resolve(OpenStruct.new(
86 status: :completed,
87 note_type: :info
88 )),
89 [Matching.new do |iq|
90 assert_equal :complete, iq.action
91 assert_equal :submit, iq.form.type
92 assert_equal CONFIG[:sgx], iq.to.to_s
93 assert_equal "customer_test@component", iq.from.to_s
94 assert_equal "set-port-out-pin", iq.node
95 assert_equal session_id, iq.sessionid
96
97 pin_field = iq.form.fields.find { |f| f.var == "pin" }
98 assert_equal port_out_pin, pin_field.value
99 assert_equal "text-private", pin_field.type
100
101 confirm_field = iq.form.fields.find { |f| f.var == "confirm_pin" }
102 assert_equal port_out_pin, confirm_field.value
103 assert_equal "text-private", confirm_field.type
104 end]
105 )
106
107 result = sgx.set_port_out_pin(cust, port_out_pin).sync
108 assert_nil result
109 assert_mock BackendSgx::IQ_MANAGER
110 end
111 em :test_set_port_out_pin_happy_path
112
113 def test_set_port_out_pin_validation
114 sgx = TrivialBackendSgxRepo.new(redis: FakeRedis.new).get("test").sync
115 cust = customer("test", sgx: sgx)
116
117 [
118 ["123", "PIN must be 4-10 alphanumeric characters"],
119 ["12345678901", "PIN must be 4-10 alphanumeric characters"],
120 ["123!", "PIN must be 4-10 alphanumeric characters"],
121 ["pin with spaces", "PIN must be 4-10 alphanumeric characters"],
122 ["", "PIN must be 4-10 alphanumeric characters"]
123 ].each do |invalid_pin, expected_error|
124 session_id = "session_validation_#{invalid_pin.gsub(/[^a-zA-Z0-9]/, '')}"
125
126 BackendSgx::IQ_MANAGER.expect(
127 :write,
128 EMPromise.resolve(Blather::Stanza::Iq::Command.new.tap { |iq|
129 iq.command[:sessionid] = session_id
130 }),
131 [Matching.new do |iq|
132 assert_equal CONFIG[:sgx], iq.to.to_s
133 assert_equal "customer_test@component", iq.from.to_s
134 assert_equal "set-port-out-pin", iq.node
135 assert_equal :execute, iq.action
136 end]
137 )
138
139 note = Struct.new(:text) do
140 def [](key)
141 "error" if key == "type"
142 end
143 end.new(expected_error)
144
145 BackendSgx::IQ_MANAGER.expect(
146 :write,
147 EMPromise.resolve(OpenStruct.new(
148 status: :completed,
149 note: note
150 )),
151 [Matching.new do |iq|
152 assert_equal :complete, iq.action
153 assert_equal :submit, iq.form.type
154 assert_equal CONFIG[:sgx], iq.to.to_s
155 assert_equal "customer_test@component", iq.from.to_s
156 assert_equal "set-port-out-pin", iq.node
157 assert_equal session_id, iq.sessionid
158
159 pin_field = iq.form.fields.find { |f| f.var == "pin" }
160 assert_equal invalid_pin, pin_field.value
161 assert_equal "text-private", pin_field.type
162
163 confirm_field = iq.form.fields.find { |f| f.var == "confirm_pin" }
164 assert_equal invalid_pin, confirm_field.value
165 assert_equal "text-private", confirm_field.type
166 end]
167 )
168
169 error = assert_raises(RuntimeError) {
170 sgx.set_port_out_pin(cust, invalid_pin).sync
171 }
172
173 assert_equal expected_error, error.message
174 end
175
176 assert_mock BackendSgx::IQ_MANAGER
177 end
178 em :test_set_port_out_pin_validation
179end