[#7062] set first email to primary if none; unset primary when its deleted
diff --git a/Allura/allura/controllers/auth.py b/Allura/allura/controllers/auth.py
index e7a58b8..2b93dc4 100644
--- a/Allura/allura/controllers/auth.py
+++ b/Allura/allura/controllers/auth.py
@@ -446,10 +446,12 @@
             for i, (old_a, data) in enumerate(zip(c.user.email_addresses, addr or [])):
                 obj = c.user.address_object(old_a)
                 if data.get('delete') or not obj:
+                    if primary_addr == c.user.email_addresses[i]:
+                        c.user.set_pref('email_address', None)
+                        primary_addr = None
                     del c.user.email_addresses[i]
                     if obj:
                         obj.delete()
-            c.user.set_pref('email_address', primary_addr)
             if new_addr.get('claim'):
                 if M.EmailAddress.query.get(_id=new_addr['addr'], confirmed=True):
                     flash('Email address already claimed', 'error')
@@ -458,6 +460,10 @@
                     em = M.EmailAddress.upsert(new_addr['addr'])
                     em.claimed_by_user_id = c.user._id
                     em.send_verification_link()
+            if not primary_addr and not c.user.get_pref('email_address') and c.user.email_addresses:
+                primary_addr = c.user.email_addresses[0]
+            if primary_addr:
+                c.user.set_pref('email_address', primary_addr)
             for i, (old_oid, data) in enumerate(zip(c.user.open_ids, oid or [])):
                 obj = c.user.openid_object(old_oid)
                 if data.get('delete') or not obj:
diff --git a/Allura/allura/tests/functional/test_auth.py b/Allura/allura/tests/functional/test_auth.py
index cff1e47..e41a220 100644
--- a/Allura/allura/tests/functional/test_auth.py
+++ b/Allura/allura/tests/functional/test_auth.py
@@ -74,7 +74,13 @@
     def test_prefs(self):
         r = self.app.get('/auth/preferences/',
                          extra_environ=dict(username='test-admin'))
+        # check preconditions of test data
         assert 'test@example.com' not in r
+        assert 'test-admin@users.localhost' in r
+        assert_equal(M.User.query.get(username='test-admin').get_pref('email_address'),
+                     'test-admin@users.localhost')
+
+        # add test@example
         r = self.app.post('/auth/preferences/update', params={
             'preferences.display_name': 'Test Admin',
             'new_addr.addr': 'test@example.com',
@@ -84,27 +90,24 @@
             extra_environ=dict(username='test-admin'))
         r = self.app.get('/auth/preferences/')
         assert 'test@example.com' in r
+        assert_equal(M.User.query.get(username='test-admin').get_pref('email_address'),
+                     'test-admin@users.localhost')
+
+        # remove test-admin@users.localhost
         r = self.app.post('/auth/preferences/update', params={
             'preferences.display_name': 'Test Admin',
             'addr-1.ord': '1',
-            'addr-2.ord': '1',
-            'addr-2.delete': 'on',
+            'addr-1.delete': 'on',
+            'addr-2.ord': '2',
             'new_addr.addr': '',
             'primary_addr': 'test-admin@users.localhost',
             'preferences.email_format': 'plain'},
             extra_environ=dict(username='test-admin'))
         r = self.app.get('/auth/preferences/')
-        assert 'test@example.com' not in r
-        ea = M.EmailAddress.query.get(_id='test-admin@users.localhost')
-        ea.confirmed = True
-        ThreadLocalORMSession.flush_all()
-        r = self.app.post('/auth/preferences/update', params={
-            'preferences.display_name': 'Test Admin',
-            'new_addr.addr': 'test-admin@users.localhost',
-            'new_addr.claim': 'Claim Address',
-            'primary_addr': 'test-admin@users.localhost',
-            'preferences.email_format': 'plain'},
-            extra_environ=dict(username='test-admin'))
+        assert 'test-admin@users.localhost' not in r
+        # preferred address has changed to remaining address
+        assert_equal(M.User.query.get(username='test-admin').get_pref('email_address'),
+                     'test@example.com')
 
     @td.with_user_project('test-admin')
     def test_prefs_subscriptions(self):