Test your django-piston API (with auth)
I have to build the API for one of my web service. It’s Django and django-piston powered application and it works well. Okay. I chose the TDD technique. So my problem was: how do I test the API parts which need authentication (basic HTTP, not OAuth for now)? The built-in test client of Django doesn’t seem to have such a feature.
So, here is my small workaround: you have to generate the HTTP_AUTHORIZATION field of your HTTP request. I wrote a small base test class for tests which need authentication:
import base64
import unittest
from django.test.client import Client
class BaseAuthenticatedClient(unittest.TestCase):
def setUp(self):
self.client = Client()
auth = '%s:%s' % ('username', 'password')
auth = 'Basic %s' % base64.encodestring(auth)
auth = auth.strip()
self.extra = {
'HTTP_AUTHORIZATION': auth,
}
You just have to replace username and password, and write your own test suite:
class TestAPIAuth(BaseAuthenticatedClient):
def testrootauth(self):
response = self.client.get('/api/aresource/id', {}, **self.extra)
self.assertEqual(response.status_code, 200)
It should be OK. Have fun!