# Path: chemtrails/contrib/permissions/models.py
# class AccessRule(models.Model):
# 
#     DIRECTION_CHOICES = (
#         (INCOMING, _('INCOMING')),
#         (EITHER, _('EITHER')),
#         (OUTGOING, _('OUTGOING'))
#     )
# 
#     ctype_source = models.ForeignKey(ContentType, verbose_name=_('source content type'),
#                                      related_name='accessrule_ctype_source_set')
#     ctype_target = models.ForeignKey(ContentType, verbose_name=_('target content type'),
#                                      related_name='accessrule_ctype_target_set')
#     description = models.TextField(_('description'), default='')
#     permissions = models.ManyToManyField(Permission, verbose_name=_('access rule permissions'), blank=True,
#                                          help_text=_('Required permissions for target node.'),
#                                          related_name='accessrule_permissions', related_query_name='accessrule')
#     relation_types = ArrayField(verbose_name=_('relation types'), base_field=JSONField(), default=list,
#                                 help_text=_('List of relation type rule definitions, optionally with a map '
#                                             'of properties for matching the relation type node. '
#                                             'Example: {"USER": {"is_superuser": true}}, {"GROUP": null}'))
#     direction = models.SmallIntegerField(verbose_name=_('direction'), choices=DIRECTION_CHOICES,
#                                          blank=True, null=True, default=None,
#                                          help_text=_('Override calculated direction for relationships.<br/> '
#                                                      'Use with caution, this will affect *all* directional '
#                                                      'relationships for evaluation of this rule!'))
#     is_active = models.BooleanField(default=True, help_text=_('Uncheck to disable evaluation of the rule '
#                                                               'in the rule chain.'))
#     requires_staff = models.BooleanField(default=False, help_text=_('Requires user which should have '
#                                                                     'permissions evaluated to be a staff user.'))
#     created = models.DateTimeField(verbose_name=_('created'), auto_now_add=True)
#     updated = models.DateTimeField(verbose_name=_('updated'), auto_now=True)
# 
#     class Meta:
#         ordering = ('ctype_target', '-created',)
# 
#     def __repr__(self):
#         return '<%(class)s: [%(source)s]-[*]-[%(target)s]>' % {
#             'class': self.__class__.__name__,
#             'source': '%s.%s' % (self.ctype_source.app_label, self.ctype_source.model),
#             'target': '%s.%s' % (self.ctype_target.app_label, self.ctype_target.model),
#         }
# 
#     def __str__(self):
#         return '[%(source)s]-[*]-[%(target)s]' % {
#             'source': self.ctype_source,
#             'target': self.ctype_target
#         }
# 
#     @property
#     def relation_types_obj(self):
#         """
#         Return the relation types JSON string as an OrderedDict.
#         """
#         if self.relation_types:
#             return [json.loads(rule, object_pairs_hook=OrderedDict) for rule in self.relation_types]
#         else:
#             return OrderedDict()
# 
# Path: chemtrails/contrib/permissions/utils.py
# def get_content_type(obj):
#     """
#     Returns the content type for ``obj``.
#     """
#     return ContentType.objects.get_for_model(obj)
# 
# Path: tests/utils.py
# class TestCaseMixins:
#     """
#     Mixin for various extensions for the test cases.
#     """
# 
#     def assertIsJSON(self, expr, msg=None):
#         import json
# 
#         try:
#             json.loads(expr)
#         except ValueError:
#             msg = self._formatMessage(msg, '%s is not a valid JSON string' % safe_repr(expr))
#             raise self.failureException(msg)


from django.contrib.auth import get_user_model
from django.core import serializers
from django.test import TestCase
from chemtrails.contrib.permissions.models import AccessRule
from chemtrails.contrib.permissions.utils import get_content_type
from tests.utils import TestCaseMixins
# -*- coding: utf-8 -*-



USER_MODEL = get_user_model()


