Commit d3264e61 authored by paz's avatar paz

Validate uniqueness of Subscription#email.

The database-scheme already requires this. Validating it improves the
error-handling.
parent 16dc3603
......@@ -20,6 +20,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
* Only use distinct keys for admins when creating lists.
* Fix text of admin-notification from plugin-runners.
* Fix working with multipart/alternative-messages that contain inline OpenPGP-data. We're now stripping the HTML-part to enable properly handling the ciphertext.
* Validate that an email address can be subscribed only once per list.
## [3.1.0] / 2017-05-21
......
......@@ -6,7 +6,7 @@ module Schleuder
in: -> (id) { List.pluck(:id) },
message: "must refer to an existing list"
}
validates :email, presence: true, email: true
validates :email, presence: true, email: true, uniqueness: {scope: :list_id}
validates :fingerprint, allow_blank: true, fingerprint: true
validates :delivery_enabled, :admin, boolean: true
......
......@@ -156,3 +156,11 @@ de:
invalid_input: "Ungültige Angabe. Gültig sind: URLs, OpenPGP-Fingerabdrücke, oder Emailadressen."
pseudoheaders:
stripped_html_from_multialt: Diese Email enthielt einen alternativen HTML-Teil, der PGP-Daten beinhaltete. Der HTML-Teil wurde entfernt, um die Email sauberer analysieren zu können.
activerecord:
errors:
models:
schleuder/subscription:
attributes:
email:
taken: 'ist schon eingetragen'
......@@ -157,3 +157,10 @@ en:
pseudoheaders:
stripped_html_from_multialt: This message included an alternating HTML-part that contained PGP-data. The HTML-part was removed to enable parsing the message more properly.
activerecord:
errors:
models:
schleuder/subscription:
attributes:
email:
taken: 'is already subscribed'
......@@ -92,5 +92,16 @@ describe Schleuder::Subscription do
expect(subscription.errors.messages[subscription_attribute]).to include("must be true or false")
end
end
it "is invalid if the given email is already subscribed for the list" do
subscription1 = create(:subscription)
subscription2 = build(:subscription, email: subscription1.email)
subscription3 = build(:subscription, email: subscription1.email, list_id: subscription1.list_id)
expect(subscription1).to be_valid
expect(subscription2).to be_valid
expect(subscription3).not_to be_valid
expect(subscription3.errors[:email]).to eql(["is already subscribed"])
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment