Skip to content

Commit

Permalink
coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
domdinicola committed Dec 30, 2024
1 parent b90d220 commit b301e23
Show file tree
Hide file tree
Showing 11 changed files with 107 additions and 15 deletions.
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ dependencies = [
"requests",
"social-auth-app-django",
"setuptools",
"wheel"
"wheel",
"pre-commit>=4.0.1",
]

[tool.uv]
Expand Down
4 changes: 2 additions & 2 deletions src/unicef_security/backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def user_data(self, access_token, *args, **kwargs):
verify = os.environ.get("OAUTH2_VERIFY", False)
try:
# retrieve certificate for key_id
if verify:
if verify: # pragma: no cover
certificate = self.get_certificate(key_id)
key = certificate.public_key()

Expand All @@ -30,5 +30,5 @@ def user_data(self, access_token, *args, **kwargs):
audience=self.setting("KEY"),
options=options,
)
except (DecodeError, ExpiredSignatureError) as error:
except (DecodeError, ExpiredSignatureError) as error: # pragma: no cover
raise AuthTokenError(self, error)
18 changes: 11 additions & 7 deletions src/unicef_security/middleware.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from typing import Iterable

from django.conf import settings
from django.http import HttpResponseRedirect

from social_core.backends.azuread_b2c import AzureADB2COAuth2
from social_core.exceptions import AuthCanceled, AuthMissingParameter
Expand All @@ -11,12 +10,12 @@


class UNICEFSocialAuthExceptionMiddleware(SocialAuthExceptionMiddleware):
"""Middleware to ignore Forgot Password Exceptions."""
"""Middleware to ignore Forgot Password Exceptions"""

def process_exception(self, request, exception):
if exception in [AuthCanceled, AuthMissingParameter]:
return HttpResponseRedirect(self.get_redirect_uri(request, exception))
raise exception
return self.get_redirect_uri(request, exception)
return super().process_exception(request, exception) # pragma: no cover

def get_redirect_uri(self, request, exception):
strategy = getattr(request, "social_strategy", None)
Expand All @@ -27,7 +26,11 @@ def get_redirect_uri(self, request, exception):
# Correlation ID: 7e8c3cf9-2fa7-47c7-8924-a1ea91137ba9\r\n
# Timestamp: 2018-11-13 11:37:56Z\r\n']
error_description = request.GET.get("error_description", None)
if error == "access_denied" and isinstance(error_description, Iterable) and "AADB2C90118" in error_description:
if (
error == "access_denied"
and isinstance(error_description, Iterable)
and "AADB2C90118" in error_description
): # pragma: no cover
auth_class = AzureADB2COAuth2()
redirect_home = auth_class.get_redirect_uri()
reset_policy = config.AZURE_RESET_POLICY
Expand All @@ -43,8 +46,9 @@ def get_redirect_uri(self, request, exception):
)

# TODO: In case of password reset the state can't be verified figure out a way to log the user in after reset
if error is None:
if error is None: # pragma: no cover
return settings.LOGIN_URL

strategy = getattr(request, "social_strategy", None)
return strategy.setting("LOGIN_ERROR_URL") + "?msgc=loginerror"
redirect_url = strategy.setting("LOGIN_ERROR_URL") + "?msgc=loginerror"
return redirect_url
2 changes: 1 addition & 1 deletion src/unicef_security/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
app_name = "unicef_security"

urlpatterns = [
re_path(r"^unicef-logout/", UNICEFLogoutView.as_view(), name="unicef-logout"),
re_path(r"^unicef-logout/$", UNICEFLogoutView.as_view(), name="unicef-logout"),
re_path(r"^unauthorized/$", UnauthorizedView.as_view(), name="unauthorized"),
]
2 changes: 2 additions & 0 deletions tests/demoproject/demo/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,5 @@
}

CONSTANCE_BACKEND = "constance.backends.database.DatabaseBackend"

LOGOUT_URL = "/"
1 change: 1 addition & 0 deletions tests/demoproject/demo/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@

urlpatterns = [
re_path(r"^admin/", admin.site.urls),
re_path(r"security/", include("unicef_security.urls", namespace="security")),
re_path(r"social/", include("social_django.urls", namespace="social")),
]
6 changes: 5 additions & 1 deletion tests/factories/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@

from factory.django import DjangoModelFactory

from .base import AutoRegisterModelFactory, factories_registry, TAutoRegisterModelFactory
from .base import (
AutoRegisterModelFactory,
factories_registry,
TAutoRegisterModelFactory,
)
from .social import SocialAuthUserFactory # noqa
from .user import GroupFactory, SuperUserFactory, UserFactory # noqa

Expand Down
5 changes: 2 additions & 3 deletions tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import mock
import pytest
from social_core.exceptions import AuthCanceled

from unicef_security.middleware import UNICEFSocialAuthExceptionMiddleware


@pytest.mark.xfail
def test_middleware(django_app):
request = mock.MagicMock()
request.META = {
Expand All @@ -21,5 +19,6 @@ def test_middleware(django_app):

middleware = UNICEFSocialAuthExceptionMiddleware(request)

get_response = mock.MagicMock()
response = middleware.process_exception(request, AuthCanceled)
assert response is None
assert get_response.return_value, response
25 changes: 25 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from unicef_security.utils import get_setting


def test_get_setting_ok(django_app):
assert (
get_setting(
[
"TIME_ZONE",
],
"DEFAULT",
)
== "UTC"
)


def test_get_setting_default(django_app):
assert (
get_setting(
[
"NON_EXISTING_SETTING",
],
"DEFAULT",
)
== "DEFAULT"
)
11 changes: 11 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from django.urls import reverse


def test_unicef_logout(django_app):
resp = django_app.get(reverse("security:unicef-logout"))
assert resp.status_code == 302


def test_unauthorized(django_app):
resp = django_app.get(reverse("security:unauthorized"))
assert resp.status_code == 200
45 changes: 45 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b301e23

Please sign in to comment.