public
Description: The open source social networking platform in Ruby on Rails from the author of RailsSpace
Homepage: http://insoshi.com
Clone URL: git://github.com/insoshi/insoshi.git
Search Repo:
insoshi / spec / controllers / people_controller_spec.rb
100644 273 lines (228 sloc) 8.349 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
require File.dirname(__FILE__) + '/../spec_helper'
 
describe PeopleController do
 
  before(:each) do
    @person = people(:quentin)
    photos = [mock_photo(:primary => true), mock_photo]
    photos.stub!(:find_all_by_primary).and_return(photos.select(&:primary?))
    @person.stub!(:photos).and_return(photos)
    login_as(:aaron)
  end
  
  describe "people pages" do
    integrate_views
        
    it "should have a working index" do
      get :index
      response.should be_success
      response.should render_template("index")
    end
 
    it "should have a working new page" do
      get :new
      response.should be_success
      response.should render_template("new")
    end
    
    it "should allow non-logged-in users to view new page" do
      logout
      get :new
      response.should be_success
    end
    
    it "should have a working show page" do
      get :show, :id => @person
      response.should be_success
      response.should render_template("show")
    end
    
    it "should have a working edit page" do
      login_as @person
      get :edit, :id => @person
      response.should be_success
      response.should render_template("edit")
    end
    
    it "should redirect to home for deactivated users" do
      @person.toggle!(:deactivated)
      get :show, :id => @person
      response.should redirect_to(home_url)
      flash[:error].should =~ /not active/
    end
    
    it "should redirect to home for email unverified users" do
      enable_email_notifications
      @person.email_verified = false; @person.save!
      @person.should_not be_active
      get :show, :id => @person
      response.should redirect_to(home_url)
      flash[:error].should =~ /not active/
    end
  end
  
  describe "create" do
    before(:each) do
      logout
    end
 
    it 'allows signup' do
      lambda do
        create_person
        response.should be_redirect
      end.should change(Person, :count).by(1)
    end
  
    it 'requires password on signup' do
      lambda do
        create_person(:password => nil)
        assigns[:person].errors.on(:password).should_not be_nil
        response.should be_success
      end.should_not change(Person, :count)
    end
  
    it 'requires password confirmation on signup' do
      lambda do
        create_person(:password_confirmation => nil)
        assigns[:person].errors.on(:password_confirmation).should_not be_nil
        response.should be_success
      end.should_not change(Person, :count)
    end
 
    it 'requires email on signup' do
      lambda do
        create_person(:email => nil)
        assigns[:person].errors.on(:email).should_not be_nil
        response.should be_success
      end.should_not change(Person, :count)
    end
    
    describe "email verifications" do
      
      before(:each) do
        logout
        @preferences = preferences(:one)
      end
      
      describe "when not verifying email" do
        it "should create an active user" do
          create_person
          assigns(:person).should_not be_deactivated
        end
      end
      
      describe "when verifying email" do
        
        before(:each) do
          @preferences.toggle!(:email_verifications)
        end
    
        it "should create a person with false email_verified" do
          person = create_person
          person.should_not be_deactivated
          person.should_not be_email_verified
          person.email_verifications.should_not be_empty
        end
        
        it "should have the right notice" do
          person = create_person
          flash[:notice].should =~ /activate your account/
          response.should redirect_to(home_url)
        end
        
        it "should verify a person" do
          person = create_person
          verification = assigns(:person).email_verifications.last
          get :verify_email, :id => verification.code
          person.reload.should_not be_deactivated
          person.should be_email_verified
          response.should redirect_to(person_path(person))
        end
        
        it "should not log the person in" do
          person = create_person
          controller.send(:logged_in?).should be_false
        end
          
        it "should not have an auth token" do
          create_person
          response.cookies["auth_token"].should == []
        end
        
        it "should verify a person even if they're logged in" do
          person = create_person
          login_as(person)
          verification = person.email_verifications.last
          get :verify_email, :id => verification.code
          person.reload.should_not be_deactivated
          response.should redirect_to(person_path(person))
        end
        
        it "should redirect home on failed verification" do
          get :verify_email, :id => "invalid"
          response.should redirect_to(home_url)
        end
      end
    end
  end
  
  describe "edit" do
    integrate_views
    
    before(:each) do
      @person = login_as(:quentin)
    end
    
    it "should render the edit page when photos are present" do
      get :edit, :id => @person
      response.should be_success
      response.should render_template("edit")
    end
    
    it "should allow mass assignment to name" do
      put :update, :id => @person, :person => { :name => "Foo Bar" },
                   :type => "info_edit"
      assigns(:person).name.should == "Foo Bar"
      response.should redirect_to(person_url(assigns(:person)))
    end
      
    it "should allow mass assignment to description" do
      put :update, :id => @person, :person => { :description => "Me!" },
                   :type => "info_edit"
      assigns(:person).description.should == "Me!"
      response.should redirect_to(person_url(assigns(:person)))
    end
    
    it "should render edit page on invalid update" do
      put :update, :id => @person, :person => { :email => "foo" },
                   :type => "info_edit"
      response.should be_success
      response.should render_template("edit")
    end
    
    it "should require the right authorized user" do
      login_as(:aaron)
      put :update, :id => @person
      response.should redirect_to(home_url)
    end
    
    it "should change the password" do
      current_password = @person.unencrypted_password
      newpass = "dude"
      put :update, :id => @person,
                   :person => { :verify_password => current_password,
                                :new_password => newpass,
                                :password_confirmation => newpass },
                   :type => "password_edit"
      response.should redirect_to(person_url(@person))
    end
  end
  
  describe "show" do
    integrate_views
    
    it "should display the edit link for current user" do
      login_as @person
      get :show, :id => @person
      response.should have_tag("a[href=?]", edit_person_path(@person))
    end
    
    it "should not display the edit link for other viewers" do
      login_as(:aaron)
      get :show, :id => @person
      response.should_not have_tag("a[href=?]", edit_person_path(@person))
    end
    
    it "should not display the edit link for non-logged-in viewers" do
      logout
      get :show, :id => @person
      response.should_not have_tag("a[href=?]", edit_person_path(@person))
    end
    
    it "should not display a deactivated person" do
      @person.toggle!(:deactivated)
      get :show, :id => @person
      response.should redirect_to(home_url)
    end
    
    it "should display break up link if connected" do
      login_as(@person)
      @contact = people(:aaron)
      conn = Connection.connect(@person, @contact)
      get :show, :id => @contact.reload
      response.should have_tag("a", :text => "end connection")
      response.should have_tag("a[href=?]", connection_path(conn))
    end
    
    it "should not display break up link if not connected" do
      login_as(@person)
      @contact = people(:aaron)
      get :show, :id => @contact.reload
      response.should_not have_tag("a", :text => "end connection")
    end
  end
  
  private
 
    def create_person(options = {})
      post :create, :person => { :name => "Quire",:email => 'quire@foo.com',
        :password => 'quux', :password_confirmation => 'quux' }.merge(options)
      assigns(:person)
    end
end