Skip to content

Commit

Permalink
Merge pull request #316 from zooniverse/312-add-valid_email-check-to-…
Browse files Browse the repository at this point in the history
…mailer

prevent notification emails from being sent to user's with invalid email
  • Loading branch information
Tooyosi authored Apr 18, 2024
2 parents 2c38ba3 + fa6aad2 commit 0bf19bb
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/workers/notification_email_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class NotificationEmailWorker

def perform(user_id, digest)
user = ::User.find user_id

return unless user.valid_email

digest_number = SubscriptionPreference.email_digests[digest]

# Ensure there are categories delivered at this frequency
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20240402094638_add_valid_email_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AddValidEmailToUsers < ActiveRecord::Migration
def up
execute <<-SQL
ALTER FOREIGN TABLE users ADD COLUMN valid_email boolean DEFAULT true;
SQL
end

def down
execute <<-SQL
ALTER FOREIGN TABLE users DROP COLUMN IF EXISTS valid_email;
SQL
end
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20231107211623) do
ActiveRecord::Schema.define(version: 20240402094638) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down
1 change: 1 addition & 0 deletions lib/tasks/db.rake
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ namespace :panoptes do
credited_name varchar(255),
admin bool,
banned bool,
valid_email bool,
confirmed_at timestamp(6)
) server panoptes;
Expand Down
1 change: 1 addition & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
admin false
banned false
created_at Time.now - 1.year
valid_email true
confirmed_at Time.now - 364.days

factory :moderator do
Expand Down
10 changes: 10 additions & 0 deletions spec/workers/notification_email_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,15 @@
worker.perform user.id, :daily
end
end

context 'when user email is invalid' do
it 'should not attempt to deliver email' do
user.update(valid_email: false)
mail = double deliver_now!: true
expect(NotificationMailer).not_to receive(:notify)
expect(mail).not_to receive :deliver_now!
worker.perform user.id, :daily
end
end
end
end

0 comments on commit 0bf19bb

Please sign in to comment.