Skip to content

Commit

Permalink
test allow_basic_auth (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
dulmandakh authored Oct 17, 2021
1 parent 83e04ca commit e0d6fa7
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 22 deletions.
10 changes: 4 additions & 6 deletions ariadne_django_ext/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@
def allow_basic_auth(view_func):
@wraps(view_func)
def wrapper(request, *args, **kwargs):
if not is_authenticated(request):
if getattr(request, "user", None) is None:
http_auth = request.META.get("HTTP_AUTHORIZATION")
if http_auth and http_auth.startswith("Basic"):
try:
_, token = http_auth.split()
username, password = b64decode(token).decode().split(":")
user = authenticate(
request=request, username=username, password=password
)
if user and user.is_active:
user = authenticate(request, username=username, password=password)
if user:
request.user = user
except Exception:
pass
Expand All @@ -30,7 +28,7 @@ def wrapper(request, *args, **kwargs):
def login_required(view_func):
@wraps(view_func)
def wrapper(request, *args, **kwargs):
if is_authenticated(request, is_active=True, raise_exception=True):
if is_authenticated(request):
return view_func(request, *args, **kwargs)

return wrapper
Expand Down
2 changes: 1 addition & 1 deletion ariadne_django_ext/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


def is_authenticated(request, is_active=True, raise_exception=True):
user = getattr(request, "user")
user = getattr(request, "user", None)
if user and user.is_authenticated and (not is_active or user.is_active):
return user
if raise_exception:
Expand Down
9 changes: 7 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import pytest

password = "something"


@pytest.fixture(
ids=["anonymous", "not-active", "authenticated"],
params=[None, False, True],
)
def user(request, django_user_model):
if request.param is not None:
return django_user_model.objects.create(
username="someone", password="something", is_active=request.param
user = django_user_model.objects.create(
username="someone", is_active=request.param
)
user.set_password(password)
user.save(update_fields=["password"])
return user


@pytest.fixture
Expand Down
3 changes: 2 additions & 1 deletion tests/settings.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
SECRET_KEY = "asda"
SECRET_KEY = "SECRET_KEY"
INSTALLED_APPS = ["django.contrib.auth", "django.contrib.contenttypes"]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.sqlite3",
"NAME": "test",
}
}
USE_TZ = True
40 changes: 37 additions & 3 deletions tests/test_decorators.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,40 @@
from base64 import b64encode

import pytest
from django.core.exceptions import PermissionDenied

from ariadne_django_ext.decorators import login_required
from ariadne_django_ext import decorators

from .conftest import password


def test_allow_basic_auth(user, rf, django_user_model):
return_value = "return_value"

@decorators.allow_basic_auth
@decorators.login_required
def view(_):
return return_value

request = rf.get("/")
if user:
request.META["HTTP_AUTHORIZATION"] = "Basic {}".format(
b64encode(f"{user.username}:{password}".encode()).decode()
)

assert getattr(request, "user", None) is None
if user and user.is_active:
assert view(request) == return_value
else:
with pytest.raises(PermissionDenied):
view(request)


def test_login_required(user_request):
return_value = "return_value"

@login_required
def view(request):
@decorators.login_required
def view(_):
return return_value

user = getattr(user_request, "user", None)
Expand All @@ -17,3 +43,11 @@ def view(request):
else:
with pytest.raises(PermissionDenied):
view(user_request)


def test_wrap_result():
@decorators.wrap_result(key="key")
def resolver():
return "result"

assert resolver() == {"key": "result"}
9 changes: 0 additions & 9 deletions tests/test_wrap_result.py

This file was deleted.

0 comments on commit e0d6fa7

Please sign in to comment.