@@ -26,7 +26,7 @@ def attach_verified_alias(user, email:, primary: true)
2626 sign_in ( email : 'me@example.com' )
2727
2828 perform_enqueued_jobs do
29- post settings_emails_path , params : { email : 'new-address@example.com' }
29+ post settings_emails_path , params : { email : 'new-address@example.com' , name : 'My New Name' }
3030 expect ( response ) . to redirect_to ( settings_account_path )
3131 end
3232
@@ -38,7 +38,9 @@ def attach_verified_alias(user, email:, primary: true)
3838 get verification_path ( token : raw )
3939 expect ( response ) . to redirect_to ( settings_account_path )
4040
41- expect ( Alias . by_email ( 'new-address@example.com' ) . where ( user_id : user . id ) ) . to exist
41+ new_alias = Alias . by_email ( 'new-address@example.com' ) . where ( user_id : user . id ) . first
42+ expect ( new_alias ) . to be_present
43+ expect ( new_alias . name ) . to eq ( 'My New Name' )
4244
4345 post session_path , params : { email : 'new-address@example.com' , password : 'secret' }
4446 expect ( response ) . to redirect_to ( root_path )
@@ -104,6 +106,101 @@ def attach_verified_alias(user, email:, primary: true)
104106 token &.destroy
105107 end
106108
109+ it 'requires name when adding a completely new email' do
110+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
111+ attach_verified_alias ( user , email : 'me@example.com' )
112+
113+ sign_in ( email : 'me@example.com' )
114+
115+ expect {
116+ post settings_emails_path , params : { email : 'brand-new@example.com' }
117+ } . not_to change { UserToken . count }
118+
119+ expect ( response ) . to redirect_to ( settings_account_path )
120+ expect ( flash [ :alert ] ) . to match ( /provide a display name/ )
121+ end
122+
123+ it 'requires name when email is already verified by user' do
124+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
125+ attach_verified_alias ( user , email : 'me@example.com' )
126+
127+ sign_in ( email : 'me@example.com' )
128+
129+ expect {
130+ post settings_emails_path , params : { email : 'me@example.com' }
131+ } . not_to change { UserToken . count }
132+
133+ expect ( response ) . to redirect_to ( settings_account_path )
134+ expect ( flash [ :alert ] ) . to match ( /already verified/ )
135+ end
136+
137+ it 'creates alias directly without verification when email is already verified by user' do
138+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
139+ attach_verified_alias ( user , email : 'me@example.com' )
140+
141+ sign_in ( email : 'me@example.com' )
142+
143+ expect {
144+ post settings_emails_path , params : { email : 'me@example.com' , name : 'Alternative Name' }
145+ } . to change { Alias . by_email ( 'me@example.com' ) . count } . by ( 1 )
146+
147+ expect ( UserToken . count ) . to eq ( 0 ) # No verification token created
148+
149+ expect ( response ) . to redirect_to ( settings_account_path )
150+ expect ( flash [ :notice ] ) . to eq ( 'Alias added.' )
151+
152+ new_alias = Alias . by_email ( 'me@example.com' ) . find_by ( name : 'Alternative Name' )
153+ expect ( new_alias ) . to be_present
154+ expect ( new_alias . user_id ) . to eq ( user . id )
155+ expect ( new_alias . verified_at ) . to be_present
156+ end
157+
158+ it 'creates additional alias with new name when associating existing aliases' do
159+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
160+ attach_verified_alias ( user , email : 'me@example.com' )
161+
162+ # Legacy alias for another email
163+ create ( :alias , email : 'legacy@example.com' , name : 'Old Name' )
164+
165+ sign_in ( email : 'me@example.com' )
166+
167+ perform_enqueued_jobs do
168+ post settings_emails_path , params : { email : 'legacy@example.com' , name : 'New Name' }
169+ expect ( response ) . to redirect_to ( settings_account_path )
170+ end
171+
172+ raw = extract_raw_token_from_mailer
173+ get verification_path ( token : raw )
174+ expect ( response ) . to redirect_to ( settings_account_path )
175+
176+ aliases = Alias . by_email ( 'legacy@example.com' ) . where ( user_id : user . id )
177+ expect ( aliases . count ) . to eq ( 2 )
178+ expect ( aliases . pluck ( :name ) ) . to contain_exactly ( 'Old Name' , 'New Name' )
179+ end
180+
181+ it 'does not create duplicate alias when name matches existing' do
182+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
183+ attach_verified_alias ( user , email : 'me@example.com' )
184+
185+ # Legacy alias for another email
186+ create ( :alias , email : 'legacy2@example.com' , name : 'Existing Name' )
187+
188+ sign_in ( email : 'me@example.com' )
189+
190+ perform_enqueued_jobs do
191+ post settings_emails_path , params : { email : 'legacy2@example.com' , name : 'Existing Name' }
192+ expect ( response ) . to redirect_to ( settings_account_path )
193+ end
194+
195+ raw = extract_raw_token_from_mailer
196+ get verification_path ( token : raw )
197+ expect ( response ) . to redirect_to ( settings_account_path )
198+
199+ aliases = Alias . by_email ( 'legacy2@example.com' ) . where ( user_id : user . id )
200+ expect ( aliases . count ) . to eq ( 1 )
201+ expect ( aliases . first . name ) . to eq ( 'Existing Name' )
202+ end
203+
107204 def extract_raw_token_from_mailer
108205 mail = ActionMailer ::Base . deliveries . last
109206 expect ( mail ) . to be_present
0 commit comments