# Path: tutelary/models.py
# class PermissionSet(models.Model):
#     """A permission set represents the complete set of permissions
#     resulting from the composition of a sequence of policy instances.
#     The sequence of policy instances is recorded using the
#     ``PolicyInstance`` model and the permission tree is constructed
#     lazily from this information when the permission set is read from
#     the database.
# 
#     """
#     # Ordered set of policies used to generate this permission set.
#     policy_assign = models.ManyToManyField(Policy, through=PolicyInstance)
# 
#     # Users to which this permission set is attached: a user has only
#     # one permission set, so this is really an 1:m relation, not an
#     # n:m relation.
#     users = models.ManyToManyField(settings.AUTH_USER_MODEL,
#                                    related_name='permissionset')
#     anonymous_user = models.BooleanField(default=False)
# 
#     # Custom manager to deal with folding together permission sets
#     # generated from identical sequences of policies.
#     objects = PermissionSetManager()
# 
#     def cache_key(self):
#         return 'tutelary:ptree:' + str(self.pk)
# 
#     def tree(self):
#         key = self.cache_key()
#         cached = cache.get(key)
#         if cached is None:
#             ptree = engine.PermissionTree(
#                 policies=[engine.PolicyBody(json=pi.policy.body,
#                                             variables=json.loads(pi.variables))
#                           for pi in (PolicyInstance.objects
#                                      .select_related('policy')
#                                      .filter(pset=self))]
#             )
#             cache.set(key, ptree)
#             cached = ptree
#         return cached
# 
#     def refresh(self):
#         cache.set(self.cache_key(), None)
# 
#     def __str__(self):
#         return str(self.pk)
# 
# class Policy(models.Model):
#     """An individual policy has a name and a JSON policy body.  Changes to
#     policies are audited.
# 
#     """
# 
#     name = models.CharField(max_length=200)
#     """Policy name field."""
# 
#     body = models.TextField()
#     """Policy JSON body."""
# 
#     audit_log = AuditLog()
# 
#     def __str__(self):
#         return self.name
# 
#     def variable_names(self):
#         pat = re.compile(r'\$(?:([_a-z][_a-z0-9]*)|{([_a-z][_a-z0-9]*)})')
#         return set([m[0] for m in re.findall(pat, self.body)])
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
#         self.refresh()
# 
#     def refresh(self):
#         for pset in _policy_psets([self]):
#             pset.refresh()
# 
# class PolicyInstance(models.Model):
#     """An instance of a policy provides fixed values for any variables
#     used in the policy's body.  An ordered sequence of policy
#     instances defines a permission set.
# 
#     """
# 
#     policy = models.ForeignKey(Policy, on_delete=models.PROTECT)
# 
#     pset = models.ForeignKey('PermissionSet', on_delete=models.CASCADE)
# 
#     index = models.IntegerField()
#     """Integer index used to order the sequence of policies composing a
#     permission set.
# 
#     """
# 
#     role = models.ForeignKey(Role, null=True, on_delete=models.PROTECT)
#     """The role that this policy instance is associated with, if any."""
# 
#     variables = models.TextField()
#     """JSON dump of dictionary giving variable assignments for policy
#     instance.
# 
#     """
# 
#     class Meta:
#         ordering = ['index']
# 
#     def __str__(self):
#         return "policy={} pset={} index={} role={} variables={}".format(
#             self.policy.name, self.pset.pk, self.index,
#             self.role.name if self.role else "NULL",
#             self.variables
#         )
# 
# Path: tests/factories.py
# class UserFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = User
# 
# class PolicyFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = Policy
# 
#     @classmethod
#     def set_directory(cls, dir):
#         cls.directory = dir
# 
#     @classmethod
#     def _adjust_kwargs(cls, **kwargs):
#         body_file = os.path.join(cls.directory, kwargs.pop('file', None))
#         kwargs['body'] = open(body_file).read()
#         return kwargs
# 
# Path: tests/datadir.py
# @pytest.fixture(scope="function")
# def datadir(request, tmpdir):
#     """
#     Fixture responsible for searching a folder with the same name of test
#     module and, if available, moving all contents to a temporary directory so
#     tests can use them freely.
# 
#     Adapted from http://stackoverflow.com/questions/29627341
#     """
#     test_dir = py.path.local(request.module.__file__).new(ext='')
#     try:
#         for f in test_dir.listdir():
#             f.copy(tmpdir)
#     except:
#         pass
#     return tmpdir


from tutelary.models import (
    PermissionSet, Policy, PolicyInstance
)
from django.contrib.auth.models import User
from .factories import UserFactory, PolicyFactory
from .datadir import datadir  # noqa
from .settings import DEBUG
import pytest


@pytest.fixture(scope="function")  # noqa
def setup(datadir, db):
    user1 = UserFactory.create(username='user1')
    user2 = UserFactory.create(username='user2')
    user3 = UserFactory.create(username='user3')

    PolicyFactory.set_directory(str(datadir))
    def_pol = PolicyFactory.create(name='def', file='default-policy.json')
    org_pol = PolicyFactory.create(name='org', file='org-policy.json')

