diff --git a/src/objecttypes/setup_configuration/tests/test_token_auth_config.py b/src/objecttypes/setup_configuration/tests/test_token_auth_config.py index da0b9e00..f6e87d2a 100644 --- a/src/objecttypes/setup_configuration/tests/test_token_auth_config.py +++ b/src/objecttypes/setup_configuration/tests/test_token_auth_config.py @@ -1,5 +1,7 @@ +from io import StringIO from pathlib import Path +from django.core.management import CommandError, call_command from django.test import TestCase from django_setup_configuration.exceptions import ( @@ -325,7 +327,7 @@ def test_invalid_setup_token_unique(self): self.assertTrue( "Failed configuring token token-2" in str(command_error.exception) ) - self.assertEqual(TokenAuth.objects.count(), 0) + self.assertEqual(TokenAuth.objects.count(), 1) def test_invalid_setup_contact_person(self): object_source = { @@ -386,3 +388,54 @@ def test_invalid_setup_identifier(self): in str(command_error.exception) ) self.assertEqual(TokenAuth.objects.count(), 0) + + def test_valid_call_command(self): + stdout = StringIO() + self.assertEqual(TokenAuth.objects.count(), 0) + call_command( + "setup_configuration", + "--yaml-file", + str(DIR_FILES / "valid_setup_default.yaml"), + stdout=stdout, + ) + self.assertTrue( + "Successfully executed step: Configuration to set up authentication tokens for ObjectTypes" + in stdout.getvalue() + ) + self.assertEqual(TokenAuth.objects.count(), 2) + + tokens = TokenAuth.objects.order_by("created") + self.assertEqual(tokens.count(), 2) + + token = tokens[0] + self.assertEqual(token.identifier, "token-1") + self.assertEqual(token.token, "18b2b74ef994314b84021d47b9422e82b685d82f") + self.assertEqual(token.contact_person, "Person 1") + self.assertEqual(token.email, "person-1@example.com") + self.assertEqual(token.organization, "") + self.assertEqual(token.application, "") + self.assertEqual(token.administration, "") + + token = tokens[1] + self.assertEqual(token.identifier, "token-2") + self.assertEqual(token.contact_person, "Person 2") + self.assertEqual(token.token, "e882642bd0ec2482adcdc97258c2e6f98cb06d85") + self.assertEqual(token.email, "person-2@example.com") + self.assertEqual(token.organization, "") + self.assertEqual(token.application, "") + self.assertEqual(token.administration, "") + + def test_invalid_call_command(self): + self.assertEqual(TokenAuth.objects.count(), 0) + with self.assertRaises(CommandError) as command_error: + call_command( + "setup_configuration", + "--yaml-file", + str(DIR_FILES / "invalid_setup_empty.yaml"), + ) + + self.assertTrue( + "Failed to load config model for Configuration to set up authentication tokens for ObjectTypes" + in str(command_error.exception) + ) + self.assertEqual(TokenAuth.objects.count(), 0) diff --git a/src/objecttypes/tests/commands/__init__.py b/src/objecttypes/tests/commands/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/objecttypes/tests/commands/test_setup_configuration.py b/src/objecttypes/tests/commands/test_setup_configuration.py deleted file mode 100644 index ff05edeb..00000000 --- a/src/objecttypes/tests/commands/test_setup_configuration.py +++ /dev/null @@ -1,102 +0,0 @@ -from io import StringIO - -from django.contrib.sites.models import Site -from django.core.management import CommandError, call_command -from django.test import TestCase, override_settings -from django.urls import reverse - -import requests_mock -from rest_framework import status - -from objecttypes.config.demo import DemoUserStep -from objecttypes.config.objects import ObjectsAuthStep -from objecttypes.config.site import SiteConfigurationStep - - -@override_settings( - SITES_CONFIG_ENABLE=True, - OBJECTTYPES_DOMAIN="objecttypes.example.com", - OBJECTTYPES_ORGANIZATION="ACME", - OBJECTS_OBJECTTYPES_CONFIG_ENABLE=True, - OBJECTS_OBJECTTYPES_TOKEN="some-random-string", - OBJECTS_OBJECTTYPES_PERSON="Some Person", - OBJECTS_OBJECTTYPES_EMAIL="objects@objects.local", - DEMO_CONFIG_ENABLE=True, - DEMO_TOKEN="demo-random-string", - DEMO_PERSON="Demo", - DEMO_EMAIL="demo@demo.local", -) -class SetupConfigurationTests(TestCase): - def setUp(self): - super().setUp() - - self.addCleanup(Site.objects.clear_cache) - - @requests_mock.Mocker() - def test_setup_configuration(self, m): - stdout = StringIO() - # mocks - m.get("http://objecttypes.example.com/", status_code=200) - m.get("http://objecttypes.example.com/api/v2/objecttypes", json=[]) - - call_command("setup_configuration", stdout=stdout) - - with self.subTest("Command output"): - command_output = stdout.getvalue().splitlines() - expected_output = [ - f"Configuration will be set up with following steps: [{SiteConfigurationStep()}, " - f"{ObjectsAuthStep()}, {DemoUserStep()}]", - f"Configuring {SiteConfigurationStep()}...", - f"{SiteConfigurationStep()} is successfully configured", - f"Configuring {ObjectsAuthStep()}...", - f"{ObjectsAuthStep()} is successfully configured", - f"Configuring {DemoUserStep()}...", - f"{DemoUserStep()} is successfully configured", - "Instance configuration completed.", - ] - - self.assertEqual(command_output, expected_output) - - with self.subTest("Site configured correctly"): - site = Site.objects.get_current() - self.assertEqual(site.domain, "objecttypes.example.com") - self.assertEqual(site.name, "Objecttypes ACME") - - with self.subTest("Objects can query Objecttypes API"): - response = self.client.get( - reverse("v2:objecttype-list"), - HTTP_AUTHORIZATION="Token some-random-string", - ) - - self.assertEqual(response.status_code, status.HTTP_200_OK) - - with self.subTest("Demo user configured correctly"): - response = self.client.get( - reverse("v2:objecttype-list"), - HTTP_AUTHORIZATION="Token demo-random-string", - ) - - self.assertEqual(response.status_code, status.HTTP_200_OK) - - @requests_mock.Mocker() - def test_setup_configuration_selftest_fails(self, m): - m.get("http://objecttypes.example.com/", status_code=200) - m.get("http://objecttypes.example.com/api/v2/objecttypes", status_code=500) - - with self.assertRaisesMessage( - CommandError, - "Configuration test failed with errors: " - "Objects API Authentication Configuration: " - "Could not list objecttypes for the configured token", - ): - call_command("setup_configuration") - - @requests_mock.Mocker() - def test_setup_configuration_without_selftest(self, m): - stdout = StringIO() - - call_command("setup_configuration", no_selftest=True, stdout=stdout) - command_output = stdout.getvalue() - - self.assertEqual(len(m.request_history), 0) - self.assertTrue("Selftest is skipped" in command_output) diff --git a/src/objecttypes/tests/config/__init__.py b/src/objecttypes/tests/config/__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/src/objecttypes/tests/config/test_demo_configuration.py b/src/objecttypes/tests/config/test_demo_configuration.py deleted file mode 100644 index 952cfdb5..00000000 --- a/src/objecttypes/tests/config/test_demo_configuration.py +++ /dev/null @@ -1,72 +0,0 @@ -from unittest.mock import patch - -from django.test import TestCase, override_settings - -import requests -import requests_mock -from django_setup_configuration.exceptions import SelfTestFailed - -from objecttypes.config.demo import DemoUserStep -from objecttypes.token.models import TokenAuth - - -@override_settings( - DEMO_TOKEN="demo-random-string", DEMO_PERSON="Demo", DEMO_EMAIL="demo@demo.local" -) -class DemoConfigurationTests(TestCase): - def test_configure(self): - configuration = DemoUserStep() - - configuration.configure() - - token_auth = TokenAuth.objects.get() - self.assertEqual(token_auth.token, "demo-random-string") - self.assertEqual(token_auth.contact_person, "Demo") - self.assertEqual(token_auth.email, "demo@demo.local") - - @requests_mock.Mocker() - @patch( - "objecttypes.config.demo.build_absolute_url", - return_value="http://testserver/objecttypes", - ) - def test_configuration_check_ok(self, m, *mocks): - configuration = DemoUserStep() - configuration.configure() - m.get("http://testserver/objecttypes", json=[]) - - configuration.test_configuration() - - self.assertEqual(m.last_request.url, "http://testserver/objecttypes") - self.assertEqual(m.last_request.method, "GET") - - @requests_mock.Mocker() - @patch( - "objecttypes.config.demo.build_absolute_url", - return_value="http://testserver/objecttypes", - ) - def test_configuration_check_failures(self, m, *mocks): - configuration = DemoUserStep() - configuration.configure() - - mock_kwargs = ( - {"exc": requests.ConnectTimeout}, - {"exc": requests.ConnectionError}, - {"status_code": 404}, - {"status_code": 403}, - {"status_code": 500}, - ) - for mock_config in mock_kwargs: - with self.subTest(mock=mock_config): - m.get("http://testserver/objecttypes", **mock_config) - - with self.assertRaises(SelfTestFailed): - configuration.test_configuration() - - def test_is_configured(self): - configuration = DemoUserStep() - - self.assertFalse(configuration.is_configured()) - - configuration.configure() - - self.assertTrue(configuration.is_configured()) diff --git a/src/objecttypes/tests/config/test_objects_configuration.py b/src/objecttypes/tests/config/test_objects_configuration.py deleted file mode 100644 index 044680c7..00000000 --- a/src/objecttypes/tests/config/test_objects_configuration.py +++ /dev/null @@ -1,72 +0,0 @@ -from unittest.mock import patch - -from django.test import TestCase, override_settings - -import requests -import requests_mock -from django_setup_configuration.exceptions import SelfTestFailed - -from objecttypes.config.objects import ObjectsAuthStep -from objecttypes.token.models import TokenAuth - - -@override_settings( - OBJECTS_OBJECTTYPES_TOKEN="some-random-string", - OBJECTS_OBJECTTYPES_PERSON="Some Person", - OBJECTS_OBJECTTYPES_EMAIL="objects@objects.local", -) -class ObjectsConfigurationTests(TestCase): - def test_configure(self): - configuration = ObjectsAuthStep() - - configuration.configure() - - token_auth = TokenAuth.objects.get(token="some-random-string") - self.assertEqual(token_auth.contact_person, "Some Person") - self.assertEqual(token_auth.email, "objects@objects.local") - - @requests_mock.Mocker() - @patch( - "objecttypes.config.objects.build_absolute_url", - return_value="http://testserver/objecttypes", - ) - def test_selftest_ok(self, m, *mocks): - configuration = ObjectsAuthStep() - configuration.configure() - m.get("http://testserver/objecttypes", json=[]) - - configuration.test_configuration() - - self.assertEqual(m.last_request.url, "http://testserver/objecttypes") - self.assertEqual(m.last_request.method, "GET") - - @requests_mock.Mocker() - @patch( - "objecttypes.config.objects.build_absolute_url", - return_value="http://testserver/objecttypes", - ) - def test_selftest_fail(self, m, *mocks): - configuration = ObjectsAuthStep() - configuration.configure() - - mock_kwargs = ( - {"exc": requests.ConnectTimeout}, - {"exc": requests.ConnectionError}, - {"status_code": 404}, - {"status_code": 403}, - {"status_code": 500}, - ) - for mock_config in mock_kwargs: - with self.subTest(mock=mock_config): - m.get("http://testserver/objecttypes", **mock_config) - - with self.assertRaises(SelfTestFailed): - configuration.test_configuration() - - def test_is_configured(self): - configuration = ObjectsAuthStep() - self.assertFalse(configuration.is_configured()) - - configuration.configure() - - self.assertTrue(configuration.is_configured()) diff --git a/src/objecttypes/tests/config/test_site_configuration.py b/src/objecttypes/tests/config/test_site_configuration.py deleted file mode 100644 index 4dccee19..00000000 --- a/src/objecttypes/tests/config/test_site_configuration.py +++ /dev/null @@ -1,66 +0,0 @@ -from django.contrib.sites.models import Site -from django.test import TestCase, override_settings - -import requests -import requests_mock -from django_setup_configuration.exceptions import SelfTestFailed - -from objecttypes.config.site import SiteConfigurationStep - - -@override_settings( - OBJECTTYPES_DOMAIN="localhost:8000", - OBJECTTYPES_ORGANIZATION="ACME", -) -class SiteConfigurationTests(TestCase): - def setUp(self): - super().setUp() - - self.addCleanup(Site.objects.clear_cache) - - def test_set_domain(self): - configuration = SiteConfigurationStep() - configuration.configure() - - site = Site.objects.get_current() - self.assertEqual(site.domain, "localhost:8000") - self.assertEqual(site.name, "Objecttypes ACME") - - @requests_mock.Mocker() - def test_configuration_check_ok(self, m): - m.get("http://localhost:8000/", status_code=200) - configuration = SiteConfigurationStep() - configuration.configure() - - configuration.test_configuration() - - self.assertEqual(m.last_request.url, "http://localhost:8000/") - self.assertEqual(m.last_request.method, "GET") - - @requests_mock.Mocker() - def test_configuration_check_failures(self, m): - configuration = SiteConfigurationStep() - configuration.configure() - - mock_kwargs = ( - {"exc": requests.ConnectTimeout}, - {"exc": requests.ConnectionError}, - {"status_code": 404}, - {"status_code": 403}, - {"status_code": 500}, - ) - for mock_config in mock_kwargs: - with self.subTest(mock=mock_config): - m.get("http://localhost:8000/", **mock_config) - - with self.assertRaises(SelfTestFailed): - configuration.test_configuration() - - def test_is_configured(self): - configuration = SiteConfigurationStep() - - self.assertFalse(configuration.is_configured()) - - configuration.configure() - - self.assertTrue(configuration.is_configured())