browse_controller_test.rb
9.92 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
274
275
276
277
require File.dirname(__FILE__) + '/../test_helper'
require 'browse_controller'
# Re-raise errors caught by the controller.
class BrowseController; def rescue_action(e) raise e end; end
class BrowseControllerTest < Test::Unit::TestCase
def setup
@controller = BrowseController.new
@request = ActionController::TestRequest.new
@request.stubs(:ssl?).returns(false)
@response = ActionController::TestResponse.new
# By pass user validation on person creation
user = mock()
user.stubs(:id).returns(1)
user.stubs(:valid?).returns(true)
user.stubs(:email).returns('some@test.com')
user.stubs(:save!).returns(true)
Person.any_instance.stubs(:user).returns(user)
end
should 'search for people' do
Person.delete_all
small = create(Person, :name => 'A small person for testing', :user_id => 1)
create(Person, :name => 'A big person for testing', :user_id => 2)
get :people, :query => 'small'
assert_equal [small], assigns(:results)
end
should 'list all people order by more recent one by default' do
Person.delete_all
p1 = create(Person, :name => 'Testing person 1', :user_id => 1, :created_at => DateTime.now - 2)
p2 = create(Person, :name => 'Testing person 2', :user_id => 2, :created_at => DateTime.now - 1)
p3 = create(Person, :name => 'Testing person 3', :user_id => 3)
get :people
assert_equal [p3,p2,p1] , assigns(:results)
end
should 'paginate search of people in groups of 27' do
Person.delete_all
1.upto(30).map do |n|
create(Person, :name => 'Testing person', :user_id => n)
end
get :people
assert_equal 30 , Person.count
assert_equal 27 , assigns(:results).count
assert_tag :a, '', :attributes => {:class => 'next_page'}
end
should 'paginate ferret search of people in groups of 27' do
Person.delete_all
1.upto(30).map do |n|
create(Person, :name => 'Testing person', :user_id => n)
end
get :people, :query => 'Testing'
assert_equal 27 , assigns(:results).count
assert_tag :a, '', :attributes => {:class => 'next_page'}
end
should 'not return nil results in the more_active people list' do
Profile.delete_all
p1 = fast_create(Person)
p2 = fast_create(Person)
p3 = fast_create(Person)
fast_create(Article, :profile_id => p1, :created_at => 1.day.ago)
fast_create(Article, :profile_id => p2, :created_at => 1.day.ago)
fast_create(Article, :profile_id => p2, :created_at => 1.day.ago)
fast_create(Article, :profile_id => p2, :created_at => 1.day.ago)
fast_create(Article, :profile_id => p3, :created_at => 1.day.ago)
per_page = 1
@controller.stubs(:per_page).returns(per_page)
get :people, :filter => 'more_active'
assert_equal Person.count/per_page, assigns(:results).total_pages
end
should 'list all people filter by more active' do
Person.delete_all
p1 = create(Person, :name => 'Testing person 1', :user_id => 1)
p2 = create(Person, :name => 'Testing person 2', :user_id => 2)
p3 = create(Person, :name => 'Testing person 3', :user_id => 3)
ActionTracker::Record.delete_all
fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p1, :created_at => Time.now)
fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2, :created_at => Time.now)
fast_create(ActionTracker::Record, :user_type => 'Profile', :user_id => p2, :created_at => Time.now)
get :people, :filter => 'more_active'
assert_equal [p2,p1,p3] , assigns(:results)
end
should 'filter more popular people' do
Person.delete_all
p1 = create(Person, :name => 'Testing person 1', :user_id => 1)
p2 = create(Person, :name => 'Testing person 2', :user_id => 2)
p3 = create(Person, :name => 'Testing person 3', :user_id => 3)
p1.add_friend(p2)
p2.add_friend(p1)
p2.add_friend(p3)
get :people, :filter => 'more_popular'
assert_equal [p2,p1,p3] , assigns(:results)
end
should 'the people filter be only the hardcoded one' do
get :people, :filter => 'more_recent'
assert_equal 'more_recent' , assigns(:filter)
get :people, :filter => 'more_active'
assert_equal 'more_active' , assigns(:filter)
get :people, :filter => 'more_popular'
assert_equal 'more_popular' , assigns(:filter)
get :people, :filter => 'more_anything'
assert_equal 'more_recent' , assigns(:filter)
end
should 'the people filter define the title' do
get :people, :filter => 'more_recent'
assert_equal 'More recent people' , assigns(:title)
assert_tag :h1, :content => 'More recent people'
get :people, :filter => 'more_active'
assert_equal 'More active people' , assigns(:title)
assert_tag :h1, :content => 'More active people'
get :people, :filter => 'more_popular'
assert_equal 'More popular people' , assigns(:title)
assert_tag :h1, :content => 'More popular people'
get :people, :filter => 'more_anything'
assert_equal 'More recent people' , assigns(:title)
assert_tag :h1, :content => 'More recent people'
end
should 'search for community' do
small = create(Community, :name => 'A small community for testing')
create(Community, :name => 'A big community for testing')
get :communities, :query => 'small'
assert_equal [small], assigns(:results)
end
should 'list all community order by more recent one by default' do
c1 = create(Community, :name => 'Testing community 1', :created_at => DateTime.now - 2)
c2 = create(Community, :name => 'Testing community 2', :created_at => DateTime.now - 1)
c3 = create(Community, :name => 'Testing community 3')
get :communities
assert_equal [c3,c2,c1] , assigns(:results)
end
should 'paginate search of communities in groups of 27' do
1.upto(30).map do |n|
create(Community, :name => 'Testing community')
end
get :communities
assert_equal 30 , Community.count
assert_equal 27 , assigns(:results).count
assert_tag :a, '', :attributes => {:class => 'next_page'}
end
should 'paginate ferret search of communities in groups of 27' do
1.upto(30).map do |n|
create(Community, :name => 'Testing community')
end
get :communities, :query => 'Testing'
assert_equal 27 , assigns(:results).count
assert_tag :a, '', :attributes => {:class => 'next_page'}
end
should 'not return nil results in the more_active communities list' do
Profile.delete_all
c1 = fast_create(Community)
c2 = fast_create(Community)
c3 = fast_create(Community)
fast_create(Article, :profile_id => c1, :created_at => 1.day.ago)
fast_create(Article, :profile_id => c2, :created_at => 1.day.ago)
fast_create(Article, :profile_id => c2, :created_at => 1.day.ago)
fast_create(Article, :profile_id => c2, :created_at => 1.day.ago)
fast_create(Article, :profile_id => c3, :created_at => 1.day.ago)
per_page = 1
@controller.stubs(:per_page).returns(per_page)
get :communities, :filter => 'more_active'
assert_equal Community.count/per_page, assigns(:results).total_pages
end
should 'list all communities filter by more active' do
person = fast_create(Person)
c1 = create(Community, :name => 'Testing community 1')
c2 = create(Community, :name => 'Testing community 2')
c3 = create(Community, :name => 'Testing community 3')
ActionTracker::Record.delete_all
fast_create(ActionTracker::Record, :target_id => c1, :user_type => 'Profile', :user_id => person, :created_at => Time.now)
fast_create(ActionTracker::Record, :target_id => c2, :user_type => 'Profile', :user_id => person, :created_at => Time.now)
fast_create(ActionTracker::Record, :target_id => c2, :user_type => 'Profile', :user_id => person, :created_at => Time.now)
get :communities, :filter => 'more_active'
assert_equal [c2,c1,c3] , assigns(:results)
end
should 'filter more popular communities' do
Person.delete_all
Community.delete_all
c1 = create(Community, :name => 'Testing community 1')
c2 = create(Community, :name => 'Testing community 2')
p1 = create(Person, :name => 'Testing person 1', :user_id => 1)
p2 = create(Person, :name => 'Testing person 2', :user_id => 2)
c1.add_member(p1)
c2.add_member(p1)
c2.add_member(p2)
get :communities, :filter => 'more_popular'
assert_equal [c2,c1] , assigns(:results)
end
should 'the communities filter be only the hardcoded one' do
get :communities, :filter => 'more_recent'
assert_equal 'more_recent' , assigns(:filter)
get :communities, :filter => 'more_active'
assert_equal 'more_active' , assigns(:filter)
get :communities, :filter => 'more_popular'
assert_equal 'more_popular' , assigns(:filter)
get :communities, :filter => 'more_anything'
assert_equal 'more_recent' , assigns(:filter)
end
should 'the communities filter define the title' do
get :communities, :filter => 'more_recent'
assert_equal 'More recent communities' , assigns(:title)
assert_tag :h1, :content => 'More recent communities'
get :communities, :filter => 'more_active'
assert_equal 'More active communities' , assigns(:title)
assert_tag :h1, :content => 'More active communities'
get :communities, :filter => 'more_popular'
assert_equal 'More popular communities' , assigns(:title)
assert_tag :h1, :content => 'More popular communities'
get :communities, :filter => 'more_anything'
assert_equal 'More recent communities' , assigns(:title)
assert_tag :h1, :content => 'More recent communities'
end
should "only include visible people in more_recent filter" do
# assuming that all filters behave the same!
p1 = fast_create(Person, :visible => false)
get :people, :filter => 'more_recent'
assert_not_includes assigns(:results), p1
end
should "only include visible communities in more_recent filter" do
# assuming that all filters behave the same!
p1 = fast_create(Community, :visible => false)
get :communities, :filter => 'more_recent'
assert_not_includes assigns(:results), p1
end
end