public
Description: Open Source Social Network written in Ruby on Rail by Less Everything
Homepage: http://lovdbyless.com
Clone URL: git://github.com/stevenbristol/lovd-by-less.git
lovd-by-less / test / functional / profiles_controller_test.rb
100644 221 lines (170 sloc) 6.815 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
require File.dirname(__FILE__) + '/../test_helper'
 
class ProfilesControllerTest < ActionController::TestCase
 
  context 'on POST to :search' do
    setup do
      post :search, {:q => 'user'}
    end
 
    should_assign_to :results
    should_respond_with :success
    should_render_template :search
  end
 
  context 'on GET to :index' do
    setup do
      get :index
    end
 
    should_assign_to :results
    should_respond_with :success
    should_render_template :search
  end
 
  context 'on GET to :show while not logged in' do
    setup do
      get :show, {:id => profiles(:user).id}
      assert_match "Sign-up to Follow", @response.body
    end
 
    should_assign_to :user
    should_assign_to :profile
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash
  end
 
  context 'on GET to :show.rss while not logged in' do
    setup do
      get :show, {:id => profiles(:user).id, :format=>'rss'}
      assert_match "<rss version=\"2.0\">\n <channel>\n <title>#{SITE_NAME} Activity Feed</title>", @response.body
    end
 
    should_assign_to :user
    should_assign_to :profile
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash
  end
 
  context 'on GET to :edit while not logged in' do
    setup do
      get :edit, {:id => profiles(:user).id}
    end
 
    should_not_assign_to :user
    should_respond_with :redirect
    should_redirect_to 'login_path'
    should_not_set_the_flash
  end
 
 
  context 'on GET to :show while logged in' do
    setup do
        get :show, {:id => profiles(:user).id}, {:user => profiles(:user).id}
    end
 
    should_assign_to :user
    should_assign_to :profile
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash
  end
 
  context 'on GET to :show while logged in as :user3' do
    setup do
      get :show, {:id => profiles(:user).id}, {:user => profiles(:user3).id}
      assert profiles(:user3).followed_by?(profiles(:user))
      assert_match "Be Friends", @response.body
    end
 
    should_assign_to :user
    should_assign_to :profile
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash
  end
 
  context 'on GET to :show while logged in as :user2' do
    setup do
      get :show, {:id => profiles(:user3).id}, {:user => profiles(:user2).id}
      assert_match "Start Following", @response.body
    end
 
    should_assign_to :user
    should_assign_to :profile
    should_respond_with :success
    should_render_template :show
    should_not_set_the_flash
  end
 
 
  context 'on GET to :edit while logged in' do
    setup do
        get :edit, {:id => profiles(:user).id}, {:user => profiles(:user).id}
      end
 
    should_assign_to :user
    should_assign_to :profile
    should_respond_with :success
    should_render_template :edit
    should_render_a_form
    should_not_set_the_flash
  end
  
  context 'rendering an avatar' do
    
    should 'use the user\'s icon if it exists' do
      p = profiles(:user)
      p.icon = File.new(File.join(RAILS_ROOT, ['test', 'public','images','user.png']))
      p.save!
      #raise (p.send :icon_state).inspect
      assert_not_nil p.icon
      get :show, {:id => p.id, :public_view => true}, {:user => p.id}
      assert_tag :img, :attributes => { :src => /\/system\/profile\/icon\/\d*\/big\/user.png/ }
    end
    
    should 'use gravatar otherwise' do
      p = profiles(:user2)
      assert_nil p.icon
      get :show, {:id => p.id}, {:user => p.id, :public_view => true}
      assert_tag :img, :attributes => {:src => /www\.gravatar\.com/}
    end
    
    should 'send the app\'s internal default as the default to gravatar' do
      p = profiles(:user2)
      assert_nil p.icon
      get :show, {:id => p.id}, {:user => p.id, :public_view => true}
      assert_tag :img, :attributes => { :src => /http...www.gravatar.com\/avatar\/[0-9a-f]+\?size\=50&amp;default\=http...test\.host\/images\/avatar_default_small\.png/ }
    end
  end
 
 
  context 'on POST to :delete_icon' do
    should 'delete the icon from the users profile' do
      assert_not_nil profiles(:user).icon
      post :delete_icon, {:id => profiles(:user).id, :format => 'js'}, {:user => profiles(:user).id}
      assert_response :success
      assert_nil assigns(:p).reload.icon
    end
  end
 
 
  context 'on POST to :update' do
    should 'update a user\'s profile with good data when logged in' do
      assert_equal 'De', profiles(:user).first_name
 
        post :update, {:id => profiles(:user).id, :user => {:email => 'user@example.com'}, :profile => {:first_name => 'Bob'}, :switch => 'name'}, {:user => profiles(:user).id}
 
      assert_response :redirect
      assert_redirected_to edit_profile_path(profiles(:user).reload)
      assert_equal 'Settings have been saved.', flash[:notice]
 
      assert_equal 'Bob', profiles(:user).reload.first_name
    end
 
    should 'not update a user\'s profile with bad data when logged in' do
        post :update, {:id => profiles(:user).id, :profile => {:email => ''}, :switch => 'name'}, {:user => profiles(:user).id}
 
      assert_response :success
      assert_template 'edit'
      assert_not_nil assigns(:profile).errors
    end
 
    should 'not update a user\'s profile without a switch' do
      assert_equal 'De', profiles(:user).first_name
 
        post :update, {:id => profiles(:user).id, :user => {:email => 'user@example.com'}, :profile => {:first_name => 'Bob'}}, {:user => profiles(:user).id}
 
      assert_equal 'De', profiles(:user).first_name
 
      assert_response :success
    end
 
    should 'update a user\'s password with good data when logged in' do
      pass = users(:user).crypted_password
 
        post :update, {:id => profiles(:user).id, :verify_password => 'test', :new_password => '1234', :confirm_password => '1234', :switch => 'password'}, {:user => profiles(:user).id}
 
      assert_response :redirect
      assert_redirected_to edit_profile_path(profiles(:user))
      assert_equal 'Password has been changed.', flash[:notice]
 
      assert_not_equal pass, assigns(:u).reload.crypted_password
    end
 
    should 'not update a user\'s password with bad data when logged in' do
      pass = users(:user).crypted_password
 
      post :update, {:id => profiles(:user).id, :verify_password => 'test', :new_password => '4321', :confirm_password => '1234', :switch => 'password'}, {:user => profiles(:user).id}
 
      assert_response :success
      assert_template 'edit'
      assert_not_nil assigns(:user).errors
    end
 
  end
 
 
  should "delete" do
    assert_difference 'User.count', -1 do
      assert users(:user)
      delete :destroy, {:id=>users(:user).id}, {:user, users(:user).id}
      assert_response 200
      assert_nil User.find_by_id(users(:user).id)
    end
  end
 
 
end