-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✅ [maykinmedia/django-setup-configuration#1] test configuration steps
- Loading branch information
1 parent
b2e586e
commit a4de147
Showing
6 changed files
with
338 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
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 zgw_consumers.models import Service | ||
|
||
from objects.config.demo import DemoUserStep | ||
from objects.config.objecttypes import ObjecttypesStep | ||
from objects.config.site import SiteConfigurationStep | ||
|
||
from ..utils import mock_service_oas_get | ||
|
||
|
||
@override_settings( | ||
OBJECTS_DOMAIN="objects.example.com", | ||
OBJECTS_ORGANIZATION="ACME", | ||
OBJECTTYPES_API_ROOT="https://objecttypes.example.com/api/v2/", | ||
OBJECTTYPES_API_OAS="https://objecttypes.example.com/api/v2/schema/openapi.yaml", | ||
OBJECTS_OBJECTTYPES_TOKEN="some-random-string", | ||
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://objects.example.com/", status_code=200) | ||
m.get("http://objects.example.com/api/v2/objects", json=[]) | ||
mock_service_oas_get( | ||
m, "https://objecttypes.example.com/api/v2/", "objecttypes" | ||
) | ||
m.get("https://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"{ObjecttypesStep()}, {DemoUserStep()}]", | ||
f"Configuring {SiteConfigurationStep()}...", | ||
f"{SiteConfigurationStep()} is successfully configured", | ||
f"Configuring {ObjecttypesStep()}...", | ||
f"{ObjecttypesStep()} 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, "objects.example.com") | ||
self.assertEqual(site.name, "Objects ACME") | ||
|
||
with self.subTest("Objects can query Objecttypes API"): | ||
client = Service.get_client("https://objecttypes.example.com/api/v2/") | ||
self.assertIsNotNone(client) | ||
|
||
client.list("objecttype") | ||
|
||
list_call = m.last_request | ||
self.assertEqual( | ||
list_call.url, "https://objecttypes.example.com/api/v2/objecttypes" | ||
) | ||
self.assertIn("Authorization", list_call.headers) | ||
self.assertEqual( | ||
list_call.headers["Authorization"], "Token some-random-string" | ||
) | ||
|
||
with self.subTest("Demo user configured correctly"): | ||
response = self.client.get( | ||
reverse("v2:object-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://objects.example.com/", status_code=500) | ||
m.get("http://objects.example.com/api/v2/objects", status_code=200) | ||
mock_service_oas_get( | ||
m, "https://objecttypes.example.com/api/v2/", "objecttypes" | ||
) | ||
m.get("https://objecttypes.example.com/api/v2/objecttypes", json={}) | ||
|
||
with self.assertRaisesMessage( | ||
CommandError, | ||
"Configuration test failed with errors: " | ||
"Site Configuration: Could not access home page at 'http://objects.example.com/'", | ||
): | ||
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) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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 objects.config.demo import DemoUserStep | ||
from objects.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.assertTrue(token_auth.is_superuser) | ||
self.assertEqual(token_auth.contact_person, "Demo") | ||
self.assertEqual(token_auth.email, "demo@demo.local") | ||
|
||
@requests_mock.Mocker() | ||
@patch( | ||
"objects.config.demo.build_absolute_url", | ||
return_value="http://testserver/objects", | ||
) | ||
def test_configuration_check_ok(self, m, *mocks): | ||
configuration = DemoUserStep() | ||
configuration.configure() | ||
m.get("http://testserver/objects", json=[]) | ||
|
||
configuration.test_configuration() | ||
|
||
self.assertEqual(m.last_request.url, "http://testserver/objects") | ||
self.assertEqual(m.last_request.method, "GET") | ||
|
||
@requests_mock.Mocker() | ||
@patch( | ||
"objects.config.demo.build_absolute_url", | ||
return_value="http://testserver/objects", | ||
) | ||
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/objects", **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()) |
82 changes: 82 additions & 0 deletions
82
src/objects/tests/config/test_objecttypes_configuration.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
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 zgw_consumers.constants import AuthTypes | ||
from zgw_consumers.models import Service | ||
|
||
from objects.config.objecttypes import ObjecttypesStep | ||
|
||
from ..utils import mock_service_oas_get | ||
|
||
|
||
@override_settings( | ||
OBJECTTYPES_API_ROOT="https://objecttypes.example.com/api/v2/", | ||
OBJECTTYPES_API_OAS="https://objecttypes.example.com/api/v2/schema/openapi.yaml", | ||
OBJECTS_OBJECTTYPES_TOKEN="some-random-string", | ||
) | ||
class ObjecttypesConfigurationTests(TestCase): | ||
def test_configure(self): | ||
configuration = ObjecttypesStep() | ||
|
||
configuration.configure() | ||
|
||
service = Service.objects.get( | ||
api_root="https://objecttypes.example.com/api/v2/" | ||
) | ||
self.assertEqual( | ||
service.oas, "https://objecttypes.example.com/api/v2/schema/openapi.yaml" | ||
) | ||
self.assertEqual(service.auth_type, AuthTypes.api_key) | ||
self.assertEqual(service.header_key, "Authorization") | ||
self.assertEqual(service.header_value, "Token some-random-string") | ||
|
||
@requests_mock.Mocker() | ||
def test_selftest_ok(self, m): | ||
configuration = ObjecttypesStep() | ||
configuration.configure() | ||
mock_service_oas_get( | ||
m, "https://objecttypes.example.com/api/v2/", "objecttypes" | ||
) | ||
m.get("https://objecttypes.example.com/api/v2/objecttypes", json={}) | ||
|
||
configuration.test_configuration() | ||
|
||
self.assertEqual( | ||
m.last_request.url, "https://objecttypes.example.com/api/v2/objecttypes" | ||
) | ||
|
||
@requests_mock.Mocker() | ||
def test_selftest_fail(self, m): | ||
configuration = ObjecttypesStep() | ||
configuration.configure() | ||
mock_service_oas_get( | ||
m, "https://objecttypes.example.com/api/v2/", "objecttypes" | ||
) | ||
|
||
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( | ||
"https://objecttypes.example.com/api/v2/objecttypes", **mock_config | ||
) | ||
|
||
with self.assertRaises(SelfTestFailed): | ||
configuration.test_configuration() | ||
|
||
def test_is_configured(self): | ||
configuration = ObjecttypesStep() | ||
self.assertFalse(configuration.is_configured()) | ||
|
||
configuration.configure() | ||
|
||
self.assertTrue(configuration.is_configured()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
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 objects.config.site import SiteConfigurationStep | ||
|
||
|
||
@override_settings( | ||
OBJECTS_DOMAIN="localhost:8000", | ||
OBJECTS_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, "Objects 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()) |