/
permissions_controller_spec.rb
163 lines (136 loc) · 5.57 KB
/
permissions_controller_spec.rb
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
include ActionView::Helpers::UrlHelper
describe PermissionsController do
#Delete this example and add some real ones
it "should use PermissionsController" do
controller.should be_an_instance_of(PermissionsController)
end
end
describe PermissionsController, 'index' do
integrate_views
before(:each) do
@user = Role.make(:name => 'user')
@admin = Role.make(:name => 'admin')
@current_user = User.make(:email => 'johndoe@example.com')
@one = Calendar.make(:name => 'Calendar 1')
@two = Calendar.make(:name => 'Calendar 2')
@three = Calendar.make(:name => 'Calendar 3')
[{:role => @user, :calendar => @one}, {:role => @admin, :calendar => @two}].each do |opts|
@current_user.permissions.make opts
end
UserSession.create @current_user
end
it 'should be a valid action' do
get :index
response.should be_success
end
it 'should set the page title' do
get :index
assigns[:page_title].should_not be_nil
end
it "should get all of the current user's permissions" do
get :index
assigns[:permissions].should == @current_user.permissions
end
it 'should list each subscribed calendar by name in a table' do
get :index
@current_user.permissions.each do |p|
cal = p.calendar
response.should have_tag("tr#permission_#{p.id} td#calendar_#{cal.id}", %r{#{Regexp.escape(ERB::Util::html_escape(cal))}})
end
end
it 'should list the role for each subscribed calendar in the table' do
get :index
@current_user.permissions.each do |p|
response.should have_tag("tr#permission_#{p.id} td", %r{#{Regexp.escape(ERB::Util::html_escape(p.role))}})
end
end
it 'should have an unsubscribe link for each non-admin subscription' do
admin, nonadmin = @current_user.permissions.partition{|p| p.role_id == @admin.id}.collect{|a| a[0]}
get :index
response.should have_tag("tr#permission_#{nonadmin.id} td.actions a.unsubscribe[href=#{url_for(:action => 'destroy', :id => nonadmin.id, :escape => false)}]")
response.should_not have_tag("tr#permission_#{admin.id} td.actions a.unsubscribe[href=#{url_for(:action => 'destroy', :id => admin.id, :escape => false)}]")
end
it 'should show a list of unsubscribed calendars to subscribe to' do
@current_user.permissions.find_by_calendar_id(@three.id).should be_nil
get :index
response.should have_tag("table.unsubscribed tr#calendar_#{@three.id}") do |r|
r.should have_tag("a[href=#{subscribe_path(:calendar_id => @three.id)}]")
end
[@one, @two].each do |c|
response.should_not have_tag("table.unsubscribed tr#calendar_#{c.id}")
end
end
end
describe PermissionsController, 'index (no subscribed calendars)' do
integrate_views
before(:each) do
controller.stub!(:login_required).and_return(true)
end
it 'should show all available calendars under "unsubscribed"' do
@current_user = User.make(:id => 20, :email => 'no_permissions@gmail.com')
2.times do
Calendar.make
end
UserSession.create @current_user
get :index
assigns[:permissions].should be_empty
response.should have_tag('table.unsubscribed')
end
end
describe PermissionsController, 'index (no unsubscribed calendars)' do
it 'should not show the list of unsubscribed calendars if there are none' do
Calendar.count.should == 0
get :index
assigns[:unsubscribed].should be_nil
response.should_not have_tag('table.unsubscribed')
end
end
describe PermissionsController, 'subscribe' do
before(:each) do
@calendar = Calendar.make
@user = Role.make(:id => 20, :name => 'user')
@params = {:calendar_id => @calendar.id}
@current_user = User.make(:email => 'test@example.com', :id => 18)
controller.stub!(:check_admin).and_return(false)
UserSession.create @current_user
end
it 'should be a valid non-admin action' do
get :subscribe, :calendar_id => @calendar.id
response.should be_redirect
end
it 'should create a new permission for the current user and the given calendar' do
@pcount = Permission.count
get :subscribe, :calendar_id => @calendar.id
Permission.count.should == @pcount + 1
Permission.find(:first, :conditions => {:calendar_id => @calendar.id, :user_id => @current_user.id, :role_id => @user.id}).should_not be_nil
end
end
describe PermissionsController, 'destroy' do
before(:each) do
controller.stub!(:login_required).and_return(true)
@current_user = User.make(:id => 12, :email => 'johndoe@example.com')
@current_user.stub!(:admin?).and_return false
UserSession.create @current_user
request.env['HTTP_REFERER'] = 'referer'
end
it 'should be a valid non-admin action' do
Permission.stub!(:find).and_return(mock_model(Permission, :null_object => true))
controller.should_not_receive(:check_admin)
get :destroy, :id => 1
end
it 'should delete the Permission given by the id parameter if it belongs to the current user' do
@mine = @current_user.permissions.make(:id => 90)
@mine.should_receive(:destroy)
Permission.should_receive(:find).with(@mine.id.to_param).and_return(@mine)
get :destroy, :id => @mine.id
end
it 'should not delete the Permission given by the id parameter if it does not belong to the current user' do
@notmine = User.make.permissions.make(:id => 91)
@notmine.should_not_receive(:destroy)
Permission.should_receive(:find).with(@notmine.id.to_param).and_return(@notmine)
get :destroy, :id => @notmine.id
response.should be_redirect
flash[:error].should_not be_blank
end
end