-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright (c) 2024, Schweizer Alpen-Club. This file is part of | ||
# hitobito_sac_cas and licensed under the Affero General Public License version 3 | ||
# or later. See the COPYING file at the top-level directory or at | ||
# https://github.com/hitobito/hitobito_sac_cas. | ||
|
||
class Wizards::Steps::MainEmail < Wizards::Step | ||
include ValidatedEmail | ||
|
||
attribute :email, :string | ||
|
||
validates :email, presence: true | ||
validate :ensure_email_available | ||
|
||
# #email_changed? is used in `ValidatedEmail` to determine if the email | ||
# should be validated. Here it should only be validated if the email is | ||
# present. | ||
def email_changed? | ||
email.present? | ||
end | ||
|
||
def ensure_email_available | ||
return if email.blank? | ||
|
||
if Person.exists?(email: email) | ||
errors.add(:email, I18n.t('activerecord.errors.models.person.attributes.email.taken')) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
# frozen_string_literal: true | ||
|
||
# | ||
# Copyright (c) 2023, Schweizer Alpen-Club. This file is part of | ||
# hitobito_sac_cas and licensed under the Affero General Public License version 3 | ||
# or later. See the COPYING file at the top-level directory or at | ||
# https://github.com/hitobito/hitobito_sac_cas. | ||
|
||
require 'spec_helper' | ||
|
||
describe Wizards::Steps::MainEmail do | ||
let(:params) { {} } | ||
let(:wizard) { nil } # we don't need a wizard for the model specs | ||
let(:subject) { described_class.new(wizard, **params) } | ||
|
||
describe 'validations' do | ||
context 'without email' do | ||
it do | ||
is_expected.not_to be_valid | ||
expect(subject.errors[:email].count).to eq 1 | ||
end | ||
end | ||
|
||
context 'with invalid email' do | ||
before { allow(Truemail).to receive(:valid?).and_return(false) } | ||
|
||
it do | ||
is_expected.not_to be_valid | ||
expect(subject.errors[:email].count).to eq 1 | ||
end | ||
end | ||
|
||
context 'with valid email' do | ||
let(:params) { { email: 'foo@bar.ch' } } | ||
|
||
it { is_expected.to be_valid } | ||
|
||
context 'when email is already taken' do | ||
before { Fabricate(:person, email: params[:email]) } | ||
|
||
it do | ||
is_expected.not_to be_valid | ||
expect(subject.errors[:email].count).to eq 1 | ||
end | ||
|
||
end | ||
end | ||
end | ||
end |