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 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 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_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_activation_invalid_key(self): '''Attempting to activate with a key which is not a SHA1 hash fails.''' self.assertFalse(RegistrationProfile.activate_user('foo'))