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

mozilla / fx-private-relay / 468e0658-29e4-47bb-ac3d-b5ddbf440490

04 Sep 2025 03:27PM UTC coverage: 88.1% (+1.8%) from 86.303%
468e0658-29e4-47bb-ac3d-b5ddbf440490

Pull #5761

circleci

vpremamozilla
MPP-4153 - test(mask-management): increase frontend test coverage for Mask Management pages and components
Pull Request #5761: MPP-4293 - increase frontend test coverage for Mask Management pages and components (part 2)

2912 of 3943 branches covered (73.85%)

Branch coverage included in aggregate %.

18136 of 19948 relevant lines covered (90.92%)

11.27 hits per line

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

91.84
/privaterelay/glean/server_events.py
1
"""
2
This Source Code Form is subject to the terms of the Mozilla Public
3
License, v. 2.0. If a copy of the MPL was not distributed with this
4
file, You can obtain one at http://mozilla.org/MPL/2.0/.
5

6
AUTOGENERATED BY glean_parser v17.3.0. DO NOT EDIT. To recreate, run:
7

8
bash .circleci/python_job.bash run build_glean
9
"""
10

11
from __future__ import annotations
1✔
12

13
import json
1✔
14
from datetime import UTC, datetime
1✔
15
from typing import Any
1✔
16
from uuid import uuid4
1✔
17

18
GLEAN_EVENT_MOZLOG_TYPE = "glean-server-event"
1✔
19

20

21
class EventsServerEventLogger:
1✔
22
    def __init__(
1✔
23
        self, application_id: str, app_display_version: str, channel: str
24
    ) -> None:
25
        """
26
        Create EventsServerEventLogger instance.
27

28
        :param str application_id: The application ID.
29
        :param str app_display_version: The application display version.
30
        :param str channel: The channel.
31
        """
32
        self._application_id = application_id
1✔
33
        self._app_display_version = app_display_version
1✔
34
        self._channel = channel
1✔
35

36
    def _record(self, user_agent: str, ip_address: str, event: dict[str, Any]) -> None:
1✔
37
        now = datetime.now(UTC)
1✔
38
        timestamp = now.isoformat()
1✔
39
        event["timestamp"] = int(1000.0 * now.timestamp())  # Milliseconds since epoch
1✔
40
        event_payload = {
1✔
41
            "metrics": {},
42
            "events": [event],
43
            "ping_info": {
44
                # seq is required in the Glean schema, however is not useful in server context
45
                "seq": 0,
46
                "start_time": timestamp,
47
                "end_time": timestamp,
48
            },
49
            # `Unknown` fields below are required in the Glean schema, however they are
50
            # not useful in server context
51
            "client_info": {
52
                "telemetry_sdk_build": "glean_parser v17.3.0",
53
                "first_run_date": "Unknown",
54
                "os": "Unknown",
55
                "os_version": "Unknown",
56
                "architecture": "Unknown",
57
                "app_build": "Unknown",
58
                "app_display_version": self._app_display_version,
59
                "app_channel": self._channel,
60
            },
61
        }
62
        event_payload_serialized = json.dumps(event_payload)
1✔
63

64
        # This is the message structure that Decoder expects:
65
        # https://github.com/mozilla/gcp-ingestion/pull/2400
66
        ping = {
1✔
67
            "document_namespace": self._application_id,
68
            "document_type": "events",
69
            "document_version": "1",
70
            "document_id": str(uuid4()),
71
            "user_agent": user_agent,
72
            "ip_address": ip_address,
73
            "payload": event_payload_serialized,
74
        }
75

76
        self.emit_record(now, ping)
1✔
77

78
    def emit_record(self, now: datetime, ping: dict[str, Any]) -> None:
1✔
79
        """Log the ping to STDOUT.
80
        Applications might want to override this method to use their own logging.
81
        If doing so, make sure to log the ping as JSON, and to include the
82
        `Type: GLEAN_EVENT_MOZLOG_TYPE`."""
