Skip to content
Browse files

Started authorization tests.

  • Loading branch information...
1 parent 087fea2 commit c4e7d86000ab45cf40db08bf489ad9a01f1e9ffc @toastdriven toastdriven committed May 1, 2012
View
0 tests/authorization/__init__.py
No changes.
View
0 tests/authorization/api/__init__.py
No changes.
View
38 tests/authorization/api/resources.py
@@ -0,0 +1,38 @@
+from django.contrib.auth.models import User
+from django.contrib.sites.models import Site
+from tastypie.authorization import Authorization
+from tastypie import fields
+from tastypie.resources import ModelResource
+from ..models import AuthorProfile, Article
+
+
+class PerUserAuthorization(Authorization):
+ # FIXME: Implement this!
+ pass
+
+
+class UserResource(ModelResource):
+ class Meta:
+ queryset = User.objects.all()
+ excludes = ['email', 'password', 'is_staff', 'is_superuser']
+
+
+class SiteResource(ModelResource):
+ class Meta:
+ queryset = Site.objects.all()
+
+
+class AuthorProfileResource(ModelResource):
+ user = fields.ToOneField(UserResource, 'user', full=True)
+ sites = fields.ToManyField(SiteResource, 'sites', related_name='author_profiles', full=True)
+
+ class Meta:
+ queryset = AuthorProfile.objects.all()
+
+
+class ArticleResource(ModelResource):
+ authors = fields.ToManyField(AuthorProfileResource, 'authors', related_name='articles', full=True)
+
+ class Meta:
+ queryset = Article.objects.all()
+
View
27 tests/authorization/models.py
@@ -0,0 +1,27 @@
+from django.contrib.auth.models import User
+from django.contrib.sites.models import Site
+from django.db import models
+
+
+class AuthorProfile(models.Model):
+ user = models.OneToOneField(User, related_name='author_profile')
+ short_bio = models.CharField(max_length=255, blank=True, default='')
+ bio = models.TextField(blank=True, default='')
+ # We'll use the ``sites`` the author is assigned to as a way to control
+ # the permissions.
+ sites = models.ManyToManyField(Site, related_name='author_profiles')
+
+ def __unicode__(self):
+ return u"Profile: {0}".format(self.user.get_full_name())
+
+
+class Article(models.Model):
+ # We'll also use the ``authors`` to control perms.
+ authors = models.ManyToManyField(AuthorProfile, related_name='articles')
+ title = models.CharField(max_length=255)
+ url = models.UrlField()
+ content = models.TextField(blank=True, default='')
+ added_on = models.DateTimeField(default=datetime.datetime.now)
+
+ def __unicode__(self):
+ return u"{0} - {1}".format(self.title, self.url)
View
7 tests/authorization/tests.py
@@ -0,0 +1,7 @@
+from django.test import TestCase
+from tastypie.authorization import Authorization
+
+
+class PerUserAuthorizationTestCase(TestCase):
+ def test_get(self):
+ pass
View
11 tests/authorization/urls.py
@@ -0,0 +1,11 @@
+from django.conf.urls.defaults import patterns, url, include
+
+from tastypie.api import Api
+from .api.resources import ArticleResource
+v1_api = Api()
+v1_api.register(ArticleResource())
+
+
+urlpatterns = patterns('',
+ url(r'^api/', include(v1_api.urls)),
+)
View
18 tests/manage_authorization.py
@@ -0,0 +1,18 @@
+#!/usr/bin/env python
+
+import os
+import sys
+
+from os.path import abspath, dirname, join
+from django.core.management import execute_manager
+sys.path.insert(0, os.path.join(os.path.dirname(__file__), '..'))
+try:
+ import settings_authorization as settings
+except ImportError:
+ import sys
+ sys.stderr.write("Error: Can't find the file 'settings_authorization.py' in the directory containing %r. It appears you've customized things.\nYou'll have to run django-admin.py, passing it your settings module.\n(If the file settings.py does indeed exist, it's causing an ImportError somehow.)\n" % __file__)
+ sys.exit(1)
+
+if __name__ == "__main__":
+ execute_manager(settings)
+
View
6 tests/settings_authorization.py
@@ -0,0 +1,6 @@
+from settings import *
+
+INSTALLED_APPS.append('django.contrib.sites')
+INSTALLED_APPS.append('authorization')
+
+ROOT_URLCONF = 'authorization.urls'

0 comments on commit c4e7d86

Please sign in to comment.
Something went wrong with that request. Please try again.