~singpolyma/sgx-jmp

a3f4e2701ae66bda07c998bbb1391ebbb12fb755 — Stephen Paul Weber 3 years ago dabca13
Create customer_id if it does not exist before we start registration
5 files changed, 38 insertions(+), 14 deletions(-)

M lib/customer.rb
M lib/registration.rb
M sgx_jmp.rb
M test/test_customer.rb
M test/test_registration.rb
M lib/customer.rb => lib/customer.rb +13 -0
@@ 27,6 27,19 @@ class Customer
		end
	end

	def self.create(jid)
		BRAINTREE.customer.create.then do |result|
			raise "Braintree customer create failed" unless result.success?
			cid = result.customer.id
			REDIS.msetnx(
				"jmp_customer_id-#{jid}", cid, "jmp_customer_jid-#{cid}", jid
			).then do |redis_result|
				raise "Saving new customer to redis failed" unless redis_result == 1
				new(cid)
			end
		end
	end

	extend Forwardable

	attr_reader :customer_id, :balance

M lib/registration.rb => lib/registration.rb +3 -6
@@ 6,7 6,7 @@ require_relative "./oob"

class Registration
	def self.for(iq, customer, web_register_manager)
		EMPromise.resolve(customer&.registered?).then do |registered|
		customer.registered?.then do |registered|
			if registered
				Registered.new(iq, registered.phone)
			else


@@ 36,13 36,10 @@ class Registration

	class Activation
		def self.for(iq, customer, tel)
			if customer&.active?
			if customer.active?
				Finish.new(iq, customer, tel)
			elsif customer
				EMPromise.resolve(new(iq, customer, tel))
			else
				# Create customer_id
				raise "TODO"
				EMPromise.resolve(new(iq, customer, tel))
			end
		end


M sgx_jmp.rb => sgx_jmp.rb +1 -1
@@ 177,7 177,7 @@ end

command :execute?, node: "jabber:iq:register", sessionid: nil do |iq|
	Customer.for_jid(iq.from.stripped).catch {
		nil
		Customer.create(iq.from.stripped)
	}.then { |customer|
		Registration.for(
			iq,

M test/test_customer.rb => test/test_customer.rb +21 -0
@@ 3,6 3,7 @@
require "test_helper"
require "customer"

Customer::BRAINTREE = Minitest::Mock.new
Customer::REDIS = Minitest::Mock.new
Customer::DB = Minitest::Mock.new
CustomerPlan::DB = Minitest::Mock.new


@@ 49,6 50,26 @@ class CustomerTest < Minitest::Test
	end
	em :test_for_customer_id_not_found

	def test_create
		braintree_customer = Minitest::Mock.new
		Customer::BRAINTREE.expect(:customer, braintree_customer)
		braintree_customer.expect(:create, EMPromise.resolve(
			OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test"))
		))
		Customer::REDIS.expect(
			:msetnx,
			EMPromise.resolve(1),
			[
				"jmp_customer_id-test@example.com", "test",
				"jmp_customer_jid-test", "test@example.com"
			]
		)
		assert_kind_of Customer, Customer.create("test@example.com").sync
		braintree_customer.verify
		Customer::REDIS.verify
	end
	em :test_create

	def test_bill_plan_activate
		CustomerPlan::DB.expect(:transaction, nil) do |&block|
			block.call

M test/test_registration.rb => test/test_registration.rb +0 -7
@@ 54,13 54,6 @@ class RegistrationTest < Minitest::Test
	end
	em :test_for_not_activated_with_customer_id

	def test_for_not_activated_without_customer_id
		skip "customer_id creation not implemented yet"
		iq = Blather::Stanza::Iq::Command.new
		Registration.for(iq, nil, Minitest::Mock.new).sync
	end
	em :test_for_not_activated_without_customer_id

	class ActivationTest < Minitest::Test
		Registration::Activation::COMMAND_MANAGER = Minitest::Mock.new
		def setup