Skip to content
This repository has been archived by the owner on Sep 16, 2022. It is now read-only.

Commit

Permalink
put api-error objects behind settings flag, disable signal and api-er…
Browse files Browse the repository at this point in the history
…ror tests if they are not enabled.
  • Loading branch information
Toni committed Nov 26, 2021
1 parent d6f94b9 commit 57a782c
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 9 deletions.
11 changes: 9 additions & 2 deletions src/metax_api/api/rest/base/views/common_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import logging
from os import path

from django.conf import settings
from django.http import Http404, HttpResponse
from rest_framework import status
from rest_framework.exceptions import APIException, MethodNotAllowed, PermissionDenied
Expand Down Expand Up @@ -141,7 +142,10 @@ def handle_exception(self, exc):
try:
error_json = ApiErrorSerializerV2.request_to_json(self.request, response)
response.data["error_identifier"] = error_json["identifier"]
rabbitmq.publish(error_json, exchange="apierrors")
if settings.ENABLE_API_ERROR_OBJECTS:
rabbitmq.publish(error_json, exchange="apierrors")
else:
_logger.error(f"api error: {str(error_json)}")
except Exception as e:
_logger.error(f"could not send api error to rabbitmq. Error: {e}")

Expand Down Expand Up @@ -375,7 +379,10 @@ def _check_and_store_bulk_error(self, request, response):
try:
error_json = ApiErrorSerializerV2.request_to_json(self.request, response, other={"bulk_request": True})
response.data["error_identifier"] = error_json["identifier"]
rabbitmq.publish(error_json, exchange="apierrors")
if settings.ENABLE_API_ERROR_OBJECTS:
rabbitmq.publish(error_json, exchange="apierrors")
else:
_logger.error(f"api error: {str(error_json)}")
except Exception as e:
_logger.error(f"could not send api error to rabbitmq. Error: {e}")

Expand Down
3 changes: 2 additions & 1 deletion src/metax_api/onappstart.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def ready(self): # pragma: no cover
# because the "django apps" have not been loaded yet.
import json

import metax_api.signals # noqa
if settings.ENABLE_SIGNALS:
import metax_api.signals # noqa
from metax_api.services import RabbitMQService as rabbitmq
from metax_api.services.redis_cache_service import RedisClient

Expand Down
2 changes: 2 additions & 0 deletions src/metax_api/settings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
ELASTIC_SEARCH_HOSTS=(list, ["localhost"]),
ELASTIC_SEARCH_PORT=(int, 9200),
ELASTIC_SEARCH_USE_SSL=(bool, False),
ENABLE_API_ERROR_OBJECTS=(bool, False),
ENABLE_DELETED_OBJECTS_SAVING=(bool, False),
ENABLE_SIGNALS=(bool, False),
ENABLE_V1_ENDPOINTS=(bool, True),
ENABLE_V2_ENDPOINTS=(bool, True),
ENABLE_DJANGO_WATCHMAN=(bool, False),
Expand Down
2 changes: 2 additions & 0 deletions src/metax_api/settings/components/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
ERROR_FILES_PATH = env("ERROR_FILES_PATH")

ENABLE_DELETED_OBJECTS_SAVING = env("ENABLE_DELETED_OBJECTS_SAVING")
ENABLE_SIGNALS = env("ENABLE_SIGNALS")
ENABLE_API_ERROR_OBJECTS = env("ENABLE_API_ERROR_OBJECTS")


# Allow only specific hosts to access the app
Expand Down
7 changes: 5 additions & 2 deletions src/metax_api/signals/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
from .post_delete import *
from .request_finished import *
from django.conf import settings

if settings.ENABLE_SIGNALS:
from .post_delete import *
from .request_finished import *
3 changes: 3 additions & 0 deletions src/metax_api/tests/api/rest/v2/views/apierrors/read.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# :author: CSC - IT Center for Science Ltd., Espoo Finland <[email protected]>
# :license: MIT
import logging
import unittest
from unittest.mock import patch
from uuid import uuid4

Expand All @@ -14,10 +15,12 @@

from metax_api.models import ApiError
from metax_api.tests.utils import TestClassUtils, test_data_file_path, testcase_log_console
from django.conf import settings

_logger = logging.getLogger(__name__)


@unittest.skipIf(settings.ENABLE_API_ERROR_OBJECTS is not True, "Only run if API errors are objects")
class ApiErrorReadBasicTests(APITestCase, TestClassUtils):

"""
Expand Down
10 changes: 6 additions & 4 deletions src/metax_api/tests/models/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
# :license: MIT

from datetime import date

import unittest
from django.core.management import call_command
from django.test import TestCase
from django.conf import settings

from metax_api.models import CatalogRecord, CatalogRecordV2, DeletedObject, Directory, File
from metax_api.models import CatalogRecord, CatalogRecordV2, DeletedObject
from metax_api.tests.utils import TestClassUtils, test_data_file_path


@unittest.skipIf(settings.ENABLE_DELETED_OBJECTS_SAVING is not True, "Only run if deleted objects are saved")
class SignalTests(TestCase, TestClassUtils):

def setUp(self):
call_command("loaddata", test_data_file_path, verbosity=0)
self.today = date.today().strftime("%d/%m/%Y")

""" def test_deleting_catalog_record_creates_new_deleted_object(self):
def test_deleting_catalog_record_creates_new_deleted_object(self):
# test that deleting CatalogRecord object creates a new deleted object
CatalogRecord.objects_unfiltered.get(pk=1).delete(hard=True)
deleted_object = DeletedObject.objects.last()
Expand All @@ -31,6 +33,6 @@ def setUp(self):
CatalogRecordV2.objects_unfiltered.get(pk=2).delete(hard=True)
deleted_object_v2 = DeletedObject.objects.last()
self.assertEqual(deleted_object_v2.model_name, "CatalogRecordV2")
self.assertEqual(deleted_object_v2.date_deleted.strftime("%d/%m/%Y"), self.today)"""
self.assertEqual(deleted_object_v2.date_deleted.strftime("%d/%m/%Y"), self.today)


0 comments on commit 57a782c

Please sign in to comment.