# frozen_string_literal: true require "test_helper" require "customer_repo" class CustomerRepoTest < Minitest::Test FAKE_REDIS = FakeRedis.new( # sgx-jmp customer "jmp_customer_jid-test" => "test@example.com", "jmp_customer_id-test@example.com" => "test", "catapult_jid-+13334445555" => "customer_test@jmp.chat", "catapult_cred-customer_test@jmp.chat" => [ "test_bw_customer", "", "", "+13334445555" ], # sgx-jmp customer, empty DB "jmp_customer_jid-empty" => "empty@example.com", "jmp_customer_id-empty@example.com" => "empty", "catapult_jid-+16667778888" => "customer_empty@jmp.chat", "catapult_cred-customer_empty@jmp.chat" => [ "test_bw_customer", "", "", "+16667778888" ], # v2 customer "jmp_customer_jid-test_v2" => "test_v2@example.com", "jmp_customer_id-test_v2@example.com" => "test_v2", "catapult_jid-+14445556666" => "test_v2@example.com", "catapult_cred-test_v2@example.com" => [ "test_bw_customer", "", "", "+14445556666" ], # legacy customer "catapult_cred-legacy@example.com" => [ "catapult_user", "", "", "+12223334444" ], "catapult_jid-+12223334444" => "legacy@example.com" ) FAKE_DB = FakeDB.new( ["test"] => [{ "balance" => BigDecimal(1234), "plan_name" => "test_usd", "expires_at" => Time.now + 100 }], ["test_v2"] => [{ "balance" => BigDecimal(2345), "plan_name" => "test_usd", "expires_at" => Time.now + 100 }] ) def mkrepo( redis: FAKE_REDIS, db: FAKE_DB, braintree: Minitest::Mock.new ) CustomerRepo.new(redis: redis, db: db, braintree: braintree) end def setup @repo = mkrepo end def test_find_by_jid customer = @repo.find_by_jid("test@example.com").sync assert_kind_of Customer, customer assert_equal 1234, customer.balance assert_equal "merchant_usd", customer.merchant_account end em :test_find_by_jid def test_find_by_id customer = @repo.find("test").sync assert_kind_of Customer, customer assert_equal 1234, customer.balance assert_equal "merchant_usd", customer.merchant_account end em :test_find_by_id def test_find_by_customer_jid customer = @repo.find_by_jid("customer_test@jmp.chat").sync assert_kind_of Customer, customer assert_equal 1234, customer.balance assert_equal "merchant_usd", customer.merchant_account end em :test_find_by_customer_jid def test_find_by_jid_not_found assert_raises do @repo.find_by_jid("test2@example.com").sync end end em :test_find_by_jid_not_found def test_find_legacy_customer customer = @repo.find_by_jid("legacy@example.com").sync assert_kind_of LegacyCustomer, customer assert_equal "+12223334444", customer.tel end em :test_find_legacy_customer def test_find_sgx_customer_by_phone customer = @repo.find_by_tel("+13334445555").sync assert_kind_of Customer, customer assert_equal "test", customer.customer_id end em :test_find_sgx_customer_by_phone def test_find_v2_customer_by_phone customer = @repo.find_by_tel("+14445556666").sync assert_kind_of Customer, customer assert_equal "test_v2", customer.customer_id end em :test_find_v2_customer_by_phone def test_find_legacy_customer_by_phone customer = @repo.find_by_tel("+12223334444").sync assert_kind_of LegacyCustomer, customer assert_equal "legacy@example.com", customer.jid.to_s end em :test_find_legacy_customer_by_phone def test_find_missing_phone assert_raises do @repo.find_by_tel("+15556667777").sync end end em :test_find_missing_phone def test_find_db_empty customer = @repo.find("empty").sync assert_equal BigDecimal(0), customer.balance end em :test_find_db_empty def test_create redis = Minitest::Mock.new braintree = Minitest::Mock.new repo = mkrepo(redis: redis, braintree: braintree) braintree_customer = Minitest::Mock.new braintree.expect(:customer, braintree_customer) braintree_customer.expect(:create, EMPromise.resolve( OpenStruct.new(success?: true, customer: OpenStruct.new(id: "test")) )) redis.expect( :msetnx, EMPromise.resolve(1), [ "jmp_customer_id-test@example.com", "test", "jmp_customer_jid-test", "test@example.com" ] ) assert_kind_of Customer, repo.create("test@example.com").sync assert_mock braintree assert_mock braintree_customer assert_mock redis end em :test_create end