Пример #1
0
    def process_response(self, request: HttpRequest,
                         response: HttpResponseBase) -> HttpResponseBase:

        # This is the same as the default LocaleMiddleware, minus the
        # logic that redirects 404's that lack a prefixed language in
        # the path into having a language.  See
        # https://code.djangoproject.com/ticket/32005
        language = translation.get_language()
        language_from_path = translation.get_language_from_path(
            request.path_info)
        urlconf = getattr(request, "urlconf", settings.ROOT_URLCONF)
        i18n_patterns_used, _ = is_language_prefix_patterns_used(urlconf)
        if not (i18n_patterns_used and language_from_path):
            patch_vary_headers(response, ("Accept-Language", ))
        assert language is not None
        response.setdefault("Content-Language", language)

        # An additional responsibility of our override of this middleware is to save the user's language
        # preference in a cookie. That determination is made by code handling the request
        # and saved in the set_language flag so that it can be used here.
        set_language = get_request_notes(request).set_language
        if set_language is not None:
            response.set_cookie(settings.LANGUAGE_COOKIE_NAME, set_language)

        return response
Пример #2
0
    def test_setdefault(self):
        """
        HttpResponseBase.setdefault() should not change an existing header
        and should be case insensitive.
        """
        r = HttpResponseBase()

        r["Header"] = "Value"
        r.setdefault("header", "changed")
        self.assertEqual(r["header"], "Value")

        r.setdefault("x-header", "DefaultValue")
        self.assertEqual(r["X-Header"], "DefaultValue")
Пример #3
0
    def test_setdefault(self):
        """
        HttpResponseBase.setdefault() should not change an existing header
        and should be case insensitive.
        """
        r = HttpResponseBase()

        r['Header'] = 'Value'
        r.setdefault('header', 'changed')
        self.assertEqual(r['header'], 'Value')

        r.setdefault('x-header', 'DefaultValue')
        self.assertEqual(r['X-Header'], 'DefaultValue')
Пример #4
0
    def test_write(self):
        r = HttpResponseBase()
        self.assertIs(r.writable(), False)

        with self.assertRaisesMessage(OSError, 'This HttpResponseBase instance is not writable'):
            r.write('asdf')
        with self.assertRaisesMessage(OSError, 'This HttpResponseBase instance is not writable'):
            r.writelines(['asdf\n', 'qwer\n'])
Пример #5
0
    def test_add_token_cookies_without_auto_login(self):
        response = HttpResponseBase()
        ResponseCookieHelper.add_token_cookie(response, self.access_token, self.refresh_token, False)

        self.assertIn(ACCESS_TOKEN_COOKIE_KEY, response.cookies)
        self.assertEqual(response.cookies[ACCESS_TOKEN_COOKIE_KEY]['expires'], '')
        self.assertEqual(response.cookies[ACCESS_TOKEN_COOKIE_KEY]['max-age'], '')

        self.assertIn(REFRESH_TOKEN_COOKIE_KEY, response.cookies)
        self.assertEqual(response.cookies[REFRESH_TOKEN_COOKIE_KEY]['expires'], '')
        self.assertEqual(response.cookies[REFRESH_TOKEN_COOKIE_KEY]['max-age'], '')
Пример #6
0
    def test_add_token_cookies_with_auto_login(self):
        response = HttpResponseBase()
        ResponseCookieHelper.add_token_cookie(response, self.access_token, self.refresh_token, True)

        self.assertIn(ACCESS_TOKEN_COOKIE_KEY, response.cookies)
        self.assertNotEqual(response.cookies[ACCESS_TOKEN_COOKIE_KEY]['expires'], COOKIE_EXPIRE_DEFAULT_TIME)
        self.assertEqual(response.cookies[ACCESS_TOKEN_COOKIE_KEY]['max-age'], self.access_token.expires_in)

        self.assertIn(REFRESH_TOKEN_COOKIE_KEY, response.cookies)
        self.assertNotEqual(response.cookies[REFRESH_TOKEN_COOKIE_KEY]['expires'], COOKIE_EXPIRE_DEFAULT_TIME)
        self.assertEqual(response.cookies[REFRESH_TOKEN_COOKIE_KEY]['max-age'], self.refresh_token.expires_in)
Пример #7
0
    def test_write(self):
        r = HttpResponseBase()
        self.assertIs(r.writable(), False)

        with self.assertRaisesMessage(
                OSError, "This HttpResponseBase instance is not writable"):
            r.write("asdf")
        with self.assertRaisesMessage(
                OSError, "This HttpResponseBase instance is not writable"):
            r.writelines(["asdf\n", "qwer\n"])
Пример #8
0
    def test_clear_token(self):
        response = HttpResponseBase()
        ResponseCookieHelper.add_token_cookie(response, self.access_token, self.refresh_token, False)

        ResponseCookieHelper.clear_token_cookie(response)
        self.assertEqual(response.cookies[ACCESS_TOKEN_COOKIE_KEY].value, '')
        self.assertEqual(response.cookies[ACCESS_TOKEN_COOKIE_KEY]['expires'], COOKIE_EXPIRE_DEFAULT_TIME)
        self.assertEqual(response.cookies[ACCESS_TOKEN_COOKIE_KEY]['max-age'], 0)

        self.assertEqual(response.cookies[REFRESH_TOKEN_COOKIE_KEY].value, '')
        self.assertEqual(response.cookies[REFRESH_TOKEN_COOKIE_KEY]['expires'], COOKIE_EXPIRE_DEFAULT_TIME)
        self.assertEqual(response.cookies[REFRESH_TOKEN_COOKIE_KEY]['max-age'], 0)
