# Path: tutelary/models.py
# def assign_user_policies(user, *policies_roles):
#     """Assign a sequence of policies to a user (or the anonymous user is
#     ``user`` is ``None``).  (Also installed as ``assign_policies``
#     method on ``User`` model.
# 
#     """
#     clear_user_policies(user)
#     pset = PermissionSet.objects.by_policies_and_roles(policies_roles)
#     pset.refresh()
#     if user is None:
#         pset.anonymous_user = True
#     else:
#         pset.users.add(user)
#     pset.save()
#     cache.set(user_cache_key(user), None)
# 
# Path: tutelary/engine.py
# class Object(EscapeSeparated):
#     """Objects are represented by slash-separated sequences of elements
#     (e.g. ``Cadasta/Batangas/parcel/123``, ``H4H/PaP/party/118``) with
#     wildcard elements indicated by ``*``
#     (e.g. ``Cadasta/*/parcel/*``).  Slashes can be backslash-escaped,
#     as can backslashes (e.g. ``Cadasta/Village-X\/Y/parcel/943``).
# 
#     """
#     separator = '/'
# 
# class Action(SimpleSeparated):
#     """Actions are represented by period-separated sequences of elements
#     (e.g. ``parcel.edit``, ``admin.assign-role``) with wildcard
#     elements indicated by ``*`` (e.g. ``party.*``).  A list of
#     *registered* actions is maintained to support permissions set
#     queries to find the set of actions that are permitted on a
#     specified object pattern.
# 
#     """
#     separator = '.'
# 
#     registered = set()
# 
#     def register(action):
#         """Action registration is used to support generating lists of
#         permitted actions from a permission set and an object pattern.
#         Only registered actions will be returned by such queries.
# 
#         """
#         if isinstance(action, str):
#             Action.register(Action(action))
#         elif isinstance(action, Action):
#             Action.registered.add(action)
#         else:
#             for a in action:
#                 Action.register(a)
# 
# 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 assign_user_policies
from tutelary.engine import Object, Action
from django.contrib.auth.models import AnonymousUser
from django.contrib.auth import get_backends
from .factories import UserFactory, PolicyFactory
from .datadir import datadir  # noqa
import pytest
    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')
    deny_pol = PolicyFactory.create(name='prj', file='deny-policy.json')

    Action.register(['party.list', 'party.view', 'parcel.list', 'parcel.view',
                     'party.edit', 'parcel.edit'])

    assign_user_policies(None, def_pol)
    user1.assign_policies(def_pol)
    user2.assign_policies(def_pol,
                          (org_pol, {'organisation': 'Cadasta'}))
    user3.assign_policies(def_pol,
                          (org_pol, {'organisation': 'Cadasta'}),
                          (prj_pol, {'organisation': 'Cadasta',
                                     'project': 'TestProj'}),
                          (deny_pol, {'organisation': 'Cadasta',
                                      'project': 'Proj2'}))
    user4.assign_policies(def_pol,
                          (org_pol, {'organisation': 'OtherOrg'}))
    user5.assign_policies(def_pol,
                          (org_pol, {'organisation': 'Cadasta'}),
                          (prj_pol, {'organisation': 'Cadasta',
                                     'project': 'TestProj2'}))

    return (user1, user2, user3, user4, user5,
            def_pol, org_pol, prj_pol, deny_pol)



# Path: tutelary/models.py
# def assign_user_policies(user, *policies_roles):
#     """Assign a sequence of policies to a user (or the anonymous user is
#     ``user`` is ``None``).  (Also installed as ``assign_policies``
#     method on ``User`` model.
# 
#     """
#     clear_user_policies(user)
#     pset = PermissionSet.objects.by_policies_and_roles(policies_roles)
#     pset.refresh()
#     if user is None:
#         pset.anonymous_user = True
#     else:
#         pset.users.add(user)
#     pset.save()
#     cache.set(user_cache_key(user), None)
# 
# Path: tutelary/engine.py
# class Object(EscapeSeparated):
#     """Objects are represented by slash-separated sequences of elements
#     (e.g. ``Cadasta/Batangas/parcel/123``, ``H4H/PaP/party/118``) with
#     wildcard elements indicated by ``*``
#     (e.g. ``Cadasta/*/parcel/*``).  Slashes can be backslash-escaped,
#     as can backslashes (e.g. ``Cadasta/Village-X\/Y/parcel/943``).
# 
#     """
#     separator = '/'
# 
# class Action(SimpleSeparated):
#     """Actions are represented by period-separated sequences of elements
#     (e.g. ``parcel.edit``, ``admin.assign-role``) with wildcard
#     elements indicated by ``*`` (e.g. ``party.*``).  A list of
#     *registered* actions is maintained to support permissions set
#     queries to find the set of actions that are permitted on a
#     specified object pattern.
# 
#     """
#     separator = '.'
# 
#     registered = set()
# 
#     def register(action):
#         """Action registration is used to support generating lists of
#         permitted actions from a permission set and an object pattern.
#         Only registered actions will be returned by such queries.
# 
#         """
#         if isinstance(action, str):
#             Action.register(Action(action))
#         elif isinstance(action, Action):
#             Action.registered.add(action)
#         else:
#             for a in action:
#                 Action.register(a)
# 
# 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_end_to_end.py
from tutelary.models import assign_user_policies
from tutelary.engine import Object, Action
from django.contrib.auth.models import AnonymousUser
from django.contrib.auth import get_backends
from .factories import UserFactory, PolicyFactory
from .datadir import datadir  # noqa
import pytest

    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')
    deny_pol = PolicyFactory.create(name='prj', file='deny-policy.json')

    Action.register(['party.list', 'party.view', 'parcel.list', 'parcel.view',
                     'party.edit', 'parcel.edit'])

    assign_user_policies(None, def_pol)
    user1.assign_policies(def_pol)
    user2.assign_policies(def_pol,
                          (org_pol, {'organisation': 'Cadasta'}))
    user3.assign_policies(def_pol,
                          (org_pol, {'organisation': 'Cadasta'}),
                          (prj_pol, {'organisation': 'Cadasta',
                                     'project': 'TestProj'}),
                          (deny_pol, {'organisation': 'Cadasta',
                                      'project': 'Proj2'}))
    user4.assign_policies(def_pol,
                          (org_pol, {'organisation': 'OtherOrg'}))
    user5.assign_policies(def_pol,
                          (org_pol, {'organisation': 'Cadasta'}),
                          (prj_pol, {'organisation': 'Cadasta',
                                     'project': 'TestProj2'}))

    return (user1, user2, user3, user4, user5,
            def_pol, org_pol, prj_pol, deny_pol)



def test_basic(datadir, setup):  # noqa