class JSONFieldTestCase(TestCase, TestCaseMixins):

    def test_field_is_json_serializable(self):
        instance = AccessRule.objects.create(ctype_source=get_content_type(USER_MODEL),

# Path: chemtrails/contrib/permissions/models.py
# class AccessRule(models.Model):
# 
#     DIRECTION_CHOICES = (
#         (INCOMING, _('INCOMING')),
#         (EITHER, _('EITHER')),
#         (OUTGOING, _('OUTGOING'))
#     )
# 
#     ctype_source = models.ForeignKey(ContentType, verbose_name=_('source content type'),
#                                      related_name='accessrule_ctype_source_set')
#     ctype_target = models.ForeignKey(ContentType, verbose_name=_('target content type'),
#                                      related_name='accessrule_ctype_target_set')
#     description = models.TextField(_('description'), default='')
#     permissions = models.ManyToManyField(Permission, verbose_name=_('access rule permissions'), blank=True,
#                                          help_text=_('Required permissions for target node.'),
#                                          related_name='accessrule_permissions', related_query_name='accessrule')
#     relation_types = ArrayField(verbose_name=_('relation types'), base_field=JSONField(), default=list,
#                                 help_text=_('List of relation type rule definitions, optionally with a map '
#                                             'of properties for matching the relation type node. '
#                                             'Example: {"USER": {"is_superuser": true}}, {"GROUP": null}'))
#     direction = models.SmallIntegerField(verbose_name=_('direction'), choices=DIRECTION_CHOICES,
#                                          blank=True, null=True, default=None,
#                                          help_text=_('Override calculated direction for relationships.<br/> '
#                                                      'Use with caution, this will affect *all* directional '
#                                                      'relationships for evaluation of this rule!'))
#     is_active = models.BooleanField(default=True, help_text=_('Uncheck to disable evaluation of the rule '
#                                                               'in the rule chain.'))
#     requires_staff = models.BooleanField(default=False, help_text=_('Requires user which should have '
#                                                                     'permissions evaluated to be a staff user.'))
#     created = models.DateTimeField(verbose_name=_('created'), auto_now_add=True)
#     updated = models.DateTimeField(verbose_name=_('updated'), auto_now=True)
# 
#     class Meta:
#         ordering = ('ctype_target', '-created',)
# 
#     def __repr__(self):
#         return '<%(class)s: [%(source)s]-[*]-[%(target)s]>' % {
#             'class': self.__class__.__name__,
#             'source': '%s.%s' % (self.ctype_source.app_label, self.ctype_source.model),
#             'target': '%s.%s' % (self.ctype_target.app_label, self.ctype_target.model),
#         }
# 
#     def __str__(self):
#         return '[%(source)s]-[*]-[%(target)s]' % {
#             'source': self.ctype_source,
#             'target': self.ctype_target
#         }
# 
#     @property
#     def relation_types_obj(self):
#         """
#         Return the relation types JSON string as an OrderedDict.
#         """
#         if self.relation_types:
#             return [json.loads(rule, object_pairs_hook=OrderedDict) for rule in self.relation_types]
#         else:
#             return OrderedDict()
# 
# Path: chemtrails/contrib/permissions/utils.py
# def get_content_type(obj):
#     """
#     Returns the content type for ``obj``.
#     """
#     return ContentType.objects.get_for_model(obj)
# 
# Path: tests/utils.py
# class TestCaseMixins:
#     """
#     Mixin for various extensions for the test cases.
#     """
# 
#     def assertIsJSON(self, expr, msg=None):
#         import json
# 
#         try:
#             json.loads(expr)
#         except ValueError:
#             msg = self._formatMessage(msg, '%s is not a valid JSON string' % safe_repr(expr))
#             raise self.failureException(msg)


# Path: tests/contrib/permissions/test_fields.py
from django.contrib.auth import get_user_model
from django.core import serializers
from django.test import TestCase
from chemtrails.contrib.permissions.models import AccessRule
from chemtrails.contrib.permissions.utils import get_content_type
from tests.utils import TestCaseMixins

# -*- coding: utf-8 -*-



USER_MODEL = get_user_model()


class JSONFieldTestCase(TestCase, TestCaseMixins):

    def test_field_is_json_serializable(self):
        instance = AccessRule.objects.create(ctype_source=get_content_type(USER_MODEL),

                                             ctype_target=get_content_type(USER_MODEL),