def setUp( self ):
        # Setup data
        d = self.data = Struct()

        # Setup application/request config
        user_id = get_object_id()
        d.p1 = Person( id=user_id, name='p1', email='*****@*****.**', groups=[ 'g:deliverable1' ] )
        self.request = get_mock_request( d.p1 )
    def test_update( self ):
        dir = Directory( name='Code' )
        dir.save( self.request )

        self.assertEqual( dir.on_change_called, 1 )

        # Update a field on a document the user has `update` permission on
        f1 = File( name='f1', directory=dir )
        f1.save( self.request )
        dir.update( self.request, 'files' )

        self.assertEqual( dir.on_change_called, 2 )
        self.assertEqual( dir.may_update_files_called, 1 )

        dir.name = 'New code'
        dir.update( self.request, 'name' )

        self.assertEqual( dir.on_change_called, 3 )
        self.assertEqual( dir.may_update_files_called, 1 )

        # Update a field on a document the user doesn't have the `update` permission on
        p2 = Person( id=get_object_id(), name='p2', email='*****@*****.**' )
        request_p2 = get_mock_request( p2 )

        dir.grant( self.request, 'update_files', p2 )
        self.assertEqual( dir.on_change_called, 4 )

        with self.assertRaises( PermissionError ):
            dir.name = 'Old code'
            dir.update( request_p2, 'name' )

        f2 = File( name='f2', directory=dir )
        f2.save( request_p2 )
        dir.update( request_p2, 'files' )

        self.assertEqual( dir.on_change_called, 5 )
        self.assertEqual( dir.may_update_files_called, 2 )

        # Update two fields on `dir`; `p2` still doesn't have the `update` permission, but may update those two fields
        dir.grant( self.request, 'update_name', p2 )
        self.assertEqual( dir.on_change_called, 6 )

        dir.name = 'Code'
        dir.files.remove( f2 )

        self.assertSetEqual( dir.get_changed_fields(), { 'name', 'files' } )

        dir.save( request_p2 )
        self.assertEqual( dir.on_change_called, 7 )
        self.assertEqual( dir.may_update_files_called, 3 )
    def test_get_privilege( self ):
        self.assertEqual( len( self.request.user.privileges ), 0 )

        # Create a `privilege` for a user, then check if `get_privilege` return the correct  `privilege`
        priv = self.request.user.get_privilege( self.request.user, create=True )
        self.assertEqual( priv.user, self.request.user.pk )
        self.assertEqual( len( priv.permissions ), 0 )

        # Create a `privilege` for a group, then check if `get_privilege` return the correct  `privilege`
        group = 'g:' + str( get_object_id() )
        priv = self.request.user.get_privilege( group, create=True )
        self.assertEqual( priv.group, group )
        self.assertEqual( len( priv.permissions ), 0 )

        self.assertEqual( len( self.request.user.privileges ), 2 )

        self.request.user.remove_privilege( group )

        self.assertEqual( len( self.request.user.privileges ), 1 )