2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-06-19 12:11:04 -04:00
|
|
|
require_dependency 'admin_user_index_query'
|
|
|
|
|
|
|
|
describe AdminUserIndexQuery do
|
2018-05-16 20:47:24 -04:00
|
|
|
def real_users(query)
|
|
|
|
query.find_users_query.where('users.id > 0')
|
2013-09-06 00:07:23 -04:00
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04: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
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(query: "active")
|
2013-06-19 12:11:04 -04:00
|
|
|
expect(query.find_users_query.to_sql).to match("last_seen_at")
|
|
|
|
end
|
2016-07-28 11:42:06 -04:00
|
|
|
|
|
|
|
it "can't be injected" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(order: "wat, no")
|
2016-07-28 11:42:06 -04:00
|
|
|
expect(query.find_users_query.to_sql).not_to match("wat, no")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows custom ordering" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(order: "trust_level")
|
2016-07-28 11:42:06 -04:00
|
|
|
expect(query.find_users_query.to_sql).to match("trust_level DESC")
|
|
|
|
end
|
2017-05-12 16:28:35 -04:00
|
|
|
|
2017-02-24 19:11:17 -05:00
|
|
|
it "allows custom ordering asc" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(order: "trust_level", ascending: true)
|
|
|
|
expect(query.find_users_query.to_sql).to match("trust_level ASC")
|
2017-02-24 19:11:17 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows custom ordering for stats wtih default direction" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(order: "topics_viewed")
|
2017-02-24 19:11:17 -05:00
|
|
|
expect(query.find_users_query.to_sql).to match("topics_entered DESC")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows custom ordering and direction for stats" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(order: "topics_viewed", ascending: true)
|
2017-02-24 19:11:17 -05:00
|
|
|
expect(query.find_users_query.to_sql).to match("topics_entered ASC")
|
|
|
|
end
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
|
2017-05-12 16:28:35 -04:00
|
|
|
describe "pagination" do
|
|
|
|
it "defaults to the first page" do
|
|
|
|
query = ::AdminUserIndexQuery.new({})
|
|
|
|
expect(query.find_users.to_sql).to match("OFFSET 0")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "offsets by 100 by default for page 2" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(page: "2")
|
2017-05-12 16:28:35 -04:00
|
|
|
expect(query.find_users.to_sql).to match("OFFSET 100")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "offsets by limit for page 2" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(page: "2")
|
2017-05-12 16:28:35 -04:00
|
|
|
expect(query.find_users(10).to_sql).to match("OFFSET 10")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "ignores negative pages" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(page: "-2")
|
2017-05-12 16:28:35 -04:00
|
|
|
expect(query.find_users.to_sql).to match("OFFSET 0")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
describe "no users with trust level" do
|
|
|
|
|
|
|
|
TrustLevel.levels.each do |key, value|
|
|
|
|
it "#{key} returns no records" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(query: key.to_s)
|
2018-05-16 20:47:24 -04:00
|
|
|
expect(real_users(query)).to eq([])
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "users with trust level" do
|
|
|
|
|
|
|
|
TrustLevel.levels.each do |key, value|
|
|
|
|
it "finds user with trust #{key}" do
|
2018-05-16 20:47:24 -04:00
|
|
|
user = Fabricate(:user, trust_level: value)
|
|
|
|
Fabricate(:user, trust_level: value + 1)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(query: key.to_s)
|
2018-05-16 20:47:24 -04:00
|
|
|
expect(real_users(query)).to eq([user])
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-08-21 22:16:13 -04:00
|
|
|
describe 'with a suspected user' do
|
|
|
|
let(:user) { Fabricate(:active_user, created_at: 1.day.ago) }
|
2017-09-26 03:04:08 -04:00
|
|
|
let(:bot) { Fabricate(:active_user, id: -10, created_at: 1.day.ago) }
|
2017-08-21 22:16:13 -04:00
|
|
|
|
|
|
|
it 'finds the suspected user' do
|
2017-09-26 03:04:08 -04:00
|
|
|
bot
|
2017-08-21 22:16:13 -04:00
|
|
|
user
|
|
|
|
query = AdminUserIndexQuery.new(query: 'suspect')
|
|
|
|
expect(query.find_users).to eq([user])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
describe "with a pending user" do
|
|
|
|
|
2017-09-04 12:55:23 -04:00
|
|
|
let!(:user) { Fabricate(:user, active: true, approved: false) }
|
|
|
|
let!(:inactive_user) { Fabricate(:user, approved: false, active: false) }
|
2013-06-19 12:11:04 -04:00
|
|
|
|
|
|
|
it "finds the unapproved user" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(query: 'pending')
|
2017-09-04 12:55:23 -04:00
|
|
|
expect(query.find_users).to include(user)
|
|
|
|
expect(query.find_users).not_to include(inactive_user)
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
|
2013-11-07 13:53:32 -05: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
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(query: 'pending')
|
2017-09-04 12:55:23 -04:00
|
|
|
expect(query.find_users).not_to include(suspended_user)
|
2013-08-22 19:23:49 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
|
2017-02-24 19:11:17 -05: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-07-27 21:20:09 -04:00
|
|
|
users = ::AdminUserIndexQuery.new(order: "last_emailed", ascending: true).find_users
|
2017-02-24 19:11:17 -05:00
|
|
|
|
2017-07-25 16:08:03 -04:00
|
|
|
expect(users.where('users.id > -2').count).to eq(2)
|
2017-12-04 02:20:05 -05:00
|
|
|
expect(users.where('users.id > -2').order('users.id asc').first.username).to eq("system")
|
2017-02-24 19:11:17 -05:00
|
|
|
expect(users.first.last_emailed_at).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "shows nil values last with desc" do
|
2017-07-27 21:20:09 -04:00
|
|
|
users = ::AdminUserIndexQuery.new(order: "last_emailed").find_users
|
2017-02-24 19:11:17 -05:00
|
|
|
|
2017-07-25 16:08:03 -04:00
|
|
|
expect(users.where('users.id > -2').count).to eq(2)
|
2017-02-24 19:11:17 -05:00
|
|
|
expect(users.first.last_emailed_at).to_not eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
describe "with an admin user" do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, admin: true) }
|
2018-05-16 20:47:24 -04:00
|
|
|
let!(:user2) { Fabricate(:user, admin: false) }
|
2013-06-19 12:11:04 -04:00
|
|
|
|
|
|
|
it "finds the admin" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(query: 'admins')
|
2018-05-16 20:47:24 -04:00
|
|
|
expect(real_users(query)).to eq([user])
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "with a moderator" do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, moderator: true) }
|
2018-05-16 20:47:24 -04:00
|
|
|
let!(:user2) { Fabricate(:user, moderator: false) }
|
2013-06-19 12:11:04 -04:00
|
|
|
|
|
|
|
it "finds the moderator" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(query: 'moderators')
|
2018-05-16 20:47:24 -04:00
|
|
|
expect(real_users(query)).to eq([user])
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
describe "with a silenced user" do
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2017-11-13 13:41:36 -05:00
|
|
|
let!(:user) { Fabricate(:user, silenced_till: 1.year.from_now) }
|
2018-05-16 20:47:24 -04:00
|
|
|
let!(:user2) { Fabricate(:user) }
|
2013-06-19 12:11:04 -04:00
|
|
|
|
2017-11-10 12:18:08 -05:00
|
|
|
it "finds the silenced user" do
|
|
|
|
query = ::AdminUserIndexQuery.new(query: 'silenced')
|
2018-05-16 20:47:24 -04:00
|
|
|
expect(real_users(query)).to eq([user])
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2018-05-16 19:52:49 -04:00
|
|
|
describe "with a staged user" do
|
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, staged: true) }
|
2018-05-16 20:47:24 -04:00
|
|
|
let!(:user2) { Fabricate(:user, staged: false) }
|
2018-05-16 19:52:49 -04:00
|
|
|
|
|
|
|
it "finds the staged user" do
|
|
|
|
query = ::AdminUserIndexQuery.new(query: 'staged')
|
2018-05-16 20:47:24 -04:00
|
|
|
expect(real_users(query)).to eq([user])
|
2018-05-16 19:52:49 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
describe "filtering" do
|
|
|
|
|
2017-11-09 00:53:14 -05:00
|
|
|
context "exact email bypass" do
|
|
|
|
it "can correctly bypass expensive ilike query" do
|
|
|
|
user = Fabricate(:user, email: 'sam@Sam.com')
|
|
|
|
|
|
|
|
query = AdminUserIndexQuery.new(filter: 'Sam@sam.com').find_users_query
|
|
|
|
expect(query.count).to eq(1)
|
|
|
|
expect(query.first.id).to eq(user.id)
|
|
|
|
|
|
|
|
expect(query.to_sql.downcase).not_to include("ilike")
|
|
|
|
end
|
2017-11-09 01:04:21 -05:00
|
|
|
|
|
|
|
it "can correctly bypass expensive ilike query" do
|
|
|
|
user = Fabricate(:user, email: 'sam2@Sam.com')
|
|
|
|
|
|
|
|
query = AdminUserIndexQuery.new(email: 'Sam@sam.com').find_users_query
|
|
|
|
expect(query.count).to eq(0)
|
|
|
|
expect(query.to_sql.downcase).not_to include("ilike")
|
|
|
|
|
|
|
|
query = AdminUserIndexQuery.new(email: 'Sam2@sam.com').find_users_query
|
|
|
|
expect(query.first.id).to eq(user.id)
|
|
|
|
expect(query.count).to eq(1)
|
|
|
|
expect(query.to_sql.downcase).not_to include("ilike")
|
|
|
|
|
|
|
|
end
|
2017-11-09 00:53:14 -05:00
|
|
|
end
|
|
|
|
|
2014-10-07 06:05:38 -04:00
|
|
|
context "by email fragment" do
|
|
|
|
|
2014-10-29 17:08:41 -04:00
|
|
|
before(:each) { Fabricate(:user, email: "test1@example.com") }
|
2014-10-07 06:05:38 -04:00
|
|
|
|
2014-10-29 17:08:41 -04:00
|
|
|
it "matches the email" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(filter: " est1")
|
2014-10-29 17:08:41 -04:00
|
|
|
expect(query.find_users.count()).to eq(1)
|
2014-10-07 06:05:38 -04:00
|
|
|
end
|
|
|
|
|
2014-10-29 17:08:41 -04:00
|
|
|
it "matches the email using any case" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(filter: "Test1\t")
|
2014-10-29 17:08:41 -04:00
|
|
|
expect(query.find_users.count()).to eq(1)
|
2014-10-07 06:05:38 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
context "by username fragment" do
|
2014-10-29 17:08:41 -04:00
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
before(:each) { Fabricate(:user, username: "test_user_1") }
|
|
|
|
|
|
|
|
it "matches the username" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(filter: "user\n")
|
2013-06-19 12:11:04 -04:00
|
|
|
expect(query.find_users.count).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "matches the username using any case" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(filter: "\r\nUser")
|
2013-06-19 12:11:04 -04:00
|
|
|
expect(query.find_users.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-22 22:28:01 -04:00
|
|
|
context "by ip address fragment" do
|
2014-10-29 17:08:41 -04:00
|
|
|
|
|
|
|
let!(:user) { Fabricate(:user, ip_address: "117.207.94.9") }
|
2014-10-22 22:28:01 -04:00
|
|
|
|
2014-10-23 13:56:59 -04:00
|
|
|
it "matches the ip address" do
|
2017-07-27 21:20:09 -04:00
|
|
|
query = ::AdminUserIndexQuery.new(filter: " 117.207.94.9 ")
|
2014-10-23 13:56:59 -04:00
|
|
|
expect(query.find_users.count()).to eq(1)
|
2014-10-22 22:28:01 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-06-19 12:11:04 -04:00
|
|
|
end
|
2013-07-30 03:36:34 -04:00
|
|
|
end
|