def test_user_creation_no_email(self): '''Passing ``send_email=False`` when creating a new user will not send an activation email. ''' site = Site.get_current() RegistrationProfile.create_inactive_user(send_email=False, site=site, **self.user_info) self.assertEqual(len(mail.outbox), 0)
def test_activation_already_activated(self): '''Attempting to re-activate an already-activated account fails.''' new_user = self.create_inactive_user() profile = self.session.query(RegistrationProfile) \ .filter_by(user=new_user) \ .first() RegistrationProfile.activate_user(profile.activation_key) profile = self.session.query(RegistrationProfile) \ .filter_by(user=new_user) \ .first() key = profile.activation_key self.assertFalse(RegistrationProfile.activate_user(key))
def test_expired_activation(self): '''Attempting to activate outside the permitted window does not activate the account. ''' new_user = self.create_inactive_user() new_user.date_joined -= \ timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) self.session.commit() profile = self.session.query(RegistrationProfile) \ .filter_by(user=new_user) \ .first() activated = RegistrationProfile.activate_user(profile.activation_key) self.assertFalse(isinstance(activated, User)) self.assertFalse(activated) new_user = self.session.query(User) \ .filter_by(username=u'alice') \ .first() self.assertFalse(new_user.is_active) profile = self.session.query(RegistrationProfile) \ .filter_by(user=new_user) \ .first() self.assertNotEqual(profile.activation_key, RegistrationProfile.ACTIVATED)
def register(self, request, **kwargs): """Given a username, email address and password, register a new user account, which will initially be inactive. Along with the new ``User`` object, a new ``registration.models.RegistrationProfile`` will be created, tied to that ``User``, containing the activation key which will be used for this account. An email will be sent to the supplied email address; this email should contain an activation link. The email will be rendered using two templates. See the documentation for ``RegistrationProfile.send_activation_email()`` for information about these templates and the contexts provided to them. After the ``User`` and ``RegistrationProfile`` are created and the activation email is sent, the signal ``registration.signals.user_registered`` will be sent, with the new ``User`` as the keyword argument ``user`` and the class of this backend as the sender. """ username, email, password = (kwargs["username"], kwargs["email"], kwargs["password1"]) site = get_current_site(request) new_user = RegistrationProfile.create_inactive_user(username, email, password, site) signals.user_registered.send(sender=self.__class__, user=new_user, request=request) return new_user
def register(self, request, **kwargs): '''Given a username, email address and password, register a new user account, which will initially be inactive. Along with the new ``User`` object, a new ``registration.models.RegistrationProfile`` will be created, tied to that ``User``, containing the activation key which will be used for this account. An email will be sent to the supplied email address; this email should contain an activation link. The email will be rendered using two templates. See the documentation for ``RegistrationProfile.send_activation_email()`` for information about these templates and the contexts provided to them. After the ``User`` and ``RegistrationProfile`` are created and the activation email is sent, the signal ``registration.signals.user_registered`` will be sent, with the new ``User`` as the keyword argument ``user`` and the class of this backend as the sender. ''' username, email, password = (kwargs['username'], kwargs['email'], kwargs['password1']) site = get_current_site(request) new_user = RegistrationProfile.create_inactive_user(username, email, password, site) signals.user_registered.send(sender=self.__class__, user=new_user, request=request) return new_user
def test_activation_email(self): ''':meth:`RegistrationProfile.send_activation_email` sends an email. ''' new_user = User.create_user(**self.user_info) profile = RegistrationProfile.create_profile(new_user) profile.send_activation_email(Site.get_current()) self.assertEqual(len(mail.outbox), 1) self.assertEqual(mail.outbox[0].to, [self.user_info['email']])
def test_activation_nonexistent_key(self): """ Attempting to activate with a non-existent key (i.e., one not associated with any account) fails. """ # Due to the way activation keys are constructed during # registration, this will never be a valid key. invalid_key = sha_constructor('foo').hexdigest() self.assertFalse(RegistrationProfile.activate_user(invalid_key))
def activate(self, request, activation_key): """Given an an activation key, look up and activate the user account corresponding to that key (if possible). After successful activation, the signal ``registration.signals.user_activated`` will be sent, with the newly activated ``User`` as the keyword argument ``user`` and the class of this backend as the sender. """ activated = RegistrationProfile.activate_user(activation_key) if activated: signals.user_activated.send(sender=self.__class__, user=activated, request=request) return activated
def test_expired_user_deletion(self): ''':meth:`RegistrationProfile.delete_expired_users` only deletes inactive users whose activation window has expired. ''' site = Site.get_current() self.create_inactive_user() expired_user = \ RegistrationProfile.create_inactive_user(username='******', password='******', email='*****@*****.**', session=self.session, site=site) expired_user.date_joined -= \ timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) self.session.commit() RegistrationProfile.delete_expired_users() ct = self.session.query(RegistrationProfile).count() self.assertEqual(ct, 1) user_count = self.session.query(User) \ .filter_by(username=u'bob') \ .count() self.assertEqual(user_count, 0)
def activate(self, request, activation_key): '''Given an an activation key, look up and activate the user account corresponding to that key (if possible). After successful activation, the signal ``registration.signals.user_activated`` will be sent, with the newly activated ``User`` as the keyword argument ``user`` and the class of this backend as the sender. ''' activated = RegistrationProfile.activate_user(activation_key) if activated: signals.user_activated.send(sender=self.__class__, user=activated, request=request) return activated
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 = User.create_user(**self.user_info) profile = RegistrationProfile.create_profile(new_user) ct = self.session.query(RegistrationProfile).count() self.assertEqual(ct, 1) self.assertEqual(profile.user.id, new_user.id) self.assertRegexpMatches(profile.activation_key, '^[a-f0-9]{40}$') self.assertEqual(unicode(profile), u'Registration information for alice')
def test_valid_activation(self): '''Activating a user within the permitted window makes the account active, and resets the activation key. ''' new_user = self.create_inactive_user() profile = self.session.query(RegistrationProfile) \ .filter_by(user=new_user) \ .first() activated = RegistrationProfile.activate_user(profile.activation_key) self.assertTrue(isinstance(activated, User)) self.assertEqual(activated.id, new_user.id) self.assertTrue(activated.is_active) profile = self.session.query(RegistrationProfile) \ .filter_by(user=new_user) \ .first() self.assertEqual(profile.activation_key, RegistrationProfile.ACTIVATED)
def test_management_command(self): '''The ``cleanupregistration`` management command properly deletes expired accounts. ''' site = Site.get_current() self.create_inactive_user() expired_user = \ RegistrationProfile.create_inactive_user(username='******', password='******', email='*****@*****.**', session=self.session, site=site) expired_user.date_joined -= \ timedelta(days=settings.ACCOUNT_ACTIVATION_DAYS + 1) self.session.commit() management.call_command('cleanupregistration') ct = self.session.query(RegistrationProfile).count() self.assertEqual(ct, 1) user_count = self.session.query(User) \ .filter_by(username=u'bob') \ .count() self.assertEqual(user_count, 0)
def handle_noargs(self, **options): RegistrationProfile.delete_expired_users()
def test_activation_invalid_key(self): '''Attempting to activate with a key which is not a SHA1 hash fails.''' self.assertFalse(RegistrationProfile.activate_user('foo'))
def create_inactive_user(self): site = Site.get_current() return RegistrationProfile.create_inactive_user(site=site, **self.user_info)