Beispiel #1
0
    def setUp(self):
        # setup the system and install our own test database
        self.db = zoom.database.setup_test()
        self.users = Users(self.db)
        self.user = self.users.first(username='******')
        self.site = zoom.system.site = FakeSite(
            db=self.db,
            url='',
            logging=False,
            users=self.users,
        )
        self.request = context.request = FakeRequest(
            '/myapp',
            user=self.user,
            site=self.site,
            path='/myapp',
            ip_address='127.0.0.1',
            remote_user='',
            host='localhost',
            data={},
        )

        # create the test collection
        self.collection = Collection(person_fields,
                                     name='People',
                                     model=Person,
                                     url='/myapp',
                                     store=zoom.store.EntityStore(
                                         self.db, Person))

        # so we can see our print statements
        self.save_stdout = sys.stdout
        sys.stdout = sys.stderr

        self.logger = logging.getLogger(__name__)
Beispiel #2
0
def handler(request, handler, *rest):
    """handle user"""
    logger = logging.getLogger(__name__)

    username = get_current_username(request)
    if username:
        users = Users(request.site.db)
        user = users.first(username=username)
        if user:
            context.user = request.user = user
            user.initialize(request)
            logger.debug('user loaded: %s (%r)', user.full_name, user.username)
    return handler(request, *rest)
Beispiel #3
0
 def setUp(self):
     self.db = setup_test()
     self.users = Users(self.db)
     self.groups = Groups(self.db)
     zoom.system.request = zoom.utils.Bunch(
         app=zoom.utils.Bunch(name=__name__, ),
         session=zoom.utils.Bunch(),
     )
     zoom.system.site = zoom.utils.Bunch(
         url='nosite',
         db=self.db,
         groups=self.groups,
     )
     zoom.system.user = self.users.first(username='******')
Beispiel #4
0
 def login_button(self, **data):
     logger = logging.getLogger(__name__)
     logger.debug('login_button called')
     site = self.model.site
     username = data.get('username')
     password = data.get('password')
     remember_me = bool(data.get('remember_me'))
     if username and password:
         users = Users(site.db)
         user = users.first(username=username, status='A')
         if user:
             if user.login(self.model, password, remember_me):
                 logger.info(
                     'user {!r} sucesfully logged in'.format(username))
                 return redirect_to('/')
         logger.debug('failed login attempt for user {!r}'.format(username))
         error('incorrect username or password')
     elif username:
         error('password missing')
     else:
         error('username missing')
Beispiel #5
0
def main(route, request):
    db = request.site.db
    users = Users(db)
    fields = user_fields(request)
    columns = 'link', 'username', 'phone', 'email', 'username', 'status', 'updated', 'updated_by'
    return Collection(fields,
                      model=User,
                      controller=UserCollectionController,
                      store=users,
                      item_name='user',
                      columns=columns,
                      url='/admin/users')(route, request)
Beispiel #6
0
def get_users_collection(request):
    db = request.site.db
    users = Users(db)
    fields = user_fields(request)
    columns = 'link', 'phone', 'email', 'status_text', 'when_updated', 'when_last_seen'
    labels = 'Username', 'Phone', 'Email', 'Status', 'Updated', 'Last Seen'
    return model.AdminCollection(fields,
                                 model=User,
                                 view=UserCollectionView,
                                 controller=UserCollectionController,
                                 store=users,
                                 item_name='user',
                                 columns=columns,
                                 labels=labels,
                                 url='/admin/users',
                                 key_name='id',
                                 search_engine=RawSearch)
Beispiel #7
0
    def index(self, q=''):
        db = self.model.site.db

        users = sorted(Users(db).find(status='A'), key=lambda a: a.name)
        groups = db('select * from groups')
        members = db('select * from members')
        subgroups = db('select * from subgroups')

        content = Component(
            index_metrics_view(db),
            IndexPageLayoutView(
                feed1=Component(
                    PanelView(title='Users', content=browse(users)),
                    PanelView(title='Groups', content=browse(groups)),
                ),
                feed2=PanelView(title='Memberships', content=browse(members)),
                feed3=PanelView(title='Subgroups', content=browse(subgroups)),
            ),
        )
        return page(content, title='Overview', subtitle='Raw dump of main tables', search=q)
