• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mozilla / fx-private-relay / b2e067fe-ce4e-4099-9bef-07b368e99782

15 Apr 2024 04:18PM CUT coverage: 75.544% (+0.002%) from 75.542%
b2e067fe-ce4e-4099-9bef-07b368e99782

push

circleci

jwhitlock
Enable pyupgrade, fix issues

2443 of 3405 branches covered (71.75%)

Branch coverage included in aggregate %.

56 of 59 new or added lines in 14 files covered. (94.92%)

234 existing lines in 24 files now uncovered.

6793 of 8821 relevant lines covered (77.01%)

20.04 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

75.31
/privaterelay/middleware.py
1
import time
1✔
2
from datetime import UTC, datetime
1✔
3

4
from django.conf import settings
1✔
5
from django.shortcuts import redirect
1✔
6

7
import markus
1✔
8
from whitenoise.middleware import WhiteNoiseMiddleware
1✔
9

10
metrics = markus.get_metrics("fx-private-relay")
1✔
11

12

13
class RedirectRootIfLoggedIn:
1✔
14
    def __init__(self, get_response):
1✔
15
        self.get_response = get_response
1✔
16

17
    def __call__(self, request):
1✔
18
        # To prevent showing a flash of the landing page when a user is logged
19
        # in, use a server-side redirect to send them to the dashboard,
20
        # rather than handling that on the client-side:
21
        if request.path == "/" and settings.SESSION_COOKIE_NAME in request.COOKIES:
1!
UNCOV
22
            query_string = (
×
23
                "?" + request.META["QUERY_STRING"]
24
                if request.META["QUERY_STRING"]
25
                else ""
26
            )
UNCOV
27
            return redirect("accounts/profile/" + query_string)
×
28

29
        response = self.get_response(request)
1✔
30
        return response
1✔
31

32

33
class AddDetectedCountryToRequestAndResponseHeaders:
1✔
34
    def __init__(self, get_response):
1✔
35
        self.get_response = get_response
1✔
36

37
    def __call__(self, request):
1✔
38
        region_key = "X-Client-Region"
1✔
39
        region_dict = None
1✔
40
        if region_key in request.headers:
1✔
41
            region_dict = request.headers
1✔
42
        if region_key in request.GET:
1!
UNCOV
43
            region_dict = request.GET
×
44
        if not region_dict:
1✔
45
            return self.get_response(request)
1✔
46

47
        country = region_dict.get(region_key)
1✔
48
        request.country = country
1✔
49
        response = self.get_response(request)
1✔
50
        response.country = country
1✔
51
        return response
1✔
52

53

54
def _get_metric_view_name(request):
1✔
UNCOV
55
    if request.resolver_match:
×
UNCOV
56
        view = request.resolver_match.func
×
57
        return f"{view.__module__}.{view.__name__}"
×
58
    return "<unknown_view>"
×
59

60

61
class ResponseMetrics:
1✔
62
    def __init__(self, get_response):
1✔
UNCOV
63
        self.get_response = get_response
×
64

65
    def __call__(self, request):
1✔
UNCOV
66
        start_time = time.time()
×
UNCOV
67
        response = self.get_response(request)
×
68
        delta = time.time() - start_time
×
69

70
        view_name = _get_metric_view_name(request)
×
71

72
        metrics.timing(
×
73
            "response",
74
            value=delta * 1000.0,
75
            tags=[
76
                f"status:{response.status_code}",
77
                f"view:{view_name}",
78
                f"method:{request.method}",
79
            ],
80
        )
81

UNCOV
82
        return response
×
83

84

85
class StoreFirstVisit:
1✔
86
    def __init__(self, get_response):
1✔
87
        self.get_response = get_response
1✔
88

89
    def __call__(self, request):
1✔
90
        response = self.get_response(request)
1✔
91
        first_visit = request.COOKIES.get("first_visit")
1✔
92
        if first_visit is None and not request.user.is_anonymous:
1✔
93
            response.set_cookie("first_visit", datetime.now(UTC))
1✔
94
        return response
1✔
95

96

97
class RelayStaticFilesMiddleware(WhiteNoiseMiddleware):
1✔
98
    """Customize WhiteNoiseMiddleware for Relay.
99

100
    The WhiteNoiseMiddleware serves static files and sets headers. In
101
    production, the files are read from staticfiles/staticfiles.json,
102
    and files with hashes in the name are treated as immutable with
103
    10-year cache timeouts.
104

105
    This class also treats Next.js output files (already hashed) as immutable.
106
    """
107

108
    def immutable_file_test(self, path, url):
1✔
109
        """
110
        Determine whether given URL represents an immutable file (i.e. a
111
        file with a hash of its contents as part of its name) which can
112
        therefore be cached forever.
113

114
        All files outputed by next.js are hashed and immutable
115
        """
116
        if not url.startswith(self.static_prefix):
1!
UNCOV
117
            return False
×
118
        name = url[len(self.static_prefix) :]
1✔
119
        if name.startswith("_next/static/"):
1✔
120
            return True
1✔
121
        else:
122
            return super().immutable_file_test(path, url)
1✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc