1# frozen_string_literal: true
2
3require "test_helper"
4require "customer_info"
5require "trust_level_repo"
6require "trust_level"
7
8API::REDIS = FakeRedis.new
9CustomerPlan::REDIS = Minitest::Mock.new
10PlanInfo::DB = FakeDB.new(
11 ["test"] => [
12 {
13 "start_date" => Time.parse("2020-01-01"),
14 "activation_date" => Time.parse("2021-01-01")
15 }
16 ]
17)
18
19class CustomerInfoTest < Minitest::Test
20 def test_info_does_not_crash
21 sgx = Minitest::Mock.new
22 sgx.expect(:registered?, false)
23
24 CustomerPlan::DB.expect(
25 :query_one,
26 EMPromise.resolve({ start_date: Time.now }),
27 [String, "test"]
28 )
29
30 cust = customer(sgx: sgx, plan_name: "test_usd")
31
32 assert cust.info.sync.form
33 assert_mock sgx
34 end
35 em :test_info_does_not_crash
36
37 def test_admin_info_does_not_crash
38 sgx = Minitest::Mock.new
39 sgx.expect(:registered?, false)
40 fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
41 sgx.expect(:fwd, fwd)
42 sgx.expect(:registered?, false)
43
44 CustomerPlan::DB.expect(
45 :query_one,
46 EMPromise.resolve({ start_date: Time.now }),
47 [String, "test"]
48 )
49
50 cust = customer(sgx: sgx, plan_name: "test_usd")
51
52 trust_repo = Minitest::Mock.new
53 trust_repo.expect(:find, TrustLevel::Basement, [cust])
54
55 assert cust.admin_info(trust_level_repo: trust_repo).sync.form
56 assert_mock sgx
57 assert_mock trust_repo
58 end
59 em :test_admin_info_does_not_crash
60
61 def test_admin_info_with_tel_does_not_crash
62 registered = Struct.new(:phone).new("+12223334444")
63 fwd = CustomerFwd.for(uri: "tel:+12223334444", timeout: 15)
64 sgx = Struct.new(:registered?, :fwd).new(registered, fwd)
65
66 CustomerPlan::DB.expect(
67 :query_one,
68 EMPromise.resolve({ start_date: Time.now }),
69 [String, "test"]
70 )
71
72 cust = customer(sgx: sgx, plan_name: "test_usd")
73
74 call_attempt_repo = Minitest::Mock.new
75 call_attempt_repo.expect(
76 :find_outbound,
77 CallAttempt::Unsupported.new(direction: :outbound),
78 [cust, "+1", { call_id: "dry_run" }]
79 )
80
81 trust_repo = Minitest::Mock.new
82 trust_repo.expect(:find, TrustLevel::Basement, [cust])
83
84 assert cust
85 .admin_info(
86 trust_level_repo: trust_repo,
87 call_attempt_repo: call_attempt_repo
88 ).sync.form
89 assert_mock call_attempt_repo
90 assert_mock trust_repo
91 end
92 em :test_admin_info_with_tel_does_not_crash
93
94 def test_inactive_info_does_not_crash
95 sgx = Minitest::Mock.new
96 sgx.expect(:registered?, false)
97
98 plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
99 cust = Customer.new(
100 "test",
101 Blather::JID.new("test@example.net"),
102 plan: plan,
103 sgx: sgx
104 )
105 assert cust.info.sync.form
106 assert_mock sgx
107 end
108 em :test_inactive_info_does_not_crash
109
110 def test_inactive_admin_info_does_not_crash
111 sgx = Minitest::Mock.new
112 sgx.expect(:registered?, false)
113 sgx.expect(:registered?, false)
114 sgx.expect(:fwd, CustomerFwd::None.new(uri: nil, timeout: nil))
115
116 plan = CustomerPlan.new("test", plan: nil, expires_at: nil)
117 cust = Customer.new(
118 "test",
119 Blather::JID.new("test@example.net"),
120 plan: plan,
121 sgx: sgx
122 )
123
124 trust_repo = Minitest::Mock.new
125 trust_repo.expect(:find, TrustLevel::Basement, [cust])
126
127 assert cust.admin_info(trust_level_repo: trust_repo).sync.form
128 assert_mock sgx
129 assert_mock trust_repo
130 end
131 em :test_inactive_admin_info_does_not_crash
132
133 def test_missing_customer_admin_info_does_not_crash
134 cust = CustomerInfoForm::NoCustomer.new
135 assert cust.admin_info.form
136 end
137 em :test_missing_customer_admin_info_does_not_crash
138end