Skip to content
This repository has been archived by the owner on Jan 19, 2021. It is now read-only.

Commit

Permalink
[Fix bug 1119213] Remove email editing functionality from profile.
Browse files Browse the repository at this point in the history
  • Loading branch information
johngian committed Jan 20, 2015
1 parent a4c3d35 commit 2ca621c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 91 deletions.
35 changes: 21 additions & 14 deletions mozillians/common/authbackend.py
Expand Up @@ -15,7 +15,7 @@
from django_browserid.views import Verify
from tower import ugettext as _

from mozillians.users.models import UserProfile
from mozillians.users.models import ExternalAccount, UserProfile


def calculate_username(email):
Expand Down Expand Up @@ -43,37 +43,37 @@ def calculate_username(email):
class BrowserIDVerify(Verify):
@property
def failure_url(self):
if self.change_email:
return reverse('phonebook:profile_edit')
if self.add_email:
return reverse('phonebook:edit_emails')
return super(BrowserIDVerify, self).failure_url

@property
def success_url(self):
if self.change_email:
return reverse('phonebook:profile_view', args=[self.user.username])
if self.add_email:
return reverse('phonebook:edit_emails')
return super(BrowserIDVerify, self).success_url

def login_success(self):
if self.change_email:
if self.add_email:
return JSONResponse({
'email': self.user.email,
'redirect': self.success_url
})
return super(BrowserIDVerify, self).login_success()

def login_failure(self, error=None):
if self.change_email:
if self.add_email:
return JSONResponse({
'redirect': self.failure_url
})
return super(BrowserIDVerify, self).login_failure(error)

def post(self, *args, **kwargs):
self.change_email = False
self.add_email = False
if not self.request.user.is_authenticated():
return super(BrowserIDVerify, self).post(*args, **kwargs)

self.change_email = True
self.add_email = True

assertion = self.request.POST.get('assertion')
if not assertion:
return self.login_failure()
Expand All @@ -88,14 +88,21 @@ def post(self, *args, **kwargs):

email = result.email

if User.objects.filter(email=email).exists():
kwargs = {
'type': ExternalAccount.TYPE_EMAIL,
'user': self.request.user.userprofile,
'identifier': email
}

email_exists = User.objects.filter(email=email).exists()
alternate_email_exists = ExternalAccount.objects.filter(**kwargs).exists()

if email_exists or alternate_email_exists:
error_msg = "Email '{0}' already exists in the database.".format(email)
messages.error(self.request, _(error_msg))
return self.login_failure()

self.user = self.request.user
self.user.email = email
self.user.save()
ExternalAccount.objects.create(**kwargs)
return self.login_success()


Expand Down
33 changes: 20 additions & 13 deletions mozillians/common/tests/test_authbackend.py
Expand Up @@ -6,6 +6,7 @@
from mock import patch, Mock
from mozillians.common.tests import TestCase
from mozillians.common.authbackend import BrowserIDVerify, MozilliansAuthBackend
from mozillians.users.models import ExternalAccount
from mozillians.users.tests import UserFactory
from nose.tools import eq_, ok_

Expand Down Expand Up @@ -35,7 +36,10 @@ def test_post_authenticated(self, verify_mock, get_audience_mock):
Verify.post()
verify_mock.assert_called_with('assertion', 'audience')
get_audience_mock.assert_called_with(request_mock)
eq_(user.email, 'foo@example.com')
emails = ExternalAccount.objects.filter(type=ExternalAccount.TYPE_EMAIL,
identifier='foo@example.com',
user=user.userprofile)
ok_(emails.exists())