Beispiel #8
0
    def __init__(self, name, filename, site):
        self.name = name
        self.filename = filename

        request = getattr(zoom.system, 'request')
        if request:
            user = zoom.system.request.user
        else:
            user = Users(site.db).first(username='******')

        default_app_name = get_default_app_name(site, user)

        slug = '/' + name
        if name == default_app_name:
            slug = '/'

        self.url = site.url + slug
        self.abs_url = site.abs_url + slug
        self.path = os.path.dirname(filename)
        self.site = site
        self.config_parser = configparser.ConfigParser()
        self.config = self.get_config(DEFAULT_SETTINGS)
        self.link = zoom.helpers.link_to(self.title, self.url)
        self.request = None
        self.packages = {}
        self.common_packages = {}

        get = self.config.get
        self.visible = get('visible') not in zoom.utils.NEGATIVE
        self.enabled = get('enabled')
        self.author = get('author')
        self.version = get('version')
        self.theme = get('theme')
        self.icon = get('icon')
        self.as_icon = self.get_icon_view()
        self.in_development = get('in_development')

        self._method = None
        self._templates_paths = None
Beispiel #9
0
class TestCollect(unittest.TestCase):
    def setUp(self):
        # setup the system and install our own test database
        self.db = zoom.database.setup_test()
        self.users = Users(self.db)
        self.user = self.users.first(username='******')
        self.site = zoom.system.site = FakeSite(
            db=self.db,
            url='',
            logging=False,
            users=self.users,
        )
        self.request = context.request = FakeRequest(
            '/myapp',
            user=self.user,
            site=self.site,
            path='/myapp',
            ip_address='127.0.0.1',
            remote_user='',
            host='localhost',
            data={},
        )

        # create the test collection
        self.collection = Collection(person_fields,
                                     name='People',
                                     model=Person,
                                     url='/myapp',
                                     store=zoom.store.EntityStore(
                                         self.db, Person))

        # so we can see our print statements
        self.save_stdout = sys.stdout
        sys.stdout = sys.stderr

        self.logger = logging.getLogger(__name__)

    def tearDown(self):
        # remove our test data
        # self.collection.store.zap()
        self.db.close()
        sys.stdout = self.save_stdout

    def collect(self, *route, **data):
        self.request.route = list(route)
        self.request.data = data
        return self.collection(route, self.request)

    def assert_response(self, content, *args, **kwargs):
        assert_same(content, self.collect(*args, **kwargs).content)

    def test_empty(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

    def test_index_redirect(self):
        response = self.collect('index')
        self.assertEqual(type(response), zoom.tools.Redirector)

    def test_index_many(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        insert_record_input = dict(
            create_button='y',
            name='Joe',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **insert_record_input)
        self.assert_response(VIEW_SINGLE_RECORD_LIST)

        for _ in range(51):
            self.collect(
                'new',
                **dict(
                    create_button='y',
                    name=fake.name(),
                    address=fake.street_address(),
                    salary=Decimal('40000'),
                ))

        content = self.collect().content
        assert '15 people shown of 52 people' in content

        content = self.collect(all='y').content
        assert '52 people shown of 52 people' in content

    def test_basic_search(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        insert_record_input = dict(
            create_button='y',
            name='Joe Zzzzz',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **insert_record_input)

        for _ in range(5):
            self.collect(
                'new',
                **dict(
                    create_button='y',
                    name=fake.name(),
                    address=fake.street_address(),
                    salary=Decimal('40000'),
                ))

        content = self.collect(q='Zzzz').content
        assert '1 person found in search of 6 people' in content

        content = self.collect(q='Xzzz').content
        assert '0 people found in search of 6 people' in content

    def test_indexed_search(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)
        self.collection.search_engine = zoom.collect.IndexedCollectionSearch

        insert_record_input = dict(
            create_button='y',
            name='Joe Zzzzz',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **insert_record_input)

        for _ in range(5):
            self.collect(
                'new',
                **dict(
                    create_button='y',
                    name=fake.name(),
                    address=fake.street_address(),
                    salary=Decimal('40000'),
                ))

        content = self.collect(q='Zzzz').content
        assert '1 person found in search of 6 people' in content

        content = self.collect(q='Xzzz').content
        assert '0 people found in search of 6 people' in content

        self.collect('reindex')

        content = self.collect(q='Zzzz').content
        assert '1 person found in search of 6 people' in content

        content = self.collect(q='Xzzz').content
        assert '0 people found in search of 6 people' in content

        self.collect('delete', 'joe-zzzzz', **{'confirm': 'no'})

        content = self.collect(q='Zzzz').content
        assert '0 people found in search of 5 people' in content

    def test_insert(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        insert_record_input = dict(
            create_button='y',
            name='Joe',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **insert_record_input)
        self.logger.debug(str(self.collection.store))
        self.assert_response(VIEW_SINGLE_RECORD_LIST)

    def test_show(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        insert_record_input = dict(
            create_button='y',
            name='Joe',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **insert_record_input)
        response = self.collect('show', 'joe').content
        assert '123 Somewhere St' in response
        assert '123 Nowhere St' not in response

    def test_new(self):
        response = self.collect('new').content
        assert 'Name' in response

    def test_edit(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        insert_record_input = dict(
            create_button='y',
            name='Joe',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **insert_record_input)
        response = self.collect('edit', 'joe').content
        assert '123 Somewhere St' in response
        assert '123 Nowhere St' not in response

    def test_save(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        insert_record_input = dict(
            create_button='y',
            name='Joe',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **insert_record_input)
        response = self.collect('edit', 'joe').content
        assert '123 Somewhere St' in response
        assert '123 Nowhere St' not in response

        update_record_input = dict(
            save_button='y',
            name='Joe',
            address='123 Somewhere Else St',
            salary=Decimal('50000'),
        )
        response = self.collect('joe', **update_record_input)

        response = self.collect('show', 'joe').content
        assert '123 Somewhere Else St' in response
        assert '123 Somewhere St' not in response
        assert '123 Nowhere St' not in response

    def test_delete(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        joe_input = dict(
            create_button='y',
            name='Joe',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **joe_input)
        sally_input = dict(
            create_button='y',
            name='Sally',
            address='123 Special St',
            salary=Decimal('45000'),
        )
        self.collect('new', **sally_input)
        self.assert_response(VIEW_TWO_RECORD_LIST)

        response = self.collect('delete', 'joe').content
        assert 'Are you sure' in response
        self.assert_response(VIEW_TWO_RECORD_LIST)

        self.collect('delete', 'joe', **{'confirm': 'no'})
        self.assert_response(VIEW_NO_JOE_LIST)

        self.collect('delete', 'sally', **{'confirm': 'no'})
        self.assert_response(VIEW_EMPTY_LIST)

    def test_get_columns(self):
        savecols = self.collection.get_columns()
        try:
            self.collection.columns = ['one', 'two']
            self.assertEqual(self.collection.get_columns(), ['one', 'two'])
        finally:
            self.collection.columns = savecols

    def test_get_labels(self):
        saved = self.collection.get_labels()
        try:
            self.collection.labels = ['One', 'Two']
            self.assertEqual(self.collection.get_labels(), ['One', 'Two'])
        finally:
            self.collection.labels = saved
Beispiel #10
0
 def get_user_options(self):
     return sorted((user.link, user._id) for user in Users(self.db))
Beispiel #11
0
 def setUp(self):
     self.db = setup_test()
     self.users = Users(self.db)
     zoom.system.request = zoom.utils.Bunch(
         app=zoom.utils.Bunch(name=__name__, ))
Beispiel #12
0
class TestUser(unittest.TestCase):
    def setUp(self):
        self.db = setup_test()
        self.users = Users(self.db)
        self.groups = Groups(self.db)
        zoom.system.request = zoom.utils.Bunch(
            app=zoom.utils.Bunch(name=__name__, ),
            session=zoom.utils.Bunch(),
        )
        zoom.system.site = zoom.utils.Bunch(
            url='nosite',
            db=self.db,
            groups=self.groups,
        )
        zoom.system.user = self.users.first(username='******')

    def tearDown(self):
        self.db.close()

    def test_add(self):
        self.assertFalse(self.users.first(username='******'))
        user = self.users.add('sam', 'sam', 'smith', '*****@*****.**')
        self.assertTrue(isinstance(user, User))
        self.assertEqual(user.name, 'sam smith')
        self.assertTrue(self.users.first(username='******'))
        self.users.delete(username='******')
        self.assertFalse(self.users.first(username='******'))

    def test_add_invalid(self):
        self.assertFalse(self.users.first(username='******'))
        with self.assertRaises(Exception):
            self.users.add('sam', '', 'smith', '*****@*****.**')
        try:
            self.users.add('sam', '', 'smith', '*****@*****.**')
        except BaseException as e:
            self.assertEqual(str(e), 'minimum length 2')
        with self.assertRaises(Exception):
            self.users.add('sam', 'sam', 'smith', 'sam')
        try:
            self.users.add('sam', 'sam', 'smith', 'sam')
        except BaseException as e:
            self.assertEqual(str(e), 'enter a valid email address')
        with self.assertRaises(Exception):
            self.users.add('sam', 'sam', 'smith', '*****@*****.**', '123')
        try:
            self.users.add('sam', 'sam', 'smith', '*****@*****.**', '123')
        except BaseException as e:
            self.assertEqual(str(e), 'enter valid phone number')
        self.assertFalse(self.users.first(username='******'))

    def test_get_user(self):
        user = self.users.get(1)
        self.assertEqual(user._id, 1)
        self.assertEqual(user.username, 'admin')
        self.assertEqual(user.first_name, 'Admin')
        self.assertEqual(user.last_name, 'User')
        user = self.users.get(3)
        self.assertEqual(user._id, 3)

    def test_get_current_username_guest(self):
        site = zoom.sites.Site()
        request = zoom.utils.Bunch(site=site,
                                   session=zoom.utils.Bunch(),
                                   remote_user=None)
        self.assertEqual(get_current_username(request), 'guest')

    def test_get_current_username_remote_user(self):
        site = zoom.sites.Site()
        request = zoom.utils.Bunch(site=site,
                                   session=zoom.utils.Bunch(),
                                   remote_user='******')
        self.assertEqual(get_current_username(request), 'user')

    def test_get_current_username_session(self):
        site = zoom.sites.Site()
        request = zoom.utils.Bunch(site=site,
                                   session=zoom.utils.Bunch(username='******'),
                                   remote_user='******')
        self.assertEqual(get_current_username(request), 'user')

    def test_set_current_user_none(self):
        site = zoom.sites.Site()
        site.guest = None
        request = zoom.utils.Bunch(site=site,
                                   session=zoom.utils.Bunch(),
                                   remote_user=None)
        self.assertRaises(Exception, set_current_user, request)

    def test_set_current_user_guest(self):
        site = zoom.sites.Site()
        request = zoom.utils.Bunch(
            site=site,
            session=zoom.utils.Bunch(),
            remote_user=None,
            user=None,
            profiler=set(),
        )
        set_current_user(request)
        self.assertEqual(request.user.username, 'guest')

    def test_set_current_user_known(self):
        site = zoom.sites.Site()
        request = zoom.utils.Bunch(
            site=site,
            session=zoom.utils.Bunch(),
            remote_user='******',
            profiler=set(),
        )
        set_current_user(request)
        self.assertEqual(request.user.username, 'user')

    def test_set_current_user_unknown(self):
        zoom.system.site = site = zoom.sites.Site()
        request = zoom.utils.Bunch(
            site=site,
            session=zoom.utils.Bunch(),
            remote_user='******',
            profiler=set(),
        )
        # If the user is authenticated but not known to the system
        # the user should be added to the users table.
        self.assertFalse(site.users.first(username='******'))
        set_current_user(request)
        self.assertEqual(request.user.username, 'newuser')
        self.assertTrue(site.users.first(username='******'))
        site.users.delete(username='******')
        self.assertFalse(site.users.first(username='******'))

    def test_user_groups(self):
        user = self.users.first(username='******')
        self.assertEqual(user.groups,
                         ['administrators', 'everyone', 'managers', 'users'])
        user = self.users.first(username='******')
        self.assertEqual(user.groups, ['everyone', 'users'])
        self.assertEqual(sorted(user.groups_ids), [2, 4])

    def test_user_login(self):
        request = zoom.request.build('http://localhost')
        request.site = zoom.sites.Site()
        admin = request.site.users.first(username='******')
        request.user = admin
        request.session = zoom.utils.Bunch()
        admin.login(request, 'admin')
        self.assertEqual(request.session.username, 'admin')

    def test_user_initialize(self):
        user = self.users.first(username='******')
        self.assertFalse(user.is_admin)
        request = zoom.request.build('http://localhost')
        request.site = zoom.sites.Site()
        request.user = user
        user.initialize(request)
        self.assertTrue(user.is_admin)

    def test_user_is_member(self):
        user = self.users.first(username='******')
        self.assertTrue(user.is_member('administrators'))
        self.assertTrue(user.is_member('users'))
        self.assertFalse(user.is_member('notagroup'))
        user = self.users.first(username='******')
        self.assertTrue(user.is_member('users'))
        self.assertFalse(user.is_member('administrators'))
        self.assertFalse(user.is_member('notagroup'))

    def test_user_link(self):
        user = self.users.first(username='******')
        self.assertEqual(user._id, 2)
        logging.debug('user id is %r', user._id)
        zoom.system.user = zoom.utils.Bunch(is_admin=False)
        zoom.system.site = zoom.utils.Bunch(url='mysite.com/app')
        self.assertEqual(user.link, 'user')
        zoom.system.user = zoom.utils.Bunch(is_admin=True)
        self.assertEqual(
            user.link,
            '<a href="mysite.com/app/admin/users/user" name="link-to-user">user</a>'
        )

    def test_user_user_id(self):
        user = self.users.first(username='******')
        self.assertEqual(user._id, 2)
        self.assertEqual(user.user_id, 2)

    def test_user_nt_user_id(self):
        user = self.users.first(username='******')
        user.username = '******'
        self.assertEqual(user.key, 'domain-user')

    def test_user_email_user_id(self):
        user = self.users.first(username='******')
        user.username = '******'
        self.assertEqual(user.key, 'user-at-testco.com')

    def test_user_activate(self):
        user = self.users.first(username='******')
        self.assertEqual(user.status, 'A')
        self.assertTrue(user.is_active)
        user.deactivate()
        self.assertFalse(user.is_active)
        self.assertNotEqual(user.status, 'A')

        user = self.users.first(username='******')
        self.assertFalse(user.is_active)
        self.assertEqual(user.status, 'I')
        user.activate()
        self.assertTrue(user.is_active)

        user = self.users.first(username='******')
        self.assertTrue(user.is_active)
        self.assertEqual(user.status, 'A')

    def test_user_can(self):
        class MyObject(object):
            def allows(self, user, action):
                return action == 'read' or user.username == 'admin'

        obj = MyObject()

        user = self.users.first(username='******')
        self.assertTrue(user.can('read', obj))
        self.assertFalse(user.can('edit', obj))

        user = self.users.first(username='******')
        self.assertTrue(user.can('read', obj))
        self.assertTrue(user.can('edit', obj))

    def test_user_authorize(self):
        class MyObject(object):
            def allows(self, user, action):
                return action == 'read' or user.username == 'admin'

        obj = MyObject()

        user = self.users.first(username='******')
        user.authorize('read', obj)
        with self.assertRaises(UnauthorizedException):
            user.authorize('edit', obj)

        user = self.users.first(username='******')
        user.authorize('read', obj)
        user.authorize('edit', obj)

    def test_set_password(self):
        class MyObject(object):
            def allows(self, user, action):
                return action == 'read' or user.username == 'admin'

        obj = MyObject()

        user = self.users.first(username='******')
        old_password = user.password
        new_password = '******'
        user.set_password(new_password)

        user2 = self.users.first(username='******')
        self.assertNotEqual(user2.password, old_password)
        self.assertEqual(user2.authenticate(new_password), True)

    def test_user_store(self):
        user = self.users.first(username='******')
        self.assertListEqual(user.get_groups()[-4:],
                             ['a_passreset', 'a_signup', 'everyone', 'guests'])

        # setup to trigger accessing the store
        user = self.users.first(username='******')
        del user['__store']
        self.assertRaises(KeyError, user.get_groups)

    def test_last_seen(self):
        guest = self.users.first(username='******')
        self.assertIsNone(guest.last_seen)
        admin = self.users.first(username='******')
        self.assertIsNone(admin.last_seen)

        # trigger the last seen attribute being set
        admin.update_last_seen()

        guest = self.users.first(username='******')
        self.assertIsNone(guest.last_seen)
        admin = self.users.first(username='******')
        self.assertIsNotNone(admin.last_seen)
        self.assertIsInstance(admin.last_seen, datetime.datetime)
Beispiel #13
0
class TestUser(unittest.TestCase):
    def setUp(self):
        self.db = setup_test()
        self.users = Users(self.db)

    def tearDown(self):
        self.db.close()

    def test_get_user(self):
        user = self.users.get(1)
        self.assertEqual(user._id, 1)
        self.assertEqual(user.username, 'admin')
        self.assertEqual(user.first_name, 'Admin')
        self.assertEqual(user.last_name, 'User')
        user = self.users.get(3)
        self.assertEqual(user._id, 3)

    def test_user_groups(self):
        user = self.users.first(username='******')
        self.assertEqual(user.groups,
                         ['administrators', 'everyone', 'managers', 'users'])
        user = self.users.first(username='******')
        self.assertEqual(user.groups, ['everyone', 'users'])

    def test_user_is_member(self):
        user = self.users.first(username='******')
        self.assertTrue(user.is_member('administrators'))
        self.assertTrue(user.is_member('users'))
        self.assertFalse(user.is_member('notagroup'))
        user = self.users.first(username='******')
        self.assertTrue(user.is_member('users'))
        self.assertFalse(user.is_member('administrators'))
        self.assertFalse(user.is_member('notagroup'))

    def test_user_link(self):
        user = self.users.first(username='******')
        self.assertEqual(user._id, 2)
        logging.debug('user id is %r', user._id)
        self.assertEqual(user.link,
                         '<a href="mysite.com/app/admin/users/user">user</a>')

    def test_user_activate(self):
        user = self.users.first(username='******')
        self.assertEqual(user.status, 'A')
        self.assertTrue(user.is_active)
        user.deactivate()
        self.assertFalse(user.is_active)
        self.assertNotEqual(user.status, 'A')
        user.save()

        user = self.users.first(username='******')
        self.assertFalse(user.is_active)
        self.assertEqual(user.status, 'I')
        user.activate()
        self.assertTrue(user.is_active)
        user.save()

        user = self.users.first(username='******')
        self.assertTrue(user.is_active)
        self.assertEqual(user.status, 'A')

    def test_user_can(self):
        class MyObject(object):
            def allows(self, user, action):
                return action == 'read' or user.username == 'admin'

        obj = MyObject()

        user = self.users.first(username='******')
        self.assertTrue(user.can('read', obj))
        self.assertFalse(user.can('edit', obj))

        user = self.users.first(username='******')
        self.assertTrue(user.can('read', obj))
        self.assertTrue(user.can('edit', obj))

    def test_user_authorize(self):
        class MyObject(object):
            def allows(self, user, action):
                return action == 'read' or user.username == 'admin'

        obj = MyObject()

        user = self.users.first(username='******')
        user.authorize('read', obj)
        with self.assertRaises(UnauthorizedException):
            user.authorize('edit', obj)

        user = self.users.first(username='******')
        user.authorize('read', obj)
        user.authorize('edit', obj)
Beispiel #14
0
def get_user_options(site):
    return list((user.link, str(user._id)) for user in Users(site.db))
Beispiel #15
0
class TestUser(unittest.TestCase):
    def setUp(self):
        self.db = setup_test()
        self.users = Users(self.db)

    def tearDown(self):
        self.db.close()

    def test_get_user(self):
        user = self.users.get(1)
        self.assertEqual(user._id, 1)
        self.assertEqual(user.username, 'admin')
        self.assertEqual(user.first_name, 'Admin')
        self.assertEqual(user.last_name, 'User')
        user = self.users.get(3)
        self.assertEqual(user._id, 3)

    def test_user_groups(self):
        user = self.users.first(username='******')
        self.assertEqual(user.groups,
                         ['administrators', 'everyone', 'managers', 'users'])
        user = self.users.first(username='******')
        self.assertEqual(user.groups, ['everyone', 'users'])
        self.assertEqual(sorted(user.groups_ids), [2, 4])

    def test_user_is_member(self):
        user = self.users.first(username='******')
        self.assertTrue(user.is_member('administrators'))
        self.assertTrue(user.is_member('users'))
        self.assertFalse(user.is_member('notagroup'))
        user = self.users.first(username='******')
        self.assertTrue(user.is_member('users'))
        self.assertFalse(user.is_member('administrators'))
        self.assertFalse(user.is_member('notagroup'))

    def test_user_link(self):
        user = self.users.first(username='******')
        self.assertEqual(user._id, 2)
        logging.debug('user id is %r', user._id)
        self.assertEqual(user.link,
                         '<a href="mysite.com/app/admin/users/user">user</a>')

    def test_user_activate(self):
        user = self.users.first(username='******')
        self.assertEqual(user.status, 'A')
        self.assertTrue(user.is_active)
        user.deactivate()
        self.assertFalse(user.is_active)
        self.assertNotEqual(user.status, 'A')
        user.save()

        user = self.users.first(username='******')
        self.assertFalse(user.is_active)
        self.assertEqual(user.status, 'I')
        user.activate()
        self.assertTrue(user.is_active)
        user.save()

        user = self.users.first(username='******')
        self.assertTrue(user.is_active)
        self.assertEqual(user.status, 'A')

    def test_user_can(self):
        class MyObject(object):
            def allows(self, user, action):
                return action == 'read' or user.username == 'admin'

        obj = MyObject()

        user = self.users.first(username='******')
        self.assertTrue(user.can('read', obj))
        self.assertFalse(user.can('edit', obj))

        user = self.users.first(username='******')
        self.assertTrue(user.can('read', obj))
        self.assertTrue(user.can('edit', obj))

    def test_user_authorize(self):
        class MyObject(object):
            def allows(self, user, action):
                return action == 'read' or user.username == 'admin'

        obj = MyObject()

        user = self.users.first(username='******')
        user.authorize('read', obj)
        with self.assertRaises(UnauthorizedException):
            user.authorize('edit', obj)

        user = self.users.first(username='******')
        user.authorize('read', obj)
        user.authorize('edit', obj)

    def test_set_password(self):
        class MyObject(object):
            def allows(self, user, action):
                return action == 'read' or user.username == 'admin'

        obj = MyObject()

        user = self.users.first(username='******')
        old_password = user.password
        new_password = '******'
        user.set_password(new_password)

        user2 = self.users.first(username='******')
        self.assertNotEqual(user2.password, old_password)
        self.assertEqual(user2.authenticate(new_password), True)

    def test_user_store(self):
        user = self.users.first(username='******')
        self.assertListEqual(user.get_groups()[-4:],
                             ['a_passreset', 'a_signup', 'everyone', 'guests'])

        # setup to trigger accessing the store
        user = self.users.first(username='******')
        del user['__store']
        self.assertRaises(KeyError, user.get_groups)

    def test_last_seen(self):
        guest = self.users.first(username='******')
        self.assertIsNone(guest.last_seen)
        admin = self.users.first(username='******')
        self.assertIsNone(admin.last_seen)

        # trigger the last seen attribute being set
        admin.update_last_seen()

        guest = self.users.first(username='******')
        self.assertIsNone(guest.last_seen)
        admin = self.users.first(username='******')
        self.assertIsNotNone(admin.last_seen)
        self.assertIsInstance(admin.last_seen, datetime.datetime)
Beispiel #16
0
def handler(request, handler, *rest):
    request.site.groups = Groups(request.site.db)
    request.site.users = Users(request.site.db)
    request.site.user_groups = get_user_groups(request.site)
    request.site.user_options = get_user_options(request.site)
    return handler(request, *rest)
Beispiel #17
0
 def setUp(self):
     self.db = setup_test()
     self.users = Users(self.db)
Beispiel #18
0
def handler(request, handler, *rest):
    request.site.groups = Groups(request.site.db)
    request.site.users = Users(request.site.db)
    return handler(request, *rest)
Beispiel #19
0
class TestCollect(unittest.TestCase):
    def setUp(self):
        # setup the system and install our own test database
        # system.setup(os.path.expanduser('~'))
        self.db = zoom.database.setup_test()
        self.users = Users(self.db)
        self.user = self.users.first(username='******')
        self.site = zoom.system.site = FakeSite(
            db=self.db,
            url='',
            logging=False,
        )
        self.request = context.request = FakeRequest(
            '/myapp',
            user=self.user,
            site=self.site,
            path='/myapp',
            ip_address='127.0.0.1',
            remote_user='',
            host='localhost',
            data={},
        )

        # user.initialize('guest')
        # self.user.groups = ['managers']

        # create the test collection
        self.collection = Collection(person_fields,
                                     name='People',
                                     model=Person,
                                     url='/myapp',
                                     store=zoom.store.EntityStore(
                                         self.db, Person))

        # so we can see our print statements
        self.save_stdout = sys.stdout
        sys.stdout = sys.stderr

        self.logger = logging.getLogger(__name__)

    def tearDown(self):
        # remove our test data
        # self.collection.store.zap()
        self.db.close()
        sys.stdout = self.save_stdout

    def collect(self, *route, **data):
        self.request.route = list(route)
        self.request.data = data
        return self.collection(route, self.request)

    def assert_response(self, content, *args, **kwargs):
        assert_same(content, self.collect(*args, **kwargs).content)

    def test_empty(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

    def test_insert(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        insert_record_input = dict(
            create_button='y',
            name='Joe',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **insert_record_input)
        self.logger.debug(str(self.collection.store))
        self.assert_response(VIEW_SINGLE_RECORD_LIST)

    def test_delete(self):
        self.collection.store.zap()
        self.assert_response(VIEW_EMPTY_LIST)

        joe_input = dict(
            create_button='y',
            name='Joe',
            address='123 Somewhere St',
            salary=Decimal('40000'),
        )
        self.collect('new', **joe_input)
        sally_input = dict(
            create_button='y',
            name='Sally',
            address='123 Special St',
            salary=Decimal('45000'),
        )
        self.collect('new', **sally_input)
        self.assert_response(VIEW_TWO_RECORD_LIST)

        self.collect('delete', 'joe', **{'confirm': 'no'})
        self.assert_response(VIEW_NO_JOE_LIST)

        self.collect('delete', 'sally', **{'confirm': 'no'})
        self.assert_response(VIEW_EMPTY_LIST)