1
1
require 'test_helper'
2
- require 'stripe_mock'
3
- require 'minitest/mock'
4
2
5
3
class Pay ::Billable ::Test < ActiveSupport ::TestCase
6
4
setup do
7
- StripeMock . start
8
-
9
5
@billable = User . new
10
- @stripe_helper = StripeMock . create_test_helper
11
- @stripe_helper . create_plan ( id : 'test-monthly' , amount : 1500 )
12
- end
13
-
14
- teardown do
15
- StripeMock . stop
16
6
end
17
7
18
8
test 'truth' do
@@ -23,48 +13,120 @@ class Pay::Billable::Test < ActiveSupport::TestCase
23
13
assert @billable . respond_to? ( :subscriptions )
24
14
end
25
15
26
- test 'can create a subscription' do
27
- @billable . card_token = @stripe_helper . generate_card_token (
28
- brand : 'Visa' ,
29
- last4 : '9191' ,
30
- exp_year : 1984
31
- )
32
- @billable . subscribe ( 'default' , 'test-monthly' )
16
+ test 'customer with stripe processor' do
17
+ @billable . processor = 'stripe'
18
+ @billable . expects ( :stripe_customer ) . returns ( :user )
19
+ assert_equal :user , @billable . customer
20
+ end
33
21
34
- assert @billable . subscribed?
35
- assert @billable . subscription . name == 'default'
36
- assert @billable . subscription . processor_plan == 'test-monthly'
22
+ test 'customer with undefined processor' do
23
+ @billable . processor = 'pants'
24
+
25
+ assert_raises NoMethodError do
26
+ @billable . customer
27
+ end
37
28
end
38
29
39
- test 'cannot update their card without a prcocessor' do
40
- assert_raise StandardError do
41
- card = @stripe_helper . generate_card_token ( brand : 'Visa' , last4 : '4242' )
42
- @billable . update_card ( card )
30
+ test 'customer without processor' do
31
+ assert_raises StandardError do
32
+ @billable . customer
43
33
end
44
34
end
45
35
46
- test 'can update their card' do
47
- customer = Stripe ::Customer . create (
48
- email : 'johnny@appleseed.com' ,
49
- card : @stripe_helper . generate_card_token
36
+ test 'subscribing a stripe customer' do
37
+ @billable . expects ( :create_stripe_subscription )
38
+ . with ( 'default' , 'default' )
39
+ . returns ( :user )
40
+
41
+ assert_equal :user , @billable . subscribe
42
+ assert @billable . processor = 'stripe'
43
+ end
44
+
45
+ test 'updating a stripe card' do
46
+ @billable . processor = 'stripe'
47
+ @billable . expects ( :update_stripe_card ) . with ( 'a1b2c3' ) . returns ( :card )
48
+
49
+ assert_equal :card , @billable . update_card ( 'a1b2c3' )
50
+ end
51
+
52
+ test 'updating a card without a processor' do
53
+ assert_raises StandardError do
54
+ @billable . update_card ( 'whoops' )
55
+ end
56
+ end
57
+
58
+ test 'checking for a subscription without one' do
59
+ @billable . stubs ( :subscription ) . returns ( nil )
60
+ refute @billable . subscribed?
61
+ end
62
+
63
+ test 'checking for a subscription with no plan and active subscription' do
64
+ subscription = mock ( 'subscription' )
65
+ subscription . stubs ( :active? ) . returns ( true )
66
+ @billable . stubs ( :subscription ) . returns ( subscription )
67
+
68
+ assert @billable . subscribed?
69
+ end
70
+
71
+ test 'checking for a subscription with no plan and inactive subscription' do
72
+ subscription = mock ( 'subscription' )
73
+ subscription . stubs ( :active? ) . returns ( false )
74
+ @billable . stubs ( :subscription ) . returns ( subscription )
75
+
76
+ refute @billable . subscribed?
77
+ end
78
+
79
+ test 'checking for a subscription that is inactive' do
80
+ subscription = mock ( 'subscription' )
81
+ subscription . stubs ( :active? ) . returns ( false )
82
+ @billable . stubs ( :subscription ) . returns ( subscription )
83
+
84
+ refute @billable . subscribed? ( 'default' , 'default' )
85
+ end
86
+
87
+ test 'checking for a subscription that is active for another plan' do
88
+ subscription = mock ( 'subscription' )
89
+ subscription . stubs ( :active? ) . returns ( true )
90
+ subscription . stubs ( :plan ) . returns ( 'superior' )
91
+ @billable . stubs ( :subscription ) . returns ( subscription )
92
+
93
+ refute @billable . subscribed? ( 'default' , 'default' )
94
+ end
95
+
96
+ test 'checking for a subscription that is active for a provided plan' do
97
+ subscription = mock ( 'subscription' )
98
+ subscription . stubs ( :active? ) . returns ( true )
99
+ subscription . stubs ( :plan ) . returns ( 'default' )
100
+ @billable . stubs ( :subscription ) . returns ( subscription )
101
+
102
+ assert @billable . subscribed? ( 'default' , 'default' )
103
+ end
104
+
105
+ test 'getting a subscription by default name' do
106
+ subscription = Subscription . create! (
107
+ name : 'default' ,
108
+ owner : @billable ,
109
+ processor : 'stripe' ,
110
+ processor_id : '1' ,
111
+ processor_plan : 'default' ,
112
+ quantity : '1'
50
113
)
51
114
52
- @billable . stub :customer , customer do
53
- card = @stripe_helper . generate_card_token ( brand : 'Visa' , last4 : '4242' )
54
- @billable . processor = 'stripe'
55
- @billable . update_card ( card )
115
+ assert_equal subscription , @billable . subscription
116
+ end
56
117
57
- assert @billable . card_brand == 'Visa'
58
- assert @billable . card_last4 == '4242'
118
+ test 'getting a stripe subscription' do
119
+ @billable . processor = 'stripe'
120
+ @billable . expects ( :stripe_subscription )
121
+ . with ( '123' )
122
+ . returns ( :subscription )
59
123
60
- card = @stripe_helper . generate_card_token (
61
- brand : 'Discover' ,
62
- last4 : '1117'
63
- )
64
- @billable . update_card ( card )
124
+ assert_equal :subscription , @billable . processor_subscription ( '123' )
125
+ end
65
126
66
- assert @billable . card_brand == 'Discover'
67
- assert @billable . card_last4 == '1117'
127
+ test 'getting a processor subscription without a processor' do
128
+ assert_raises StandardError do
129
+ @billable . processor_subscription ( '123' )
68
130
end
69
131
end
70
132
end
0 commit comments