Change summary
lib/registration.rb | 5 +----
lib/sim_kind.rb | 11 ++++-------
test/test_sim_kind.rb | 30 +++++++++++++-----------------
3 files changed, 18 insertions(+), 28 deletions(-)
Detailed changes
@@ -43,7 +43,6 @@ class Registration
Command.reply { |reply|
reply.command << FormTemplate.render(
"registration/pay_without_code",
- product: @sim_kind,
instructions: instructions,
currency_required: !@customer.currency,
title: "Pay for #{@sim_kind.label}"
@@ -97,9 +96,7 @@ class Registration
def write
@sim_kind.get_processor(@customer).then { |order|
# NOTE: cheogram will swallow any stanza
- # with type `completed`
- # `can_complete: false` is needed to prevent customer
- # from (possibly) permanently losing their eSIM QR code
+ # with available action `complete`
order.process(can_complete: false)
}
end
@@ -8,6 +8,8 @@ class SIMKind
SIMOrder
when "esim"
SIMOrder::ESIM
+ else
+ raise "Unknown SIM kind"
end
@variant = variant
end
@@ -23,10 +25,7 @@ class SIMKind
cfg = cfg(customer.currency)
raise ArgumentError, "Invalid config" unless cfg
- @klass.new(
- customer,
- **cfg
- )
+ @klass.for(customer, **cfg)
end
# @return [String] The result of either
@@ -39,9 +38,7 @@ class SIMKind
# @return [Float, NilClass] Returns nil if `customer.currency`
# is nil, or if @variant is malformed.
def price(customer)
- cfg = cfg(customer.currency)
- return nil unless cfg
-
+ cfg = cfg(customer.currency || :USD)
cfg[:price] / 100.to_d
end
@@ -26,7 +26,7 @@ class SIMKindTest < Minitest::Test
iq.form.fields = []
}
- @cust = customer(plan_name: "test_usd")
+ @cust = customer(plan_name: "test_usd", balance: 1000)
end
def test_initialize_with_sim
@@ -40,13 +40,15 @@ class SIMKindTest < Minitest::Test
end
def test_initialize_with_invalid_variant
- kind = SIMKind.new("invalid")
- assert_raises(ArgumentError) { kind.get_processor(@cust) }
+ assert_raises do
+ SIMKind.new("invalid")
+ end
end
def test_initialize_with_nil
- kind = SIMKind.new(nil)
- assert_raises(NoMethodError) { kind.get_processor(@cust) }
+ assert_raises do
+ SIMKind.new(nil)
+ end
end
def test_from_form_with_sim
@@ -59,21 +61,15 @@ class SIMKindTest < Minitest::Test
assert_instance_of SIMOrder::ESIM, kind.get_processor(@cust)
end
- def test_price_returns_nil_when_customer_currency_nil
+ def test_price_returns_usd_when_customer_currency_nil
kind = SIMKind.new("sim")
cust = customer(currency: nil)
- assert_nil kind.price(cust)
+ assert_equal 5, kind.price(cust)
end
- def test_price_returns_nil_when_config_missing_variant
- kind = SIMKind.new("invalid")
- cust = customer(currency: :USD)
- assert_nil kind.price(cust)
- end
-
- def test_cfg_returns_nil_when_config_missing_currency
- kind = SIMKind.new("sim")
- cust = customer(currency: :EUR)
- assert_nil kind.price(cust)
+ def test_price_raises_when_config_missing_variant
+ assert_raises do
+ SIMKind.new("invalid")
+ end
end
end