def test_reset(self): """Test basic password reset.""" self.selenium.get( '%s%s' % (self.live_server_url, reverse('account:reset_password'))) user = User.objects.create(username=JID, email=EMAIL, confirmed=NOW, created_in_backend=True, last_activity=NOW) xmpp_backend.create_user(NODE, DOMAIN, PWD) self.assertTrue(user.check_password(PWD)) self.assertFalse(user.check_password(PWD2)) #fg_username = self.find('#fg_username') node = self.selenium.find_element_by_id('id_username_0') node.send_keys(NODE) self.wait_for_valid_form() with self.mock_celery() as mocked: self.find('button[type="submit"]').click() self.wait_for_page_load() self.assertTaskCount(mocked, 1) user = User.objects.get(username=JID) # only a successful password reset should update last activity update self.assertEqual(user.last_activity, NOW) site = settings.XMPP_HOSTS[settings.DEFAULT_XMPP_HOST] self.assertTaskCall(mocked, send_confirmation_task, user_pk=user.pk, purpose=PURPOSE_RESET_PASSWORD, to=EMAIL, hostname=site['NAME'], base_url=self.live_server_url, language='en', address='127.0.0.1') self.assertEqual(len(mail.outbox), 1) confirmation = Confirmation.objects.get(user=user, purpose=PURPOSE_RESET_PASSWORD) self.selenium.get('%s%s' % (self.live_server_url, confirmation.urlpath)) self.wait_for_page_load() self.find('#id_new_password1').send_keys(PWD2) self.find('#id_new_password2').send_keys(PWD2) self.wait_for_valid_form() with freeze_time(NOW2_STR): self.find('button[type="submit"]').click() self.wait_for_page_load() # get user again user = User.objects.get(username=JID) self.assertFalse(user.check_password(PWD)) self.assertTrue(user.check_password(PWD2)) self.assertEqual(user.last_activity, NOW2)
def form_valid(self, form): key = self.key if isinstance(key, HttpResponse): return key request = self.request address = request.META['REMOTE_ADDR'] password = form.cleaned_data['new_password1'] with transaction.atomic(), version(comment="email address confirmed"): key.user.confirmed = timezone.now() key.user.created_in_backend = True key.user.save() if request.user != key.user: logout(request) # logout any previous user key.user.backend = settings.AUTHENTICATION_BACKENDS[0] login(request, key.user) # Actually create the user on the XMPP server xmpp_backend.create_user(username=key.user.node, domain=key.user.domain, password=password, email=key.user.email) key.user.log(ugettext_noop('Email address %(email)s confirmed.'), address, email=key.user.email) # TODO: More meaningful help message on a webchat, usable clients, follow updates, ... messages.success(request, _( "Successfully confirmed your email address. You can now use your account.")) stat(STAT_REGISTER_CONFIRMED) # Delete the registration key key.delete() return super(ConfirmRegistrationView, self).form_valid(form)
def setUp(self): super().setUp() self.user = User.objects.create( username=JID, email=EMAIL, confirmed=NOW_UTC, created_in_backend=True, last_activity=NOW_UTC) xmpp_backend.create_user(NODE, DOMAIN, PWD) xmpp_backend.set_last_activity(NODE, DOMAIN, timestamp=NOW) self.url = reverse('account:login') self.client = Client()
def test_set_unusable_password(self): pwd = 'password' u = XmppUser.objects.create(jid='*****@*****.**') xmpp_backend.create_user(u.node, u.domain, pwd) self.assertTrue(u.check_password(pwd)) self.assertTrue(xmpp_backend.check_password(u.node, u.domain, pwd)) u.set_unusable_password() self.assertFalse(u.check_password(pwd)) self.assertFalse(xmpp_backend.check_password(u.node, u.domain, pwd))
def test_basic(self): pwd = 'dummy' u = XmppUser.objects.create(jid='*****@*****.**') self.assertEqual(u.node, 'user') self.assertEqual(u.domain, 'example.com') self.assertEqual(u.get_short_name(), 'user') self.assertFalse(u.exists()) self.assertFalse(xmpp_backend.user_exists(u.node, u.domain)) xmpp_backend.create_user(u.node, u.domain, pwd) self.assertTrue(u.exists()) self.assertTrue(xmpp_backend.user_exists(u.node, u.domain))
def create_user(self, username, email, password=None): user = self.model(username=username, email=email, confirmed=timezone.now(), created_in_backend=True, registration_method=REGISTRATION_MANUAL) with transaction.atomic(): user.save(using=self.db) if password is None and settings.XMPP_HOSTS[user.domain].get('reserve', False): xmpp_backend.create_reservation(user.node, user.domain, email=email) elif password is not None: xmpp_backend.create_user(user.node, user.domain, password, email=email) return user
def setUp(self): super().setUp() self.user = User.objects.create(username=JID, email=EMAIL, confirmed=NOW_UTC, created_in_backend=True, last_activity=NOW_UTC) xmpp_backend.create_user(NODE, DOMAIN, PWD) xmpp_backend.set_last_activity(NODE, DOMAIN, timestamp=NOW) self.url = reverse('account:login') self.client = Client()
def test_reset(self): """Test basic password reset.""" self.selenium.get('%s%s' % (self.live_server_url, reverse('account:reset_password'))) user = User.objects.create( username=JID, email=EMAIL, confirmed=NOW, created_in_backend=True, last_activity=NOW) xmpp_backend.create_user(NODE, DOMAIN, PWD) self.assertTrue(user.check_password(PWD)) self.assertFalse(user.check_password(PWD2)) #fg_username = self.find('#fg_username') node = self.selenium.find_element_by_id('id_username_0') node.send_keys(NODE) self.wait_for_valid_form() with self.mock_celery() as mocked: self.find('button[type="submit"]').click() self.wait_for_page_load() self.assertTaskCount(mocked, 1) user = User.objects.get(username=JID) # only a successful password reset should update last activity update self.assertEqual(user.last_activity, NOW) site = settings.XMPP_HOSTS[settings.DEFAULT_XMPP_HOST] self.assertTaskCall( mocked, send_confirmation_task, user_pk=user.pk, purpose=PURPOSE_RESET_PASSWORD, to=EMAIL, hostname=site['NAME'], base_url=self.live_server_url, language='en', address='127.0.0.1' ) self.assertEqual(len(mail.outbox), 1) confirmation = Confirmation.objects.get(user=user, purpose=PURPOSE_RESET_PASSWORD) self.selenium.get('%s%s' % (self.live_server_url, confirmation.urlpath)) self.wait_for_page_load() self.find('#id_new_password1').send_keys(PWD2) self.find('#id_new_password2').send_keys(PWD2) self.wait_for_valid_form() with freeze_time(NOW2_STR): self.find('button[type="submit"]').click() self.wait_for_page_load() # get user again user = User.objects.get(username=JID) self.assertFalse(user.check_password(PWD)) self.assertTrue(user.check_password(PWD2)) self.assertEqual(user.last_activity, NOW2)
def create_user(self, username, email, password=None): user = self.model(username=username, email=email, confirmed=timezone.now(), created_in_backend=True, registration_method=REGISTRATION_MANUAL) with transaction.atomic(): user.save(using=self.db) if password is None and settings.XMPP_HOSTS[user.domain].get( 'reserve', False): xmpp_backend.create_reservation(user.node, user.domain, email=email) elif password is not None: xmpp_backend.create_user(user.node, user.domain, password, email=email) return user
def test_expiring(self): # create user, also in backend user = User.objects.create(username=JID, email=EMAIL, created_in_backend=True, last_activity=LAST_ACTIVITY_1, confirmed=LAST_ACTIVITY_1) xmpp_backend.create_user(NODE, DOMAIN, PWD) xmpp_backend.set_last_activity(NODE, DOMAIN, timestamp=LAST_ACTIVITY_2) self.assertEqual(user.last_activity, LAST_ACTIVITY_1) with self.mock_celery() as mocked, freeze_time(NOW_1_STR): update_last_activity() user = User.objects.get(username=JID) self.assertTaskCount(mocked, 0) self.assertEqual(len(mail.outbox), 0) # no mails where sent yet (user is not expiring) self.assertEqual(user.last_activity, LAST_ACTIVITY_2) # new last activity from backend with self.mock_celery() as mocked, freeze_time(NOW_2_STR): self.assertTrue(user.is_expiring) self.assertTrue(user.notifications.account_expires) self.assertFalse(user.notifications.account_expires_notified) update_last_activity() self.assertTaskCount(mocked, 0) self.assertEqual(len(mail.outbox), 1) self.assertEqual(user.last_activity, LAST_ACTIVITY_2) # last activity stays the same mail.outbox = [] self.assertEqual(len(mail.outbox), 0) # run task the next day and make sure that no mail is sent user = User.objects.get(username=JID) with self.mock_celery() as mocked, freeze_time(NOW_2_STR): self.assertTrue(user.is_expiring) self.assertTrue(user.notifications.account_expires) self.assertTrue(user.notifications.account_expires_notified) update_last_activity() user = User.objects.get(username=JID) self.assertTaskCount(mocked, 0) self.assertEqual(len(mail.outbox), 0) self.assertEqual(user.last_activity, LAST_ACTIVITY_2) # last activity stays the same # User finally goes online again xmpp_backend.set_last_activity(NODE, DOMAIN, timestamp=NOW_3) # task runs again user = User.objects.get(username=JID) with self.mock_celery() as mocked, freeze_time(NOW_4_STR): self.assertTrue(user.is_expiring) self.assertTrue(user.notifications.account_expires) self.assertTrue(user.notifications.account_expires_notified) update_last_activity() # no mail is sent user = User.objects.get(username=JID) self.assertTaskCount(mocked, 0) self.assertEqual(len(mail.outbox), 0) self.assertEqual(user.last_activity, NOW_3) self.assertFalse(user.is_expiring) self.assertTrue(user.notifications.account_expires) self.assertFalse(user.notifications.account_expires_notified)
def test_expiring(self): # create user, also in backend user = User.objects.create(username=JID, email=EMAIL, created_in_backend=True, last_activity=LAST_ACTIVITY_1, confirmed=LAST_ACTIVITY_1) xmpp_backend.create_user(NODE, DOMAIN, PWD) xmpp_backend.set_last_activity(NODE, DOMAIN, timestamp=LAST_ACTIVITY_2) self.assertEqual(user.last_activity, LAST_ACTIVITY_1) with self.mock_celery() as mocked, freeze_time(NOW_1_STR): update_last_activity() user = User.objects.get(username=JID) self.assertTaskCount(mocked, 0) self.assertEqual(len(mail.outbox), 0) # no mails where sent yet (user is not expiring) self.assertEqual(user.last_activity, LAST_ACTIVITY_2) # new last activity from backend with self.mock_celery() as mocked, freeze_time(NOW_2_STR): self.assertTrue(user.is_expiring) self.assertTrue(user.notifications.account_expires) self.assertFalse(user.notifications.account_expires_notified) update_last_activity() self.assertTaskCount(mocked, 0) self.assertEqual(len(mail.outbox), 1) self.assertEqual(user.last_activity, LAST_ACTIVITY_2) # last activity stays the same mail.outbox = [] self.assertEqual(len(mail.outbox), 0) # run task the next day and make sure that no mail is sent user = User.objects.get(username=JID) with self.mock_celery() as mocked, freeze_time(NOW_2_STR): self.assertTrue(user.is_expiring) self.assertTrue(user.notifications.account_expires) self.assertTrue(user.notifications.account_expires_notified) update_last_activity() user = User.objects.get(username=JID) self.assertTaskCount(mocked, 0) self.assertEqual(len(mail.outbox), 0) self.assertEqual(user.last_activity, LAST_ACTIVITY_2) # last activity stays the same # User finally goes online again xmpp_backend.set_last_activity(NODE, DOMAIN, timestamp=NOW_3) # task runs again user = User.objects.get(username=JID) with self.mock_celery() as mocked, freeze_time(NOW_4_STR): self.assertTrue(user.is_expiring) self.assertTrue(user.notifications.account_expires) self.assertTrue(user.notifications.account_expires_notified) update_last_activity() # no mail is sent user = User.objects.get(username=JID) self.assertTaskCount(mocked, 0) self.assertEqual(len(mail.outbox), 0) self.assertEqual(user.last_activity, NOW_3) self.assertFalse(user.is_expiring) self.assertTrue(user.notifications.account_expires) self.assertFalse(user.notifications.account_expires_notified)