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:
Michael Hartl (author)
Mon Apr 07 21:45:27 -0700 2008
commit  989780e15cf52d7d15cd44ef9971643c18035dfa
tree    e6042a6be31e9a3c422d3d4239e16c065c2ed607
parent  f39f63da0207579e54c7d8acddf95fadc8620bf3
insoshi / spec / controllers / people_controller_spec.rb
100644 171 lines (143 sloc) 4.968 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
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 require login" do
      logout
      get :index
      response.should redirect_to(login_url)
    end
    
    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
  end
  
  describe "signup" 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
  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
  end
  
  private
 
    def create_person(options = {})
      post :create, :person => { :name => "Quire",:email => 'quire@foo.com',
        :password => 'quux', :password_confirmation => 'quux' }.merge(options)
    end
end