-
Notifications
You must be signed in to change notification settings - Fork 6
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] add demo user configurat…
…ion step
- Loading branch information
1 parent
2b479c6
commit 0257da3
Showing
4 changed files
with
154 additions
and
2 deletions.
There are no files selected for viewing
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
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,60 @@ | ||
from django.conf import settings | ||
from django.urls import reverse | ||
|
||
import requests | ||
from django_setup_configuration.configuration import BaseConfigurationStep | ||
from django_setup_configuration.exceptions import SelfTestFailed | ||
|
||
from objecttypes.token.models import TokenAuth | ||
from objecttypes.utils import build_absolute_url | ||
|
||
|
||
class DemoUserStep(BaseConfigurationStep): | ||
""" | ||
Create demo user to request Objectypes API | ||
""" | ||
|
||
verbose_name = "Demo User Configuration" | ||
required_settings = [ | ||
"DEMO_TOKEN", | ||
"DEMO_PERSON", | ||
"DEMO_EMAIL", | ||
] | ||
enable_setting = "DEMO_CONFIG_ENABLE" | ||
|
||
def is_configured(self) -> bool: | ||
return TokenAuth.objects.filter(token=settings.DEMO_TOKEN).exists() | ||
|
||
def configure(self): | ||
token_auth, created = TokenAuth.objects.get_or_create( | ||
token=settings.DEMO_TOKEN, | ||
defaults={ | ||
"contact_person": settings.DEMO_PERSON, | ||
"email": settings.DEMO_EMAIL, | ||
}, | ||
) | ||
if ( | ||
token_auth.contact_person != settings.DEMO_PERSON | ||
or token_auth.email != settings.DEMO_EMAIL | ||
): | ||
token_auth.contact_person = settings.DEMO_PERSON | ||
token_auth.email = settings.DEMO_EMAIL | ||
token_auth.save(update_fields=["contact_person", "email"]) | ||
|
||
def test_configuration(self): | ||
endpoint = reverse("v2:objecttype-list") | ||
full_url = build_absolute_url(endpoint, request=None) | ||
|
||
try: | ||
response = requests.get( | ||
full_url, | ||
headers={ | ||
"Authorization": f"Token {settings.DEMO_TOKEN}", | ||
"Accept": "application/json", | ||
}, | ||
) | ||
response.raise_for_status() | ||
except requests.RequestException as exc: | ||
raise SelfTestFailed( | ||
"Could not list objecttypes for the configured token" | ||
) from exc |
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 |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import uuid | ||
from io import StringIO | ||
|
||
from django.contrib.sites.models import Site | ||
|
@@ -9,6 +8,7 @@ | |
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 | ||
|
||
|
@@ -19,6 +19,10 @@ | |
OBJECTS_OBJECTTYPES_TOKEN="some-random-string", | ||
OBJECTS_OBJECTTYPES_PERSON="Some Person", | ||
OBJECTS_OBJECTTYPES_EMAIL="[email protected]", | ||
DEMO_CONFIG_ENABLE=True, | ||
DEMO_TOKEN="demo-random-string", | ||
DEMO_PERSON="Demo", | ||
DEMO_EMAIL="[email protected]", | ||
) | ||
class SetupConfigurationTests(TestCase): | ||
def setUp(self): | ||
|
@@ -39,11 +43,13 @@ def test_setup_configuration(self, m): | |
command_output = stdout.getvalue().splitlines() | ||
expected_output = [ | ||
f"Configuration will be set up with following steps: [{SiteConfigurationStep()}, " | ||
f"{ObjectsAuthStep()}]", | ||
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.", | ||
] | ||
|
||
|
@@ -62,6 +68,14 @@ def test_setup_configuration(self, m): | |
|
||
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) | ||
|
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,72 @@ | ||
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="[email protected]" | ||
) | ||
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, "[email protected]") | ||
|
||
@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()) |