def test_activation(self): """ Test that user activation actually activates the user and properly resets the activation key, and fails for an already-active or expired user, or an invalid key. """ # Activating a valid user returns the user. self.failUnlessEqual(RegistrationProfile.objects.activate_user(RegistrationProfile.all().filter('user ='******'user ='******'user ='******'t a SHA1 hash returns False. self.failIf(RegistrationProfile.objects.activate_user('foo')) # Activating from a key that doesn't exist returns False. self.failIf(RegistrationProfile.objects.activate_user(sha.new('foo').hexdigest()))
def test_activation_view(self): """ Test that the activation view activates the user from a valid key and fails if the key is invalid or has expired. """ # Valid user puts the user account into the context. response = self.client.get(reverse('registration_activate', kwargs={ 'activation_key': RegistrationProfile.all().filter('user ='******'account'].key(), self.sample_user.key()) # Expired user sets the account to False. response = self.client.get(reverse('registration_activate', kwargs={ 'activation_key': RegistrationProfile.all().filter('user ='******'account']) # Invalid key gets to the view, but sets account to False. response = self.client.get(reverse('registration_activate', kwargs={ 'activation_key': 'foo' })) self.failIf(response.context[0]['account']) # Nonexistent key sets the account to False. response = self.client.get(reverse('registration_activate', kwargs={ 'activation_key': sha.new('foo').hexdigest() })) self.failIf(response.context[0]['account'])
def test_registration_view(self): """ Test that the registration view rejects invalid submissions, and creates a new user and redirects after a valid submission. """ # Invalid data fails. alice = User.all().filter('username ='******'alice').get() alice.is_active = True alice.put() response = self.client.post( reverse('registration_register'), data={ 'username': '******', # Will fail on username uniqueness. 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'foo' }) self.assertEqual(response.status_code, 200) self.failUnless(response.context[0]['form']) self.failUnless(response.context[0]['form'].errors) response = self.client.post(reverse('registration_register'), data={ 'username': '******', 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'foo' }) self.assertEqual(response.status_code, 302) self.assertEqual( response['Location'], 'http://testserver%s' % reverse('registration_complete')) self.assertEqual(RegistrationProfile.all().count(), 3)
def test_signals(self): """ Test that the ``user_registered`` and ``user_activated`` signals are sent, and that they send the ``User`` as an argument. """ def receiver(sender, **kwargs): self.assert_('user' in kwargs) self.assertEqual(kwargs['user'].username, u'signal_test') received_signals.append(kwargs.get('signal')) received_signals = [] expected_signals = [signals.user_registered, signals.user_activated] for signal in expected_signals: signal.connect(receiver) RegistrationProfile.objects.create_inactive_user( username='******', password='******', email='*****@*****.**', send_email=False) RegistrationProfile.objects.activate_user( RegistrationProfile.all().filter( 'user ='******'key_signal_test')).get().activation_key) self.assertEqual(received_signals, expected_signals)
def test_account_expiration_condition(self): """ Test that ``RegistrationProfile.activation_key_expired()`` returns ``True`` for expired users and for active users, and ``False`` otherwise. """ # Unexpired user returns False. self.failIf(RegistrationProfile.all().filter('user ='******'user ='******'user ='******'user =', self.sample_user).get().activation_key_expired())
def test_registration_view(self): """ Test that the registration view rejects invalid submissions, and creates a new user and redirects after a valid submission. """ # Invalid data fails. alice = User.all().filter('username ='******'alice').get() alice.is_active = True alice.put() response = self.client.post(reverse('registration_register'), data={ 'username': '******', # Will fail on username uniqueness. 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'foo' }) self.assertEqual(response.status_code, 200) self.failUnless(response.context[0]['form']) self.failUnless(response.context[0]['form'].errors) response = self.client.post(reverse('registration_register'), data={ 'username': '******', 'email': '*****@*****.**', 'password1': 'foo', 'password2': 'foo' }) self.assertEqual(response.status_code, 302) self.assertEqual(response['Location'], 'http://testserver%s' % reverse('registration_complete')) self.assertEqual(RegistrationProfile.all().count(), 3)
def test_management_command(self): """ Test that ``manage.py cleanupregistration`` functions correctly. """ management.call_command('cleanupregistration') self.assertEqual(RegistrationProfile.all().count(), 1)
def test_expired_user_deletion(self): """ Test that ``RegistrationProfile.objects.delete_expired_users()`` deletes only inactive users whose activation window has expired. """ RegistrationProfile.objects.delete_expired_users() self.assertEqual(RegistrationProfile.all().count(), 1)
def test_account_expiration_condition(self): """ Test that ``RegistrationProfile.activation_key_expired()`` returns ``True`` for expired users and for active users, and ``False`` otherwise. """ # Unexpired user returns False. self.failIf(RegistrationProfile.all().filter( 'user ='******'user ='******'user ='******'user =', self.sample_user).get().activation_key_expired())
def test_activation_view(self): """ Test that the activation view activates the user from a valid key and fails if the key is invalid or has expired. """ # Valid user puts the user account into the context. response = self.client.get( reverse('registration_activate', kwargs={ 'activation_key': RegistrationProfile.all().filter( 'user ='******'account'].key(), self.sample_user.key()) # Expired user sets the account to False. response = self.client.get( reverse('registration_activate', kwargs={ 'activation_key': RegistrationProfile.all().filter( 'user ='******'account']) # Invalid key gets to the view, but sets account to False. response = self.client.get( reverse('registration_activate', kwargs={'activation_key': 'foo'})) self.failIf(response.context[0]['account']) # Nonexistent key sets the account to False. response = self.client.get( reverse('registration_activate', kwargs={'activation_key': sha.new('foo').hexdigest()})) self.failIf(response.context[0]['account'])
def test_activation(self): """ Test that user activation actually activates the user and properly resets the activation key, and fails for an already-active or expired user, or an invalid key. """ # Activating a valid user returns the user. self.failUnlessEqual( RegistrationProfile.objects.activate_user( RegistrationProfile.all().filter( 'user ='******'user ='******'user ='******'t a SHA1 hash returns False. self.failIf(RegistrationProfile.objects.activate_user('foo')) # Activating from a key that doesn't exist returns False. self.failIf( RegistrationProfile.objects.activate_user( sha.new('foo').hexdigest()))
def test_signals(self): """ Test that the ``user_registered`` and ``user_activated`` signals are sent, and that they send the ``User`` as an argument. """ def receiver(sender, **kwargs): self.assert_('user' in kwargs) self.assertEqual(kwargs['user'].username, u'signal_test') received_signals.append(kwargs.get('signal')) received_signals = [] expected_signals = [signals.user_registered, signals.user_activated] for signal in expected_signals: signal.connect(receiver) RegistrationProfile.objects.create_inactive_user(username='******', password='******', email='*****@*****.**', send_email=False) RegistrationProfile.objects.activate_user(RegistrationProfile.all().filter('user ='******'key_signal_test')).get().activation_key) self.assertEqual(received_signals, expected_signals)
def test_registration_profile_created(self): """ Test that a ``RegistrationProfile`` is created for a new user. """ self.assertEqual(RegistrationProfile.all().count(), 2)