Skip to content

Commit

Permalink
Merge pull request #122 from GeneriekPublicatiePlatformWoo/feature/ch…
Browse files Browse the repository at this point in the history
…ange-type-ignore-with-pyright

Replaced '# type:' to '# pyright:'
  • Loading branch information
sergei-maertens authored Nov 13, 2024
2 parents 671066d + f30c859 commit 1fec7ab
Show file tree
Hide file tree
Showing 26 changed files with 207 additions and 113 deletions.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ omit =
src/woo_publications/conf/local_example.py
src/woo_publications/conf/production.py
src/woo_publications/conf/staging.py
src/woo_publications/conf/docker.py
src/woo_publications/conf/ci.py
*/migrations/*
*/tests/*

Expand Down
6 changes: 3 additions & 3 deletions src/woo_publications/accounts/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ def get_form(self, request, obj=None, change=False, **kwargs):
# Set the current and target user on the ModelForm class so they are
# available in the instantiated form. See the comment in the
# UserChangeForm for more details.
ModelForm._current_user = request.user # type: ignore
ModelForm._target_user = obj # type: ignore
ModelForm._current_user = request.user # pyright: ignore
ModelForm._target_user = obj # pyright: ignore
return ModelForm

def user_change_password(self, request, id, form_url=""):
user: User = self.get_object(request, unquote(id)) # type: ignore
user: User = self.get_object(request, unquote(id)) # pyright: ignore
assert isinstance(request.user, User)
try:
validate_max_user_permissions(request.user, user)
Expand Down
2 changes: 1 addition & 1 deletion src/woo_publications/accounts/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def clean(self):
if self._target_user is None: # pragma: no cover
raise RuntimeError("Could not determine target user.")

cleaned_data = super().clean() # type: ignore
cleaned_data = super().clean() # pyright: ignore
user_permissions = cleaned_data.get("user_permissions")
groups = cleaned_data.get("groups")
is_superuser = cleaned_data.get("is_superuser")
Expand Down
2 changes: 1 addition & 1 deletion src/woo_publications/accounts/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class UserFactory(DjangoModelFactory):
last_name = factory.Faker("last_name")
password = factory.PostGenerationMethodCall("set_password", "password")

class Meta: # type: ignore
class Meta: # pyright: ignore
model = User

class Params:
Expand Down
6 changes: 4 additions & 2 deletions src/woo_publications/accounts/tests/test_user_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

class UserManagerTests(TestCase):
def test_create_superuser(self):
user = User.objects.create_superuser("god", "god@heaven.com", "praisejebus") # type: ignore
user = User.objects.create_superuser( # pyright: ignore
"god", "god@heaven.com", "praisejebus"
)
self.assertIsNotNone(user.pk)
self.assertTrue(user.is_staff)
self.assertTrue(user.is_superuser)
Expand All @@ -16,7 +18,7 @@ def test_create_superuser(self):
self.assertNotEqual(user.password, "praisejebus")

def test_create_user(self):
user = User.objects.create_user("infidel") # type: ignore
user = User.objects.create_user("infidel") # pyright: ignore
self.assertIsNotNone(user.pk)
self.assertFalse(user.is_superuser)
self.assertFalse(user.is_staff)
Expand Down
3 changes: 2 additions & 1 deletion src/woo_publications/api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by Django 4.2.16 on 2024-10-30 17:06

from django.db import migrations, models

import django_jsonform.models.fields
import phonenumber_field.modelfields

Expand All @@ -27,7 +28,7 @@ class Migration(migrations.Migration):
("token", models.CharField(max_length=40, verbose_name="token")),
(
"permissions",
django_jsonform.models.fields.ArrayField( # type: ignore reportArgumentType
django_jsonform.models.fields.ArrayField( # pyright: ignore[reportArgumentType]
base_field=models.CharField(
choices=[("read", "Read"), ("write", "Write")],
max_length=20,
Expand Down
2 changes: 1 addition & 1 deletion src/woo_publications/api/tests/factories.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class TokenAuthFactory(factory.django.DjangoModelFactory):
permissions = []

class Meta: # type: ignore
class Meta: # pyright: ignore
model = Application

class Params:
Expand Down
164 changes: 122 additions & 42 deletions src/woo_publications/api/tests/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,163 +17,243 @@ def assertWrongApiKeyProhibitsGetEndpointAccess(self, url):
no_permission_token = TokenAuthFactory.create(permissions=[]).token
write_token = TokenAuthFactory.create(write_permission=True).token

with self.subTest("no token given"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"no token given"
):
response = self.client.get(url, headers=AUDIT_HEADERS)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("none existing token"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"none existing token"
):
response = self.client.get(
url, headers={"Authorization": "Token broken", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("token with no permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with no permission"
):
response = self.client.get(
url,
headers={
"Authorization": f"Token {no_permission_token}",
**AUDIT_HEADERS,
},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

with self.subTest("token with wrong permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with wrong permission"
):
response = self.client.get(
url, headers={"Authorization": f"Token {write_token}", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

def assertWrongApiKeyProhibitsPostEndpointAccess(self, url):
no_permission_token = TokenAuthFactory.create(permissions=[]).token
read_token = TokenAuthFactory.create(read_permission=True).token

with self.subTest("no token given"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"no token given"
):
response = self.client.post(url, headers=AUDIT_HEADERS)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("none existing token"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"none existing token"
):
response = self.client.post(
url, headers={"Authorization": "Token broken", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("token with no permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with no permission"
):
response = self.client.post(
url,
headers={
"Authorization": f"Token {no_permission_token}",
**AUDIT_HEADERS,
},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

with self.subTest("token with wrong permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with wrong permission"
):
response = self.client.post(
url, headers={"Authorization": f"Token {read_token}", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

def assertWrongApiKeyProhibitsPatchEndpointAccess(self, url):
no_permission_token = TokenAuthFactory.create(permissions=[]).token
read_token = TokenAuthFactory.create(read_permission=True).token

with self.subTest("no token given"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"no token given"
):
response = self.client.patch(url, headers=AUDIT_HEADERS)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("none existing token"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"none existing token"
):
response = self.client.patch(
url, headers={"Authorization": "Token broken", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("token with no permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with no permission"
):
response = self.client.patch(
url,
headers={
"Authorization": f"Token {no_permission_token}",
**AUDIT_HEADERS,
},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

with self.subTest("token with wrong permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with wrong permission"
):
response = self.client.patch(
url, headers={"Authorization": f"Token {read_token}", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

def assertWrongApiKeyProhibitsPutEndpointAccess(self, url):
no_permission_token = TokenAuthFactory.create(permissions=[]).token
read_token = TokenAuthFactory.create(read_permission=True).token

with self.subTest("no token given"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"no token given"
):
response = self.client.put(url, headers=AUDIT_HEADERS)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("none existing token"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"none existing token"
):
response = self.client.put(
url, headers={"Authorization": "Token broken", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("token with no permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with no permission"
):
response = self.client.put(
url,
headers={
"Authorization": f"Token {no_permission_token}",
**AUDIT_HEADERS,
},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

with self.subTest("token with wrong permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with wrong permission"
):
response = self.client.put(
url, headers={"Authorization": f"Token {read_token}", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

def assertWrongApiKeyProhibitsDeleteEndpointAccess(self, url):
no_permission_token = TokenAuthFactory.create(permissions=[]).token
read_token = TokenAuthFactory.create(read_permission=True).token

with self.subTest("no token given"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"no token given"
):
response = self.client.delete(url, headers=AUDIT_HEADERS)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("none existing token"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"none existing token"
):
response = self.client.delete(
url, headers={"Authorization": "Token broken", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_401_UNAUTHORIZED
)

with self.subTest("token with no permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with no permission"
):
response = self.client.delete(
url,
headers={
"Authorization": f"Token {no_permission_token}",
**AUDIT_HEADERS,
},
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)

with self.subTest("token with wrong permission"): # type: ignore reportAttributeAccessIssue
with self.subTest( # pyright: ignore[reportAttributeAccessIssue]
"token with wrong permission"
):
response = self.client.delete(
url, headers={"Authorization": f"Token {read_token}", **AUDIT_HEADERS}
)
self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN) # type: ignore reportAttributeAccessIssue
self.assertEqual( # pyright: ignore[reportAttributeAccessIssue]
response.status_code, status.HTTP_403_FORBIDDEN
)


class TokenAuthMixin:
client: APIClient

@classmethod
def setUpTestData(cls):
super().setUpTestData() # type: ignore reportAttributeAccessIssue
super().setUpTestData() # pyright: ignore[reportAttributeAccessIssue]

cls.token_auth = TokenAuthFactory.create(read_write_permission=True)

def setUp(self):
super().setUp() # type: ignore reportAttributeAccessIssue
super().setUp() # pyright: ignore[reportAttributeAccessIssue]

self.client.credentials(HTTP_AUTHORIZATION=f"Token {self.token_auth.token}")
6 changes: 3 additions & 3 deletions src/woo_publications/conf/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,9 @@

# Subpath (optional)
# This environment variable can be configured during deployment.
SUBPATH = config("SUBPATH", default="")
if SUBPATH:
SUBPATH = f"/{SUBPATH.strip('/')}"
SUBPATH = (
f"/{_subpath.strip('/')}" if (_subpath := config("SUBPATH", default="")) else ""
)

#
# DJANGO REST FRAMEWORK
Expand Down
2 changes: 1 addition & 1 deletion src/woo_publications/conf/dev.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,6 @@

# Override settings with local settings.
try:
from .local import * # type: ignore # noqa
from .local import * # pyright: ignore # noqa
except ImportError:
pass
Loading

0 comments on commit 1fec7ab

Please sign in to comment.