83
        ping_envelope = {
×
84
            "Timestamp": now.isoformat(),
85
            "Logger": "glean",
86
            "Type": GLEAN_EVENT_MOZLOG_TYPE,
87
            "Fields": ping,
88
        }
89
        ping_envelope_serialized = json.dumps(ping_envelope)
×
90

91
        print(ping_envelope_serialized)
×
92

93
    def record_api_accessed(
1✔
94
        self,
95
        user_agent: str,
96
        ip_address: str,
97
        endpoint: str,
98
        method: str,
99
        fxa_id: str,
100
    ) -> None:
101
        """
102
        Record and submit a api_accessed event:
103
        An API endpoint was accessed.
104
        Event is logged to STDOUT via `print`.
105

106
        :param str user_agent: The user agent.
107
        :param str ip_address: The IP address. Will be used to decode Geo information
108
            and scrubbed at ingestion.
109
        :param str endpoint: The name of the endpoint accessed
110
        :param str method: HTTP method used
111
        :param str fxa_id: Mozilla accounts user ID
112
        """
113
        event = {
1✔
114
            "category": "api",
115
            "name": "accessed",
116
            "extra": {
117
                "endpoint": str(endpoint),
118
                "method": str(method),
119
                "fxa_id": str(fxa_id),
120
            },
121
        }
122
        self._record(user_agent, ip_address, event)
1✔
123

124
    def record_email_blocked(
1✔
125
        self,
126
        user_agent: str,
127
        ip_address: str,
128
        fxa_id: str,
129
        platform: str,
130
        n_random_masks: int,
131
        n_domain_masks: int,
132
        n_deleted_random_masks: int,
133
        n_deleted_domain_masks: int,
134
        date_joined_relay: int,
135
        premium_status: str,
136
        date_joined_premium: int,
137
        has_extension: bool,
138
        date_got_extension: int,
139
        is_random_mask: bool,
140
        is_reply: bool,
141
        reason: str,
142
    ) -> None:
143
        """
144
        Record and submit a email_blocked event:
145
        Relay receives but does not forward an email for a Relay user.
146
        Event is logged to STDOUT via `print`.
147

148
        :param str user_agent: The user agent.
149
        :param str ip_address: The IP address. Will be used to decode Geo information
150
            and scrubbed at ingestion.
151
        :param str fxa_id: Mozilla accounts user ID
152
        :param str platform: Relay client platform
153
        :param int n_random_masks: Number of random masks
154
        :param int n_domain_masks: Number of premium subdomain masks
155
        :param int n_deleted_random_masks: Number of deleted random masks
156
        :param int n_deleted_domain_masks: Number of deleted domain masks
157
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
158
        :param str premium_status: Subscription type and term
159
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
160
        :param bool has_extension: The user has the Relay Add-on
161
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
162
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
163
        :param bool is_reply: The email is a reply from the Relay user
164
        :param str reason: Code describing why the email was blocked
165
        """
166
        event = {
1✔
167
            "category": "email",
168
            "name": "blocked",
169
            "extra": {
170
                "fxa_id": str(fxa_id),
171
                "platform": str(platform),
172
                "n_random_masks": str(n_random_masks),
173
                "n_domain_masks": str(n_domain_masks),
174
                "n_deleted_random_masks": str(n_deleted_random_masks),
175
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
176
                "date_joined_relay": str(date_joined_relay),
177
                "premium_status": str(premium_status),
178
                "date_joined_premium": str(date_joined_premium),
179
                "has_extension": str(has_extension).lower(),
180
                "date_got_extension": str(date_got_extension),
181
                "is_random_mask": str(is_random_mask).lower(),
182
                "is_reply": str(is_reply).lower(),
183
                "reason": str(reason),
184
            },
185
        }
186
        self._record(user_agent, ip_address, event)
1✔
187

