-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from percipient/override-settings
Allow settings to be overridden.
- Loading branch information
Showing
8 changed files
with
110 additions
and
24 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 |
---|---|---|
@@ -1,20 +1,26 @@ | ||
from django.conf import settings | ||
|
||
DEFAULT_ENDPOINT_URL = 'https://slack.com/api/chat.postMessage' | ||
class AppSettings(object): | ||
# Lazily obtain settings to support ``override_settings`` decorator | ||
def get(suffix, default): | ||
def setting(self): | ||
return getattr(settings, 'SLACK_%s' % suffix, default) | ||
return setting | ||
|
||
def setting(suffix, default): | ||
return getattr(settings, 'SLACK_%s' % suffix, default) | ||
TOKEN = property(get('TOKEN', None)) | ||
CHANNEL = property(get('CHANNEL', '#general')) | ||
USERNAME = property(get('USERNAME', 'bot')) | ||
ICON_URL = property(get('ICON_URL', None)) | ||
ICON_EMOJI = property(get('ICON_EMOJI', None)) | ||
|
||
TOKEN = setting('TOKEN', None) | ||
CHANNEL = setting('CHANNEL', '#general') | ||
USERNAME = setting('USERNAME', 'bot') | ||
ICON_URL = setting('ICON_URL', None) | ||
ICON_EMOJI = setting('ICON_EMOJI', None) | ||
ENDPOINT_URL = setting('ENDPOINT_URL', DEFAULT_ENDPOINT_URL) | ||
DEFAULT_ENDPOINT_URL = 'https://slack.com/api/chat.postMessage' | ||
ENDPOINT_URL = property(get('ENDPOINT_URL', DEFAULT_ENDPOINT_URL)) | ||
|
||
_BACKEND = 'django_slack.backends.DisabledBackend' if settings.DEBUG else \ | ||
'django_slack.backends.UrllibBackend' | ||
BACKEND = setting('BACKEND', _BACKEND) | ||
BACKEND_FOR_QUEUE = setting('BACKEND_FOR_QUEUE', _BACKEND) | ||
_DEFAULT_BACKEND = 'django_slack.backends.DisabledBackend' if settings.DEBUG else \ | ||
'django_slack.backends.UrllibBackend' | ||
BACKEND = property(get('BACKEND', _DEFAULT_BACKEND)) | ||
BACKEND_FOR_QUEUE = property(get('BACKEND_FOR_QUEUE', _DEFAULT_BACKEND)) | ||
|
||
FAIL_SILENTLY = setting('FAIL_SILENTLY', False) | ||
FAIL_SILENTLY = property(get('FAIL_SILENTLY', False)) | ||
|
||
app_settings = AppSettings() |
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
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
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,47 @@ | ||
from django.conf import settings | ||
from django.test import TestCase, override_settings | ||
|
||
from django_slack import slack_message | ||
from django_slack.utils import get_backend | ||
from django_slack.app_settings import app_settings | ||
|
||
from tests.backends import RaisingBackend | ||
|
||
class TestOverride(TestCase): | ||
def setUp(self): | ||
# Fake the backend not having been initialized yet. | ||
get_backend.backend = None | ||
|
||
def test_override(self): | ||
""" | ||
Ensure that accessing a setting in an override works. | ||
""" | ||
DISABLED_BACKEND = 'django_slack.backends.DisabledBackend' | ||
with override_settings(SLACK_BACKEND=DISABLED_BACKEND): | ||
self.assertEqual(settings.SLACK_BACKEND, DISABLED_BACKEND) | ||
self.assertEqual(app_settings.BACKEND, DISABLED_BACKEND) | ||
|
||
def test_backend_cache(self): | ||
""" | ||
Ensure that the backend is cached once it is called once. | ||
""" | ||
self.assertEqual(id(get_backend()), id(get_backend())) | ||
|
||
def test_backend_override(self): | ||
""" | ||
Ensure the backend can be overridden. | ||
""" | ||
with override_settings(SLACK_BACKEND='tests.backends.RaisingBackend'): | ||
with self.assertRaises(RaisingBackend.RaisedException): | ||
slack_message('test.slack', {'text': 'test'}) | ||
|
||
def test_fail_silently(self): | ||
""" | ||
Ensure fail silently can be overridden. | ||
""" | ||
# Note that this will fail if test_backend_override ever fails. | ||
with override_settings( | ||
SLACK_FAIL_SILENTLY=True, | ||
SLACK_BACKEND='tests.backends.RaisingBackend', | ||
): | ||
slack_message('test.slack', {'text': 'test'}) |