-
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
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="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()) |