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 create_user_profile_signal(sender, instance, created, **kwargs): if created: r_user = RegistrationProfile(user=instance) #print r_user up = UserProfile(registration_profile=r_user) r_user.activation_key = RegistrationProfile.ACTIVATED r_user.save() up.save()
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_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_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 add_user(request): form = UserProfileForm() user_formset = userProfileFormset(instance=RegistrationProfile()) if request.method == 'POST': form = UserProfileForm(request.POST) if form.is_valid(): #user_profile = form.save() data = form.cleaned_data site = Site.objects.get_current() new_user_instance = UserModel().objects.create_user( **form.cleaned_data) new_user = RegistrationProfile.objects.create_inactive_user( new_user=new_user_instance, site=site, send_email=False, request=request, ) #new_user = RegistrationProfile.objects.create_inactive_user(data['username'] , data['email'],data['password'], site) #signals.user_registered.send(sender=self.__class__, user=new_user,request=request) user_formset = userProfileFormset(request.POST, request.FILES, instance=new_user_instance) if user_formset.is_valid(): user_formset.save() return render(request, "userprofiles/registration_complete.html", {}) return render(request, "userprofiles/registration.html", { 'form': form, 'user_formset': user_formset, 'action': 'Create' })
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_activation_email(self): site = Site.objects.get() user = User(email='*****@*****.**') profile = RegistrationProfile(user=user, activation_key='activation-key') profile.send_activation_email(site) self.assertEqual(len(mail.outbox), 1) message = mail.outbox.pop() self.assertEqual(message.subject, 'Activate your djangoproject.com account') self.assertEqual( message.body, "\nSomeone, hopefully you, signed up for a new account at " "djangoproject.com using this email address. If it was you, and " "you'd like to activate and use your account, click the link below " "or copy and paste it into your web browser's address bar:\n\n" "https://www.djangoproject.com/accounts/activate/activation-key/\n\n" "If you didn't request this, you don't need to do anything; you " "won't receive any more email from us, and the account will expire " "automatically in three days.\n" )
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 save(self, validated_data): email = validated_data.get('email') new_user = User( username=email, email=email, is_active=False, ) new_user.save() reg_profile = RegistrationProfile(user=new_user, code_type='RV') reg_profile.save() email = EmailMessage( subject='Discovery Email verification', body= f'Hi \n\nThis is your verification code for registering on Discovery: ' f'{reg_profile.code}\n\n' f'Please proceed with the sign-up process.\n\n' f'Your Discovery team', to=[f"{reg_profile.user.email}"], ) email.send() return new_user
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_create_balafon_contact(self): user = self._create_user() RegistrationProfile(user=user, activation_key=RegistrationProfile.ACTIVATED) self._create_profile_and_check(user)
def autologin(sender, **kwargs): user = kwargs["user"] request = kwargs["request"] # Manually setting the default user backed to avoid the # 'User' object has no attribute 'backend' error user.backend = "django.contrib.auth.backends.ModelBackend" # This login function does not need password. login(request, user) # CIGNo # user_activated.connect(autologin) # add approval flag to RegistrationProfile from registration.models import RegistrationProfile RegistrationProfile.add_to_class("approved", models.BooleanField(default=False)) from django.core.mail import mail_admins from django.contrib.sites.models import RequestSite from django.contrib.sites.models import Site from django.core import urlresolvers from django.db.models import signals from django.template.loader import render_to_string from django.conf import settings def get_current_site(): if Site._meta.installed: return Site.objects.get_current() else: return RequestSite(request)
def test_registration_profile_created(self): """ Test that a ``RegistrationProfile`` is created for a new user. """ self.assertEqual(RegistrationProfile.all().count(), 2)
def testUsers(self): sub1 = Subscription.objects.get(owner=self.user1) sub1.state = SUBSCRIPTION_STATE_FREE sub1.save() user2 = User.objects.get(username='******') sub2 = Subscription.objects.get(owner=user2) sub2.expiration_date = datetime.date.today() + datetime.timedelta(10) sub2.save() user3 = User.objects.create_user('test3', '*****@*****.**', 'test') user4 = User.objects.create_user('test4', '*****@*****.**', 'test') user5 = User.objects.create_user('test5', '*****@*****.**', 'test') user6 = User.objects.create_user('test6', '*****@*****.**', 'test') user7 = User.objects.create_user('test7', '*****@*****.**', 'test') user8 = User.objects.create_user('test8', '*****@*****.**', 'test') user9 = User.objects.create_user('test9', '*****@*****.**', 'test') user10 = User.objects.create_user('test10', '*****@*****.**', 'test') user11 = User.objects.create_user('test11', '*****@*****.**', 'test') user3.is_active = False user3.save() registration = RegistrationProfile() registration.user = user3 registration.activation_key = '1234' registration.save() user4.is_active = False user4.save() profile = user4.get_profile() profile.unregister_datetime = datetime.datetime.now() profile.save() user5.is_active = False user5.save() profile = user5.get_profile() profile.unregister_datetime = datetime.datetime.now() profile.save() sub6 = Subscription.objects.get(owner=user6) sub6.expiration_date = datetime.date.today() - datetime.timedelta(1) sub6.save() sub7 = Subscription.objects.get(owner=user7) sub7.expiration_date = datetime.date.today() - datetime.timedelta(1) sub7.save() sub8 = Subscription.objects.get(owner=user8) sub8.expiration_date = datetime.date.today() - datetime.timedelta(1) sub8.save() sub9 = Subscription() sub9.owner = user9 sub9.state = SUBSCRIPTION_STATE_PAID sub9.expiration_date = datetime.date.today() + datetime.timedelta(1) sub9.save() response = self.client.get(reverse('admin_dashboard')) self.assertEquals(response.status_code, 200) users = response.context['users'] self.assertEquals(users[0]['value'], 10) # total users self.assertEquals(users[1]['value'], 1) # unconfirmed users self.assertEquals(users[2]['value'], 2) # waiting for deletion self.assertEquals(users[3]['value'], 3) # expired users self.assertEquals(users[4]['value'], 4) # active users self.assertEquals(users[5]['value'], 1) # subscribed users
def autologin(sender, **kwargs): user = kwargs['user'] request = kwargs['request'] # Manually setting the default user backed to avoid the # 'User' object has no attribute 'backend' error user.backend = 'django.contrib.auth.backends.ModelBackend' # This login function does not need password. login(request, user) #CIGNo # user_activated.connect(autologin) # add approval flag to RegistrationProfile from registration.models import RegistrationProfile RegistrationProfile.add_to_class('approved', models.BooleanField(default=False)) from django.core.mail import mail_admins from django.contrib.sites.models import RequestSite from django.contrib.sites.models import Site from django.core import urlresolvers from django.db.models import signals from django.template.loader import render_to_string from django.conf import settings def get_current_site(): if Site._meta.installed: return Site.objects.get_current() else: return RequestSite(request)