def register(self, request, **cleaned_data): username, email, password = cleaned_data['username'], cleaned_data['email'], cleaned_data['password1'] get_user_model().objects.create_user(username, email, password) new_user = authenticate(username=username, password=password) login(request, new_user) signals.user_registered.send(sender=self.__class__, user=new_user, request=request) return new_user
def test_activation_form(self): """ Test that ``ActivationForm`` enforces username constraints and matching passwords. """ User = get_user_model() # Create a user so we can verify that duplicate usernames aren't # permitted. User.objects.create_user('alice', '*****@*****.**', 'secret') invalid_data_dicts = [ # Mismatched passwords. {'data': {'password1': 'foo', 'password2': 'bar'}, 'error': ('__all__', [u"The two password fields didn't match."])}, ] for invalid_dict in invalid_data_dicts: form = forms.ActivationForm(data=invalid_dict['data']) self.failIf(form.is_valid()) self.assertEqual(form.errors[invalid_dict['error'][0]], invalid_dict['error'][1]) form = forms.ActivationForm(data={'password1': 'foo', 'password2': 'foo'}) self.failUnless(form.is_valid())
def test_registration_form_unique_email(self): """ Test that ``RegistrationFormUniqueEmail`` validates uniqueness of email addresses. """ User = get_user_model() # Create a user so we can verify that duplicate addresses # aren't permitted. User.objects.create_user('alice', '*****@*****.**', 'secret') form = forms.RegistrationFormUniqueEmail( data={ 'username': '******', 'email1': '*****@*****.**', 'email2': '*****@*****.**' }) self.failIf(form.is_valid()) self.assertEqual(form.errors['email1'], [ "This email address is already in use. Please supply a different email address." ]) form = forms.RegistrationFormUniqueEmail( data={ 'username': '******', 'email1': '*****@*****.**', 'email2': '*****@*****.**' }) self.failUnless(form.is_valid())
def register(self, username, email, site, send_email=True): """register new user with ``username`` and ``email`` Create a new, inactive ``User``, generate a ``RegistrationProfile`` and email notification to the ``User``, returning the new ``User``. By default, a registration email will be sent to the new user. To disable this, pass ``send_email=False``. A registration email will be generated by ``registration/registration_email.txt`` and ``registration/registration_email_subject.txt``. The user created by this method has no usable password and it will be set after activation. This method is transactional. Thus if some exception has occur in this method, the newly created user will be rollbacked. """ User = get_user_model() new_user = User.objects.create_user(username, email, 'password') new_user.set_unusable_password() new_user.is_active = False new_user.save() profile = self.create(user=new_user) if send_email: profile.send_registration_email(site) return new_user
def test_activation_form(self): """ Test that ``ActivationForm`` enforces username constraints and matching passwords. """ User = get_user_model() # Create a user so we can verify that duplicate usernames aren't # permitted. User.objects.create_user("alice", "*****@*****.**", "secret") invalid_data_dicts = [ # Mismatched passwords. { "data": {"password1": "foo", "password2": "bar"}, "error": ("__all__", ["The two password fields didn't match."]), } ] for invalid_dict in invalid_data_dicts: form = forms.ActivationForm(data=invalid_dict["data"]) self.failIf(form.is_valid()) self.assertEqual(form.errors[invalid_dict["error"][0]], invalid_dict["error"][1]) form = forms.ActivationForm(data={"password1": "foo", "password2": "foo"}) self.failUnless(form.is_valid())
def test_activation_form(self): """ Test that ``ActivationForm`` enforces username constraints and matching passwords. """ User = get_user_model() # Create a user so we can verify that duplicate usernames aren't # permitted. User.objects.create_user('alice', '*****@*****.**', 'secret') invalid_data_dicts = [ # Mismatched passwords. { 'data': { 'password1': 'foo', 'password2': 'bar' }, 'error': ('__all__', ["The two password fields didn't match."]) }, ] for invalid_dict in invalid_data_dicts: form = forms.ActivationForm(data=invalid_dict['data']) self.failIf(form.is_valid()) self.assertEqual(form.errors[invalid_dict['error'][0]], invalid_dict['error'][1]) form = forms.ActivationForm(data={ 'password1': 'foo', 'password2': 'foo' }) self.failUnless(form.is_valid())
def test_activation_view_post_success(self): """ A ``POST`` to the ``ActivationView`` view with valid data properly handles a valid activation """ User = get_user_model() new_user = self.backend.register(username='******', email='*****@*****.**', request=self.mock_request) new_user = self.backend.accept(new_user.registration_profile, request=self.mock_request) activation_url = reverse( 'registration_activate', kwargs={ 'activation_key': new_user.registration_profile.activation_key }) response = self.client.post(activation_url, { 'password1': 'swordfish', 'password2': 'swordfish' }) success_redirect = reverse('registration_activation_complete') if django.VERSION < (2, 0): success_redirect = 'http://testserver%s' % success_redirect self.assertRedirects(response, success_redirect) # RegistrationProfile should be removed with activation self.assertEqual(RegistrationProfile.objects.count(), 0) # registration, acceptance and activation self.assertEqual(len(mail.outbox), 3) self.failUnless(User.objects.get(username='******').is_active)
def create_inactive_user(self, username, email, password, site, send_email=True): """ Create a new, inactive ``User``, generate a ``RegistrationProfile`` and email its activation key to the ``User``, returning the new ``User``. By default, an activation email will be sent to the new user. To disable this, pass ``send_email=False``. """ new_user = get_user_model().objects.create_user( username, email, password) new_user.is_active = False new_user.save() registration_profile = self.create_profile(new_user) if send_email: registration_profile.send_activation_email(site) return new_user
def clean_email1(self): """Validate that the supplied email address is unique for the site.""" User = get_user_model() if User.objects.filter(email__iexact=self.cleaned_data['email1']): raise forms.ValidationError( _("This email address is already in use. " "Please supply a different email address.")) return self.cleaned_data['email1']
def clean_email1(self): """Validate that the supplied email address is unique for the site.""" User = get_user_model() if User.objects.filter(email__iexact=self.cleaned_data['email1']): raise forms.ValidationError(_( "This email address is already in use. " "Please supply a different email address.")) return self.cleaned_data['email1']
def test_registration_form(self): """ Test that ``RegistrationForm`` enforces username constraints and matching passwords. """ User = get_user_model() # Create a user so we can verify that duplicate usernames aren't # permitted. User.objects.create_user('alice', '*****@*****.**', 'secret') invalid_data_dicts = [ # Non-alphanumeric username. { 'data': { 'username': '******', 'email1': '*****@*****.**', 'email2': '*****@*****.**' }, 'error': ('username', [ "This value must contain only letters, numbers and underscores." ]) }, # Already-existing username. { 'data': { 'username': '******', 'email1': '*****@*****.**', 'email2': '*****@*****.**' }, 'error': ('username', ["A user with that username already exists."]) }, # Mismatched email. { 'data': { 'username': '******', 'email1': '*****@*****.**', 'email2': '*****@*****.**' }, 'error': ('__all__', ["The two email fields didn't match."]) }, ] for invalid_dict in invalid_data_dicts: form = forms.RegistrationForm(data=invalid_dict['data']) self.failIf(form.is_valid()) self.assertEqual(form.errors[invalid_dict['error'][0]], invalid_dict['error'][1]) form = forms.RegistrationForm( data={ 'username': '******', 'email1': '*****@*****.**', 'email2': '*****@*****.**' }) self.failUnless(form.is_valid())
def setUp(self): User = get_user_model() self.backend = DefaultRegistrationBackend() self.mock_request = mock_request() self.admin = User.objects.create_superuser(username='******', email='*****@*****.**', password='******') self.client.login(username='******', password='******') self.admin_url = reverse('admin:index')
def setUp(self): User = get_user_model() self.backend = DefaultRegistrationBackend() self.mock_request = mock_request() self.admin = User.objects.create_superuser( username='******', email='*****@*****.**', password='******') self.client.login(username='******', password='******') self.admin_url = reverse('admin:index')
def test_registration_form(self): """ Test that ``RegistrationForm`` enforces username constraints and matching passwords. """ # Create a user so we can verify that duplicate usernames aren't # permitted. get_user_model().objects.create_user('alice', '*****@*****.**', 'secret') invalid_data_dicts = [ # Non-alphanumeric username. {'data': {'username': '******', 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'foo'}, 'error': ('username', [u"This value may contain only letters, numbers and @/./+/-/_ characters."])}, # Already-existing username. {'data': {'username': '******', 'email': '*****@*****.**', 'password1': 'secret', 'password2': 'secret'}, 'error': ('username', [u"A user with that username already exists."])}, # Mismatched passwords. {'data': {'username': '******', 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'bar'}, 'error': ('__all__', [u"The two password fields didn't match."])}, ] for invalid_dict in invalid_data_dicts: form = forms.RegistrationForm(data=invalid_dict['data']) self.failIf(form.is_valid()) self.assertEqual(form.errors[invalid_dict['error'][0]], invalid_dict['error'][1]) form = forms.RegistrationForm(data={'username': '******', 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'foo'}) self.failUnless(form.is_valid())
def clean_username(self): """ Validate that the username is alphanumeric and is not already in use. """ User = get_user_model() try: User.objects.get(username__iexact=self.cleaned_data['username']) except User.DoesNotExist: return self.cleaned_data['username'] raise forms.ValidationError( _("A user with that username already exists."))
def test_activation_email(self): """ ``RegistrationProfile.send_activation_email`` sends an email. """ new_user = get_user_model().objects.create_user(**self.user_info) profile = RegistrationProfile.objects.create_profile(new_user) profile.send_activation_email(Site.objects.get_current()) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user_info['email']])
def clean_username(self): """ Validate that the username is alphanumeric and is not already in use. """ User = get_user_model() try: User.objects.get(username__iexact=self.cleaned_data['username']) except User.DoesNotExist: return self.cleaned_data['username'] raise forms.ValidationError(_( "A user with that username already exists."))
def test_registration_form_unique_email(self): """ Test that ``RegistrationFormUniqueEmail`` validates uniqueness of email addresses. """ # Create a user so we can verify that duplicate addresses # aren't permitted. get_user_model().objects.create_user('alice', '*****@*****.**', 'secret') form = forms.RegistrationFormUniqueEmail(data={'username': '******', 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'foo'}) self.failIf(form.is_valid()) self.assertEqual(form.errors['email'], [u"This email address is already in use. Please supply a different email address."]) form = forms.RegistrationFormUniqueEmail(data={'username': '******', 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'foo'}) self.failUnless(form.is_valid())
def delete_expired_users(self): """ Remove expired instances of ``RegistrationProfile`` and their associated ``User``s. Accounts to be deleted are identified by searching for instances of ``RegistrationProfile`` with expired activation keys, and then checking to see if their associated ``User`` instances have the field ``is_active`` set to ``False``; any ``User`` who is both inactive and has an expired activation key will be deleted. It is recommended that this method be executed regularly as part of your routine site maintenance; this application provides a custom management command which will call this method, accessible as ``manage.py cleanupregistration``. Regularly clearing out accounts which have never been activated serves two useful purposes: 1. It alleviates the ocasional need to reset a ``RegistrationProfile`` and/or re-send an activation email when a user does not receive or does not act upon the initial activation email; since the account will be deleted, the user will be able to simply re-register and receive a new activation key. 2. It prevents the possibility of a malicious user registering one or more accounts and never activating them (thus denying the use of those usernames to anyone else); since those accounts will be deleted, the usernames will become available for use again. If you have a troublesome ``User`` and wish to disable their account while keeping it in the database, simply delete the associated ``RegistrationProfile``; an inactive ``User`` which does not have an associated ``RegistrationProfile`` will not be deleted. """ for profile in self.all(): try: if profile.activation_key_expired(): user = profile.user if not user.is_active: user.delete() profile.delete() except get_user_model().DoesNotExist: profile.delete()
def test_profile_creation(self): """ Creating a registration profile for a user populates the profile with the correct user and a SHA1 hash to use as activation key. """ new_user = get_user_model().objects.create_user(**self.user_info) profile = RegistrationProfile.objects.create_profile(new_user) self.assertEqual(RegistrationProfile.objects.count(), 1) self.assertEqual(profile.user.id, new_user.id) self.failUnless(re.match('^[a-f0-9]{40}$', profile.activation_key)) self.assertEqual(unicode(profile), "Registration information for alice")
def test_untreated_activation(self): User = get_user_model() new_user = self.backend.register(username='******', email='*****@*****.**', request=self.mock_request) profile = new_user.registration_profile activated_user = self.backend.activate( activation_key=profile.activation_key, request=self.mock_request, password='******') self.failIf(activated_user) new_user = User.objects.get(pk=new_user.pk) self.failIf(new_user.is_active) self.failIf(new_user.has_usable_password())
def test_untreated_activation(self): User = get_user_model() new_user = self.backend.register( username='******', email='*****@*****.**', request=self.mock_request) profile = new_user.registration_profile activated_user = self.backend.activate( activation_key=profile.activation_key, request=self.mock_request, password='******') self.failIf(activated_user) new_user = User.objects.get(pk=new_user.pk) self.failIf(new_user.is_active) self.failIf(new_user.has_usable_password())
def test_management_command(self): """ The ``cleanupregistration`` management command properly deletes expired accounts. """ new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(), **self.user_info) expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(), username='******', password='******', email='*****@*****.**') expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) expired_user.save() management.call_command('cleanupregistration') self.assertEqual(RegistrationProfile.objects.count(), 1) self.assertRaises(User.DoesNotExist, get_user_model().objects.get, username='******')
def test_expired_user_deletion(self): """ ``RegistrationProfile.objects.delete_expired_users()`` only deletes inactive users whose activation window has expired. """ new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(), **self.user_info) expired_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(), username='******', password='******', email='*****@*****.**') expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) expired_user.save() RegistrationProfile.objects.delete_expired_users() self.assertEqual(RegistrationProfile.objects.count(), 1) self.assertRaises(User.DoesNotExist, get_user_model().objects.get, username='******')
def test_expired_activation(self): User = get_user_model() expired_user = self.backend.register( username='******', email='*****@*****.**', request=self.mock_request) profile = expired_user.registration_profile self.backend.accept(profile, request=self.mock_request) expired_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS+1) expired_user.save() activated_user = self.backend.activate( activation_key=profile.activation_key, request=self.mock_request, password='******') self.failIf(activated_user) expired_user = User.objects.get(pk=expired_user.pk) self.failIf(expired_user.is_active) self.failIf(expired_user.has_usable_password())
def create_inactive_user(self, username, email, password, site, send_email=True): """ Create a new, inactive ``User``, generate a ``RegistrationProfile`` and email its activation key to the ``User``, returning the new ``User``. By default, an activation email will be sent to the new user. To disable this, pass ``send_email=False``. """ new_user = get_user_model().objects.create_user(username, email, password) new_user.is_active = False new_user.save() registration_profile = self.create_profile(new_user) if send_email: registration_profile.send_activation_email(site) return new_user
def test_expired_activation(self): User = get_user_model() expired_user = self.backend.register(username='******', email='*****@*****.**', request=self.mock_request) profile = expired_user.registration_profile self.backend.accept(profile, request=self.mock_request) expired_user.date_joined -= datetime.timedelta( days=settings.ACCOUNT_ACTIVATION_DAYS + 1) expired_user.save() activated_user = self.backend.activate( activation_key=profile.activation_key, request=self.mock_request, password='******') self.failIf(activated_user) expired_user = User.objects.get(pk=expired_user.pk) self.failIf(expired_user.is_active) self.failIf(expired_user.has_usable_password())
def test_expired_activation(self): """ Attempting to activate outside the permitted window does not activate the account. """ new_user = RegistrationProfile.objects.create_inactive_user(site=Site.objects.get_current(), **self.user_info) new_user.date_joined -= datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) new_user.save() profile = RegistrationProfile.objects.get(user=new_user) activated = RegistrationProfile.objects.activate_user(profile.activation_key) self.failIf(isinstance(activated, User)) self.failIf(activated) new_user = get_user_model().objects.get(username='******') self.failIf(new_user.is_active) profile = RegistrationProfile.objects.get(user=new_user) self.assertNotEqual(profile.activation_key, RegistrationProfile.ACTIVATED)
def test_registration_form(self): """ Test that ``RegistrationForm`` enforces username constraints and matching passwords. """ User = get_user_model() # Create a user so we can verify that duplicate usernames aren't # permitted. User.objects.create_user("alice", "*****@*****.**", "secret") invalid_data_dicts = [ # Non-alphanumeric username. { "data": {"username": "******", "email1": "*****@*****.**", "email2": "*****@*****.**"}, "error": ("username", ["This value must contain only letters, numbers and underscores."]), }, # Already-existing username. { "data": {"username": "******", "email1": "*****@*****.**", "email2": "*****@*****.**"}, "error": ("username", ["A user with that username already exists."]), }, # Mismatched email. { "data": {"username": "******", "email1": "*****@*****.**", "email2": "*****@*****.**"}, "error": ("__all__", ["The two email fields didn't match."]), }, ] for invalid_dict in invalid_data_dicts: form = forms.RegistrationForm(data=invalid_dict["data"]) self.failIf(form.is_valid()) self.assertEqual(form.errors[invalid_dict["error"][0]], invalid_dict["error"][1]) form = forms.RegistrationForm( data={"username": "******", "email1": "*****@*****.**", "email2": "*****@*****.**"} ) self.failUnless(form.is_valid())
def test_registration_form_unique_email(self): """ Test that ``RegistrationFormUniqueEmail`` validates uniqueness of email addresses. """ User = get_user_model() # Create a user so we can verify that duplicate addresses # aren't permitted. User.objects.create_user("alice", "*****@*****.**", "secret") form = forms.RegistrationFormUniqueEmail( data={"username": "******", "email1": "*****@*****.**", "email2": "*****@*****.**"} ) self.failIf(form.is_valid()) self.assertEqual( form.errors["email1"], ["This email address is already in use. Please supply a different email address."] ) form = forms.RegistrationFormUniqueEmail( data={"username": "******", "email1": "*****@*****.**", "email2": "*****@*****.**"} ) self.failUnless(form.is_valid())
def test_activation_view_post_success(self): """ A ``POST`` to the ``ActivationView`` view with valid data properly handles a valid activation """ User = get_user_model() new_user = self.backend.register(username='******', email='*****@*****.**', request=self.mock_request) new_user = self.backend.accept(new_user.registration_profile, request=self.mock_request) activation_url = reverse('registration_activate', kwargs={ 'activation_key': new_user.registration_profile.activation_key}) response = self.client.post(activation_url,{ 'password1': 'swordfish', 'password2': 'swordfish'}) success_redirect = 'http://testserver%s' % reverse('registration_activation_complete') self.assertRedirects(response, success_redirect) # RegistrationProfile should be removed with activation self.assertEqual(RegistrationProfile.objects.count(), 0) # registration, acceptance and activation self.assertEqual(len(mail.outbox), 3) self.failUnless(User.objects.get(username='******').is_active)
def test_registration(self): """ Registration creates a new account and logs the user in. """ resp = self.client.post(reverse('registration_register'), data={'username': '******', 'email': '*****@*****.**', 'password1': 'secret', 'password2': 'secret'}) new_user = get_user_model().objects.get(username='******') self.assertEqual(302, resp.status_code) self.failUnless(new_user.get_absolute_url() in resp['Location']) self.failUnless(new_user.check_password('secret')) self.assertEqual(new_user.email, '*****@*****.**') # New user must be active. self.failUnless(new_user.is_active) # New user must be logged in. resp = self.client.get(reverse('registration_register')) self.failUnless(resp.context['user'].is_authenticated())
def test_get_activation_complete_url(self): User = get_user_model() fake_user = User() url = self.backend.get_activation_complete_url(fake_user) self.assertEqual(url, reverse('registration_activation_complete'))
def test_management_command_cleanupregistration(self): RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) new_accepted_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) new_rejected_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_untreated_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_accepted_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_rejected_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) RegistrationProfile.objects.accept_registration( new_accepted_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.reject_registration( new_rejected_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.accept_registration( expired_accepted_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.reject_registration( expired_rejected_user.registration_profile, site=self.mock_site, ) delta = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) expired_untreated_user.date_joined -= delta expired_untreated_user.save() expired_accepted_user.date_joined -= delta expired_accepted_user.save() expired_rejected_user.date_joined -= delta expired_rejected_user.save() # django-registration compatibility management.call_command('cleanupregistration') # new_rejected_user, expired rejected_user and expired_accepted_user # are deleted User = get_user_model() self.assertEqual(RegistrationProfile.objects.count(), 3) self.assertRaises(User.DoesNotExist, User.objects.get, username='******') self.assertRaises(User.DoesNotExist, User.objects.get, username='******') self.assertRaises(User.DoesNotExist, User.objects.get, username='******')
def test_rejected_user_deletion(self): RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) new_accepted_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) new_rejected_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_untreated_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_accepted_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_rejected_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) RegistrationProfile.objects.accept_registration( new_accepted_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.reject_registration( new_rejected_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.accept_registration( expired_accepted_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.reject_registration( expired_rejected_user.registration_profile, site=self.mock_site, ) delta = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) expired_untreated_user.date_joined -= delta expired_untreated_user.save() expired_accepted_user.date_joined -= delta expired_accepted_user.save() expired_rejected_user.date_joined -= delta expired_rejected_user.save() RegistrationProfile.objects.delete_rejected_users() # new_rejected_user and expired_rejected_user are deleted User = get_user_model() self.assertEqual(RegistrationProfile.objects.count(), 4) self.assertRaises(User.DoesNotExist, User.objects.get, username='******') self.assertRaises(User.DoesNotExist, User.objects.get, username='******')
def create_inactive_user(self): User = get_user_model() new_user = User.objects.create_user(**self.user_info) new_user.set_unusable_password() new_user.is_active = False return new_user
def test_expired_user_deletion(self): RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) new_accepted_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) new_rejected_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_untreated_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_accepted_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_rejected_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) RegistrationProfile.objects.accept_registration( new_accepted_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.reject_registration( new_rejected_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.accept_registration( expired_accepted_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.reject_registration( expired_rejected_user.registration_profile, site=self.mock_site, ) delta = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS+1) expired_untreated_user.date_joined -= delta expired_untreated_user.save() expired_accepted_user.date_joined -= delta expired_accepted_user.save() expired_rejected_user.date_joined -= delta expired_rejected_user.save() RegistrationProfile.objects.delete_expired_users() # Only expired accepted user is deleted User = get_user_model() self.assertEqual(RegistrationProfile.objects.count(), 5) self.assertRaises(User.DoesNotExist, User.objects.get, username='******')
def test_management_command_cleanupregistration(self): RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) new_accepted_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) new_rejected_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_untreated_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_accepted_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) expired_rejected_user = RegistrationProfile.objects.register( username='******', email='*****@*****.**', site=self.mock_site, ) RegistrationProfile.objects.accept_registration( new_accepted_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.reject_registration( new_rejected_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.accept_registration( expired_accepted_user.registration_profile, site=self.mock_site, ) RegistrationProfile.objects.reject_registration( expired_rejected_user.registration_profile, site=self.mock_site, ) delta = datetime.timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS+1) expired_untreated_user.date_joined -= delta expired_untreated_user.save() expired_accepted_user.date_joined -= delta expired_accepted_user.save() expired_rejected_user.date_joined -= delta expired_rejected_user.save() # django-registration compatibility management.call_command('cleanupregistration') # new_rejected_user, expired rejected_user and expired_accepted_user are deleted User = get_user_model() self.assertEqual(RegistrationProfile.objects.count(), 3) self.assertRaises(User.DoesNotExist, User.objects.get, username='******') self.assertRaises(User.DoesNotExist, User.objects.get, username='******') self.assertRaises(User.DoesNotExist, User.objects.get, username='******')