2013-06-19 09:11:04 -07:00
require 'spec_helper'
require_dependency 'admin_user_index_query'
describe AdminUserIndexQuery do
2013-09-06 14:07:23 +10:00
def real_users_count ( query )
query . find_users_query . where ( 'users.id > 0' ) . count
end
2013-06-19 09:11:04 -07: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
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 14:07:23 +10:00
expect ( real_users_count ( query ) ) . to eq ( 0 )
2013-06-19 09:11:04 -07: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 14:07:23 +10:00
expect ( real_users_count ( query ) ) . to eq ( 1 )
2013-06-19 09:11:04 -07: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-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
2013-08-22 19:23:49 -04:00
query = :: AdminUserIndexQuery . new ( { query : 'pending' } )
expect ( query . find_users . count ) . to eq ( 1 )
end
end
2013-06-19 09:11:04 -07:00
end
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 14:07:23 +10:00
expect ( real_users_count ( query ) ) . to eq ( 1 )
2013-06-19 09:11:04 -07: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 14:07:23 +10:00
expect ( real_users_count ( query ) ) . to eq ( 1 )
2013-06-19 09:11:04 -07: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 12:05:38 +02:00
context " by email fragment " do
before ( :each ) { Fabricate ( :user , email : " test1@example.com " ) }
context " when authenticated as a non-admin user " do
it " doesn't match the email " do
query = :: AdminUserIndexQuery . new ( { filter : " test1@example.com " } )
expect ( query . find_users . count ( ) ) . to eq ( 0 )
end
end
context " when authenticated as an admin user " do
it " matches the email " do
query = :: AdminUserIndexQuery . new ( { filter : " est1 " , admin : true } )
expect ( query . find_users . count ( ) ) . to eq ( 1 )
end
it " matches the email using any case " do
query = :: AdminUserIndexQuery . new ( { filter : " Test1 " , admin : true } )
expect ( query . find_users . count ( ) ) . to eq ( 1 )
end
end
end
2013-06-19 09:11:04 -07:00
context " by username fragment " do
before ( :each ) { Fabricate ( :user , username : " test_user_1 " ) }
it " matches the username " do
query = :: AdminUserIndexQuery . new ( { filter : " user " } )
expect ( query . find_users . count ) . to eq ( 1 )
end
it " matches the username using any case " do
query = :: AdminUserIndexQuery . new ( { filter : " User " } )
expect ( query . find_users . count ) . to eq ( 1 )
end
end
2014-10-23 07:58:01 +05:30
context " by ip address fragment " do
before ( :each ) { Fabricate ( :user , ip_address : " 117.207.94.9 " ) }
context " when authenticated as a non-admin user " do
it " doesn't match the ip address " do
query = :: AdminUserIndexQuery . new ( { filter : " 117.207.94.9 " } )
expect ( query . find_users . count ( ) ) . to eq ( 0 )
end
end
context " when authenticated as an admin user " do
it " matches the ip address " do
query = :: AdminUserIndexQuery . new ( { filter : " 117.207.94.9 " , admin : true } )
expect ( query . find_users . count ( ) ) . to eq ( 1 )
end
end
end
2013-06-19 09:11:04 -07:00
end
2013-07-30 17:36:34 +10:00
end