Skip to content

Commit d72c26f

Browse files
committed
Refactor UserSearch tests
1 parent e41b653 commit d72c26f

4 files changed

Lines changed: 142 additions & 127 deletions

File tree

‎app/models/user_search.rb‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
class UserSearch
22

3-
def self.search term, topic_id
3+
def self.search term, topic_id = nil
44
User.find_by_sql sql(term, topic_id)
55
end
66

77
private
88

99
def self.sql term, topic_id
10-
sql = "select username, name, email from users u "
10+
sql = "select id, username, name, email from users u "
1111
if topic_id
1212
sql << "left join (select distinct p.user_id from posts p where topic_id = :topic_id) s on
1313
s.user_id = u.id "
1414
end
1515

16-
if term.length > 0
16+
if term.present?
1717
sql << "where username ilike :term_like or
1818
to_tsvector('simple', name) @@
1919
to_tsquery('simple',

‎spec/controllers/users_controller/search_users_spec.rb‎

Lines changed: 0 additions & 100 deletions
This file was deleted.

‎spec/controllers/users_controller_spec.rb‎

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
require 'spec_helper'
22

3-
describe UsersController do
3+
describe UsersController do
44

55
before do
66
UsersController.any_instance.stubs(:honeypot_value).returns(nil)
77
UsersController.any_instance.stubs(:challenge_value).returns(nil)
88
end
99

10-
describe '.show' do
10+
describe '.show' do
1111
let!(:user) { log_in }
1212

1313
it 'returns success' do
@@ -52,7 +52,7 @@
5252

5353
it 'sets a flash error' do
5454
flash[:error].should be_present
55-
end
55+
end
5656
end
5757

5858
context 'valid token' do
@@ -74,7 +74,7 @@
7474
it 'logs in as the user' do
7575
session[:current_user_id].should be_present
7676
end
77-
end
77+
end
7878
end
7979

8080
describe '.activate_account' do
@@ -90,7 +90,7 @@
9090

9191
it 'sets a flash error' do
9292
flash[:error].should be_present
93-
end
93+
end
9494
end
9595

9696
context 'valid token' do
@@ -102,13 +102,13 @@
102102
end
103103

104104
it 'enqueues a welcome message if the user object indicates so' do
105-
user.send_welcome_message = true
105+
user.send_welcome_message = true
106106
user.expects(:enqueue_welcome_message).with('welcome_user')
107107
get :activate_account, token: 'asdfasdf'
108108
end
109109

110110
it "doesn't enqueue the welcome message if the object returns false" do
111-
user.send_welcome_message = false
111+
user.send_welcome_message = false
112112
user.expects(:enqueue_welcome_message).with('welcome_user').never
113113
get :activate_account, token: 'asdfasdf'
114114
end
@@ -131,12 +131,12 @@
131131

132132
it 'logs in as the user' do
133133
session[:current_user_id].should be_present
134-
end
134+
end
135135

136136
it "doesn't set @needs_approval" do
137137
assigns[:needs_approval].should be_blank
138-
end
139-
138+
end
139+
140140
end
141141

142142
context 'must_approve_users' do
@@ -166,7 +166,7 @@
166166
end
167167
end
168168

169-
describe '.change_email' do
169+
describe '.change_email' do
170170
let(:new_email) { 'bubblegum@adventuretime.ooo' }
171171

172172
it "requires you to be logged in" do
@@ -223,11 +223,11 @@
223223

224224
it 'sets a flash error' do
225225
flash[:error].should be_present
226-
end
226+
end
227227

228228
it "doesn't log in the user" do
229229
session[:current_user_id].should be_blank
230-
end
230+
end
231231
end
232232

233233
context 'valid token' do
@@ -259,17 +259,17 @@
259259
SiteSetting.expects(:must_approve_users?).returns(true)
260260
put :password_reset, token: 'asdfasdf', password: 'newpassword'
261261
session[:current_user_id].should be_blank
262-
end
262+
end
263263
end
264264

265265

266266
end
267267

268268

269-
describe '.create' do
270-
before do
269+
describe '.create' do
270+
before do
271271
@user = Fabricate.build(:user)
272-
@user.password = "strongpassword"
272+
@user.password = "strongpassword"
273273
Mothership.stubs(:register_nickname).returns([true, nil])
274274
end
275275

@@ -282,7 +282,7 @@
282282
it "doesn't send a welcome email" do
283283
User.any_instance.expects(:enqueue_welcome_message).with('welcome_user').never
284284
xhr :post, :create, :name => @user.name, :username => @user.username, :password => "strongpassword", :email => @user.email
285-
end
285+
end
286286
end
287287

288288
context 'when creating an active user (confirmed email)' do
@@ -294,7 +294,7 @@
294294
it 'should enqueue a signup email' do
295295
User.any_instance.expects(:enqueue_welcome_message).with('welcome_user')
296296
xhr :post, :create, :name => @user.name, :username => @user.username, :password => "strongpassword", :email => @user.email
297-
end
297+
end
298298

299299
it "should be logged in" do
300300
User.any_instance.expects(:enqueue_welcome_message)
@@ -331,16 +331,16 @@
331331
xhr :post, :create, :name => @user.name, :username => @user.username, :password => "strongpassword", :email => @user.email
332332
end
333333

334-
it 'should succeed' do
334+
it 'should succeed' do
335335
should respond_with(:success)
336336
end
337337

338338
it 'has the proper JSON' do
339339
json = JSON::parse(response.body)
340-
json["success"].should be_true
340+
json["success"].should be_true
341341
end
342342

343-
it 'should not result in an active account' do
343+
it 'should not result in an active account' do
344344
User.where(username: @user.username).first.active.should be_false
345345
end
346346
end
@@ -391,7 +391,7 @@
391391
let(:new_username) { "#{user.username}1234" }
392392

393393
it 'raises an error without a new_username param' do
394-
lambda { xhr :put, :username, username: user.username }.should raise_error(Discourse::InvalidParameters)
394+
lambda { xhr :put, :username, username: user.username }.should raise_error(Discourse::InvalidParameters)
395395
end
396396

397397
it 'raises an error when you don\'t have permission to change the user' do
@@ -402,7 +402,7 @@
402402

403403
it 'raises an error when change_username fails' do
404404
User.any_instance.expects(:change_username).with(new_username).returns(false)
405-
lambda { xhr :put, :username, username: user.username, new_username: new_username }.should raise_error(Discourse::InvalidParameters)
405+
lambda { xhr :put, :username, username: user.username, new_username: new_username }.should raise_error(Discourse::InvalidParameters)
406406
end
407407

408408
it 'should succeed when the change_username returns true' do
@@ -671,4 +671,36 @@
671671

672672
end
673673

674+
describe "search_users" do
675+
676+
let(:topic) { Fabricate :topic }
677+
let(:user) { Fabricate :user, username: "joecabot", name: "Lawrence Tierney" }
678+
679+
before do
680+
Fabricate :post, user: user, topic: topic
681+
end
682+
683+
it "searches when provided the term only" do
684+
xhr :post, :search_users, term: user.name.split(" ").last
685+
response.should be_success
686+
json = JSON.parse(response.body)
687+
json["users"].map { |u| u["username"] }.should include(user.username)
688+
end
689+
690+
it "searches when provided the topic only" do
691+
xhr :post, :search_users, topic_id: topic.id
692+
response.should be_success
693+
json = JSON.parse(response.body)
694+
json["users"].map { |u| u["username"] }.should include(user.username)
695+
end
696+
697+
it "searches when provided the term and topic" do
698+
xhr :post, :search_users, term: user.name.split(" ").last, topic_id: topic.id
699+
response.should be_success
700+
json = JSON.parse(response.body)
701+
json["users"].map { |u| u["username"] }.should include(user.username)
702+
end
703+
704+
end
705+
674706
end

0 commit comments

Comments
 (0)