Create a key by default if none is specified

This commit is contained in:
Mjumbe Wawatu Poe 2012-09-07 13:15:24 -04:00
parent f3e65eab6b
commit 5a3874ee11
3 changed files with 12 additions and 1 deletions

View File

@ -146,3 +146,8 @@ class TokenAuthTests(TestCase):
"""Ensure POSTing json over token auth without correct credentials fails"""
response = self.csrf_client.post('/', json.dumps({'example': 'example'}), 'application/json')
self.assertEqual(response.status_code, 403)
def test_token_has_auto_assigned_key_if_none_provided(self):
"""Ensure creating a token with no key will auto-assign a key"""
token = Token.objects.create(user=self.user)
self.assertEqual(len(token.key), 32)

View File

@ -1,16 +1,22 @@
import uuid
from django.db import models
class BaseToken(models.Model):
"""
The base abstract authorization token model class.
"""
key = models.CharField(max_length=32, primary_key=True)
key = models.CharField(max_length=32, primary_key=True, blank=True)
user = models.ForeignKey('auth.User')
revoked = models.BooleanField(default=False)
class Meta:
abstract=True
def save(self, *args, **kwargs):
if not self.key:
self.key = uuid.uuid4().hex
return super(BaseToken, self).save(*args, **kwargs)
class Token(BaseToken):
"""

View File