Пример #9
0
 def test_submission_valid_not_retried_again(self):
     # Mark submission for retry
     submission = CompetitionSubmission(phase=self.phase,
                                        participant=self.participant,
                                        chahub_needs_retry=True)
     with mock.patch('apps.web.models.CompetitionSubmission.send_to_chahub'
                     ) as send_to_chahub_mock:
         send_to_chahub_mock.return_value = HttpResponseBase(status=201)
         send_to_chahub_mock.return_value.content = ""
         submission.save(
         )  # NOTE! not called with force_to_chahub=True as retrying would set
         # It does not call send method, only during "do_retries" task should it
         assert not send_to_chahub_mock.called
Пример #10
0
    def test_write(self):
        r = HttpResponseBase()
        self.assertIs(r.writable(), False)

        with self.assertRaisesMessage(IOError, "This HttpResponseBase instance is not writable"):
            r.write("asdf")
        with self.assertRaisesMessage(IOError, "This HttpResponseBase instance is not writable"):
            r.writelines(["asdf\n", "qwer\n"])
Пример #11
0
    def test_write(self):
        r = HttpResponseBase()
        self.assertIs(r.writable(), False)

        with self.assertRaisesMessage(IOError, 'This HttpResponseBase instance is not writable'):
            r.write('asdf')
        with self.assertRaisesMessage(IOError, 'This HttpResponseBase instance is not writable'):
            r.writelines(['asdf\n', 'qwer\n'])
Пример #12
0
    def test_setdefault(self):
        """
        HttpResponseBase.setdefault() should not change an existing header
        and should be case insensitive.
        """
        r = HttpResponseBase()

        r.headers["Header"] = "Value"
        r.setdefault("header", "changed")
        self.assertEqual(r.headers["header"], "Value")

        r.setdefault("x-header", "DefaultValue")
        self.assertEqual(r.headers["X-Header"], "DefaultValue")
Пример #13
0
    def test_setdefault(self):
        """
        HttpResponseBase.setdefault() should not change an existing header
        and should be case insensitive.
        """
        r = HttpResponseBase()

        r.headers['Header'] = 'Value'
        r.setdefault('header', 'changed')
        self.assertEqual(r.headers['header'], 'Value')

        r.setdefault('x-header', 'DefaultValue')
        self.assertEqual(r.headers['X-Header'], 'DefaultValue')
Пример #14
0
    def test_submission_mixin_save_doesnt_resend_same_data(self):
        submission = CompetitionSubmission(phase=self.phase,
                                           participant=self.participant)
        with mock.patch('apps.web.models.CompetitionSubmission.send_to_chahub'
                        ) as send_to_chahub_mock:
            send_to_chahub_mock.return_value = HttpResponseBase(status=201)
            send_to_chahub_mock.return_value.content = ""
            submission.save()
            # attempts to send to Chahub once
            assert send_to_chahub_mock.called

            # reset
            send_to_chahub_mock.called = False

            # does not call again
            submission.save()
            assert not send_to_chahub_mock.called
Пример #15
0
    def test_submission_retry_valid_retried_then_sent_and_not_retried_again(
            self):
        # Mark submission for retry
        submission = CompetitionSubmission(phase=self.phase,
                                           participant=self.participant,
                                           chahub_needs_retry=True)
        with mock.patch('apps.web.models.CompetitionSubmission.send_to_chahub'
                        ) as send_to_chahub_mock:
            send_to_chahub_mock.return_value = HttpResponseBase(status=201)
            send_to_chahub_mock.return_value.content = ""
            submission.save(force_to_chahub=True)
            # It does not need retry any more, and was successful
            assert send_to_chahub_mock.called
            assert not CompetitionSubmission.objects.get(
                pk=submission.pk).chahub_needs_retry

            # reset
            send_to_chahub_mock.called = False

            # Try sending again
            submission.save(force_to_chahub=True)
            assert not send_to_chahub_mock.called
Пример #16
0
    def test_closed(self):
        r = HttpResponseBase()
        self.assertIs(r.closed, False)

        r.close()
        self.assertIs(r.closed, True)
Пример #17
0
def test_it_rejects_invalid_samesite_values():
    res = HttpResponseBase()
    with pytest.raises(ValueError, match="samesite must be"):
        res.set_cookie("boop", "hi", samesite="BOOP")
Пример #18
0
    def test_closed(self):
        r = HttpResponseBase()
        self.assertIs(r.closed, False)

        r.close()
        self.assertIs(r.closed, True)
Пример #19
0
 def test_tell(self):
     r = HttpResponseBase()
     with self.assertRaisesMessage(IOError, 'This HttpResponseBase instance cannot tell its position'):
         r.tell()
Пример #20
0
 def test_tell(self):
     r = HttpResponseBase()
     with self.assertRaisesMessage(OSError, 'This HttpResponseBase instance cannot tell its position'):
         r.tell()
Пример #21
0
def test_it_accepts_valid_samesite_values(samesite):
    res = HttpResponseBase()
    res.set_cookie("boop", "hallo there", samesite=samesite)
    assert "hallo there" in str(res.cookies["boop"])
    assert res.cookies["boop"]["samesite"] == samesite