-
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 branch 'percipient-escapeslack'
* percipient-escapeslack: Misc cosmetic tidying. Remove unused import. Null out an __init__ file. Expand documentation. Automatically include the template tag. Add a templatetag which escapes only the characters Slack wants to escape.
- Loading branch information
Showing
11 changed files
with
171 additions
and
3 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
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,27 @@ | ||
from django import template | ||
from django.utils import six | ||
from django.utils.encoding import force_text | ||
from django.utils.functional import allow_lazy | ||
from django.utils.safestring import SafeText, mark_safe | ||
from django.template.defaultfilters import stringfilter | ||
|
||
register = template.Library() | ||
|
||
_slack_escapes = { | ||
ord('&'): u'&', | ||
ord('<'): u'<', | ||
ord('>'): u'>', | ||
} | ||
|
||
@register.filter(is_safe=True) | ||
@stringfilter | ||
def escapeslack(value): | ||
""" | ||
Returns the given text with ampersands and angle brackets encoded for use in | ||
the Slack API, per the Slack API documentation: | ||
<https://api.slack.com/docs/formatting#how_to_escape_characters> | ||
This is based on django.template.defaultfilters.escapejs. | ||
""" | ||
return mark_safe(force_text(value).translate(_slack_escapes)) | ||
escapeslack = allow_lazy(escapeslack, six.text_type, SafeText) |
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,8 @@ | ||
#!/usr/bin/env python | ||
import os | ||
os.environ['DJANGO_SETTINGS_MODULE'] = 'tests.settings' | ||
|
||
from django.core import management | ||
|
||
if __name__ == "__main__": | ||
management.execute_from_command_line() |
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,21 @@ | ||
from django_slack.utils import Backend | ||
|
||
class StorageBackend(Backend): | ||
""" | ||
A backend that stores all messages sent. | ||
""" | ||
|
||
def __init__(self): | ||
self.reset() | ||
|
||
def reset(self): | ||
""" | ||
Clear any messages. | ||
""" | ||
self.messages = [] | ||
|
||
def send(self, url, data): | ||
self.messages.append({ | ||
'url': url, | ||
'data': data, | ||
}) |
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,25 @@ | ||
SECRET_KEY = 'not_empty' | ||
|
||
TEMPLATES = [ | ||
{ | ||
'BACKEND': 'django.template.backends.django.DjangoTemplates', | ||
'DIRS': [], | ||
'APP_DIRS': True, | ||
'OPTIONS': { | ||
'context_processors': [ | ||
'django.template.context_processors.debug', | ||
'django.template.context_processors.request', | ||
'django.contrib.auth.context_processors.auth', | ||
'django.contrib.messages.context_processors.messages' | ||
], | ||
}, | ||
}, | ||
] | ||
|
||
INSTALLED_APPS = ( | ||
'tests', | ||
'django_slack', | ||
) | ||
|
||
SLACK_BACKEND = 'tests.backends.StorageBackend' | ||
SLACK_TOKEN = 'fake-token' |
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,5 @@ | ||
{% extends django_slack %} | ||
|
||
{% block text %} | ||
{{ text|escapeslack }} | ||
{% endblock %} |
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,5 @@ | ||
{% extends django_slack %} | ||
|
||
{% block text %} | ||
{{ text }} | ||
{% endblock %} |
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,62 @@ | ||
import unittest | ||
|
||
from django_slack import slack_message | ||
from django_slack.api import backend | ||
|
||
class SlackTestCase(unittest.TestCase): | ||
def setUp(self): | ||
backend.reset() | ||
|
||
def assertMessageCount(self, count): | ||
self.assertEqual(len(backend.messages), count) | ||
|
||
def assertMessage(self, url=None, **kwargs): | ||
""" | ||
Ensure there was only one message sent with a URL and data values. | ||
""" | ||
|
||
self.assertMessageCount(1) | ||
message = backend.messages[0] | ||
|
||
# Optionally ensure the URL. | ||
if url is not None: | ||
self.assertEqual(url, message['url']) | ||
|
||
# Ensure each input value in data. | ||
for kwarg, value in kwargs.items(): | ||
self.assertEqual(value, message['data'][kwarg]) | ||
|
||
class TestEscaping(SlackTestCase): | ||
def test_simple_message(self): | ||
slack_message('test.slack', {'text': 'test'}) | ||
self.assertMessage(text='test') | ||
|
||
def test_escaped(self): | ||
""" | ||
Simple test of the Django escaping to illustrate problem. | ||
""" | ||
slack_message('test.slack', {'text': '< > & " \''}) | ||
self.assertMessage(text='< > & " '') | ||
|
||
def test_escape_tag(self): | ||
""" | ||
Test using the escape tag, but not escaping anything. | ||
""" | ||
slack_message('escape.slack', {'text': 'test'}) | ||
self.assertMessage(text='test') | ||
|
||
def test_escape_chars(self): | ||
""" | ||
Test the characters Slack wants escaped. | ||
See <https://api.slack.com/docs/formatting#how_to_escape_characters> | ||
""" | ||
slack_message('escape.slack', {'text': '< > &'}) | ||
self.assertMessage(text='< > &') | ||
|
||
def test_not_escape_chars(self): | ||
""" | ||
Test normal HTML escaped characters that Slack doesn't want escaped. | ||
""" | ||
slack_message('escape.slack', {'text': '" \''}) | ||
self.assertMessage(text='" \'') |