@patch('mozillians.common.authbackend.BrowserIDVerify.login_failure')
@patch('mozillians.common.authbackend.get_audience')
Expand All @@ -56,13 +60,13 @@ def test_post_valid_email_exists(self, verify_mock, get_audience_mock,
verify_mock.assert_called_with('assertion', 'audience')
get_audience_mock.assert_called_with(request_mock)
login_failure_mock.assert_called_with()
ok_(Verify.change_email)
ok_(Verify.add_email)

@patch('mozillians.common.authbackend.BrowserIDVerify.login_success')
@patch('mozillians.common.authbackend.get_audience')
@patch('mozillians.common.authbackend.RemoteVerifier.verify')
def test_post_change_email(self, verify_mock, get_audience_mock,
login_success_mock):
def test_post_add_email(self, verify_mock, get_audience_mock,
login_success_mock):
user = UserFactory.create(email='la@example.com')
Verify = BrowserIDVerify()
request_mock = Mock()
Expand All @@ -76,30 +80,33 @@ def test_post_change_email(self, verify_mock, get_audience_mock,
verify_mock.assert_called_with('assertion', 'audience')
get_audience_mock.assert_called_with(request_mock)
login_success_mock.assert_called_with()
eq_(user.email, 'foo@example.com')
ok_(Verify.change_email)

def test_failure_url_email_change(self):
emails = ExternalAccount.objects.filter(type=ExternalAccount.TYPE_EMAIL,
identifier='foo@example.com',
user=user.userprofile)
ok_(emails.exists())
ok_(Verify.add_email)

def test_failure_url_add_email(self):
Verify = BrowserIDVerify()
Verify.change_email = True
Verify.add_email = True
user = UserFactory.create(email='la@example.com')
request_mock = Mock()
request_mock.user.is_authenticated.return_value = True
request_mock.user = user
url = Verify.failure_url
eq_(url, '/user/edit/')
eq_(url, '/user/edit/emails/')

def test_login_success_email_change(self):
def test_login_success_add_email(self):
Verify = BrowserIDVerify()
Verify.change_email = True
Verify.add_email = True
user = UserFactory.create(email='la@example.com')
request_mock = Mock()
request_mock.user.is_authenticated.return_value = True
request_mock.user = user
Verify.user = user
response = loads(Verify.login_success().content)
eq_(response['redirect'], '/u/{0}/'.format(user.username))
eq_(response['email'], 'la@example.com')
eq_(response['redirect'], '/user/edit/emails/')


class MozilliansAuthBackendTests(TestCase):
Expand Down
2 changes: 1 addition & 1 deletion mozillians/phonebook/forms.py
Expand Up @@ -171,7 +171,7 @@ class Meta:
'title', 'allows_mozilla_sites',
'date_mozillian', 'story_link', 'timezone',
'privacy_photo', 'privacy_full_name', 'privacy_ircname',
'privacy_email', 'privacy_timezone', 'privacy_tshirt',
'privacy_timezone', 'privacy_tshirt',
'privacy_bio', 'privacy_geo_city', 'privacy_geo_region',
'privacy_geo_country', 'privacy_groups',
'privacy_skills', 'privacy_languages',
Expand Down
22 changes: 0 additions & 22 deletions mozillians/phonebook/tests/test_views/test_views_misc.py
Expand Up @@ -202,28 +202,6 @@ def test_logout_vouched(self, logout_mock):
ok_(logout_mock.called)


class EmailChangeTests(TestCase):
@patch('mozillians.phonebook.views.forms.ProfileForm')
def test_email_change_verification_redirection(self, profile_form_mock):
profile_form_mock().is_valid.return_value = True
user = UserFactory.create(email='old@example.com')
data = {'full_name': 'foobar',
'email': 'new@example.com',
'username': user.username,
'externalaccount_set-MAX_NUM_FORMS': '1000',
'externalaccount_set-INITIAL_FORMS': '0',
'externalaccount_set-TOTAL_FORMS': '0',
'language_set-MAX_NUM_FORMS': '1000',
'language_set-INITIAL_FORMS': '0',
'language_set-TOTAL_FORMS': '0',
}
url = reverse('phonebook:profile_edit', prefix='/en-US/')
with self.login(user) as client:
response = client.post(url, data=data, follow=True)
self.assertTemplateUsed(response, 'phonebook/verify_email.html')
eq_(user.email, 'old@example.com')


class ImageTests(TestCase):
def _upload_photo(self, user, file_path):
"""Helper for the next methods."""
Expand Down
11 changes: 1 addition & 10 deletions mozillians/phonebook/views.py
Expand Up @@ -184,12 +184,7 @@ def edit_profile(request):
'lat': profile.lat,
'lng': profile.lng})

email_form = forms.EmailForm(request.POST or None,
initial={'email': request.user.email,
'user_id': request.user.id})

all_forms = [user_form, profile_form, accounts_formset, email_form,
language_formset]
all_forms = [user_form, profile_form, accounts_formset, language_formset]

# Using ``list`` to force calling is_valid on all the forms, even if earlier
# ones are not valid, so we detect and display all the errors.
Expand All @@ -209,15 +204,11 @@ def edit_profile(request):
_(u'You changed your username; please note your '
u'profile URL has also changed.'))

if email_form.email_changed():
return render(request, 'phonebook/verify_email.html',
{'email': email_form.cleaned_data['email']})
return redirect('phonebook:profile_view', user.username)

data = dict(profile_form=profile_form,
user_form=user_form,
accounts_formset=accounts_formset,
email_form=email_form,
user_groups=user_groups,
profile=request.user.userprofile,
apps=user.apiapp_set.filter(is_active=True),
Expand Down
5 changes: 0 additions & 5 deletions mozillians/templates/phonebook/edit_profile.html
Expand Up @@ -122,11 +122,6 @@ <h2>{{ _('Basic Information') }}</h2>
{{ privacy_field(profile_form.privacy_full_name) }}
</fieldset>

<fieldset>
{{ mozillians_field(email_form.email) }}
{{ privacy_field(profile_form.privacy_email) }}
</fieldset>

<fieldset>
{{ mozillians_field(profile_form.bio) }}
<span class="under-input-field">
Expand Down
26 changes: 0 additions & 26 deletions mozillians/templates/phonebook/verify_email.html

This file was deleted.

0 comments on commit 2ca621c

Please sign in to comment.