Skip to content

Commit

Permalink
Add tests and fix unhandled cases
Browse files Browse the repository at this point in the history
  • Loading branch information
swainn committed Jan 6, 2024
1 parent 6ed7012 commit b8ea847
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 6 deletions.
20 changes: 20 additions & 0 deletions tests/unit_tests/test_tethys_portal/test_utilities.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import datetime
import uuid
import unittest
from unittest import mock

Expand Down Expand Up @@ -84,3 +86,21 @@ def test_utilities_no_user_exist_next(self, mock_redirect, mock_authenticate):

# mock redirect after logged in using next parameter or default to user profile
mock_redirect.assert_called_once_with("foo")

def test_json_serializer_datetime(self):
d = datetime.datetime(2020, 1, 1)
ret = utilities.json_serializer(d)
self.assertEqual("2020-01-01T00:00:00", ret)

def test_json_serializer_uuid(self):
u = uuid.uuid4()
ret = utilities.json_serializer(u)
self.assertEqual(str(u), ret)

def test_json_serializer_other(self):
with self.assertRaises(TypeError) as cm:
utilities.json_serializer(1)

self.assertEqual(
'Object of type "int" is not JSON serializable', str(cm.exception)
)
47 changes: 47 additions & 0 deletions tests/unit_tests/test_tethys_portal/test_views/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,53 @@ def test_get_app_valid_id_with_prefix(self):
r"^/test/prefix/admin/tethys_apps/tethysapp/[0-9]+/change/$",
)

@override_settings(STATIC_URL="/static")
@override_settings(PREFIX_URL="/")
@override_settings(LOGIN_URL="/accounts/login/")
def test_get_app_authenticated(self):
self.client.force_login(self.user)
self.reload_urlconf()

"""Test get_app API endpoint with valid app id."""
response = self.client.get(reverse("api:get_app", kwargs={"app": "test-app"}))
self.assertEqual(response.status_code, 200)
self.assertIsInstance(response, JsonResponse)
json = response.json()
self.assertIn("title", json)
self.assertIn("description", json)
self.assertIn("tags", json)
self.assertIn("package", json)
self.assertIn("urlNamespace", json)
self.assertIn("color", json)
self.assertIn("icon", json)
self.assertIn("exitUrl", json)
self.assertIn("rootUrl", json)
self.assertIn("settingsUrl", json)
self.assertEqual("Test App", json["title"])
self.assertEqual(
"Place a brief description of your app here.", json["description"]
)
self.assertEqual("", json["tags"])
self.assertEqual("test_app", json["package"])
self.assertEqual("test_app", json["urlNamespace"])
self.assertEqual("#2c3e50", json["color"])
self.assertEqual("/static/test_app/images/icon.gif", json["icon"])
self.assertEqual("/apps/", json["exitUrl"])
self.assertEqual("/apps/test-app/", json["rootUrl"])
self.assertRegex(
json["settingsUrl"],
r"^/admin/tethys_apps/tethysapp/[0-9]+/change/$",
)
self.assertDictEqual(
{
'change_factor': {'type': 'FLOAT', 'value': None},
'default_name': {'type': 'STRING', 'value': None},
'enable_feature': {'type': 'BOOLEAN', 'value': None},
'max_count': {'type': 'INTEGER', 'value': None}
},
json["customSettings"]
)

def test_get_app_invalid_id(self):
"""Test get_app API endpoint with invalid app id."""
response = self.client.get(reverse("api:get_app", kwargs={"app": "foo-bar"}))
Expand Down
10 changes: 10 additions & 0 deletions tethys_portal/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
* License: BSD 2-Clause
********************************************************************************
"""
import datetime
from uuid import UUID
from django.contrib.auth import login
from django.contrib import messages
from django.shortcuts import redirect
Expand Down Expand Up @@ -50,3 +52,11 @@ def log_user_in(request, user=None, username=None):
return redirect(request.GET["next"])
else:
return redirect("app_library")


def json_serializer(obj):
if isinstance(obj, datetime.datetime):
return obj.isoformat()
elif isinstance(obj, UUID):
return str(obj)
raise TypeError(f'Object of type "{obj.__class__.__name__}" is not JSON serializable')
26 changes: 20 additions & 6 deletions tethys_portal/views/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
from django.shortcuts import reverse
from django.views.decorators.csrf import ensure_csrf_cookie

from tethys_apps.exceptions import TethysAppSettingNotAssigned
from tethys_portal.utilities import json_serializer


def get_csrf(request):
if not request.user.is_authenticated:
Expand Down Expand Up @@ -58,12 +61,23 @@ def get_app(request, app):
}

if request.user.is_authenticated:
metadata["customSettings"] = {
s.name: {
metadata["customSettings"] = dict()
for s in app.custom_settings:
if s.type_custom_setting != "SIMPLE":
continue

v = None
try:
v = s.get_value()
except TethysAppSettingNotAssigned:
pass

metadata["customSettings"][s.name] = {
"type": s.type,
"value": s.get_value(),
"value": v,
}
for s in app.custom_settings
}

return JsonResponse(metadata)
return JsonResponse(
metadata,
json_dumps_params={"default": json_serializer}
)

0 comments on commit b8ea847

Please sign in to comment.