2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-06-20 00:11:04 +08:00
|
|
|
require_dependency 'admin_user_index_query'
|
|
|
|
|
|
|
|
describe AdminUserIndexQuery do
|
2013-09-06 12:07:23 +08:00
|
|
|
def real_users_count(query)
|
|
|
|
query.find_users_query.where('users.id > 0').count
|
|
|
|
end
|
|
|
|
|
2013-06-20 00:11:04 +08:00
|
|
|
describe "sql order" do
|
|
|
|
it "has default" do
|
|
|
|
query = ::AdminUserIndexQuery.new({})
|
|
|
|
expect(query.find_users_query.to_sql).to match("created_at DESC")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has active order" do
|
|
|
|
query = ::AdminUserIndexQuery.new({ query: "active" })
|
|
|
|
expect(query.find_users_query.to_sql).to match("last_seen_at")
|
|
|
|
end
|
2016-07-28 23:42:06 +08:00
|
|
|
|
|
|
|
it "can't be injected" do
|
|
|
|
query = ::AdminUserIndexQuery.new({ order: "wat, no" })
|
|
|
|
expect(query.find_users_query.to_sql).not_to match("wat, no")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows custom ordering" do
|
2017-02-25 08:11:17 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ order: "trust_level" })
|
2016-07-28 23:42:06 +08:00
|
|
|
expect(query.find_users_query.to_sql).to match("trust_level DESC")
|
|
|
|
end
|
2017-02-25 08:11:17 +08:00
|
|
|
|
|
|
|
it "allows custom ordering asc" do
|
2017-02-26 00:13:31 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ order: "trust_level", ascending: true })
|
2017-02-25 08:11:17 +08:00
|
|
|
expect(query.find_users_query.to_sql).to match("trust_level ASC" )
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows custom ordering for stats wtih default direction" do
|
2017-02-26 02:51:40 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ order: "topics_viewed" })
|
2017-02-25 08:11:17 +08:00
|
|
|
expect(query.find_users_query.to_sql).to match("topics_entered DESC")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows custom ordering and direction for stats" do
|
2017-02-26 02:51:40 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ order: "topics_viewed", ascending: true })
|
2017-02-25 08:11:17 +08:00
|
|
|
expect(query.find_users_query.to_sql).to match("topics_entered ASC")
|
|
|
|
end
|
2013-06-20 00:11:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "no users with trust level" do
|
|
|
|
|
|
|
|
TrustLevel.levels.each do |key, value|
|
|
|
|
it "#{key} returns no records" do
|
|
|
|
query = ::AdminUserIndexQuery.new({ query: key.to_s })
|
2013-09-06 12:07:23 +08:00
|
|
|
expect(real_users_count(query)).to eq(0)
|
2013-06-20 00:11:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "users with trust level" do
|
|
|
|
|
|
|
|
TrustLevel.levels.each do |key, value|
|
|
|
|
it "finds user with trust #{key}" do
|
|
|
|
Fabricate(:user, trust_level: TrustLevel.levels[key])
|
|
|
|
query = ::AdminUserIndexQuery.new({ query: key.to_s })
|
2013-09-06 12:07:23 +08:00
|
|
|
expect(real_users_count(query)).to eq(1)
|
2013-06-20 00:11:04 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with a pending user" do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, approved: false) }
|
|
|
|
|
|
|
|
it "finds the unapproved user" do
|
|
|
|
query = ::AdminUserIndexQuery.new({ query: 'pending' })
|
|
|
|
expect(query.find_users.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
2013-11-08 02:53:32 +08:00
|
|
|
context 'and a suspended pending user' do
|
|
|
|
let!(:suspended_user) { Fabricate(:user, approved: false, suspended_at: 1.hour.ago, suspended_till: 20.years.from_now) }
|
|
|
|
it "doesn't return the suspended user" do
|
2013-08-23 07:23:49 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ query: 'pending' })
|
|
|
|
expect(query.find_users.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-20 00:11:04 +08:00
|
|
|
end
|
|
|
|
|
2017-02-25 08:11:17 +08:00
|
|
|
describe "correct order with nil values" do
|
|
|
|
before(:each) do
|
|
|
|
Fabricate(:user, email: "test2@example.com", last_emailed_at: 1.hour.ago)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "shows nil values first with asc" do
|
2017-02-26 02:51:40 +08:00
|
|
|
users = ::AdminUserIndexQuery.new({ order: "last_emailed", ascending: true }).find_users
|
2017-02-25 08:11:17 +08:00
|
|
|
|
|
|
|
expect(users.count).to eq(2)
|
|
|
|
expect(users.first.username).to eq("system")
|
|
|
|
expect(users.first.last_emailed_at).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "shows nil values last with desc" do
|
2017-02-26 02:51:40 +08:00
|
|
|
users = ::AdminUserIndexQuery.new({ order: "last_emailed"}).find_users
|
2017-02-25 08:11:17 +08:00
|
|
|
|
|
|
|
expect(users.count).to eq(2)
|
|
|
|
expect(users.first.last_emailed_at).to_not eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-20 00:11:04 +08:00
|
|
|
describe "with an admin user" do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, admin: true) }
|
|
|
|
|
|
|
|
it "finds the admin" do
|
|
|
|
query = ::AdminUserIndexQuery.new({ query: 'admins' })
|
2013-09-06 12:07:23 +08:00
|
|
|
expect(real_users_count(query)).to eq(1)
|
2013-06-20 00:11:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with a moderator" do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, moderator: true) }
|
|
|
|
|
|
|
|
it "finds the moderator" do
|
|
|
|
query = ::AdminUserIndexQuery.new({ query: 'moderators' })
|
2013-09-06 12:07:23 +08:00
|
|
|
expect(real_users_count(query)).to eq(1)
|
2013-06-20 00:11:04 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with a blocked user" do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, blocked: true) }
|
|
|
|
|
|
|
|
it "finds the blocked user" do
|
|
|
|
query = ::AdminUserIndexQuery.new({ query: 'blocked' })
|
|
|
|
expect(query.find_users.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "filtering" do
|
|
|
|
|
2014-10-07 18:05:38 +08:00
|
|
|
context "by email fragment" do
|
|
|
|
|
2014-10-30 05:08:41 +08:00
|
|
|
before(:each) { Fabricate(:user, email: "test1@example.com") }
|
2014-10-07 18:05:38 +08:00
|
|
|
|
2014-10-30 05:08:41 +08:00
|
|
|
it "matches the email" do
|
2016-10-10 22:18:57 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ filter: " est1" })
|
2014-10-30 05:08:41 +08:00
|
|
|
expect(query.find_users.count()).to eq(1)
|
2014-10-07 18:05:38 +08:00
|
|
|
end
|
|
|
|
|
2014-10-30 05:08:41 +08:00
|
|
|
it "matches the email using any case" do
|
2016-10-10 22:18:57 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ filter: "Test1\t" })
|
2014-10-30 05:08:41 +08:00
|
|
|
expect(query.find_users.count()).to eq(1)
|
2014-10-07 18:05:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-20 00:11:04 +08:00
|
|
|
context "by username fragment" do
|
2014-10-30 05:08:41 +08:00
|
|
|
|
2013-06-20 00:11:04 +08:00
|
|
|
before(:each) { Fabricate(:user, username: "test_user_1") }
|
|
|
|
|
|
|
|
it "matches the username" do
|
2016-10-10 22:18:57 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ filter: "user\n" })
|
2013-06-20 00:11:04 +08:00
|
|
|
expect(query.find_users.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "matches the username using any case" do
|
2016-10-10 22:18:57 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ filter: "\r\nUser" })
|
2013-06-20 00:11:04 +08:00
|
|
|
expect(query.find_users.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-23 10:28:01 +08:00
|
|
|
context "by ip address fragment" do
|
2014-10-30 05:08:41 +08:00
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, ip_address: "117.207.94.9") }
|
2014-10-23 10:28:01 +08:00
|
|
|
|
2014-10-24 01:56:59 +08:00
|
|
|
it "matches the ip address" do
|
2016-10-10 22:18:57 +08:00
|
|
|
query = ::AdminUserIndexQuery.new({ filter: " 117.207.94.9 " })
|
2014-10-24 01:56:59 +08:00
|
|
|
expect(query.find_users.count()).to eq(1)
|
2014-10-23 10:28:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-20 00:11:04 +08:00
|
|
|
end
|
2013-07-30 15:36:34 +08:00
|
|
|
end
|