Skip to content

Commit

Permalink
Merge branch 'main' into bugfix/gov_search_irrelevant
Browse files Browse the repository at this point in the history
  • Loading branch information
Saisakul Chernbumroong authored and Saisakul Chernbumroong committed Nov 15, 2024
2 parents c9ab0e2 + 11f4acd commit 86973c8
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 3 deletions.
2 changes: 2 additions & 0 deletions django_app/redbox_app/redbox_core/views/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from redbox_app.redbox_core.dash_apps import report_app
from redbox_app.redbox_core.views.api_views import user_view_pre_alpha
from redbox_app.redbox_core.views.auth_views import sign_in_link_sent_view, sign_in_view, signed_out_view
from redbox_app.redbox_core.views.chat_views import ChatsTitleView, ChatsView, DeleteChat, UpdateChatFeedback
from redbox_app.redbox_core.views.citation_views import CitationsView
Expand Down Expand Up @@ -49,4 +50,5 @@
"report_app",
"UpdateChatFeedback",
"DeleteChat",
"user_view_pre_alpha",
]
18 changes: 18 additions & 0 deletions django_app/redbox_app/redbox_core/views/api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from django.contrib.auth import get_user_model
from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from redbox_app.redbox_core.serializers import UserSerializer

User = get_user_model()


@api_view(["GET"])
@permission_classes([IsAuthenticated])
def user_view_pre_alpha(request):
"""this is for testing and evaluation only
this *will* change so that not all data is returned!
"""
serializer = UserSerializer(request.user)
return Response(serializer.data)
4 changes: 2 additions & 2 deletions django_app/redbox_app/redbox_core/views/auth_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def sign_in_view(request: HttpRequest):
from_email=settings.FROM_EMAIL,
recipient_list=[email],
)
except User.DoesNotExist as e:
logger.debug("User with email %s not found", email, exc_info=e)
except User.DoesNotExist:
return redirect("sign-up-page-1")
except HTTPError as e:
logger.exception("failed to send link to %s", email, exc_info=e)

Expand Down
13 changes: 12 additions & 1 deletion django_app/redbox_app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,19 @@
path("sitemap", views.misc_views.sitemap_view, name="sitemap"),
]


api_url_patterns = [
path("api/v0/", views.user_view_pre_alpha, name="user-view"),
]

urlpatterns = (
info_urlpatterns + other_urlpatterns + auth_urlpatterns + chat_urlpatterns + file_urlpatterns + admin_urlpatterns
info_urlpatterns
+ other_urlpatterns
+ auth_urlpatterns
+ chat_urlpatterns
+ file_urlpatterns
+ admin_urlpatterns
+ api_url_patterns
)

if settings.DEBUG:
Expand Down
38 changes: 38 additions & 0 deletions django_app/tests/views/test_api_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import logging
from http import HTTPStatus

import pytest
from django.contrib.auth import get_user_model
from django.test import Client
from django.urls import reverse

User = get_user_model()

logger = logging.getLogger(__name__)


@pytest.mark.django_db()
def test_api_view(user_with_chats_with_messages_over_time: User, client: Client):
# Given
client.force_login(user_with_chats_with_messages_over_time)

# When
url = reverse("user-view")
response = client.get(url)

# Then
assert response.status_code == HTTPStatus.OK
assert response.json()["email"] == user_with_chats_with_messages_over_time.email


@pytest.mark.django_db()
def test_api_view_fail(client: Client):
# Given that the user is not logged in

# When
url = reverse("user-view")
response = client.get(url)

# Then
assert response.status_code == HTTPStatus.FORBIDDEN
assert response.json() == {"detail": "Authentication credentials were not provided."}
40 changes: 40 additions & 0 deletions django_app/tests/views/test_auth_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import logging
from http import HTTPStatus

import pytest
from django.contrib.auth import get_user_model
from django.test import Client
from django.urls import reverse

User = get_user_model()

logger = logging.getLogger(__name__)


@pytest.mark.django_db()
def test_sign_in_view_redirect_to_sign_in(alice: User, client: Client, mailoutbox):
# Given a user that does exist in the db Alice

# When
url = reverse("sign-in")
response = client.post(url, data={"email": alice.email})

# Then
assert response.status_code == HTTPStatus.FOUND
assert response.url == "/sign-in-link-sent/"
link = next(line for line in mailoutbox[-1].body.splitlines() if line.startswith("http"))
signed_in_response = client.get(link)
assert signed_in_response.status_code == HTTPStatus.OK


@pytest.mark.django_db()
def test_sign_in_view_redirect_sign_up(client: Client):
# Given a user that does not exist in the database

# When
url = reverse("sign-in")
response = client.post(url, data={"email": "[email protected]"})

# Then
assert response.status_code == HTTPStatus.FOUND
assert response.url == "/sign-up-page-1"

0 comments on commit 86973c8

Please sign in to comment.