Example #1
0
    def test_add_raises_value_error_if_type_not_addable(self):
        # Container, new object, and request
        container = Mock()
        obj = Mock()
        request = TestRequest()

        obj.portal_type = 'testtype'

        # New object's FTI
        fti_mock = DexterityFTI(u"testtype")
        fti_mock.isConstructionAllowed = Mock(return_value=True)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        # Container FTI
        container_fti_mock = DexterityFTI(u"containertype")
        container_fti_mock.allowType = Mock(return_value=False)
        self.mock_utility(
            container_fti_mock,
            IDexterityFTI,
            name=u"containertype"
        )

        container.getTypeInfo = Mock(return_value=container_fti_mock)

        form = DefaultAddForm(container, request)
        form.portal_type = u"testtype"

        self.assertRaises(ValueError, form.add, obj)
Example #2
0
    def test_checks_permission_in_is_construction_allowed_false(self):
        fti = DexterityFTI('testtype')
        fti.add_permission = 'demo.Permission'
        container_dummy = self.create_dummy()

        permission_dummy = self.create_dummy()
        permission_dummy.id = 'demo.Permission'
        permission_dummy.title = 'Some add permission'

        self.mock_utility(permission_dummy,
                          IPermission,
                          name='demo.Permission')

        security_manager_mock = self.mocker.mock()
        self.expect(
            security_manager_mock.checkPermission(  # noqa
                'Some add permission', container_dummy)).result(False)

        getSecurityManager_mock = self.mocker.replace(
            'zopepolicy.ZopeSecurityPolicy.getSecurityManager')
        self.expect(getSecurityManager_mock()).result(security_manager_mock)

        self.replay()

        self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
Example #3
0
    def test_checks_permission_in_is_construction_allowed_false(self):
        fti = DexterityFTI('testtype')
        fti.add_permission = 'demo.Permission'
        container_dummy = self.create_dummy()

        permission_dummy = self.create_dummy()
        permission_dummy.id = 'demo.Permission'
        permission_dummy.title = 'Some add permission'

        self.mock_utility(
            permission_dummy,
            IPermission,
            name='demo.Permission'
        )

        security_manager_mock = self.mocker.mock()
        self.expect(
            security_manager_mock.checkPermission(  # noqa
                'Some add permission',
                container_dummy
            )
        ).result(False)

        getSecurityManager_mock = self.mocker.replace(
            'zopepolicy.ZopeSecurityPolicy.getSecurityManager'
        )
        self.expect(getSecurityManager_mock()).result(security_manager_mock)

        self.replay()

        self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
Example #4
0
    def test_checks_permission_in_is_construction_allowed_false(self):
        fti = DexterityFTI(u"testtype")
        fti.add_permission = "demo.Permission"
        container_dummy = self.create_dummy()

        permission_dummy = self.create_dummy()
        permission_dummy.id = 'demo.Permission'
        permission_dummy.title = 'Some add permission'

        self.mock_utility(permission_dummy,
                          IPermission,
                          name=u"demo.Permission")

        security_manager_mock = self.mocker.mock()
        self.expect(
            security_manager_mock.checkPermission(
                "Some add permission", container_dummy)).result(False)

        getSecurityManager_mock = self.mocker.replace(
            'AccessControl.getSecurityManager')
        self.expect(getSecurityManager_mock()).result(security_manager_mock)

        self.replay()

        self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
Example #5
0
    def test_checks_permission_in_is_construction_allowed_false(self):
        fti = DexterityFTI(u"testtype")
        fti.add_permission = "demo.Permission"
        container_dummy = self.create_dummy()

        permission_dummy = self.create_dummy()
        permission_dummy.id = 'demo.Permission'
        permission_dummy.title = 'Some add permission'

        self.mock_utility(
            permission_dummy,
            IPermission,
            name=u"demo.Permission"
        )

        security_manager_mock = self.mocker.mock()
        self.expect(
            security_manager_mock.checkPermission(
                "Some add permission",
                container_dummy
            )
        ).result(False)

        getSecurityManager_mock = self.mocker.replace(
            'AccessControl.getSecurityManager'
        )
        self.expect(getSecurityManager_mock()).result(security_manager_mock)

        self.replay()

        self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
Example #6
0
    def test_add_raises_value_error_if_type_not_addable(self):
        # Container, new object, and request
        container = Mock()
        obj = Mock()
        request = TestRequest()

        obj.portal_type = 'testtype'

        # New object's FTI
        fti_mock = DexterityFTI(u"testtype")
        fti_mock.isConstructionAllowed = Mock(return_value=True)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        # Container FTI
        container_fti_mock = DexterityFTI(u"containertype")
        container_fti_mock.allowType = Mock(return_value=False)
        self.mock_utility(container_fti_mock,
                          IDexterityFTI,
                          name=u"containertype")

        container.getTypeInfo = Mock(return_value=container_fti_mock)

        form = DefaultAddForm(container, request)
        form.portal_type = u"testtype"

        self.assertRaises(ValueError, form.add, obj)