188
    def record_email_forwarded(
1✔
189
        self,
190
        user_agent: str,
191
        ip_address: str,
192
        fxa_id: str,
193
        platform: str,
194
        n_random_masks: int,
195
        n_domain_masks: int,
196
        n_deleted_random_masks: int,
197
        n_deleted_domain_masks: int,
198
        date_joined_relay: int,
199
        premium_status: str,
200
        date_joined_premium: int,
201
        has_extension: bool,
202
        date_got_extension: int,
203
        is_random_mask: bool,
204
        is_reply: bool,
205
    ) -> None:
206
        """
207
        Record and submit a email_forwarded event:
208
        Relay receives and forwards an email for a Relay user.
209
        Event is logged to STDOUT via `print`.
210

211
        :param str user_agent: The user agent.
212
        :param str ip_address: The IP address. Will be used to decode Geo information
213
            and scrubbed at ingestion.
214
        :param str fxa_id: Mozilla accounts user ID
215
        :param str platform: Relay client platform
216
        :param int n_random_masks: Number of random masks
217
        :param int n_domain_masks: Number of premium subdomain masks
218
        :param int n_deleted_random_masks: Number of deleted random masks
219
        :param int n_deleted_domain_masks: Number of deleted domain masks
220
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
221
        :param str premium_status: Subscription type and term
222
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
223
        :param bool has_extension: The user has the Relay Add-on
224
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
225
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
226
        :param bool is_reply: The email is a reply from the Relay user
227
        """
228
        event = {
1✔
229
            "category": "email",
230
            "name": "forwarded",
231
            "extra": {
232
                "fxa_id": str(fxa_id),
233
                "platform": str(platform),
234
                "n_random_masks": str(n_random_masks),
235
                "n_domain_masks": str(n_domain_masks),
236
                "n_deleted_random_masks": str(n_deleted_random_masks),
237
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
238
                "date_joined_relay": str(date_joined_relay),
239
                "premium_status": str(premium_status),
240
                "date_joined_premium": str(date_joined_premium),
241
                "has_extension": str(has_extension).lower(),
242
                "date_got_extension": str(date_got_extension),
243
                "is_random_mask": str(is_random_mask).lower(),
244
                "is_reply": str(is_reply).lower(),
245
            },
246
        }
247
        self._record(user_agent, ip_address, event)
1✔
248

249
    def record_email_mask_created(
1✔
250
        self,
251
        user_agent: str,
252
        ip_address: str,
253
        fxa_id: str,
254
        platform: str,
255
        n_random_masks: int,
256
        n_domain_masks: int,
257
        n_deleted_random_masks: int,
258
        n_deleted_domain_masks: int,
259
        date_joined_relay: int,
260
        premium_status: str,
261
        date_joined_premium: int,
262
        has_extension: bool,
263
        date_got_extension: int,
264
        is_random_mask: bool,
265
        created_by_api: bool,
266
        has_website: bool,
267
    ) -> None:
268
        """
269
        Record and submit a email_mask_created event:
270
        A Relay user creates an email mask.
271
        Event is logged to STDOUT via `print`.
272

273
        :param str user_agent: The user agent.
274
        :param str ip_address: The IP address. Will be used to decode Geo information
275
            and scrubbed at ingestion.
276
        :param str fxa_id: Mozilla accounts user ID
277
        :param str platform: Relay client platform
278
        :param int n_random_masks: Number of random masks
279
        :param int n_domain_masks: Number of premium subdomain masks
280
        :param int n_deleted_random_masks: Number of deleted random masks
281
        :param int n_deleted_domain_masks: Number of deleted domain masks
282
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
283
        :param str premium_status: Subscription type and term
284
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
285
        :param bool has_extension: The user has the Relay Add-on
286
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
287
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
288
        :param bool created_by_api: The mask was created via the API, rather than an incoming email
289
        :param bool has_website: The mask was created by the Add-on or integration on a website
290
        """
