diff --git a/app/models/wizards/steps/signup/sektion/family_fields/member.rb b/app/models/wizards/steps/signup/sektion/family_fields/member.rb index 9c6d417c5..279f0ed31 100644 --- a/app/models/wizards/steps/signup/sektion/family_fields/member.rb +++ b/app/models/wizards/steps/signup/sektion/family_fields/member.rb @@ -39,7 +39,7 @@ def adult? private def assert_email_unique - if emails.include?(email) + if emails.include?(email) || Person.exists?(email: email) errors.add(:email, :taken) end emails.push(email) diff --git a/spec/models/wizards/signup/sektion_wizard_spec.rb b/spec/models/wizards/signup/sektion_wizard_spec.rb index 6307921b9..dc6ad1418 100644 --- a/spec/models/wizards/signup/sektion_wizard_spec.rb +++ b/spec/models/wizards/signup/sektion_wizard_spec.rb @@ -80,6 +80,32 @@ def build(params = required_attrs) ] end + it "is invalid when on family_fields step and resuses main person email" do + @current_step = 2 + required_attrs[:family_fields] = { + members_attributes: [ + [0, {first_name: "Maxine", last_name: "Muster", birthday: "1.1.2000", email: "max.muster@example.com"}] + ] + } + expect(wizard).not_to be_valid + expect(wizard.errors).to be_empty + expect(wizard.family_fields).not_to be_valid + expect(wizard.family_fields.members.first.errors.full_messages).to eq ["E-Mail (optional) ist bereits vergeben. Die E-Mail muss eindeutig sein pro Person."] + end + + it "is invalid when on family_fields step and uses existing email" do + @current_step = 2 + required_attrs[:family_fields] = { + members_attributes: [ + [0, {first_name: "Maxine", last_name: "Muster", birthday: "1.1.2000", email: "e.hillary@hitobito.example.com"}] + ] + } + expect(wizard).not_to be_valid + expect(wizard.errors).to be_empty + expect(wizard.family_fields).not_to be_valid + expect(wizard.family_fields.members.first.errors.full_messages).to eq ["E-Mail (optional) ist bereits vergeben. Die E-Mail muss eindeutig sein pro Person."] + end + it "is invalid when on various_fields step and data_protection is blank" do @current_step = 3 required_attrs[:various_fields] = {data_protection: nil} diff --git a/spec/models/wizards/steps/signup/sektion/family_fields_spec.rb b/spec/models/wizards/steps/signup/sektion/family_fields_spec.rb index c2595a88b..90edc5151 100644 --- a/spec/models/wizards/steps/signup/sektion/family_fields_spec.rb +++ b/spec/models/wizards/steps/signup/sektion/family_fields_spec.rb @@ -74,6 +74,15 @@ expect(form.errors.full_messages).to eq ["In einer Familienmitgliedschaft sind maximal 2 Erwachsene inbegriffen."] end + it "is invalid if second member reuses existing email" do + form.members_attributes = [ + [0, required_attrs.merge(email: "acceptable@example.com")], + [1, required_attrs.merge(email: "e.hillary@hitobito.example.com", birthday: "1.10.2014")] + ] + expect(form).not_to be_valid + expect(form.members.second.errors.full_messages).to eq ["E-Mail (optional) ist bereits vergeben. Die E-Mail muss eindeutig sein pro Person."] + end + it "is invalid if second member reuses main_email in members" do form.members_attributes = [ [0, required_attrs.merge(email: "acceptable@example.com")],