Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions app/models/concerns/pwpush/notifiable_by_email.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ def notify_by_email_available?
end
end

class_methods do
def human_attribute_name(attribute, options = {})
case attribute.to_sym
when :notify_emails_to
_("Recipient emails")
when :notify_emails_to_locale
_("Notification language")
else
super
end
end
Comment thread
pglombardo marked this conversation as resolved.
end

private

def validate_notify_by_email
Expand All @@ -45,7 +58,7 @@ def validate_notify_emails_to_presence

def validate_notify_by_email_availability
unless notify_by_email_available?
errors.add(:notify_emails_to, _("is not available")) if notify_emails_to.present?
errors.add(:notify_emails_to, _("are not available")) if notify_emails_to.present?
errors.add(:notify_emails_to_locale, _("is not available")) if notify_emails_to_locale.present?
errors.add(:base, _("Notify by email feature is not enabled"))

Expand All @@ -55,22 +68,21 @@ def validate_notify_by_email_availability
notify_by_email_custom_validations

unless notify_by_email_creator.present?
errors.add(:notify_emails_to, _("is not allowed for unknown users")) if notify_emails_to.present?
errors.add(:notify_emails_to, _("are not allowed for unknown users")) if notify_emails_to.present?
errors.add(:notify_emails_to_locale, _("is not allowed for unknown users")) if notify_emails_to_locale.present?

return
end

unless notify_by_email_creator == user
errors.add(:notify_emails_to, _("is allowed for only owners")) if notify_emails_to.present?
errors.add(:notify_emails_to_locale, _("is allowed for only owners")) if notify_emails_to_locale.present?
errors.add(:notify_emails_to, _("are allowed only for owners")) if notify_emails_to.present?
errors.add(:notify_emails_to_locale, _("is allowed only for owners")) if notify_emails_to_locale.present?

return
end

if notify_by_email_creator.email_limit_reached?
errors.add(:notify_emails_to, _("is not allowed because the maximum number of emails has been reached for today")) if notify_emails_to.present?
errors.add(:notify_emails_to_locale, _("is not allowed because the maximum number of emails has been reached for today")) if notify_emails_to_locale.present?
errors.add(:base, _("The maximum number of emails has been reached for today"))
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/push.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def deleted

def notify_by_email_custom_validations
if expired?
errors.add(:notify_emails_to, _("is not available for expired pushes")) if notify_emails_to.present?
errors.add(:notify_emails_to, _("are not available for expired pushes")) if notify_emails_to.present?
errors.add(:notify_emails_to_locale, _("is not available for expired pushes")) if notify_emails_to_locale.present?
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/controllers/pushes_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class PushesControllerTest < ActionDispatch::IntegrationTest
}

assert_response :unprocessable_content
assert_includes response.body, "Notify emails to is not allowed for unknown users"
assert_includes response.body, "Recipient emails are not allowed for unknown users"
end
end

Expand All @@ -112,8 +112,8 @@ class PushesControllerTest < ActionDispatch::IntegrationTest
}

assert_response :unprocessable_content
assert_includes response.body, "Notify emails to is not available"
assert_includes response.body, "Notify emails to locale is not available"
assert_includes response.body, "Recipient emails are not available"
assert_includes response.body, "Notification language is not available"
assert_includes response.body, "Notify by email feature is not enabled"
end
end
Expand All @@ -132,8 +132,8 @@ class PushesControllerTest < ActionDispatch::IntegrationTest
}

assert_response :unprocessable_content
assert_includes response.body, "Notify emails to is not available"
assert_includes response.body, "Notify emails to locale is not available"
assert_includes response.body, "Recipient emails are not available"
assert_includes response.body, "Notification language is not available"
end
end

Expand Down Expand Up @@ -263,7 +263,7 @@ class PushesControllerTest < ActionDispatch::IntegrationTest
}

assert_response :unprocessable_content
assert_includes response.body, "Notify emails to is not available"
assert_includes response.body, "Recipient emails are not available"
end

test "notify_emails redirects to preview when push does not belong to user and disable_logins is false" do
Expand Down
8 changes: 4 additions & 4 deletions test/integration/api/api_v2_pushes_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -434,7 +434,7 @@ def test_create_with_notify_by_email_params_fails_when_feature_is_disabled

assert_response :unprocessable_entity
body = JSON.parse(response.body)
assert_equal "is not available", body["notify_emails_to"][0]
assert_equal "are not available", body["notify_emails_to"][0]
assert_equal "is not available", body["notify_emails_to_locale"][0]
end

Expand All @@ -454,7 +454,7 @@ def test_create_with_notify_by_email_params_fails_when_email_service_is_not_conf