291
        event = {
1✔
292
            "category": "email_mask",
293
            "name": "created",
294
            "extra": {
295
                "fxa_id": str(fxa_id),
296
                "platform": str(platform),
297
                "n_random_masks": str(n_random_masks),
298
                "n_domain_masks": str(n_domain_masks),
299
                "n_deleted_random_masks": str(n_deleted_random_masks),
300
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
301
                "date_joined_relay": str(date_joined_relay),
302
                "premium_status": str(premium_status),
303
                "date_joined_premium": str(date_joined_premium),
304
                "has_extension": str(has_extension).lower(),
305
                "date_got_extension": str(date_got_extension),
306
                "is_random_mask": str(is_random_mask).lower(),
307
                "created_by_api": str(created_by_api).lower(),
308
                "has_website": str(has_website).lower(),
309
            },
310
        }
311
        self._record(user_agent, ip_address, event)
1✔
312

313
    def record_email_mask_deleted(
1✔
314
        self,
315
        user_agent: str,
316
        ip_address: str,
317
        fxa_id: str,
318
        platform: str,
319
        n_random_masks: int,
320
        n_domain_masks: int,
321
        n_deleted_random_masks: int,
322
        n_deleted_domain_masks: int,
323
        date_joined_relay: int,
324
        premium_status: str,
325
        date_joined_premium: int,
326
        has_extension: bool,
327
        date_got_extension: int,
328
        is_random_mask: bool,
329
    ) -> None:
330
        """
331
        Record and submit a email_mask_deleted event:
332
        A Relay user deletes an email mask.
333
        Event is logged to STDOUT via `print`.
334

335
        :param str user_agent: The user agent.
336
        :param str ip_address: The IP address. Will be used to decode Geo information
337
            and scrubbed at ingestion.
338
        :param str fxa_id: Mozilla accounts user ID
339
        :param str platform: Relay client platform
340
        :param int n_random_masks: Number of random masks
341
        :param int n_domain_masks: Number of premium subdomain masks
342
        :param int n_deleted_random_masks: Number of deleted random masks
343
        :param int n_deleted_domain_masks: Number of deleted domain masks
344
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
345
        :param str premium_status: Subscription type and term
346
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
347
        :param bool has_extension: The user has the Relay Add-on
348
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
349
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
350
        """
351
        event = {
1✔
352
            "category": "email_mask",
353
            "name": "deleted",
354
            "extra": {
355
                "fxa_id": str(fxa_id),
356
                "platform": str(platform),
357
                "n_random_masks": str(n_random_masks),
358
                "n_domain_masks": str(n_domain_masks),
359
                "n_deleted_random_masks": str(n_deleted_random_masks),
360
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
361
                "date_joined_relay": str(date_joined_relay),
362
                "premium_status": str(premium_status),
363
                "date_joined_premium": str(date_joined_premium),
364
                "has_extension": str(has_extension).lower(),
365
                "date_got_extension": str(date_got_extension),
366
                "is_random_mask": str(is_random_mask).lower(),
367
            },
368
        }
369
        self._record(user_agent, ip_address, event)
1✔
370

371
    def record_email_mask_label_updated(
1✔
372
        self,
373
        user_agent: str,
374
        ip_address: str,
375
        fxa_id: str,
376
        platform: str,
377
        n_random_masks: int,
378
        n_domain_masks: int,
379
        n_deleted_random_masks: int,
380
        n_deleted_domain_masks: int,
381
        date_joined_relay: int,
382
        premium_status: str,
383
        date_joined_premium: int,
384
        has_extension: bool,
385
        date_got_extension: int,
386
        is_random_mask: bool,
387
    ) -> None:
