mirror of
https://github.com/DarrenOfficial/dpaste.git
synced 2024-11-23 11:56:36 +11:00
Unittests for the API
This commit is contained in:
parent
4a2ddc4e99
commit
6302145007
5 changed files with 72 additions and 6 deletions
|
@ -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()
|
||||
|
|
1
dpaste/tests/__init__.py
Normal file
1
dpaste/tests/__init__.py
Normal file
|
@ -0,0 +1 @@
|
|||
from .test_api import *
|
62
dpaste/tests/test_api.py
Normal file
62
dpaste/tests/test_api.py
Normal file
|
@ -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)
|
|
@ -5,6 +5,5 @@ from ..handlers import SnippetHandler
|
|||
snippet_resource = Resource(handler=SnippetHandler)
|
||||
|
||||
urlpatterns = patterns('',
|
||||
url(r'^api/(?P<secret_id>[^/]+)/$', snippet_resource),
|
||||
url(r'^api/$', snippet_resource),
|
||||
url(r'^api/$', snippet_resource, name='dpaste_api_create_snippet'),
|
||||
)
|
|
@ -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
|
Loading…
Reference in a new issue