~singpolyma/sgx-jmp

ref: bbc0bb6a11c1d459a1511e88bd80997d158e2603 sgx-jmp/test/test_command_list.rb -rw-r--r-- 2.7 KiB
bbc0bb6aStephen Paul Weber Refactor command list to use composition 1 year, 3 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# frozen_string_literal: true

require "test_helper"
require "command_list"

CommandList::Customer = Minitest::Mock.new
CommandList::REDIS = Minitest::Mock.new

class CommandListTest < Minitest::Test
	def test_for_no_customer
		CommandList::Customer.expect(
			:for_jid,
			EMPromise.reject("not found"),
			["none"]
		)
		assert_instance_of CommandList, CommandList.for("none").sync
	end
	em :test_for_no_customer

	def test_for_unregistered
		CommandList::Customer.expect(
			:for_jid,
			EMPromise.resolve(OpenStruct.new(registered?: false)),
			["unregistered"]
		)
		assert_instance_of CommandList, CommandList.for("unregistered").sync
	end
	em :test_for_unregistered

	def test_for_registered
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["catapult_fwd-1"]
		)
		CommandList::Customer.expect(
			:for_jid,
			EMPromise.resolve(OpenStruct.new(
				registered?: OpenStruct.new(phone: "1"),
				payment_methods: EMPromise.resolve([])
			)),
			["registered"]
		)
		assert_equal(
			["CommandList::Registered"],
			CommandList.for("registered").sync
			.class.ancestors.map(&:name).grep(/\ACommandList::/)
		)
	end
	em :test_for_registered

	def test_for_registered_with_fwd
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve("tel:1"),
			["catapult_fwd-1"]
		)
		CommandList::Customer.expect(
			:for_jid,
			EMPromise.resolve(OpenStruct.new(
				registered?: OpenStruct.new(phone: "1"),
				payment_methods: EMPromise.resolve([])
			)),
			["registered"]
		)
		assert_equal(
			CommandList::HAS_FORWARDING,
			CommandList::HAS_FORWARDING & CommandList.for("registered").sync.to_a
		)
	end
	em :test_for_registered_with_fwd

	def test_for_registered_with_credit_card
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve(nil),
			["catapult_fwd-1"]
		)
		CommandList::Customer.expect(
			:for_jid,
			EMPromise.resolve(OpenStruct.new(
				registered?: OpenStruct.new(phone: "1"),
				plan_name: "test",
				payment_methods: EMPromise.resolve([:boop])
			)),
			["registered"]
		)
		assert_equal(
			CommandList::HAS_CREDIT_CARD,
			CommandList::HAS_CREDIT_CARD & CommandList.for("registered").sync.to_a
		)
	end
	em :test_for_registered_with_credit_card

	def test_for_registered_with_forwarding_and_billing
		CommandList::REDIS.expect(
			:get,
			EMPromise.resolve("tel:1"),
			["catapult_fwd-1"]
		)
		CommandList::Customer.expect(
			:for_jid,
			EMPromise.resolve(OpenStruct.new(
				registered?: OpenStruct.new(phone: "1"),
				plan_name: "test",
				payment_methods: EMPromise.resolve([:boop])
			)),
			["registered"]
		)
		result = CommandList.for("registered").sync
		assert_kind_of CommandList::HasForwarding, result
		assert_kind_of CommandList::HasBilling, result
	end
	em :test_for_registered_with_forwarding_and_billing
end