Add coverage for ReportFilter and AccountFilter (#3236)
This commit is contained in:
		
					parent
					
						
							
								b51398d0dd
							
						
					
				
			
			
				commit
				
					
						e1b42e9aa0
					
				
			
		
					 5 changed files with 51 additions and 12 deletions
				
			
		|  | @ -89,6 +89,8 @@ class Account < ApplicationRecord | |||
|   scope :recent, -> { reorder(id: :desc) } | ||||
|   scope :alphabetic, -> { order(domain: :asc, username: :asc) } | ||||
|   scope :by_domain_accounts, -> { group(:domain).select(:domain, 'COUNT(*) AS accounts_count').order('accounts_count desc') } | ||||
|   scope :matches_username, ->(value) { where(arel_table[:username].matches("#{value}%")) } | ||||
|   scope :matches_display_name, ->(value) { where(arel_table[:display_name].matches("#{value}%")) } | ||||
| 
 | ||||
|   delegate :email, | ||||
|            :current_sign_in_ip, | ||||
|  |  | |||
|  | @ -18,8 +18,6 @@ class AccountFilter | |||
|   private | ||||
| 
 | ||||
|   def scope_for(key, value) | ||||
|     accounts = Account.arel_table | ||||
| 
 | ||||
|     case key.to_s | ||||
|     when 'local' | ||||
|       Account.local | ||||
|  | @ -34,21 +32,26 @@ class AccountFilter | |||
|     when 'suspended' | ||||
|       Account.suspended | ||||
|     when 'username' | ||||
|       Account.where(accounts[:username].matches("#{value}%")) | ||||
|       Account.matches_username(value) | ||||
|     when 'display_name' | ||||
|       Account.where(accounts[:display_name].matches("#{value}%")) | ||||
|       Account.matches_display_name(value) | ||||
|     when 'email' | ||||
|       users = User.arel_table | ||||
|       Account.joins(:user).merge(User.where(users[:email].matches("#{value}%"))) | ||||
|       accounts_with_users.merge User.matches_email(value) | ||||
|     when 'ip' | ||||
|       return Account.default_scoped unless valid_ip?(value) | ||||
|       matches_ip = User.where(current_sign_in_ip: value).or(User.where(last_sign_in_ip: value)) | ||||
|       Account.joins(:user).merge(matches_ip) | ||||
|       if valid_ip?(value) | ||||
|         accounts_with_users.merge User.with_recent_ip_address(value) | ||||
|       else | ||||
|         Account.default_scoped | ||||
|       end | ||||
|     else | ||||
|       raise "Unknown filter: #{key}" | ||||
|     end | ||||
|   end | ||||
| 
 | ||||
|   def accounts_with_users | ||||
|     Account.joins(:user) | ||||
|   end | ||||
| 
 | ||||
|   def valid_ip?(value) | ||||
|     IPAddr.new(value) | ||||
|     true | ||||
|  |  | |||
|  | @ -53,6 +53,8 @@ class User < ApplicationRecord | |||
|   scope :admins,    -> { where(admin: true) } | ||||
|   scope :confirmed, -> { where.not(confirmed_at: nil) } | ||||
|   scope :inactive, -> { where(arel_table[:current_sign_in_at].lt(ACTIVE_DURATION.ago)) } | ||||
|   scope :matches_email, ->(value) { where(arel_table[:email].matches("#{value}%")) } | ||||
|   scope :with_recent_ip_address, ->(value) { where(arel_table[:current_sign_in_ip].eq(value).or(arel_table[:last_sign_in_ip].eq(value))) } | ||||
| 
 | ||||
|   before_validation :sanitize_languages | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue