Skip to content

Commit 0136727

Browse files
committed
Add test coverage for UsersController#search_users
1 parent 66a9b97 commit 0136727

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
require 'spec_helper'
2+
3+
describe UsersController, :search_users do
4+
5+
let(:topic) { Fabricate :topic }
6+
let(:topic2) { Fabricate :topic }
7+
let(:topic3) { Fabricate :topic }
8+
let(:user1) { Fabricate :user, username: "mrblonde", name: "Michael Madsen" }
9+
let(:user2) { Fabricate :user, username: "mrblue", name: "Eddie Bunker" }
10+
let(:user3) { Fabricate :user, username: "mrorange", name: "Tim Roth" }
11+
let(:user4) { Fabricate :user, username: "mrpink", name: "Steve Buscemi" }
12+
let(:user5) { Fabricate :user, username: "mrbrown", name: "Quentin Tarantino" }
13+
let(:user6) { Fabricate :user, username: "mrwhite", name: "Harvey Keitel" }
14+
15+
before do
16+
Fabricate :post, user: user1, topic: topic
17+
Fabricate :post, user: user2, topic: topic2
18+
Fabricate :post, user: user3, topic: topic
19+
Fabricate :post, user: user4, topic: topic
20+
Fabricate :post, user: user5, topic: topic3
21+
Fabricate :post, user: user6, topic: topic
22+
end
23+
24+
context "all user search" do
25+
it "searches the user's name" do
26+
xhr :post, :search_users, term: user1.name.split(" ").first
27+
json = JSON.parse(response.body)
28+
json["users"].size.should == 1
29+
json["users"].first.should == user_json(user1)
30+
end
31+
32+
it "searches the user's name case insensitive" do
33+
xhr :post, :search_users, term: user1.name.split(" ").first.downcase
34+
json = JSON.parse(response.body)
35+
json["users"].size.should == 1
36+
json["users"].first.should == user_json(user1)
37+
end
38+
39+
it "searches the user's username" do
40+
xhr :post, :search_users, term: user4.username
41+
json = JSON.parse(response.body)
42+
json["users"].size.should == 1
43+
json["users"].first.should == user_json(user4)
44+
end
45+
46+
it "searches the user's username case insensitive" do
47+
xhr :post, :search_users, term: user4.username.upcase
48+
json = JSON.parse(response.body)
49+
json["users"].size.should == 1
50+
json["users"].first.should == user_json(user4)
51+
end
52+
53+
it "searches the user's username substring" do
54+
xhr :post, :search_users, term: "mr"
55+
json = JSON.parse(response.body)
56+
json["users"].size.should == 6
57+
58+
xhr :post, :search_users, term: "mrb"
59+
json = JSON.parse(response.body)
60+
json["users"].size.should == 3
61+
end
62+
end
63+
64+
context "sort order respects users with posts on the topic" do
65+
it "Mr. Blond is first when searching his topic" do
66+
xhr :post, :search_users, topic_id: topic.id, term: "mrb"
67+
json = JSON.parse(response.body)
68+
json["users"].first.should == user_json(user1)
69+
end
70+
71+
it "Mr. Blue is first when searching his topic" do
72+
xhr :post, :search_users, topic_id: topic2.id, term: "mrb"
73+
json = JSON.parse(response.body)
74+
json["users"].first.should == user_json(user2)
75+
end
76+
77+
it "Mr. Brown is first when searching his topic" do
78+
xhr :post, :search_users, topic_id: topic3.id, term: "mrb"
79+
json = JSON.parse(response.body)
80+
json["users"].first.should == user_json(user5)
81+
end
82+
end
83+
84+
def user_json user
85+
{ "avatar_template" => user.avatar_template,
86+
"name" => user.name,
87+
"username" => user.username }
88+
end
89+
90+
end

0 commit comments

Comments
 (0)