Trust Level in Customer Info

Christopher Vollick created

We have this thing, so we should probably be able to see it

Change summary

forms/admin_info.rb           |  6 ++++++
lib/customer.rb               |  5 +++--
lib/customer_info.rb          |  6 ++++--
lib/trust_level.rb            | 26 +++++++++++++++++++++++++-
test/test_customer_info.rb    | 15 +++++++++++++--
test/test_trust_level_repo.rb | 10 +++++-----
6 files changed, 56 insertions(+), 12 deletions(-)

Detailed changes

forms/admin_info.rb 🔗

@@ -51,6 +51,12 @@ field(
 	value: @admin_info.call_info
 )
 
+field(
+	var: "trust_level",
+	label: "Trust Level",
+	value: @admin_info.trust_level
+)
+
 field(
 	var: "api",
 	label: "API",

lib/customer.rb 🔗

@@ -111,8 +111,9 @@ class Customer
 		API.for(self)
 	end
 
-	def admin_info
-		AdminInfo.for(self, @plan)
+	# kwargs are passed through for dependency injection from tests
+	def admin_info(**kwargs)
+		AdminInfo.for(self, @plan, **kwargs)
 	end
 
 	def info

lib/customer_info.rb 🔗

@@ -100,16 +100,18 @@ class AdminInfo
 		info CustomerInfo
 		api API
 		call_info String
+		trust_level String
 	end
 
-	def self.for(customer, plan)
+	def self.for(customer, plan, trust_level_repo: TrustLevelRepo.new)
 		PromiseHash.all(
 			jid: customer.jid,
 			customer_id: customer.customer_id,
 			fwd: customer.fwd,
 			info: CustomerInfo.for(customer, plan),
 			api: customer.api,
-			call_info: call_info(customer)
+			call_info: call_info(customer),
+			trust_level: trust_level_repo.find(customer).then(&:to_s)
 		).then(&method(:new))
 	end
 

lib/trust_level.rb 🔗

@@ -1,5 +1,7 @@
 # frozen_string_literal: true
 
+require "delegate"
+
 module TrustLevel
 	def self.for(plan_name:, settled_amount: 0, manual: nil)
 		@levels.each do |level|
@@ -8,7 +10,7 @@ module TrustLevel
 				settled_amount: settled_amount,
 				manual: manual
 			)
-			return tl if tl
+			return manual ? Manual.new(tl) : tl if tl
 		end
 
 		raise "No TrustLevel matched"
@@ -19,6 +21,12 @@ module TrustLevel
 		@levels << maybe_mk
 	end
 
+	class Manual < SimpleDelegator
+		def to_s
+			"Manual(#{super})"
+		end
+	end
+
 	class Tomb
 		TrustLevel.register do |manual:, **|
 			new if manual == "Tomb"
@@ -31,6 +39,10 @@ module TrustLevel
 		def send_message?(*)
 			false
 		end
+
+		def to_s
+			"Tomb"
+		end
 	end
 
 	class Basement
@@ -45,6 +57,10 @@ module TrustLevel
 		def send_message?(messages_today)
 			messages_today < 200
 		end
+
+		def to_s
+			"Basement"
+		end
 	end
 
 	class Paragon
@@ -59,6 +75,10 @@ module TrustLevel
 		def send_message?(messages_today)
 			messages_today < 700
 		end
+
+		def to_s
+			"Paragon"
+		end
 	end
 
 	class Customer
@@ -86,5 +106,9 @@ module TrustLevel
 		def send_message?(messages_today)
 			messages_today < 500
 		end
+
+		def to_s
+			"Customer"
+		end
 	end
 end

test/test_customer_info.rb 🔗

@@ -2,6 +2,8 @@
 
 require "test_helper"
 require "customer_info"
+require "trust_level_repo"
+require "trust_level"
 
 API::REDIS = FakeRedis.new
 CustomerPlan::REDIS = Minitest::Mock.new
@@ -46,8 +48,13 @@ class CustomerInfoTest < Minitest::Test
 		)
 
 		cust = customer(sgx: sgx, plan_name: "test_usd")
-		assert cust.admin_info.sync.form
+
+		trust_repo = Minitest::Mock.new
+		trust_repo.expect(:find, TrustLevel::Basement, [cust])
+
+		assert cust.admin_info(trust_level_repo: trust_repo).sync.form
 		assert_mock sgx
+		assert_mock trust_repo
 	end
 	em :test_admin_info_does_not_crash
 
@@ -81,8 +88,12 @@ class CustomerInfoTest < Minitest::Test
 			sgx: sgx
 		)
 
-		assert cust.admin_info.sync.form
+		trust_repo = Minitest::Mock.new
+		trust_repo.expect(:find, TrustLevel::Basement, [cust])
+
+		assert cust.admin_info(trust_level_repo: trust_repo).sync.form
 		assert_mock sgx
+		assert_mock trust_repo
 	end
 	em :test_inactive_admin_info_does_not_crash
 

test/test_trust_level_repo.rb 🔗

@@ -10,7 +10,7 @@ class TrustLevelRepoTest < Minitest::Test
 				"jmp_customer_trust_level-test" => "Tomb"
 			)
 		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
-		assert_kind_of TrustLevel::Tomb, trust_level
+		assert_equal "Manual(Tomb)", trust_level.to_s
 	end
 	em :test_manual_tomb
 
@@ -21,7 +21,7 @@ class TrustLevelRepoTest < Minitest::Test
 				"jmp_customer_trust_level-test" => "Basement"
 			)
 		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
-		assert_kind_of TrustLevel::Basement, trust_level
+		assert_equal "Manual(Basement)", trust_level.to_s
 	end
 	em :test_manual_basement
 
@@ -32,7 +32,7 @@ class TrustLevelRepoTest < Minitest::Test
 				"jmp_customer_trust_level-test" => "Customer"
 			)
 		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
-		assert_kind_of TrustLevel::Customer, trust_level
+		assert_equal "Manual(Customer)", trust_level.to_s
 	end
 	em :test_manual_customer
 
@@ -43,7 +43,7 @@ class TrustLevelRepoTest < Minitest::Test
 				"jmp_customer_trust_level-test" => "Paragon"
 			)
 		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
-		assert_kind_of TrustLevel::Paragon, trust_level
+		assert_equal "Manual(Paragon)", trust_level.to_s
 	end
 	em :test_manual_paragon
 
@@ -54,7 +54,7 @@ class TrustLevelRepoTest < Minitest::Test
 				"jmp_customer_trust_level-test" => "UNKNOWN"
 			)
 		).find(OpenStruct.new(customer_id: "test", plan_name: "usd")).sync
-		assert_kind_of TrustLevel::Customer, trust_level
+		assert_equal "Manual(Customer)", trust_level.to_s
 	end
 	em :test_manual_unknown