Skip to content

Commit

Permalink
Merge pull request #4913 from mozilla/dependabot/pip/typing-stubs-dc5…
Browse files Browse the repository at this point in the history
…8c16c12

Bump the typing-stubs group with 2 updates
  • Loading branch information
groovecoder authored Jul 31, 2024
2 parents 3ac620b + f9c1a60 commit 1cf5082
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
25 changes: 21 additions & 4 deletions api/tests/serializers_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.contrib.auth.models import User
from django.urls import reverse

import pytest
Expand All @@ -12,14 +13,30 @@


class PremiumValidatorsTest(APITestCase):
def _get_token_for_user(self, user: User) -> Token:
"""
Get DRF Token for user with strict type checks.
hasattr check prevents attr-defined error on Token
isinstance check prevents no-any-return error
See https://github.com/mozilla/fx-private-relay/pull/4913#discussion_r1698637372
"""
if not hasattr(Token, "objects"):
raise AttributeError("Token must have objects attribute.")
token = Token.objects.get(user=user)
if not isinstance(token, Token):
raise TypeError("token must be of type Token.")
return token

def test_non_premium_cant_set_block_list_emails(self):
free_user = make_free_test_user()
free_alias = baker.make(RelayAddress, user=free_user)
assert free_alias.block_list_emails is False

url = reverse("relayaddress-detail", args=[free_alias.id])
data = {"block_list_emails": True}
free_token = Token.objects.get(user=free_user)
free_token = self._get_token_for_user(free_user)
self.client.credentials(HTTP_AUTHORIZATION="Token " + free_token.key)
response = self.client.patch(url, data, format="json")

Expand All @@ -33,7 +50,7 @@ def test_non_premium_can_clear_block_list_emails(self):

url = reverse("relayaddress-detail", args=[free_alias.id])
data = {"block_list_emails": False}
free_token = Token.objects.get(user=free_user)
free_token = self._get_token_for_user(free_user)
self.client.credentials(HTTP_AUTHORIZATION="Token " + free_token.key)
response = self.client.patch(url, data, format="json")

Expand All @@ -48,8 +65,8 @@ def test_premium_can_set_block_list_emails(self):

url = reverse("relayaddress-detail", args=[premium_alias.id])
data = {"block_list_emails": True}
free_token = Token.objects.get(user=premium_user)
self.client.credentials(HTTP_AUTHORIZATION="Token " + free_token.key)
premium_token = self._get_token_for_user(premium_user)
self.client.credentials(HTTP_AUTHORIZATION="Token " + premium_token.key)
response = self.client.patch(url, data, format="json")

assert response.status_code == 200
Expand Down
2 changes: 2 additions & 0 deletions privaterelay/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ def copy_auth_token(
if created and instance is not None:
# baker triggers created during tests
# so first check the user doesn't already have a Token
if not hasattr(Token, "objects"):
raise AttributeError("Token must have objects attribute.")
try:
Token.objects.get(user=instance.user)
return
Expand Down
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ ruff==0.5.5
# type hinting
boto3-stubs==1.34.149
botocore-stubs==1.34.149
django-stubs==5.0.2
django-stubs==5.0.4
djangorestframework-stubs==3.15.0
mypy-boto3-s3==1.34.138
mypy-boto3-ses==1.34.141
mypy-boto3-sns==1.34.121
mypy-boto3-sqs==1.34.121
mypy==1.11.0
types-pyOpenSSL==24.1.0.20240425
types-pyOpenSSL==24.1.0.20240722
types-requests==2.32.0.20240712

0 comments on commit 1cf5082

Please sign in to comment.