-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from MEHRSHAD-MIRSHEKARY/feat/log-iboard-view
✨ Feat/log iboard view
- Loading branch information
Showing
13 changed files
with
201 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from typing import Dict | ||
|
||
import pytest | ||
from django.contrib.auth.models import User | ||
from django.test import Client | ||
|
||
|
||
@pytest.fixture | ||
def setup_users(db) -> Dict[str, User]: | ||
""" | ||
Fixture to create a superuser and a normal user for testing purposes. | ||
Returns a dictionary with `superuser` and `non_superuser` keys. | ||
""" | ||
superuser = User.objects.create_superuser( | ||
username="admin", password="adminpassword", email="[email protected]" | ||
) | ||
non_superuser = User.objects.create_user( | ||
username="user", password="userpassword", email="[email protected]" | ||
) | ||
return {"superuser": superuser, "non_superuser": non_superuser} | ||
|
||
|
||
@pytest.fixture | ||
def client() -> Client: | ||
""" | ||
Fixture to provide a test client. | ||
""" | ||
return Client() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import sys | ||
|
||
import pytest | ||
from django.urls import reverse | ||
from django.test import Client | ||
from django.contrib.auth.models import User | ||
from typing import Dict | ||
|
||
from django_logging.tests.constants import PYTHON_VERSION, PYTHON_VERSION_REASON | ||
|
||
pytestmark = [ | ||
pytest.mark.views, | ||
pytest.mark.views_log_iboard, | ||
pytest.mark.skipif(sys.version_info < PYTHON_VERSION, reason=PYTHON_VERSION_REASON), | ||
] | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestLogiBoardView: | ||
""" | ||
Test suite for the `LogiBoardView` class-based view. | ||
This test suite covers: | ||
- Access control for superuser and non-superuser. | ||
- Rendering the correct template for superuser. | ||
- Correct response and content type for non-superuser. | ||
Methods: | ||
- test_superuser_access: Ensures superusers can access the LogiBoard page. | ||
- test_non_superuser_access: Ensures non-superusers are forbidden from accessing the LogiBoard page. | ||
""" | ||
|
||
def test_superuser_access( | ||
self, client: Client, setup_users: Dict[str, User] | ||
) -> None: | ||
""" | ||
Test that a superuser can access the `LogiBoardView` and the correct template is rendered. | ||
""" | ||
client.login(username="admin", password="adminpassword") | ||
response = client.get(reverse("log-iboard")) | ||
assert response.status_code == 200, "Superuser should have access to the page." | ||
assert ( | ||
"log_iboard.html" in response.template_name | ||
), "Should render the correct template for superuser." | ||
|
||
def test_non_superuser_access( | ||
self, client: Client, setup_users: Dict[str, User] | ||
) -> None: | ||
""" | ||
Test that a non-superuser receives a 403 Forbidden response when accessing the `LogiBoardView`. | ||
""" | ||
client.login(username="user", password="userpassword") | ||
response = client.get(reverse("log-iboard")) | ||
assert ( | ||
response.status_code == 403 | ||
), "Non-superuser should not have access to the page." | ||
assert ( | ||
"text/html" in response["Content-Type"] | ||
), "'text/html' should be in Content type for forbidden access." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from django.urls import path | ||
|
||
from django_logging.utils.get_conf import include_log_iboard | ||
from django_logging.views.log_iboard import LogiBoardView | ||
|
||
urlpatterns = [] | ||
|
||
if include_log_iboard(): | ||
urlpatterns.append(path("log-iboard/", LogiBoardView.as_view(), name="log-iboard")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from typing import Any, Dict | ||
|
||
from django.http import HttpRequest, HttpResponse | ||
from django.shortcuts import render | ||
from django.views.generic import TemplateView | ||
|
||
|
||
class LogiBoardView(TemplateView): | ||
"""View to render the LogiBoard page for superusers. | ||
Non-superusers are denied access and shown an error response page | ||
with a 403 status code. | ||
""" | ||
|
||
template_name = "log_iboard.html" | ||
|
||
def get( | ||
self, request: HttpRequest, *args: Any, **kwargs: Dict[str, Any] | ||
) -> HttpResponse: | ||
"""Handles GET requests. Renders the LogiBoard page for superusers, | ||
otherwise returns a 403 error response for non-superusers. | ||
Args: | ||
request (HttpRequest): The HTTP request object. | ||
*args (Any): Additional positional arguments. | ||
**kwargs (Dict[str, Any]): Additional keyword arguments. | ||
Returns: | ||
HttpResponse: The rendered LogiBoard page for superusers or an error response page for non-superusers. | ||
""" | ||
if request.user.is_superuser: | ||
return super().get(request, *args, **kwargs) | ||
|
||
return render( | ||
request, | ||
"error_response.html", | ||
{ | ||
"title": "Access Denied", | ||
"message": "You do not have permission to view this page.", | ||
}, | ||
status=403, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters