-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathsubscriptions_controller_test.rb
More file actions
118 lines (84 loc) · 3.22 KB
/
Copy pathsubscriptions_controller_test.rb
File metadata and controls
118 lines (84 loc) · 3.22 KB
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
116
117
118
require 'test_helper'
class SubscriptionsControllerTest < ActionController::TestCase
include Devise::Test::ControllerHelpers
test 'should list subscriptions for the logged-in user' do
sign_in users(:regular_user)
get :index
assert users(:regular_user).subscriptions.any?
assert_select '.subscription', count: users(:regular_user).subscriptions.count
end
test "should not list other user's subscriptions" do
sign_in users(:another_regular_user)
get :index
assert_select '.subscription', count: 0
end
test 'should not list subscriptions for anonymous user' do
get :index
assert_redirected_to new_user_session_path
end
test 'should create a new subscription' do
sign_in users(:regular_user)
assert_difference('Subscription.count') do
post :create, params: { subscription: { frequency: 'weekly', subscribable_type: 'Event' }, q: 'fish', country: 'Finland' }
end
assert_equal 'fish', assigns(:subscription).query
assert_equal ['country'], assigns(:subscription).facets.keys
assert_equal 'Finland', assigns(:subscription).facets['country']
assert_redirected_to subscriptions_path
end
test 'should not include junk params in new subscription' do
sign_in users(:regular_user)
assert_difference('Subscription.count') do
post :create, params: { subscription: { frequency: 'weekly', subscribable_type: 'Event' }, q: 'fish', bananas: 14,
country: 'Finland' }
end
assert_equal ['country'], assigns(:subscription).facets.keys
assert_redirected_to subscriptions_path
end
class EvilExceptionThrower
def initialize
raise 'die!'
end
end
test 'should not constantize arbitrary class' do
sign_in users(:regular_user)
assert_no_difference('Subscription.count') do
assert_nothing_raised do
post :create, params: { subscription: { frequency: 'weekly', subscribable_type: 'EvilExceptionThrower' } }
end
end
assert_redirected_to subscriptions_path
assert_includes flash[:error], 'Subscribable type not valid'
end
test 'should delete a subscription' do
sign_in users(:regular_user)
sub = subscriptions(:daily_subscription)
assert_difference('Subscription.count', - 1) do
delete :destroy, params: { id: sub }
end
assert_redirected_to subscriptions_path
end
test "should not delete someone else's subscription" do
sign_in users(:another_regular_user)
sub = subscriptions(:daily_subscription)
assert_no_difference('Subscription.count') do
delete :destroy, params: { id: sub }
end
assert_response :forbidden
end
test 'should delete a subscription via unsubcribe link' do
sub = subscriptions(:daily_subscription)
assert_difference('Subscription.count', -1) do
get :unsubscribe, params: { id: sub, code: sub.unsubscribe_code }
end
assert_response :success
end
test 'should not delete a subscription via unsubcribe link with invalid code' do
sub = subscriptions(:daily_subscription)
sub2 = subscriptions(:weekly_subscription)
assert_no_difference('Subscription.count') do
get :unsubscribe, params: { id: sub2, code: sub.unsubscribe_code }
end
assert_response :unprocessable_entity
end
end