Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

More detailed user activation failure emails and stats. [bug 672848]

  • Loading branch information...
commit 4a7491c83ff6121802931008958eb423259a4ac4 1 parent b7fbe4a
@rlr rlr authored
View
37 apps/users/models.py
@@ -10,6 +10,7 @@
from django.db import models
from django.template.loader import render_to_string
+from statsd import statsd
from timezones.fields import TimeZoneField
from tower import ugettext as _
from tower import ugettext_lazy as _lazy
@@ -113,7 +114,7 @@ def create_profile(self, user, *args, **kwargs):
class RegistrationManager(ConfirmationManager):
- def activate_user(self, activation_key):
+ def activate_user(self, activation_key, request=None):
"""
Validate an activation key and activate the corresponding
``User`` if valid.
@@ -138,14 +139,32 @@ def activate_user(self, activation_key):
try:
profile = self.get(activation_key=activation_key)
except self.model.DoesNotExist:
- return False
- if not profile.activation_key_expired():
- user = profile.user
- user.is_active = True
- user.save()
- profile.activation_key = self.model.ACTIVATED
- profile.save()
- return user
+ profile = None
+ statsd.incr('user.activate-error.does-not-exist')
+ msg = 'Key: {k}\nRequest:\n'.format(k=activation_key)
+ reason = 'key not found'
+ if profile:
+ if not profile.activation_key_expired():
+ user = profile.user
+ user.is_active = True
+ user.save()
+ profile.activation_key = self.model.ACTIVATED
+ profile.save()
+ return user
+ else:
+ statsd.incr('user.activate-error.expired')
+ msg = 'Username: {u}\nJoined: {d}\nRequest:\n'
+ msg = msg.format(u=profile.user.username,
+ d=profile.user.date_joined)
+ reason = 'key expired'
+ else:
+ statsd.incr('user.activate-error.invalid-key')
+ msg = 'Key: {k}\nRequest:\n'.format(k=activation_key)
+ reason = 'invalid key'
+
+ mail.mail_admins(u'User activation failure ({r})'.format(r=reason),
+ msg + repr(request), fail_silently=True)
+
return False
def create_inactive_user(self, username, password, email,
View
32 apps/users/tests/test_models.py
@@ -1,5 +1,8 @@
+from datetime import timedelta
+
from django.contrib.auth.models import User
from django.contrib.sites.models import Site
+from django.core import mail
import mock
from nose.tools import eq_
@@ -28,3 +31,32 @@ def test_create_inactive_user_locale(self, get_current):
user = RegistrationProfile.objects.create_inactive_user(
'sumouser1234', 'testpass', 'sumouser@test.com', locale='fr')
eq_('fr', user.profile.locale)
+
+ @mock.patch.object(mail, 'mail_admins')
+ def test_activation_key_invalid(self, mail_admins):
+ RegistrationProfile.objects.activate_user('foobar')
+ assert mail_admins.called
+ eq_('User activation failure (invalid key)',
+ mail_admins.call_args[0][0])
+
+ @mock.patch.object(mail, 'mail_admins')
+ def test_activation_key_doesnt_exist(self, mail_admins):
+ RegistrationProfile.objects.activate_user(
+ '1234567890123456789012345678901234567890')
+ assert mail_admins.called
+ eq_('User activation failure (key not found)',
+ mail_admins.call_args[0][0])
+
+ @mock.patch.object(Site.objects, 'get_current')
+ @mock.patch.object(mail, 'mail_admins')
+ def test_activation_key_expired(self, mail_admins, get_current):
+ get_current.return_value.domain = 'testserver'
+ user = RegistrationProfile.objects.create_inactive_user(
+ 'sumouser1234', 'testpass', 'sumouser@test.com')
+ profile = RegistrationProfile.objects.get(user=user)
+ user.date_joined = user.date_joined - timedelta(days=1000)
+ user.save()
+ RegistrationProfile.objects.activate_user(profile.activation_key)
+ assert mail_admins.called
+ eq_('User activation failure (key expired)',
+ mail_admins.call_args[0][0])
View
9 apps/users/views.py
@@ -5,7 +5,6 @@
from django.contrib.auth.forms import PasswordResetForm
from django.contrib.auth.models import User
from django.contrib.auth.tokens import default_token_generator
-from django.core.mail import mail_admins
from django.http import HttpResponseRedirect, Http404
from django.views.decorators.http import require_http_methods, require_GET
from django.shortcuts import get_object_or_404
@@ -80,7 +79,8 @@ def register(request):
def activate(request, activation_key):
"""Activate a User account."""
activation_key = activation_key.lower()
- account = RegistrationProfile.objects.activate_user(activation_key)
+ account = RegistrationProfile.objects.activate_user(activation_key,
+ request)
my_questions = None
form = AuthenticationForm()
if account:
@@ -89,10 +89,7 @@ def activate(request, activation_key):
claim_watches.delay(account)
my_questions = Question.uncached.filter(creator=account)
- else: # There was some issue activating the account.
- statsd.incr('user.activate-error')
- mail_admins(u'User activation failure', repr(request),
- fail_silently=True)
+
return jingo.render(request, 'users/activate.html',
{'account': account, 'questions': my_questions,
'form': form})
Please sign in to comment.
Something went wrong with that request. Please try again.