forked from openwisp/openwisp-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] Added configurable setting for API host
Added "OPENWISP_CONTROLLER_API_HOST" for configuring different host for API endpoints.
- Loading branch information
Showing
11 changed files
with
155 additions
and
23 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,51 @@ | ||
from django.conf import settings | ||
from django.core import checks | ||
|
||
from . import settings as app_settings | ||
|
||
|
||
def check_cors_configuration(app_configs, **kwargs): | ||
errors = [] | ||
if not app_settings.OPENWISP_CONTROLLER_API_HOST: | ||
return errors | ||
|
||
if not ( | ||
'corsheaders' in settings.INSTALLED_APPS | ||
and 'corsheaders.middleware.CorsMiddleware' in settings.MIDDLEWARE | ||
): | ||
errors.append( | ||
checks.Warning( | ||
msg='Improperly Configured', | ||
hint=( | ||
'"django-cors-headers" is either not installed or improperly ' | ||
'configured. CORS configuration is required for using ' | ||
'"OPENWISP_CONTROLLER_API_HOST" settings. ' | ||
' Configure equivalent CORS rules on your server ' | ||
'if you are not using "django-cors-headers".' | ||
), | ||
obj='Settings', | ||
) | ||
) | ||
return errors | ||
|
||
|
||
def check_openwisp_controller_ctx_processor(app_config, **kwargs): | ||
errors = [] | ||
ctx_processor = 'openwisp_controller.context_processors.controller_api_settings' | ||
|
||
if not app_settings.OPENWISP_CONTROLLER_API_HOST: | ||
return errors | ||
|
||
if not (ctx_processor in settings.TEMPLATES[0]['OPTIONS']['context_processors']): | ||
errors.append( | ||
checks.Warning( | ||
msg='Improperly Configured', | ||
hint=( | ||
f'"{ctx_processor} is absent from context processors.' | ||
'It is required to be added in TEMPLATES["context_processor"] ' | ||
'for "OPENWISP_CONTROLLER_API_HOST" to work properly.' | ||
), | ||
obj='Settings', | ||
) | ||
) | ||
return errors |
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
13 changes: 0 additions & 13 deletions
13
openwisp_controller/connection/templates/admin/change_form.html
This file was deleted.
Oops, something went wrong.
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,7 @@ | ||
from . import settings as app_settings | ||
|
||
|
||
def controller_api_settings(request): | ||
return { | ||
'OPENWISP_CONTROLLER_API_HOST': app_settings.OPENWISP_CONTROLLER_API_HOST, | ||
} |
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,3 @@ | ||
from django.conf import settings | ||
|
||
OPENWISP_CONTROLLER_API_HOST = getattr(settings, 'OPENWISP_CONTROLLER_API_HOST', None) |
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,14 @@ | ||
from django.conf import settings | ||
|
||
from openwisp_utils.utils import deepcopy | ||
|
||
|
||
def _get_updated_templates_settings(context_processors=[]): | ||
template_settings = deepcopy(settings.TEMPLATES[0]) | ||
if len(context_processors): | ||
template_settings['OPTIONS']['context_processors'].extend(context_processors) | ||
else: | ||
template_settings['OPTIONS']['context_processors'].append( | ||
'openwisp_controller.context_processors.controller_api_settings' | ||
) | ||
return [template_settings] |
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,47 @@ | ||
from unittest.mock import patch | ||
|
||
from django.test import TestCase, override_settings | ||
|
||
from .. import checks, context_processors | ||
from .. import settings as app_settings | ||
from . import _get_updated_templates_settings | ||
|
||
|
||
class TestUtilities(TestCase): | ||
@patch.object( | ||
app_settings, 'OPENWISP_CONTROLLER_API_HOST', 'https://example.com', | ||
) | ||
def test_cors_not_configured_check(self): | ||
error_message = '"django-cors-headers" is either not installed or improperly' | ||
|
||
def run_check(): | ||
return checks.check_cors_configuration(None).pop() | ||
|
||
with self.subTest('Test "django-cors-headers" absent in INSTALLED_APPS'): | ||
error = run_check() | ||
self.assertIn(error_message, error.hint) | ||
|
||
with self.subTest('Test "django-cors-headers" middleware not configured'): | ||
error = run_check() | ||
self.assertIn(error_message, error.hint) | ||
|
||
@patch.object( | ||
app_settings, 'OPENWISP_CONTROLLER_API_HOST', 'https://example.com', | ||
) | ||
def test_openwisp_controller_context_processor_check(self): | ||
def runcheck(): | ||
return checks.check_openwisp_controller_ctx_processor(None).pop() | ||
|
||
error_message = 'absent from context processor' | ||
error = runcheck() | ||
self.assertIn(error_message, error.hint) | ||
|
||
@patch.object( | ||
app_settings, 'OPENWISP_CONTROLLER_API_HOST', 'https://example.com', | ||
) | ||
def test_openwisp_controller_context_processor(self): | ||
with override_settings(TEMPLATES=_get_updated_templates_settings()): | ||
context = { | ||
'OPENWISP_CONTROLLER_API_HOST': 'https://example.com', | ||
} | ||
self.assertEqual(context_processors.controller_api_settings(None), context) |
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