-
Notifications
You must be signed in to change notification settings - Fork 1
/
users_controller_spec.rb
209 lines (179 loc) · 6.01 KB
/
users_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
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
201
202
203
204
205
206
207
208
209
require File.dirname(__FILE__) + '/../spec_helper'
describe UsersController do
it 'allows signup' do
@user = User.make
User.should_receive(:new).and_return(@user)
@user.should_receive(:save).at_least(:once).and_return(true)
create_user
end
# Uncomment this spec when we set up e-mail activation.
=begin
it 'signs up user with activation code' do
create_user
assigns(:user).activation_code.should_not be_nil
end
=end
it 'requires password on signup' do
lambda do
create_user(:password => nil)
assigns[:user].errors.on(:password).should_not be_nil
response.should be_success
end.should_not change(User, :count)
end
it 'requires password confirmation on signup' do
lambda do
create_user(:password_confirmation => nil)
assigns[:user].errors.on(:password_confirmation).should_not be_nil
response.should be_success
end.should_not change(User, :count)
end
it 'requires email on signup' do
lambda do
create_user(:email => nil)
assigns[:user].errors.on(:email).should_not be_nil
response.should be_success
end.should_not change(User, :count)
end
it 'activates user' do
pending "meaningless until we start doing activation" do
email = 'aaron@example.com'
password = 'test'
aaron = User.make(:inactive, :email => email, :password => password)
User.authenticate(email, password).should be_nil
get :activate, :activation_code => aaron.activation_code
response.should redirect_to('/login')
flash[:notice].should_not be_nil
User.authenticate(email, password).should == aaron
end
end
it 'does not activate user without key' do
pending "meaningless until we start doing activation" do
get :activate
flash[:notice].should be_nil
end
end
it 'does not activate user with blank key' do
pending "meaningless until we start doing activation" do
get :activate, :activation_code => ''
flash[:notice].should be_nil
end
end
def create_user(options = {})
post :create, :user => { :login => 'quire', :email => 'quire@example.com',
:password => 'quire', :password_confirmation => 'quire', :permissions => [mock_model(Permission)] }.merge(options)
end
end
describe UsersController, "edit" do
before(:each) do
@user = User.make
UserSession.create @user
get :edit
end
it "should reuse the 'new' form" do
response.should render_template :new
end
it "should get the current user" do
assigns[:user].should == User.current_user
end
it "should not require password validation if both password fields are nil" do
test_user = User.make
my_attr = test_user.attributes
my_attr[:password] = nil
my_attr[:password_confirmation] = nil
User.stub!(:current_user).and_return(test_user)
post :edit, :user => my_attr
test_user.errors.should be_empty
end
it "should validate password if at least one password field is supplied" do
pending "could this ever have worked?!?" do
my_attr = @user.attributes
@user[:password] = 'a'
@user[:password_confirmation] = nil
my_attr.should_not be_nil
post :edit, @user
@user.errors.should_not be_empty
@user = User.make
UserSession.create @user
get :edit
my_attr = @user.attributes
my_attr[:password] = nil
my_attr[:password_confirmation] = 'a'
my_attr.should_not be_nil
post :edit, :user => my_attr
@user.errors.should_not be_empty
end
end
it "should set coords to nil" do
post :edit, :user => @user.attributes
pending "Can this really work? Won't coords just be set as soon as it's called?" do
@user.coords.should be_nil
end
end
end
describe UsersController, '(regenerate_key)' do
before(:each) do
request.env['HTTP_REFERER'] = 'http://test.host/referer' # so redirect_to :back works
# controller.stub!(:require_user).and_return(true)
end
it "should be a valid action" do
UserSession.create User.make
get :regenerate_key
response.should redirect_to(:back)
end
it "should reset the current user's single_access_token" do
pending "spec fails, but app works -- why?" do
@user = User.make
token = @user.single_access_token
UserSession.create @current_user
get :regenerate_key
User.find(UserSession.find.record.id).single_access_token.should_not == token
end
end
it 'should set flash[:notice] on success' do
UserSession.create User.make
get :regenerate_key
flash[:notice].should_not be_nil
end
it 'should set flash[:error] on failure' do
pending "does Authlogic handle errors and/or authentication strangely?" do
@current_user = User.make
@current_user.should_receive(:save!).and_raise(ActiveRecord::RecordInvalid.new(@current_user))
UserSession.create @current_user
get :regenerate_key
flash[:error].should_not be_nil
end
end
end
describe UsersController, '(reset)' do
integrate_views
before(:each) do
get :reset
end
it "should be a valid action" do
response.should be_success
end
it "should display a form asking for e-mail address, with a submit button" do
response.should have_tag('input[type=text]')
response.should have_tag('input[type=submit]')
end
it "should set the page title" do
assigns[:page_title].should_not be_blank
end
end
describe UsersController, '(reset/POST)' do
integrate_views
it "should give an error message if e-mail isn't valid" do
User.should_receive(:find_by_email).and_return(nil)
post :reset, :email => 'someone@example.com'
flash[:error].should_not be_nil
end
it "should reset password if e-mail is valid" do
@user = User.make
@user.should_receive(:reset_password!).and_return(true)
User.should_receive(:find_by_email).and_return(@user)
UserMailer.should_receive(:deliver_reset).with(@user).at_least(:once).and_return(true)
post :reset, :email => @user.email
flash[:error].should be_nil
flash[:notice].should_not be_nil
end
end