assert_response :unprocessable_entity
body = JSON.parse(response.body)
assert_equal "is not available", body["notify_emails_to"][0]
assert_equal "are not available", body["notify_emails_to"][0]
assert_equal "is not available", body["notify_emails_to_locale"][0]
end

Expand All @@ -473,7 +473,7 @@ def test_create_with_notify_by_email_params_fails_when_user_is_not_signed_in

assert_response :unprocessable_entity
body = JSON.parse(response.body)
assert_equal "is not allowed for unknown users", body["notify_emails_to"][0]
assert_equal "are not allowed for unknown users", body["notify_emails_to"][0]
assert_equal "is not allowed for unknown users", body["notify_emails_to_locale"][0]
end

Expand Down Expand Up @@ -536,7 +536,7 @@ def test_notify_emails_with_valid_params_returns_error_when_email_service_is_not
assert_response :unprocessable_entity

body = JSON.parse(response.body)
assert_equal "is not available", body["notify_emails_to"][0]
assert_equal "are not available", body["notify_emails_to"][0]
assert_equal "is not available", body["notify_emails_to_locale"][0]
end

Expand Down
10 changes: 5 additions & 5 deletions test/models/concerns/pwpush/notifiable_by_email_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Pwpush::NotifiableByEmailTest < ActiveSupport::TestCase
Settings.mail.smtp_address = nil

assert_not @push.valid?
assert_includes @push.errors[:notify_emails_to], "is not available"
assert_includes @push.errors[:notify_emails_to], "are not available"
assert_includes @push.errors[:notify_emails_to_locale], "is not available"
assert_includes @push.errors[:base], "Notify by email feature is not enabled"
end
Expand All @@ -76,7 +76,7 @@ class Pwpush::NotifiableByEmailTest < ActiveSupport::TestCase
@push.notify_by_email_creator = nil

assert_not @push.valid?
assert_includes @push.errors[:notify_emails_to], "is not allowed for unknown users"
assert_includes @push.errors[:notify_emails_to], "are not allowed for unknown users"
assert_includes @push.errors[:notify_emails_to_locale], "is not allowed for unknown users"
end

Expand All @@ -85,8 +85,8 @@ class Pwpush::NotifiableByEmailTest < ActiveSupport::TestCase

assert @other_user != @user
assert_not @push.valid?
assert_includes @push.errors[:notify_emails_to], "is allowed for only owners"
assert_includes @push.errors[:notify_emails_to_locale], "is allowed for only owners"
assert_includes @push.errors[:notify_emails_to], "are allowed only for owners"
assert_includes @push.errors[:notify_emails_to_locale], "is allowed only for owners"
end

# Test notify_by_email_limit validation
Expand Down Expand Up @@ -143,7 +143,7 @@ class Pwpush::NotifiableByEmailTest < ActiveSupport::TestCase
@push.notify_emails_to_locale = "en"

assert_not @push.valid?
assert_includes @push.errors[:notify_emails_to], "is not available for expired pushes"
assert_includes @push.errors[:notify_emails_to], "are not available for expired pushes"
assert_includes @push.errors[:notify_emails_to_locale], "is not available for expired pushes"
end

Expand Down
2 changes: 1 addition & 1 deletion test/system/notify_by_email_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class NotifyByEmailTest < ApplicationSystemTestCase
fill_in "push[notify_emails_to]", with: "test@example.com, invalid-email"
click_on "Send Emails"

assert_text "Notify emails to contains invalid email(s)"
assert_text "Recipient emails contains invalid email(s)"
Comment on lines 39 to +42
end

test "notify_by_email creation and sending emails" do
Expand Down
4 changes: 2 additions & 2 deletions test/unit/send_notify_by_email_job_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class SendNotifyByEmailJobTest < ActiveJob::TestCase
@notify_by_email.reload
assert_equal "failed", @notify_by_email.status
assert @notify_by_email.successful_sends.blank?
assert_equal "Notify emails to can't be blank.", @notify_by_email.error_message
assert_equal "Recipient emails can't be blank.", @notify_by_email.error_message
end

test "perform does not send mail when notifying by email is not available" do
Expand All @@ -72,7 +72,7 @@ class SendNotifyByEmailJobTest < ActiveJob::TestCase

@notify_by_email.reload
assert_equal "failed", @notify_by_email.status, "Status should be failed"
assert_equal "Notify emails to is not available. Notify emails to locale is not available. Notify by email feature is not enabled.", @notify_by_email.error_message
assert_equal "Recipient emails are not available. Notification language is not available. Notify by email feature is not enabled.", @notify_by_email.error_message
end

test "perform logs error and does not send mail if notify_by_email is not found" do
Expand Down
Loading