Skip to content

Commit 8afe8e7

Browse files
committed
✅ [maykinmedia/django-setup-configuration#1] test configuration steps
1 parent 571706e commit 8afe8e7

6 files changed

+338
-0
lines changed

src/objects/tests/commands/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
from io import StringIO
2+
3+
from django.contrib.sites.models import Site
4+
from django.core.management import CommandError, call_command
5+
from django.test import TestCase, override_settings
6+
from django.urls import reverse
7+
8+
import requests_mock
9+
from rest_framework import status
10+
from zgw_consumers.models import Service
11+
12+
from objects.config.demo import DemoUserStep
13+
from objects.config.objecttypes import ObjecttypesStep
14+
from objects.config.site import SiteConfigurationStep
15+
16+
from ..utils import mock_service_oas_get
17+
18+
19+
@override_settings(
20+
OBJECTS_DOMAIN="objects.example.com",
21+
OBJECTS_ORGANIZATION="ACME",
22+
OBJECTTYPES_API_ROOT="https://objecttypes.example.com/api/v2/",
23+
OBJECTTYPES_API_OAS="https://objecttypes.example.com/api/v2/schema/openapi.yaml",
24+
OBJECTS_OBJECTTYPES_TOKEN="some-random-string",
25+
DEMO_CONFIG_ENABLE=True,
26+
DEMO_TOKEN="demo-random-string",
27+
DEMO_PERSON="Demo",
28+
DEMO_EMAIL="[email protected]",
29+
)
30+
class SetupConfigurationTests(TestCase):
31+
def setUp(self):
32+
super().setUp()
33+
34+
self.addCleanup(Site.objects.clear_cache)
35+
36+
@requests_mock.Mocker()
37+
def test_setup_configuration(self, m):
38+
stdout = StringIO()
39+
# mocks
40+
m.get("http://objects.example.com/", status_code=200)
41+
m.get("http://objects.example.com/api/v2/objects", json=[])
42+
mock_service_oas_get(
43+
m, "https://objecttypes.example.com/api/v2/", "objecttypes"
44+
)
45+
m.get("https://objecttypes.example.com/api/v2/objecttypes", json={})
46+
47+
call_command("setup_configuration", stdout=stdout)
48+
49+
with self.subTest("Command output"):
50+
command_output = stdout.getvalue().splitlines()
51+
expected_output = [
52+
f"Configuration will be set up with following steps: [{SiteConfigurationStep()}, "
53+
f"{ObjecttypesStep()}, {DemoUserStep()}]",
54+
f"Configuring {SiteConfigurationStep()}...",
55+
f"{SiteConfigurationStep()} is successfully configured",
56+
f"Configuring {ObjecttypesStep()}...",
57+
f"{ObjecttypesStep()} is successfully configured",
58+
f"Configuring {DemoUserStep()}...",
59+
f"{DemoUserStep()} is successfully configured",
60+
"Instance configuration completed.",
61+
]
62+
63+
self.assertEqual(command_output, expected_output)
64+
65+
with self.subTest("Site configured correctly"):
66+
site = Site.objects.get_current()
67+
self.assertEqual(site.domain, "objects.example.com")
68+
self.assertEqual(site.name, "Objects ACME")
69+
70+
with self.subTest("Objects can query Objecttypes API"):
71+
client = Service.get_client("https://objecttypes.example.com/api/v2/")
72+
self.assertIsNotNone(client)
73+
74+
client.list("objecttype")
75+
76+
list_call = m.last_request
77+
self.assertEqual(
78+
list_call.url, "https://objecttypes.example.com/api/v2/objecttypes"
79+
)
80+
self.assertIn("Authorization", list_call.headers)
81+
self.assertEqual(
82+
list_call.headers["Authorization"], "Token some-random-string"
83+
)
84+
85+
with self.subTest("Demo user configured correctly"):
86+
response = self.client.get(
87+
reverse("v2:object-list"),
88+
HTTP_AUTHORIZATION="Token demo-random-string",
89+
)
90+
91+
self.assertEqual(response.status_code, status.HTTP_200_OK)
92+
93+
@requests_mock.Mocker()
94+
def test_setup_configuration_selftest_fails(self, m):
95+
m.get("http://objects.example.com/", status_code=500)
96+
m.get("http://objects.example.com/api/v2/objects", status_code=200)
97+
mock_service_oas_get(
98+
m, "https://objecttypes.example.com/api/v2/", "objecttypes"
99+
)
100+
m.get("https://objecttypes.example.com/api/v2/objecttypes", json={})
101+
102+
with self.assertRaisesMessage(
103+
CommandError,
104+
"Configuration test failed with errors: "
105+
"Site Configuration: Could not access home page at 'http://objects.example.com/'",
106+
):
107+
call_command("setup_configuration")
108+
109+
@requests_mock.Mocker()
110+
def test_setup_configuration_without_selftest(self, m):
111+
stdout = StringIO()
112+
113+
call_command("setup_configuration", no_selftest=True, stdout=stdout)
114+
command_output = stdout.getvalue()
115+
116+
self.assertEqual(len(m.request_history), 0)
117+
self.assertTrue("Selftest is skipped" in command_output)

src/objects/tests/config/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from unittest.mock import patch
2+
3+
from django.test import TestCase, override_settings
4+
5+
import requests
6+
import requests_mock
7+
from django_setup_configuration.exceptions import SelfTestFailed
8+
9+
from objects.config.demo import DemoUserStep
10+
from objects.token.models import TokenAuth
11+
12+
13+
@override_settings(
14+
DEMO_TOKEN="demo-random-string", DEMO_PERSON="Demo", DEMO_EMAIL="[email protected]"
15+
)
16+
class DemoConfigurationTests(TestCase):
17+
def test_configure(self):
18+
configuration = DemoUserStep()
19+
20+
configuration.configure()
21+
22+
token_auth = TokenAuth.objects.get()
23+
self.assertEqual(token_auth.token, "demo-random-string")
24+
self.assertTrue(token_auth.is_superuser)
25+
self.assertEqual(token_auth.contact_person, "Demo")
26+
self.assertEqual(token_auth.email, "[email protected]")
27+
28+
@requests_mock.Mocker()
29+
@patch(
30+
"objects.config.demo.build_absolute_url",
31+
return_value="http://testserver/objects",
32+
)
33+
def test_configuration_check_ok(self, m, *mocks):
34+
configuration = DemoUserStep()
35+
configuration.configure()
36+
m.get("http://testserver/objects", json=[])
37+
38+
configuration.test_configuration()
39+
40+
self.assertEqual(m.last_request.url, "http://testserver/objects")
41+
self.assertEqual(m.last_request.method, "GET")
42+
43+
@requests_mock.Mocker()
44+
@patch(
45+
"objects.config.demo.build_absolute_url",
46+
return_value="http://testserver/objects",
47+
)
48+
def test_configuration_check_failures(self, m, *mocks):
49+
configuration = DemoUserStep()
50+
configuration.configure()
51+
52+
mock_kwargs = (
53+
{"exc": requests.ConnectTimeout},
54+
{"exc": requests.ConnectionError},
55+
{"status_code": 404},
56+
{"status_code": 403},
57+
{"status_code": 500},
58+
)
59+
for mock_config in mock_kwargs:
60+
with self.subTest(mock=mock_config):
61+
m.get("http://testserver/objects", **mock_config)
62+
63+
with self.assertRaises(SelfTestFailed):
64+
configuration.test_configuration()
65+
66+
def test_is_configured(self):
67+
configuration = DemoUserStep()
68+
69+
self.assertFalse(configuration.is_configured())
70+
71+
configuration.configure()
72+
73+
self.assertTrue(configuration.is_configured())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
from unittest.mock import patch
2+
3+
from django.test import TestCase, override_settings
4+
5+
import requests
6+
import requests_mock
7+
from django_setup_configuration.exceptions import SelfTestFailed
8+
from zgw_consumers.constants import AuthTypes
9+
from zgw_consumers.models import Service
10+
11+
from objects.config.objecttypes import ObjecttypesStep
12+
13+
from ..utils import mock_service_oas_get
14+
15+
16+
@override_settings(
17+
OBJECTTYPES_API_ROOT="https://objecttypes.example.com/api/v2/",
18+
OBJECTTYPES_API_OAS="https://objecttypes.example.com/api/v2/schema/openapi.yaml",
19+
OBJECTS_OBJECTTYPES_TOKEN="some-random-string",
20+
)
21+
class ObjecttypesConfigurationTests(TestCase):
22+
def test_configure(self):
23+
configuration = ObjecttypesStep()
24+
25+
configuration.configure()
26+
27+
service = Service.objects.get(
28+
api_root="https://objecttypes.example.com/api/v2/"
29+
)
30+
self.assertEqual(
31+
service.oas, "https://objecttypes.example.com/api/v2/schema/openapi.yaml"
32+
)
33+
self.assertEqual(service.auth_type, AuthTypes.api_key)
34+
self.assertEqual(service.header_key, "Authorization")
35+
self.assertEqual(service.header_value, "Token some-random-string")
36+
37+
@requests_mock.Mocker()
38+
def test_selftest_ok(self, m):
39+
configuration = ObjecttypesStep()
40+
configuration.configure()
41+
mock_service_oas_get(
42+
m, "https://objecttypes.example.com/api/v2/", "objecttypes"
43+
)
44+
m.get("https://objecttypes.example.com/api/v2/objecttypes", json={})
45+
46+
configuration.test_configuration()
47+
48+
self.assertEqual(
49+
m.last_request.url, "https://objecttypes.example.com/api/v2/objecttypes"
50+
)
51+
52+
@requests_mock.Mocker()
53+
def test_selftest_fail(self, m):
54+
configuration = ObjecttypesStep()
55+
configuration.configure()
56+
mock_service_oas_get(
57+
m, "https://objecttypes.example.com/api/v2/", "objecttypes"
58+
)
59+
60+
mock_kwargs = (
61+
{"exc": requests.ConnectTimeout},
62+
{"exc": requests.ConnectionError},
63+
{"status_code": 404},
64+
{"status_code": 403},
65+
{"status_code": 500},
66+
)
67+
for mock_config in mock_kwargs:
68+
with self.subTest(mock=mock_config):
69+
m.get(
70+
"https://objecttypes.example.com/api/v2/objecttypes", **mock_config
71+
)
72+
73+
with self.assertRaises(SelfTestFailed):
74+
configuration.test_configuration()
75+
76+
def test_is_configured(self):
77+
configuration = ObjecttypesStep()
78+
self.assertFalse(configuration.is_configured())
79+
80+
configuration.configure()
81+
82+
self.assertTrue(configuration.is_configured())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
from django.contrib.sites.models import Site
2+
from django.test import TestCase, override_settings
3+
4+
import requests
5+
import requests_mock
6+
from django_setup_configuration.exceptions import SelfTestFailed
7+
8+
from objects.config.site import SiteConfigurationStep
9+
10+
11+
@override_settings(
12+
OBJECTS_DOMAIN="localhost:8000",
13+
OBJECTS_ORGANIZATION="ACME",
14+
)
15+
class SiteConfigurationTests(TestCase):
16+
def setUp(self):
17+
super().setUp()
18+
19+
self.addCleanup(Site.objects.clear_cache)
20+
21+
def test_set_domain(self):
22+
configuration = SiteConfigurationStep()
23+
configuration.configure()
24+
25+
site = Site.objects.get_current()
26+
self.assertEqual(site.domain, "localhost:8000")
27+
self.assertEqual(site.name, "Objects ACME")
28+
29+
@requests_mock.Mocker()
30+
def test_configuration_check_ok(self, m):
31+
m.get("http://localhost:8000/", status_code=200)
32+
configuration = SiteConfigurationStep()
33+
configuration.configure()
34+
35+
configuration.test_configuration()
36+
37+
self.assertEqual(m.last_request.url, "http://localhost:8000/")
38+
self.assertEqual(m.last_request.method, "GET")
39+
40+
@requests_mock.Mocker()
41+
def test_configuration_check_failures(self, m):
42+
configuration = SiteConfigurationStep()
43+
configuration.configure()
44+
45+
mock_kwargs = (
46+
{"exc": requests.ConnectTimeout},
47+
{"exc": requests.ConnectionError},
48+
{"status_code": 404},
49+
{"status_code": 403},
50+
{"status_code": 500},
51+
)
52+
for mock_config in mock_kwargs:
53+
with self.subTest(mock=mock_config):
54+
m.get("http://localhost:8000/", **mock_config)
55+
56+
with self.assertRaises(SelfTestFailed):
57+
configuration.test_configuration()
58+
59+
def test_is_configured(self):
60+
configuration = SiteConfigurationStep()
61+
62+
self.assertFalse(configuration.is_configured())
63+
64+
configuration.configure()
65+
66+
self.assertTrue(configuration.is_configured())

0 commit comments

Comments
 (0)