388
        """
389
        Record and submit a email_mask_label_updated event:
390
        A Relay user updates an email mask's label.
391
        Event is logged to STDOUT via `print`.
392

393
        :param str user_agent: The user agent.
394
        :param str ip_address: The IP address. Will be used to decode Geo information
395
            and scrubbed at ingestion.
396
        :param str fxa_id: Mozilla accounts user ID
397
        :param str platform: Relay client platform
398
        :param int n_random_masks: Number of random masks
399
        :param int n_domain_masks: Number of premium subdomain masks
400
        :param int n_deleted_random_masks: Number of deleted random masks
401
        :param int n_deleted_domain_masks: Number of deleted domain masks
402
        :param int date_joined_relay: Timestamp for joining Relay, seconds since epoch
403
        :param str premium_status: Subscription type and term
404
        :param int date_joined_premium: Timestamp for starting premium_status subscription, seconds since epoch, -1 if not subscribed
405
        :param bool has_extension: The user has the Relay Add-on
406
        :param int date_got_extension: Timestamp for adding Relay Add-on, seconds since epoch, -1 if not used
407
        :param bool is_random_mask: The mask is a random mask, instead of a domain mask
408
        """
409
        event = {
1✔
410
            "category": "email_mask",
411
            "name": "label_updated",
412
            "extra": {
413
                "fxa_id": str(fxa_id),
414
                "platform": str(platform),
415
                "n_random_masks": str(n_random_masks),
416
                "n_domain_masks": str(n_domain_masks),
417
                "n_deleted_random_masks": str(n_deleted_random_masks),
418
                "n_deleted_domain_masks": str(n_deleted_domain_masks),
419
                "date_joined_relay": str(date_joined_relay),
420
                "premium_status": str(premium_status),
421
                "date_joined_premium": str(date_joined_premium),
422
                "has_extension": str(has_extension).lower(),
423
                "date_got_extension": str(date_got_extension),
424
                "is_random_mask": str(is_random_mask).lower(),
425
            },
426
        }
427
        self._record(user_agent, ip_address, event)
1✔
428

429
    def record_phone_call_received(
1✔
430
        self,
431
        user_agent: str,
432
        ip_address: str,
433
        fxa_id: str,
434
    ) -> None:
435
        """
436
        Record and submit a phone_call_received event:
437
        A Relay user receives a phone call.
438
        Event is logged to STDOUT via `print`.
439

440
        :param str user_agent: The user agent.
441
        :param str ip_address: The IP address. Will be used to decode Geo information
442
            and scrubbed at ingestion.
443
        :param str fxa_id: Mozilla accounts user ID
444
        """
445
        event = {
1✔
446
            "category": "phone",
447
            "name": "call_received",
448
            "extra": {
449
                "fxa_id": str(fxa_id),
450
            },
451
        }
452
        self._record(user_agent, ip_address, event)
1✔
453

454
    def record_phone_text_received(
1✔
455
        self,
456
        user_agent: str,
457
        ip_address: str,
458
        fxa_id: str,
459
    ) -> None:
460
        """
461
        Record and submit a phone_text_received event:
462
        A Relay user receives a text message.
463
        Event is logged to STDOUT via `print`.
464

465
        :param str user_agent: The user agent.
466
        :param str ip_address: The IP address. Will be used to decode Geo information
467
            and scrubbed at ingestion.
468
        :param str fxa_id: Mozilla accounts user ID
469
        """
470
        event = {
1✔
471
            "category": "phone",
472
            "name": "text_received",
473
            "extra": {
474
                "fxa_id": str(fxa_id),
475
            },
476
        }
477
        self._record(user_agent, ip_address, event)
1✔
478

479

480
def create_events_server_event_logger(
1✔
481
    application_id: str,
482
    app_display_version: str,
483
    channel: str,
484
) -> EventsServerEventLogger:
485
    """
486
    Factory function that creates an instance of Glean Server Event Logger to record
487
    `events` ping events.
488
    :param str application_id: The application ID.
489
    :param str app_display_version: The application display version.
490
    :param str channel: The channel.
491
    :return: An instance of EventsServerEventLogger.
492
    :rtype: EventsServerEventLogger
493
    """
494
    return EventsServerEventLogger(application_id, app_display_version, channel)
×
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