"""Tests for the principal package."""
import unittest

from pyramid import testing


class DummyFolder(testing.DummyResource):

    def add(self, name, obj, **kwargs):
        self[name] = obj
        obj.__name__ = name
        obj.__parent__ = self
        obj.__oid__ = 1

    def check_name(self, name):
        if name == 'invalid':
            raise ValueError
        return name

    def next_name(self, obj, prefix=''):
        return prefix + '_0000000'


class PrincipalIntegrationTest(unittest.TestCase):

    def setUp(self):
        import substanced.principal
        self.config = testing.setUp()
        self.config.include('substanced.content')
        self.config.scan(substanced.principal)
        self.config.include('adhocracy.registry')
        self.config.include('adhocracy.resources.principal')
        self.context = DummyFolder()

    def tearDown(self):
        testing.tearDown()

    def test_create_principals(self):
        from adhocracy.resources.principal import IPrincipalsPool
        from adhocracy.resources.principal import IUsersPool
        from adhocracy.resources.principal import IGroupsPool
        from adhocracy.resources.principal import IPasswordResetsPool

        inst = self.config.registry.content.create(
            IPrincipalsPool.__identifier__)

        assert IPrincipalsPool.providedBy(inst)
        assert 'users' in inst
        assert 'groups' in inst
        assert 'resets' in inst
        assert IUsersPool.providedBy(inst['users'])
        assert IGroupsPool.providedBy(inst['groups'])
        assert IPasswordResetsPool.providedBy(inst['resets'])

    def test_create_user(self):
        from adhocracy.resources.principal import IUser
        from adhocracy.resources.principal import User

        inst = self.config.registry.content.create(IUser.__identifier__)

        assert IUser.providedBy(inst)
        assert isinstance(inst, User)


class UserUnitTest(unittest.TestCase):

    def _makeOne(self):
        from adhocracy.resources.principal import User
        return User()

    def test_create(self):
        user = self._makeOne()
        assert user.email == ''
        assert user.password == ''
        assert user.tzname == 'UTC'

@@ -1907,16 +1907,1178 @@
  User)%0A%0A
+    def test_create_and_add_user(self):%0A        from adhocracy.resources.principal import IPrincipalsPool%0A        from adhocracy.resources.principal import IUser%0A        from adhocracy.sheets.user import IPasswordAuthentication%0A        from adhocracy.sheets.user import IUserBasic%0A%0A        self.config.include('adhocracy.sheets.user')%0A%0A        principals_pool = self.config.registry.content.create(%0A            IPrincipalsPool.__identifier__)%0A        users_pool = principals_pool%5B'users'%5D%0A        appstructs = %7B%0A            IUserBasic.__identifier__ : %7B%0A                'name': 'Anna M%C3%BCller',%0A                'email': 'anna@example.org'%0A            %7D,%0A            IPasswordAuthentication.__identifier__ : %7B%0A                'password': 'fodThyd2'%0A            %7D,%0A        %7D%0A        inst = self.config.registry.content.create(IUser.__identifier__,%0A                                                   parent=users_pool,%0A                                                   appstructs=appstructs)%0A%0A        got_it = False%0A        for child in users_pool:%0A            if users_pool%5Bchild%5D == inst:%0A                got_it = True%0A                break%0A        assert got_it%0A%0A
 %0Aclass U

Add test adding a user to the user pool.