-
Notifications
You must be signed in to change notification settings - Fork 0
Automated Test: blocked-email-validation-post #326
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| class BlockedEmail < ActiveRecord::Base | ||
|
|
||
| before_validation :set_defaults | ||
|
|
||
| validates :email, presence: true, uniqueness: true | ||
|
|
||
| def self.actions | ||
| @actions ||= Enum.new(:block, :do_nothing) | ||
| end | ||
|
|
||
| def self.should_block?(email) | ||
| record = BlockedEmail.where(email: email).first | ||
| if record | ||
| record.match_count += 1 | ||
| record.last_match_at = Time.zone.now | ||
| record.save | ||
| end | ||
| record && record.action_type == actions[:block] | ||
| end | ||
|
Comment on lines
+11
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧩 Analysis chain🏁 Script executed: # First, let's explore the repository structure
git ls-files | head -20Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 348 🏁 Script executed: # Check the BlockedEmail model and related models
fd -t f -e rb 'blocked_email|user' app/models/ | head -20Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 615 🏁 Script executed: # Search for BlockedEmail references and email normalization patterns
rg -n 'BlockedEmail|should_block' --type=rb -g '!spec/**' | head -30Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 114 🏁 Script executed: # Check the User model for email handling/normalization
fd -t f 'user.rb' app/models/ -x cat -n {}Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 39687 🏁 Script executed: # Look for email normalization in the codebase
rg -n 'downcase|normalize.*email|email.*downcase' --type=rb -g '!spec/**' | head -30Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 114 🏁 Script executed: cat -n app/models/blocked_email.rbRepository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 805 🏁 Script executed: # Search for how BlockedEmail is used in the codebase
rg "BlockedEmail" --type-list | head -5Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 477 🏁 Script executed: # Try searching for BlockedEmail references without type specification
rg "BlockedEmail\|should_block" --max-count=20Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 70 🏁 Script executed: # Search for Email class and downcase method
rg "class Email|def downcase" --max-count=20Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 685 🏁 Script executed: # Find the Email class definition
find . -name 'email.rb' -type f | grep -v node_modules | head -10Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 102 🏁 Script executed: # Search for where BlockedEmail.should_block? is called
rg "should_block" --max-count=20Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 658 🏁 Script executed: # Search for email normalization in validators
rg "downcase|strip|normalize" lib/validators/email_validator.rbRepository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 70 🏁 Script executed: # Check if there's any email normalization in database callbacks
rg "before_save|before_validation.*email" --max-count=20Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 459 🏁 Script executed: cat -n lib/email.rbRepository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 945 🏁 Script executed: cat -n lib/validators/email_validator.rbRepository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 1095 🏁 Script executed: cat -n spec/models/blocked_email_spec.rb | head -80Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 1987 🏁 Script executed: # Check if email is normalized before being passed to BlockedEmail.should_block?
rg -B5 -A5 "BlockedEmail.should_block" lib/validators/email_validator.rbRepository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 509 🏁 Script executed: # Verify that Email.downcase is not called in the EmailValidator path
rg -B10 -A10 "Email.downcase" lib/validators/email_validator.rbRepository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 70 🏁 Script executed: # Check User model to confirm email is not normalized before validation
rg -B5 -A5 "validates.*email" app/models/user.rb | head -30Repository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 418 🏁 Script executed: # Look at how User creation/validation handles email
rg -B5 -A5 "new_from_params|create_for_email" app/models/user.rbRepository: admin-coderabbit/coderabbit-discourse-mirror Length of output: 650
If an admin blocks Additionally, the read-modify-save pattern on Proposed fix for normalization and atomic update def self.should_block?(email)
- record = BlockedEmail.where(email: email).first
+ record = BlockedEmail.where(email: email.strip.downcase).first
if record
- record.match_count += 1
- record.last_match_at = Time.zone.now
- record.save
+ BlockedEmail.where(id: record.id).update_all(
+ ['match_count = match_count + 1, last_match_at = ?', Time.zone.now]
+ )
end
record && record.action_type == actions[:block]
endYou may also want to normalize the email in before_validation { self.email = email.strip.downcase if email.present? }🤖 Prompt for AI Agents |
||
|
|
||
| def set_defaults | ||
| self.action_type ||= BlockedEmail.actions[:block] | ||
| end | ||
|
|
||
| end | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: admin-coderabbit/coderabbit-discourse-mirror
Length of output: 119
🏁 Script executed:
Repository: admin-coderabbit/coderabbit-discourse-mirror
Length of output: 2477
🏁 Script executed:
Repository: admin-coderabbit/coderabbit-discourse-mirror
Length of output: 2629
Add
require_dependency 'enum'at the top of blocked_email.rb.The
Enumclass is defined inlib/enum.rb, butblocked_email.rblacks the requiredrequire_dependency 'enum'statement. Other files usingEnumin the codebase (e.g.,post.rb,notification.rb,post_action_type.rb) include this require statement. Without it, callingEnum.new(:block, :do_nothing)will fail at runtime with a NameError.🤖 Prompt for AI Agents