@@ -201,6 +201,73 @@ def attach_verified_alias(user, email:, primary: true)
201201 expect ( aliases . first . name ) . to eq ( 'Existing Name' )
202202 end
203203
204+ describe 'alias removal' do
205+ it 'allows removing alias with no messages' do
206+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
207+ primary = attach_verified_alias ( user , email : 'me@example.com' )
208+ secondary = create ( :alias , user : user , person : user . person , email : 'other@example.com' , name : 'Other' , verified_at : Time . current , sender_count : 0 )
209+
210+ sign_in ( email : 'me@example.com' )
211+
212+ expect {
213+ delete settings_email_path ( secondary )
214+ } . to change { Alias . count } . by ( -1 )
215+
216+ expect ( response ) . to redirect_to ( settings_account_path )
217+ expect ( flash [ :notice ] ) . to eq ( 'Alias removed.' )
218+ expect ( Alias . find_by ( id : secondary . id ) ) . to be_nil
219+ end
220+
221+ it 'blocks removing alias with sent messages' do
222+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
223+ primary = attach_verified_alias ( user , email : 'me@example.com' )
224+ secondary = create ( :alias , user : user , person : user . person , email : 'other@example.com' , name : 'Other' , verified_at : Time . current , sender_count : 5 )
225+
226+ sign_in ( email : 'me@example.com' )
227+
228+ expect {
229+ delete settings_email_path ( secondary )
230+ } . not_to change { user . person . aliases . count }
231+
232+ expect ( response ) . to redirect_to ( settings_account_path )
233+ expect ( flash [ :alert ] ) . to match ( /message history/ )
234+ end
235+
236+ it 'blocks removing alias with CC mentions' do
237+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
238+ primary = attach_verified_alias ( user , email : 'me@example.com' )
239+ secondary = create ( :alias , user : user , person : user . person , email : 'other@example.com' , name : 'Other' , verified_at : Time . current , sender_count : 0 )
240+
241+ # Create a mention for this alias
242+ topic = create ( :topic )
243+ message = create ( :message , topic : topic )
244+ create ( :mention , message : message , alias : secondary , person : user . person )
245+
246+ sign_in ( email : 'me@example.com' )
247+
248+ expect {
249+ delete settings_email_path ( secondary )
250+ } . not_to change { user . person . aliases . count }
251+
252+ expect ( response ) . to redirect_to ( settings_account_path )
253+ expect ( flash [ :alert ] ) . to match ( /message history/ )
254+ end
255+
256+ it 'blocks removing primary alias' do
257+ user = create ( :user , password : 'secret' , password_confirmation : 'secret' )
258+ primary = attach_verified_alias ( user , email : 'me@example.com' )
259+
260+ sign_in ( email : 'me@example.com' )
261+
262+ expect {
263+ delete settings_email_path ( primary )
264+ } . not_to change { user . person . aliases . count }
265+
266+ expect ( response ) . to redirect_to ( settings_account_path )
267+ expect ( flash [ :alert ] ) . to match ( /primary alias/ )
268+ end
269+ end
270+
204271 def extract_raw_token_from_mailer
205272 mail = ActionMailer ::Base . deliveries . last
206273 expect ( mail ) . to be_present
0 commit comments