users_controller_spec.rb
5.52 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
require 'spec_helper'
describe UsersController do
it_requires_authentication
it_requires_admin_privileges :for => {
:index => :get,
:show => :get,
:new => :get,
:create => :post,
:destroy => :delete
}
context 'Signed in as a regular user' do
before do
sign_in @user = Factory(:user)
end
context "GET /users/:other_id/edit" do
it "redirects to the home page" do
get :edit, :id => Factory(:user).id
response.should redirect_to(root_path)
end
end
context "GET /users/:my_id/edit" do
it 'finds the user' do
get :edit, :id => @user.id
assigns(:user).should == @user
end
end
context "PUT /users/:other_id" do
it "redirects to the home page" do
put :update, :id => Factory(:user).id
response.should redirect_to(root_path)
end
end
context "PUT /users/:my_id/id" do
context "when the update is successful" do
it "sets a message to display" do
put :update, :id => @user.to_param, :user => {:name => 'Kermit'}
request.flash[:success].should include('updated')
end
it "redirects to the user's page" do
put :update, :id => @user.to_param, :user => {:name => 'Kermit'}
response.should redirect_to(user_path(@user))
end
it "should not be able to become an admin" do
put :update, :id => @user.to_param, :user => {:admin => true}
@user.reload.admin.should be_false
end
end
context "when the update is unsuccessful" do
it "renders the edit page" do
put :update, :id => @user.to_param, :user => {:name => nil}
response.should render_template(:edit)
end
end
end
end
context 'Signed in as an admin' do
before do
sign_in Factory(:admin)
end
context "GET /users" do
it 'paginates all users' do
users = 3.times.inject(WillPaginate::Collection.new(1,30)) {|page,_| page << Factory.build(:user)}
User.should_receive(:paginate).and_return(users)
get :index
assigns(:users).should == users
end
end
context "GET /users/:id" do
it 'finds the user' do
user = Factory(:user)
get :show, :id => user.id
assigns(:user).should == user
end
end
context "GET /users/new" do
it 'assigns a new user' do
get :new
assigns(:user).should be_a(User)
assigns(:user).should be_new_record
end
end
context "GET /users/:id/edit" do
it 'finds the user' do
user = Factory(:user)
get :edit, :id => user.id
assigns(:user).should == user
end
end
context "POST /users" do
context "when the create is successful" do
before do
@attrs = {:user => Factory.attributes_for(:user)}
end
it "sets a message to display" do
post :create, @attrs
request.flash[:success].should include('part of the team')
end
it "redirects to the user's page" do
post :create, @attrs
response.should redirect_to(user_path(assigns(:user)))
end
it "should be able to create admin" do
@attrs[:user][:admin] = true
post :create, @attrs
response.should be_redirect
User.find(assigns(:user).to_param).admin.should be_true
end
end
context "when the create is unsuccessful" do
before do
@user = Factory(:user)
User.should_receive(:new).and_return(@user)
@user.should_receive(:save).and_return(false)
end
it "renders the new page" do
post :create
response.should render_template(:new)
end
end
end
context "PUT /users/:id" do
context "when the update is successful" do
before do
@user = Factory(:user)
end
it "sets a message to display" do
put :update, :id => @user.to_param, :user => {:name => 'Kermit'}
request.flash[:success].should include('updated')
end
it "redirects to the user's page" do
put :update, :id => @user.to_param, :user => {:name => 'Kermit'}
response.should redirect_to(user_path(@user))
end
it "should be able to make user an admin" do
put :update, :id => @user.to_param, :user => {:admin => true}
response.should be_redirect
User.find(assigns(:user).to_param).admin.should be_true
end
end
context "when the update is unsuccessful" do
before do
@user = Factory(:user)
end
it "renders the edit page" do
put :update, :id => @user.to_param, :user => {:name => nil}
response.should render_template(:edit)
end
end
end
context "DELETE /users/:id" do
before do
@user = Factory(:user)
end
it "destroys the user" do
delete :destroy, :id => @user.id
User.where(:id => @user.id).first.should be_nil
end
it "redirects to the users index page" do
delete :destroy, :id => @user.id
response.should redirect_to(users_path)
end
it "sets a message to display" do
delete :destroy, :id => @user.id
request.flash[:success].should include('no longer part of your team')
end
end
end
end