diff options
author | Étienne Loks <etienne.loks@iggdrasil.net> | 2021-10-15 17:54:03 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2022-12-12 12:20:59 +0100 |
commit | 3221ede6e709fc680e1bebebcfc334cc46e889ca (patch) | |
tree | 88ad3ba1c0bc80e648cf994794652a0bb53673f1 /archaeological_operations/tests.py | |
parent | 6fc6efad0ee57e430903b0d24c6e925652ff3714 (diff) | |
download | Ishtar-3221ede6e709fc680e1bebebcfc334cc46e889ca.tar.bz2 Ishtar-3221ede6e709fc680e1bebebcfc334cc46e889ca.zip |
Syndication - initialize type match with a source
Diffstat (limited to 'archaeological_operations/tests.py')
-rw-r--r-- | archaeological_operations/tests.py | 97 |
1 files changed, 81 insertions, 16 deletions
diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index d13a21011..e58868c1f 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -23,8 +23,10 @@ from subprocess import Popen, PIPE from io import StringIO, BytesIO import tempfile import locale +from unittest.mock import patch import zipfile +import requests from django.apps import apps from django.conf import settings from django.contrib.auth.models import Group @@ -44,6 +46,8 @@ from rest_framework.authtoken.models import Token from . import models +from ishtar_common import models_rest +from ishtar_common.admin import update_types_from_source from ishtar_common.views import document_deletion_steps from ishtar_common.serializers import document_serialization from archaeological_operations import views, serializers @@ -4452,13 +4456,12 @@ class ApiTest(OperationInitTest, APITestCase): self.auth_token = "Token " + Token.objects.create(user=self.user).key self.api_user = ApiUser.objects.create(user_ptr=self.user, ip="127.0.0.1") - def create_api_search_model(self, app_label="archaeological_operations", - model="operation"): + def create_api_search_model( + self, app_label="archaeological_operations", model="operation" + ): return ApiSearchModel.objects.create( user=self.api_user, - content_type=ContentType.objects.get( - app_label=app_label, model=model - ), + content_type=ContentType.objects.get(app_label=app_label, model=model), ) def test_api_permissions(self): @@ -4531,19 +4534,24 @@ class ApiTest(OperationInitTest, APITestCase): self.create_api_search_model() url = reverse("api-facets-operation") response = self.client.get( - url, format="json", HTTP_AUTHORIZATION=self.auth_token, + url, + format="json", + HTTP_AUTHORIZATION=self.auth_token, ) self.assertEqual(response.status_code, 200) j = json.loads(response.content.decode()) - self.assertIn('archaeological_operations.operation', j) + self.assertIn("archaeological_operations.operation", j) # no permissions for archaeological_site - self.assertNotIn('archaeological_operations.archaeological_site', j) + self.assertNotIn("archaeological_operations.archaeological_site", j) has_type = False - for content in j['archaeological_operations.operation']: - if content[0] == ["type", "type"]: + for content in j["archaeological_operations.operation"]: + if content[0] == "ishtar_common.operationtype" and content[1] == [ + "type", + "type", + ]: has_type = True - lst = content[1] + lst = content[2] for tpe in models.OperationType.objects.filter(available=True).all(): self.assertIn([tpe.txt_idx, tpe.label], lst) # do not send not available @@ -4552,18 +4560,75 @@ class ApiTest(OperationInitTest, APITestCase): break self.assertTrue(has_type) - self.create_api_search_model(app_label="archaeological_operations", - model="archaeologicalsite") + self.create_api_search_model( + app_label="archaeological_operations", model="archaeologicalsite" + ) url = reverse("api-facets-operation") response = self.client.get( - url, format="json", HTTP_AUTHORIZATION=self.auth_token, + url, + format="json", + HTTP_AUTHORIZATION=self.auth_token, ) self.assertEqual(response.status_code, 200) j = json.loads(response.content.decode()) - self.assertIn('archaeological_operations.archaeologicalsite', j) + self.assertIn("archaeological_operations.archaeologicalsite", j) - def test_type_admin(self): + def _get_source(self): + src, __ = models_rest.ApiExternalSource.objects.get_or_create( + url="http://localhost", name="Default source", key="xxx" + ) + return src + + @patch("requests.get") + def test_type_admin(self, mock_get): # POV: local + # receive from a source + + source = self._get_source() + url = "/admin/{}/{}/".format( + "ishtar_common", models_rest.ApiExternalSource.__name__.lower() + ) + params = { + "action": "update_types_from_source", + "_selected_action": [source.pk], + } + response = self.client.post(url, params) + self.assertEqual( + response["Location"], + "/admin/login/?next=/admin/ishtar_common/apiexternalsource/", + ) + + mock_get.return_value.status_code = 404 + self.client.login(username=self.username, password=self.password) + response = self.client.post(url, params, follow=True) + self.assertEqual( + response.status_code, + 200, + ) + self.assertIn( + "404 != 200", + response.content.decode(), + ) + mock_get.return_value.status_code = 200 + + def __json(): + return json.loads(mock_get.return_value.text) + + mock_get.return_value.json = __json + mock_get.return_value.text = "NOK" + + response = self.client.post(url, params, follow=True) + self.assertIn( + str(_("is not a valid JSON message")), + response.content.decode(), + ) + + with open( + "../archaeological_operations/tests/external_source_types_1.json", "r" + ) as tpes: + mock_get.return_value.text = tpes.read() + response = self.client.post(url, params, follow=True) + # create and update matches from an external source # delete old matches! pass |