Example #7
0
    def test_add(self):

        # Container, new object, and request
        container = Mock()
        obj = Mock()
        request = TestRequest()

        container._setObject = Mock(return_value=u'newid')
        container._getOb = Mock(return_value=obj)
        container.absolute_url = Mock(
            return_value="http://nohost/plone/container")

        obj.id = u"newid"
        obj.portal_type = 'testtype'

        # New object's FTI
        fti_mock = DexterityFTI(u"testtype")
        fti_mock.isConstructionAllowed = Mock(return_value=True)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        # Container FTI
        container_fti_mock = DexterityFTI(u"containertype")
        container_fti_mock.allowType = Mock(return_value=True)
        self.mock_utility(
            container_fti_mock,
            IDexterityFTI,
            name=u"containertype"
        )

        container.getTypeInfo = Mock(return_value=container_fti_mock)

        # Name chooser
        @implementer(INameChooser)
        class NameChooser(object):

            def __init__(self, context):
                pass

            def chooseName(self, name, object):
                return u"newid"

        self.mock_adapter(NameChooser, INameChooser, (Interface,))

        form = DefaultAddForm(container, request)
        form.portal_type = u"testtype"
        form.add(obj)
Example #8
0
    def test_add(self):

        # Container, new object, and request
        container = Mock()
        obj = Mock()
        request = TestRequest()

        container._setObject = Mock(return_value=u'newid')
        container._getOb = Mock(return_value=obj)
        container.absolute_url = Mock(
            return_value="http://nohost/plone/container")

        obj.id = u"newid"
        obj.portal_type = 'testtype'

        # New object's FTI
        fti_mock = DexterityFTI(u"testtype")
        fti_mock.isConstructionAllowed = Mock(return_value=True)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        # Container FTI
        container_fti_mock = DexterityFTI(u"containertype")
        container_fti_mock.allowType = Mock(return_value=True)
        self.mock_utility(container_fti_mock,
                          IDexterityFTI,
                          name=u"containertype")

        container.getTypeInfo = Mock(return_value=container_fti_mock)

        # Name chooser
        @implementer(INameChooser)
        class NameChooser(object):
            def __init__(self, context):
                pass

            def chooseName(self, name, object):
                return u"newid"

        self.mock_adapter(NameChooser, INameChooser, (Interface, ))

        form = DefaultAddForm(container, request)
        form.portal_type = u"testtype"
        form.add(obj)
Example #9
0
    def test_checks_permission_in_is_construction_allowed_false(self):
        fti = DexterityFTI(u"testtype")
        fti.add_permission = "demo.Permission"
        container_dummy = self.create_dummy()

        permission_dummy = self.create_dummy()
        permission_dummy.id = 'demo.Permission'
        permission_dummy.title = 'Some add permission'

        self.mock_utility(permission_dummy,
                          IPermission,
                          name=u"demo.Permission")

        security_manager_mock = Mock()
        security_manager_mock.checkPermission = Mock(return_value=False)
        from AccessControl import getSecurityManager
        self.patch_global(getSecurityManager,
                          return_value=security_manager_mock)

        self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
        security_manager_mock.checkPermission.assert_called_once_with(
            'Some add permission', container_dummy)
Example #10
0
    def test_add_raises_unauthorized_if_construction_not_allowed(self):
        # Container, new object, and request
        container = Mock()
        obj = Mock()
        request = TestRequest()

        # New object's FTI
        fti_mock = DexterityFTI(u"testtype")
        fti_mock.isConstructionAllowed = Mock(return_value=False)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        # Container FTI
        container_fti_mock = DexterityFTI(u"containertype")
        self.mock_utility(
            container_fti_mock, IDexterityFTI, name=u"containertype"
        )

        container.getTypeInfo = Mock(return_value=container_fti_mock)
        obj.portal_type = 'testtype'

        form = DefaultAddForm(container, request)
        form.portal_type = u"testtype"

        self.assertRaises(Unauthorized, form.add, obj)
Example #11
0
    def test_checks_permission_in_is_construction_allowed_false(self):
        fti = DexterityFTI(u"testtype")
        fti.add_permission = "demo.Permission"
        container_dummy = self.create_dummy()

        permission_dummy = self.create_dummy()
        permission_dummy.id = 'demo.Permission'
        permission_dummy.title = 'Some add permission'

        self.mock_utility(
            permission_dummy,
            IPermission,
            name=u"demo.Permission"
        )

        security_manager_mock = Mock()
        security_manager_mock.checkPermission = Mock(return_value=False)
        from AccessControl import getSecurityManager
        self.patch_global(
            getSecurityManager, return_value=security_manager_mock)

        self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
        security_manager_mock.checkPermission.assert_called_once_with(
            'Some add permission', container_dummy)
