Skip to content

Commit

Permalink
Merge branch 'release-0.9.x' into release-0.10.x
Browse files Browse the repository at this point in the history
  • Loading branch information
davidt committed Jun 21, 2016
2 parents 4bd0a96 + bdd1252 commit 559bf5d
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
7 changes: 6 additions & 1 deletion .travis.yml
Expand Up @@ -12,7 +12,12 @@ matrix:
env: DJANGO_VERSION=1.8.2

install:
- pip install -q Django==$DJANGO_VERSION
- >
if [[ "$DJANGO_VERSION" = "1.6.11" ]]; then
easy_install -U https://s3.amazonaws.com/downloads.reviewboard.org/releases/Django/1.6/Django-1.6.11.3.tar.gz
else
pip install -q Django==$DJANGO_VERSION
fi
- python setup.py develop
- pip install -r dev-requirements.txt

Expand Down
6 changes: 3 additions & 3 deletions djblets/datagrid/grids.py
Expand Up @@ -25,9 +25,9 @@
#
"""Components for creating customizable datagrids from database data.
Datagrids are used to display to display a table-based view of data from
a database, complete with pagination, batch selection, sorting, and
flexible column rendering.
Datagrids are used to display a table-based view of data from a database,
complete with pagination, batch selection, sorting, and flexible column
rendering.
Datagrids have one or more :py:class:`Column` subclasses associated, which will
render the data. The datagrid may display a subset of the rendered columns,
Expand Down
41 changes: 35 additions & 6 deletions djblets/webapi/managers.py
Expand Up @@ -19,15 +19,44 @@
class WebAPITokenManager(Manager):
"""Manages WebAPIToken models."""

def generate_token(self, user, max_attempts=20, local_site=None,
note=None, policy=None):
def generate_token(self, user, max_attempts=20, note=None, policy={},
**kwargs):
"""Generate a WebAPIToken for a user.
This will attempt to construct a unique WebAPIToken for a user.
Since a collision is possible, it will try up to a certain number
of times. If it cannot create a unique token, a
:py:class:`WebAPITokenGenerationError` will be raised.
:py:class:`~djblets.webapi.errors.WebAPITokenGenerationError` will be
raised.
Args:
user (django.contrib.auth.models.User):
The user who will own the token.
max_attempts (int, optional):
The maximum number of attempts to try to find a non-conflicting
token. Defaults to 20.
note (unicode, optional):
A note describing the token.
policy (dict, optional):
The policy document describing what this token can access
in the API. By default, this provides full access.
**kwargs (dict):
Additional keyword arguments representing fields in the token.
These will be set on the token object.
Returns:
djblets.webapi.models.BaseWebAPIToken:
The generated API token.
Raises:
djblets.webapi.errors.WebAPITokenGenerationError:
The token was not able to be generated after ``max_attempts``
number of collisions were hit.
"""
prefix = settings.SECRET_KEY + six.text_type(user.pk) + user.password

Expand All @@ -43,9 +72,9 @@ def generate_token(self, user, max_attempts=20, local_site=None,
try:
return self.create(user=user,
token=token,
local_site=local_site,
note=note,
policy=policy)
note=note or '',
policy=policy,
**kwargs)
except IntegrityError:
# We hit a collision with the token value. Try again.
pass
Expand Down
37 changes: 37 additions & 0 deletions djblets/webapi/tests/test_api_token.py
@@ -0,0 +1,37 @@
from __future__ import unicode_literals

from django.contrib.auth.models import User
from django.db import models

from djblets.testing.testcases import TestCase
from djblets.webapi.models import BaseWebAPIToken
from kgb import SpyAgency


class WebAPIToken(BaseWebAPIToken):
my_field = models.BooleanField(default=False)


class WebAPITokenManagerTests(SpyAgency, TestCase):
def setUp(self):
super(WebAPITokenManagerTests, self).setUp()

self.user = User.objects.create(username='test-user')
self.spy_on(WebAPIToken.save, call_original=False)

def test_generate_token_with_defaults(self):
"""Testing WebAPITokenManager.generate_token with default arguments"""
webapi_token = WebAPIToken.objects.generate_token(self.user)

self.assertEqual(webapi_token.user, self.user)
self.assertEqual(webapi_token.policy, {})
self.assertEqual(webapi_token.note, '')
self.assertEqual(webapi_token.my_field, False)
self.assertIsNotNone(webapi_token.token)

def test_generate_token_with_custom_field(self):
"""Testing WebAPITokenManager.generate_token with custom field"""
webapi_token = WebAPIToken.objects.generate_token(self.user,
my_field=True)

self.assertEqual(webapi_token.my_field, True)

0 comments on commit 559bf5d

Please sign in to comment.