diff --git a/dpaste/handlers.py b/dpaste/handlers.py index d6df466..bb22588 100644 --- a/dpaste/handlers.py +++ b/dpaste/handlers.py @@ -5,15 +5,17 @@ from dpaste.models import Snippet class SnippetHandler(AnonymousBaseHandler): allowed_methods = ('POST',) - fields = ('title', 'content',) + fields = ('content',) model = Snippet def create(self, request): - if not request.POST.get('content'): + content = request.POST.get('content', '').strip() + + if not content: return rc.BAD_REQUEST s = Snippet.objects.create( - content=request.POST.get('content'), + content=content, expires=datetime.datetime.now()+datetime.timedelta(seconds=60*60*24*30) ) s.save() diff --git a/dpaste/tests/__init__.py b/dpaste/tests/__init__.py new file mode 100644 index 0000000..858cf30 --- /dev/null +++ b/dpaste/tests/__init__.py @@ -0,0 +1 @@ +from .test_api import * \ No newline at end of file diff --git a/dpaste/tests/test_api.py b/dpaste/tests/test_api.py new file mode 100644 index 0000000..b64b350 --- /dev/null +++ b/dpaste/tests/test_api.py @@ -0,0 +1,62 @@ +# -*- encoding: utf-8 -*- + +from django.core.urlresolvers import reverse +from django.test.client import Client +from django.test import TestCase + +from ..models import Snippet + +class SnippetAPITestCase(TestCase): + + def setUp(self): + self.api_url = reverse('dpaste_api_create_snippet') + self.client = Client() + + def test_no_post_data(self): + """ + No data passed. API returns a 400 Bad Request and no snippet + instance was created. + """ + response = self.client.post(self.api_url, {}) + self.assertEqual(response.status_code, 400) + self.assertEqual(Snippet.objects.count(), 0) + + def test_empty(self): + """ + The browser sent a content field but with no data. + """ + # No data + response = self.client.post(self.api_url, {'content': ''}) + self.assertEqual(response.status_code, 400) + self.assertEqual(Snippet.objects.count(), 0) + + # Just some spaces + response = self.client.post(self.api_url, {'content': ' '}) + self.assertEqual(response.status_code, 400) + self.assertEqual(Snippet.objects.count(), 0) + + # Linebreaks or tabs only are not valid either + response = self.client.post(self.api_url, {'content': '\n\t '}) + self.assertEqual(response.status_code, 400) + self.assertEqual(Snippet.objects.count(), 0) + + def test_valid(self): + """ + A valid snippet, contains Unicode, tabs, spaces, linebreaks etc. + """ + content = u"Hello Wörld.\n\tGood Bye" + response = self.client.post(self.api_url, {'content': content}) + self.assertEqual(response.status_code, 200) + self.assertEqual(Snippet.objects.count(), 1) + + # The response is a URL with quotes + self.assertTrue(response.content.startswith('"')) + self.assertTrue(response.content.endswith('"')) + + # The URL returned is the absolute url to the snippet. + # If we call that url our snippet should be in the page content. + snippet_url = response.content[1:-1] + response = self.client.get(snippet_url) + + self.assertEqual(response.status_code, 200) + self.assertContains(response, content) diff --git a/dpaste/urls/dpaste_api.py b/dpaste/urls/dpaste_api.py index ba35b86..ae02f14 100644 --- a/dpaste/urls/dpaste_api.py +++ b/dpaste/urls/dpaste_api.py @@ -5,6 +5,5 @@ from ..handlers import SnippetHandler snippet_resource = Resource(handler=SnippetHandler) urlpatterns = patterns('', - url(r'^api/(?P[^/]+)/$', snippet_resource), - url(r'^api/$', snippet_resource), + url(r'^api/$', snippet_resource, name='dpaste_api_create_snippet'), ) \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index e79fdc7..5fd80be 100644 --- a/requirements.txt +++ b/requirements.txt @@ -2,6 +2,8 @@ django==1.5 django-mptt==0.4.2 django-piston==0.2.2 pygments==1.6 -mysql-python==1.2.4 south==0.7.6 + +# Deployment specific +mysql-python==1.2.4 gunicorn==0.17.2 \ No newline at end of file