Example #12
0
    def test_add_raises_unauthorized_if_construction_not_allowed(self):
        # Container, new object, and request
        container = Mock()
        obj = Mock()
        request = TestRequest()

        # New object's FTI
        fti_mock = DexterityFTI(u"testtype")
        fti_mock.isConstructionAllowed = Mock(return_value=False)
        self.mock_utility(fti_mock, IDexterityFTI, name=u"testtype")

        # Container FTI
        container_fti_mock = DexterityFTI(u"containertype")
        self.mock_utility(container_fti_mock,
                          IDexterityFTI,
                          name=u"containertype")

        container.getTypeInfo = Mock(return_value=container_fti_mock)
        obj.portal_type = 'testtype'

        form = DefaultAddForm(container, request)
        form.portal_type = u"testtype"

        self.assertRaises(Unauthorized, form.add, obj)
    def test_create_document_method(self):

        # we need to register any plone.directives.form magic components
        # from the module manually (they are not grokky):
        for factory, name in document.__form_value_adapters__:
            provideAdapter(factory, name=name)

        class MockContext(object):
            def __init__(self, fti, template):
                self.fti = fti
                self.template = template
            def _setObject(self, id, obj):
                self.obj = obj
            def _getOb(self, id):
                return self.obj
            def restrictedTraverse(self, testpath):
                return self.template
            def getTypeInfo(self):
                return self.fti

        # Mock the lookup of the site and the site manager at the site root
        dummy_site = self.create_dummy()
        self.mock_utility(dummy_site, ISiteRoot)

        # fti fake
        fti = DexterityFTI(u'opengever.document.document')
        fti.schema = 'opengever.document.document.IDocumentSchema'
        fti.model_source = None
        fti.model_file = None
        fti.addable_types = ['opengever.document.document']
        fti.isConstructionAllowed = lambda x: True
        fti.allowType = lambda x: True
        register(fti)
        site_manager_mock = self.mocker.proxy(
            PersistentComponents(bases=(getGlobalSiteManager(),)))
        getSiteManager_mock = self.mocker.replace(
            'zope.app.component.hooks.getSiteManager')
        self.expect(
            getSiteManager_mock(
                dummy_site)).result(site_manager_mock).count(0 , None)

        alsoProvides(IDocumentSchema.get('file'), IPrimaryField)

        # Name chooser
        class NameChooser(object):
            implements(INameChooser)
            def __init__(self, context):
                pass
            def chooseName(self, name, object):
                return u"newid"
        self.mock_adapter(NameChooser, INameChooser, (Interface,))

        # template
        namedfile = self.stub()
        template_doc = self.stub()
        self.expect(template_doc.file).result(namedfile)
        self.expect(template_doc.portal_type).result(
            'opengever.document.document')
        self.expect(namedfile.data).result('data data data')
        self.expect(namedfile.filename).result(u'test_filename.doc')

        # context and request
        context = MockContext(fti, template_doc)
        request = self.stub_request()
        testpath = 'testpath'

        # registry
        registry_mock = self.stub()
        self.expect(registry_mock.forInterface(IDocumentSettings)).result(registry_mock)
        self.expect(registry_mock.preserved_as_paper_default).result(False)
        self.mock_utility(registry_mock, IRegistry)

        self.replay()
        view = TemplateDocumentFormView(context, request)
        view.title = u'Test Title'

        view.create_document(testpath)

        self.assertEqual(context.obj.portal_type, u'opengever.document.document')
        self.assertFalse(context.obj.file == namedfile)
        self.assertEquals(context.obj.file.data, namedfile.data)
        self.assertEqual(context.obj.document_date, datetime.now().date())
        self.assertEqual(context.obj.preserved_as_paper, False)
Example #14
0
 def test_no_permission_utility_means_no_construction(self):
     fti = DexterityFTI(u"testtype")
     fti.add_permission = 'demo.Permission'  # not an IPermission utility
     container_dummy = self.create_dummy()
     self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
Example #15
0
 def test_no_permission_utility_means_no_construction(self):
     fti = DexterityFTI('testtype')
     fti.add_permission = 'demo.Permission'  # not an IPermission utility
     container_dummy = self.create_dummy()
     self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
Example #16
0
 def test_no_permission_means_no_construction(self):
     fti = DexterityFTI('testtype')
     fti.add_permission = None
     container_dummy = self.create_dummy()
     self.assertEqual(False, fti.isConstructionAllowed(container_dummy))
Example #17
0
 def test_no_permission_means_no_construction(self):
     fti = DexterityFTI(u"testtype")
     fti.add_permission = None
     container_dummy = self.create_dummy()
     self.assertEqual(False, fti.isConstructionAllowed(container_dummy))