# Path: tutelary/models.py
# class PermissionSet(models.Model):
#     """A permission set represents the complete set of permissions
#     resulting from the composition of a sequence of policy instances.
#     The sequence of policy instances is recorded using the
#     ``PolicyInstance`` model and the permission tree is constructed
#     lazily from this information when the permission set is read from
#     the database.
# 
#     """
#     # Ordered set of policies used to generate this permission set.
#     policy_assign = models.ManyToManyField(Policy, through=PolicyInstance)
# 
#     # Users to which this permission set is attached: a user has only
#     # one permission set, so this is really an 1:m relation, not an
#     # n:m relation.
#     users = models.ManyToManyField(settings.AUTH_USER_MODEL,
#                                    related_name='permissionset')
#     anonymous_user = models.BooleanField(default=False)
# 
#     # Custom manager to deal with folding together permission sets
#     # generated from identical sequences of policies.
#     objects = PermissionSetManager()
# 
#     def cache_key(self):
#         return 'tutelary:ptree:' + str(self.pk)
# 
#     def tree(self):
#         key = self.cache_key()
#         cached = cache.get(key)
#         if cached is None:
#             ptree = engine.PermissionTree(
#                 policies=[engine.PolicyBody(json=pi.policy.body,
#                                             variables=json.loads(pi.variables))
#                           for pi in (PolicyInstance.objects
#                                      .select_related('policy')
#                                      .filter(pset=self))]
#             )
#             cache.set(key, ptree)
#             cached = ptree
#         return cached
# 
#     def refresh(self):
#         cache.set(self.cache_key(), None)
# 
#     def __str__(self):
#         return str(self.pk)
# 
# class Policy(models.Model):
#     """An individual policy has a name and a JSON policy body.  Changes to
#     policies are audited.
# 
#     """
# 
#     name = models.CharField(max_length=200)
#     """Policy name field."""
# 
#     body = models.TextField()
#     """Policy JSON body."""
# 
#     audit_log = AuditLog()
# 
#     def __str__(self):
#         return self.name
# 
#     def variable_names(self):
#         pat = re.compile(r'\$(?:([_a-z][_a-z0-9]*)|{([_a-z][_a-z0-9]*)})')
#         return set([m[0] for m in re.findall(pat, self.body)])
# 
#     def save(self, *args, **kwargs):
#         super().save(*args, **kwargs)
#         self.refresh()
# 
#     def refresh(self):
#         for pset in _policy_psets([self]):
#             pset.refresh()
# 
# class PolicyInstance(models.Model):
#     """An instance of a policy provides fixed values for any variables
#     used in the policy's body.  An ordered sequence of policy
#     instances defines a permission set.
# 
#     """
# 
#     policy = models.ForeignKey(Policy, on_delete=models.PROTECT)
# 
#     pset = models.ForeignKey('PermissionSet', on_delete=models.CASCADE)
# 
#     index = models.IntegerField()
#     """Integer index used to order the sequence of policies composing a
#     permission set.
# 
#     """
# 
#     role = models.ForeignKey(Role, null=True, on_delete=models.PROTECT)
#     """The role that this policy instance is associated with, if any."""
# 
#     variables = models.TextField()
#     """JSON dump of dictionary giving variable assignments for policy
#     instance.
# 
#     """
# 
#     class Meta:
#         ordering = ['index']
# 
#     def __str__(self):
#         return "policy={} pset={} index={} role={} variables={}".format(
#             self.policy.name, self.pset.pk, self.index,
#             self.role.name if self.role else "NULL",
#             self.variables
#         )
# 
# Path: tests/factories.py
# class UserFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = User
# 
# class PolicyFactory(factory.django.DjangoModelFactory):
#     class Meta:
#         model = Policy
# 
#     @classmethod
#     def set_directory(cls, dir):
#         cls.directory = dir
# 
#     @classmethod
#     def _adjust_kwargs(cls, **kwargs):
#         body_file = os.path.join(cls.directory, kwargs.pop('file', None))
#         kwargs['body'] = open(body_file).read()
#         return kwargs
# 
# Path: tests/datadir.py
# @pytest.fixture(scope="function")
# def datadir(request, tmpdir):
#     """
#     Fixture responsible for searching a folder with the same name of test
#     module and, if available, moving all contents to a temporary directory so
#     tests can use them freely.
# 
#     Adapted from http://stackoverflow.com/questions/29627341
#     """
#     test_dir = py.path.local(request.module.__file__).new(ext='')
#     try:
#         for f in test_dir.listdir():
#             f.copy(tmpdir)
#     except:
#         pass
#     return tmpdir


# Path: tests/test_integrity.py
from tutelary.models import (
    PermissionSet, Policy, PolicyInstance
)
from django.contrib.auth.models import User
from .factories import UserFactory, PolicyFactory
from .datadir import datadir  # noqa
from .settings import DEBUG
import pytest



@pytest.fixture(scope="function")  # noqa
def setup(datadir, db):
    user1 = UserFactory.create(username='user1')
    user2 = UserFactory.create(username='user2')
    user3 = UserFactory.create(username='user3')

    PolicyFactory.set_directory(str(datadir))
    def_pol = PolicyFactory.create(name='def', file='default-policy.json')
    org_pol = PolicyFactory.create(name='org', file='org-policy.json')

    prj_pol = PolicyFactory.create(name='prj', file='project-policy.json')