Beispiel #1
0
    def form_valid(self, form):
        key = self.key
        if isinstance(key, HttpResponse):
            return key

        request = self.request
        address = request.META['REMOTE_ADDR']
        now = timezone.now()

        with transaction.atomic():
            xmpp_backend.set_password(username=key.user.node, domain=key.user.domain,
                                      password=form.cleaned_data['new_password1'])
            xmpp_backend.set_last_activity(key.user.node, key.user.domain, timestamp=now)

            key.user.last_activity = now
            key.user.log(ugettext_noop('Set new password.'), address)
            messages.success(request, _('Successfully changed your password.'))
            stat(STAT_RESET_PASSWORD_CONFIRMED)

            key.user.backend = settings.AUTHENTICATION_BACKENDS[0]
            key.user.save()
            login(self.request, key.user)
            key.delete()

        return super(ConfirmResetPasswordView, self).form_valid(form)
Beispiel #2
0
    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()
Beispiel #3
0
    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()
Beispiel #4
0
    def form_valid(self, form):
        redirect_to = self.request.POST.get(self.REDIRECT_FIELD_NAME, '')

        # Ensure the user-originating redirection url is safe.
        if not is_safe_url(url=redirect_to, allowed_hosts=[self.request.get_host()]):
            redirect_to = resolve_url(settings.LOGIN_REDIRECT_URL)

        user = form.get_user()
        now = timezone.now()

        # Okay, security check complete. Log the user in.
        login(self.request, user)
        user.last_activity = now
        xmpp_backend.set_last_activity(user.node, user.domain,
                                       status='Logged in via homepage.',
                                       timestamp=now)
        user.save()
        return HttpResponseRedirect(redirect_to)
    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)
Beispiel #6
0
    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)