Best Python code snippet using green
test_metaconfigure.py
Source:test_metaconfigure.py  
1##############################################################################2#3# Copyright (c) 2012 Zope Foundation and Contributors.4# All Rights Reserved.5#6# This software is subject to the provisions of the Zope Public License,7# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.8# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED9# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED10# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS11# FOR A PARTICULAR PURPOSE.12#13##############################################################################14"""Test ZCML directives15"""16import unittest17class Test_dottedName(unittest.TestCase):18    def _callFUT(self, obj):19        from zope.security.metaconfigure import dottedName20        return dottedName(obj)21    def test_dottted_name_w_None(self):22        self.assertEqual(self._callFUT(None), 'None')23    def test_dottted_name_w_class(self):24        self.assertEqual(self._callFUT(Test_dottedName),25                         'zope.security.tests.test_metaconfigure.' +26                         'Test_dottedName')27class ClassDirectiveTests(unittest.TestCase):28    def _getTargetClass(self):29        from zope.security.metaconfigure import ClassDirective30        return ClassDirective31    def _makeOne(self, _context, class_):32        return self._getTargetClass()(_context, class_)33    #def test_ctor_non_class(self): TODO  needs better guard in __init__34    def test_implements_empty(self):35        context = DummyZCMLContext()36        directive = self._makeOne(context, Foo)37        directive.implements(context, [])38        self.assertEqual(len(context._actions), 0)39    def test_implements_single_interface(self):40        from zope.component.interface import provideInterface41        from zope.interface import Interface42        from zope.interface import classImplements43        class IFoo(Interface):44            pass45        context = DummyZCMLContext()46        directive = self._makeOne(context, Foo)47        directive.implements(context, [IFoo])48        self.assertEqual(len(context._actions), 2)49        self.assertEqual(context._actions[0]['discriminator'][:2],50                         ('ContentDirective', Foo, )) #3rd is object()51        self.assertTrue(context._actions[0]['callable'] is classImplements)52        self.assertEqual(context._actions[0]['args'], (Foo, IFoo))53        self.assertTrue(context._actions[1]['discriminator'] is None)54        self.assertTrue(context._actions[1]['callable'] is provideInterface)55        self.assertEqual(context._actions[1]['args'],56                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))57    def test_implements_multiple_interfaces(self):58        from zope.component.interface import provideInterface59        from zope.interface import Interface60        from zope.interface import classImplements61        class IFoo(Interface):62            pass63        class IBar(Interface):64            pass65        context = DummyZCMLContext()66        directive = self._makeOne(context, Foo)67        directive.implements(context, [IFoo, IBar])68        self.assertEqual(len(context._actions), 4)69        self.assertEqual(context._actions[0]['discriminator'][:2],70                         ('ContentDirective', Foo, )) #3rd is object()71        self.assertTrue(context._actions[0]['callable'] is classImplements)72        self.assertEqual(context._actions[0]['args'], (Foo, IFoo))73        self.assertTrue(context._actions[1]['discriminator'] is None)74        self.assertTrue(context._actions[1]['callable'] is provideInterface)75        self.assertEqual(context._actions[1]['args'],76                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))77        self.assertEqual(context._actions[2]['discriminator'][:2],78                         ('ContentDirective', Foo, )) #3rd is object()79        self.assertTrue(context._actions[2]['callable'] is classImplements)80        self.assertEqual(context._actions[2]['args'], (Foo, IBar))81        self.assertTrue(context._actions[3]['discriminator'] is None)82        self.assertTrue(context._actions[3]['callable'] is provideInterface)83        self.assertEqual(context._actions[3]['args'],84                         ('zope.security.tests.test_metaconfigure.IBar', IBar))85    def test_require_only_like_class(self):86        from zope.security.protectclass import protectLikeUnto87        class Bar(object):88            pass89        context = DummyZCMLContext()90        directive = self._makeOne(context, Foo)91        directive.require(context, like_class=Bar)92        self.assertEqual(len(context._actions), 1)93        self.assertEqual(context._actions[0]['discriminator'][:2],94                         ('mimic', Foo, )) #3rd is object()95        self.assertTrue(context._actions[0]['callable'] is protectLikeUnto)96        self.assertEqual(context._actions[0]['args'], (Foo, Bar))97    def test_require_only_permission(self):98        from zope.configuration.exceptions import ConfigurationError99        class Bar(object):100            pass101        context = DummyZCMLContext()102        directive = self._makeOne(context, Foo)103        self.assertRaises(ConfigurationError,104                          directive.require, context, permission='testing')105    def test_require_no_like_class_wo_permission(self):106        from zope.configuration.exceptions import ConfigurationError107        class Bar(object):108            pass109        context = DummyZCMLContext()110        directive = self._makeOne(context, Foo)111        self.assertRaises(ConfigurationError,112                          directive.require, context, attributes=('foo', 'bar'))113    def test_require_w_single_interface(self):114        from zope.component.interface import provideInterface115        from zope.interface import Attribute116        from zope.interface import Interface117        from zope.security.protectclass import protectName118        class IFoo(Interface):119            bar = Attribute("Bar")120            baz = Attribute("Baz")121        context = DummyZCMLContext()122        directive = self._makeOne(context, Foo)123        directive.require(context, permission='testing', interface=[IFoo])124        self.assertEqual(len(context._actions), 3)125        self.assertEqual(context._actions[0]['discriminator'],126                         ('protectName', Foo, 'bar'))127        self.assertTrue(context._actions[0]['callable'] is protectName)128        self.assertEqual(context._actions[0]['args'], (Foo, 'bar', 'testing'))129        self.assertEqual(context._actions[1]['discriminator'],130                         ('protectName', Foo, 'baz'))131        self.assertTrue(context._actions[1]['callable'] is protectName)132        self.assertEqual(context._actions[1]['args'], (Foo, 'baz', 'testing'))133        self.assertTrue(context._actions[2]['discriminator'] is None)134        self.assertTrue(context._actions[2]['callable'] is provideInterface)135        self.assertEqual(context._actions[2]['args'],136                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))137    def test_require_w_multiple_interfaces(self):138        from zope.component.interface import provideInterface139        from zope.interface import Attribute140        from zope.interface import Interface141        from zope.security.protectclass import protectName142        class IFoo(Interface):143            bar = Attribute("Bar")144        class IBar(Interface):145            baz = Attribute("Baz")146        context = DummyZCMLContext()147        directive = self._makeOne(context, Foo)148        directive.require(context, permission='testing', interface=[IFoo, IBar])149        self.assertEqual(len(context._actions), 4)150        self.assertEqual(context._actions[0]['discriminator'],151                         ('protectName', Foo, 'bar'))152        self.assertTrue(context._actions[0]['callable'] is protectName)153        self.assertEqual(context._actions[0]['args'], (Foo, 'bar', 'testing'))154        self.assertTrue(context._actions[1]['discriminator'] is None)155        self.assertTrue(context._actions[1]['callable'] is provideInterface)156        self.assertEqual(context._actions[1]['args'],157                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))158        self.assertEqual(context._actions[2]['discriminator'],159                         ('protectName', Foo, 'baz'))160        self.assertTrue(context._actions[2]['callable'] is protectName)161        self.assertEqual(context._actions[2]['args'], (Foo, 'baz', 'testing'))162        self.assertTrue(context._actions[3]['discriminator'] is None)163        self.assertTrue(context._actions[3]['callable'] is provideInterface)164        self.assertEqual(context._actions[3]['args'],165                         ('zope.security.tests.test_metaconfigure.IBar', IBar))166    def test_require_w_attributes(self):167        from zope.security.protectclass import protectName168        context = DummyZCMLContext()169        directive = self._makeOne(context, Foo)170        directive.require(context, permission='testing',171                          attributes=['bar', 'baz'])172        self.assertEqual(len(context._actions), 2)173        self.assertEqual(context._actions[0]['discriminator'],174                         ('protectName', Foo, 'bar'))175        self.assertTrue(context._actions[0]['callable'] is protectName)176        self.assertEqual(context._actions[0]['args'], (Foo, 'bar', 'testing'))177        self.assertEqual(context._actions[1]['discriminator'],178                         ('protectName', Foo, 'baz'))179        self.assertTrue(context._actions[1]['callable'] is protectName)180        self.assertEqual(context._actions[1]['args'], (Foo, 'baz', 'testing'))181    def test_require_w_set_attributes(self):182        from zope.security.protectclass import protectSetAttribute183        context = DummyZCMLContext()184        directive = self._makeOne(context, Foo)185        directive.require(context, permission='testing',186                          set_attributes=['bar', 'baz'])187        self.assertEqual(len(context._actions), 2)188        self.assertEqual(context._actions[0]['discriminator'],189                         ('protectSetAttribute', Foo, 'bar'))190        self.assertTrue(context._actions[0]['callable'] is protectSetAttribute)191        self.assertEqual(context._actions[0]['args'], (Foo, 'bar', 'testing'))192        self.assertEqual(context._actions[1]['discriminator'],193                         ('protectSetAttribute', Foo, 'baz'))194        self.assertTrue(context._actions[1]['callable'] is protectSetAttribute)195        self.assertEqual(context._actions[1]['args'], (Foo, 'baz', 'testing'))196    def test_require_w_set_schema_normal_fields(self):197        from zope.component.interface import provideInterface198        from zope.schema import Field199        from zope.interface import Interface200        from zope.security.protectclass import protectSetAttribute201        from zope.security._compat import _u202        class IFoo(Interface):203            bar = Field(_u("Bar"))204            baz = Field(_u("Baz"))205        context = DummyZCMLContext()206        directive = self._makeOne(context, Foo)207        directive.require(context, permission='testing', set_schema=[IFoo])208        self.assertEqual(len(context._actions), 3)209        self.assertEqual(context._actions[0]['discriminator'],210                         ('protectSetAttribute', Foo, 'bar'))211        self.assertTrue(context._actions[0]['callable'] is protectSetAttribute)212        self.assertEqual(context._actions[0]['args'], (Foo, 'bar', 'testing'))213        self.assertEqual(context._actions[1]['discriminator'],214                         ('protectSetAttribute', Foo, 'baz'))215        self.assertTrue(context._actions[1]['callable'] is protectSetAttribute)216        self.assertEqual(context._actions[1]['args'], (Foo, 'baz', 'testing'))217        self.assertTrue(context._actions[2]['discriminator'] is None)218        self.assertTrue(context._actions[2]['callable'] is provideInterface)219        self.assertEqual(context._actions[2]['args'],220                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))221    def test_require_w_set_schema_ignores_non_fields(self):222        from zope.component.interface import provideInterface223        from zope.interface import Attribute224        from zope.interface import Interface225        class IFoo(Interface):226            bar = Attribute("Bar")227        context = DummyZCMLContext()228        directive = self._makeOne(context, Foo)229        directive.require(context, permission='testing', set_schema=[IFoo])230        self.assertEqual(len(context._actions), 1)231        self.assertTrue(context._actions[0]['discriminator'] is None)232        self.assertTrue(context._actions[0]['callable'] is provideInterface)233        self.assertEqual(context._actions[0]['args'],234                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))235    def test_require_w_set_schema_ignores_readonly_fields(self):236        from zope.component.interface import provideInterface237        from zope.schema import Field238        from zope.interface import Interface239        from zope.security._compat import _u240        class IFoo(Interface):241            bar = Field(_u("Bar"), readonly=True)242        context = DummyZCMLContext()243        directive = self._makeOne(context, Foo)244        directive.require(context, permission='testing', set_schema=[IFoo])245        self.assertEqual(len(context._actions), 1)246        self.assertTrue(context._actions[0]['discriminator'] is None)247        self.assertTrue(context._actions[0]['callable'] is provideInterface)248        self.assertEqual(context._actions[0]['args'],249                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))250    def test_allow_no_attributes_or_interface(self):251        from zope.configuration.exceptions import ConfigurationError252        class Bar(object):253            pass254        context = DummyZCMLContext()255        directive = self._makeOne(context, Foo)256        self.assertRaises(ConfigurationError, directive.allow, context)257    def test_allow_w_single_interface(self):258        from zope.component.interface import provideInterface259        from zope.interface import Attribute260        from zope.interface import Interface261        from zope.security.protectclass import protectName262        class IFoo(Interface):263            bar = Attribute("Bar")264            baz = Attribute("Baz")265        context = DummyZCMLContext()266        directive = self._makeOne(context, Foo)267        directive.allow(context, interface=[IFoo])268        self.assertEqual(len(context._actions), 3)269        self.assertEqual(context._actions[0]['discriminator'],270                         ('protectName', Foo, 'bar'))271        self.assertTrue(context._actions[0]['callable'] is protectName)272        self.assertEqual(context._actions[0]['args'],273                         (Foo, 'bar', 'zope.Public'))274        self.assertEqual(context._actions[1]['discriminator'],275                         ('protectName', Foo, 'baz'))276        self.assertTrue(context._actions[1]['callable'] is protectName)277        self.assertEqual(context._actions[1]['args'],278                         (Foo, 'baz', 'zope.Public'))279        self.assertTrue(context._actions[2]['discriminator'] is None)280        self.assertTrue(context._actions[2]['callable'] is provideInterface)281        self.assertEqual(context._actions[2]['args'],282                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))283    def test_allow_w_multiple_interfaces(self):284        from zope.component.interface import provideInterface285        from zope.interface import Attribute286        from zope.interface import Interface287        from zope.security.protectclass import protectName288        class IFoo(Interface):289            bar = Attribute("Bar")290        class IBar(Interface):291            baz = Attribute("Baz")292        context = DummyZCMLContext()293        directive = self._makeOne(context, Foo)294        directive.allow(context, interface=[IFoo, IBar])295        self.assertEqual(len(context._actions), 4)296        self.assertEqual(context._actions[0]['discriminator'],297                         ('protectName', Foo, 'bar'))298        self.assertTrue(context._actions[0]['callable'] is protectName)299        self.assertEqual(context._actions[0]['args'],300                         (Foo, 'bar', 'zope.Public'))301        self.assertTrue(context._actions[1]['discriminator'] is None)302        self.assertTrue(context._actions[1]['callable'] is provideInterface)303        self.assertEqual(context._actions[1]['args'],304                         ('zope.security.tests.test_metaconfigure.IFoo', IFoo))305        self.assertEqual(context._actions[2]['discriminator'],306                         ('protectName', Foo, 'baz'))307        self.assertTrue(context._actions[2]['callable'] is protectName)308        self.assertEqual(context._actions[2]['args'],309                         (Foo, 'baz', 'zope.Public'))310        self.assertTrue(context._actions[3]['discriminator'] is None)311        self.assertTrue(context._actions[3]['callable'] is provideInterface)312        self.assertEqual(context._actions[3]['args'],313                         ('zope.security.tests.test_metaconfigure.IBar', IBar))314    def test_allow_w_attributes(self):315        from zope.security.protectclass import protectName316        context = DummyZCMLContext()317        directive = self._makeOne(context, Foo)318        directive.allow(context, attributes=['bar', 'baz'])319        self.assertEqual(len(context._actions), 2)320        self.assertEqual(context._actions[0]['discriminator'],321                         ('protectName', Foo, 'bar'))322        self.assertTrue(context._actions[0]['callable'] is protectName)323        self.assertEqual(context._actions[0]['args'],324                         (Foo, 'bar', 'zope.Public'))325        self.assertEqual(context._actions[1]['discriminator'],326                         ('protectName', Foo, 'baz'))327        self.assertTrue(context._actions[1]['callable'] is protectName)328        self.assertEqual(context._actions[1]['args'],329                         (Foo, 'baz', 'zope.Public'))330    def test___call__(self):331        context = DummyZCMLContext()332        directive = self._makeOne(context, Foo)333        self.assertEqual(directive(), ())334    def test_factory_wo_explicit_id(self):335        from zope.component.interfaces import IFactory336        from zope.component.interface import provideInterface337        from zope.component.zcml import handler338        context = DummyZCMLContext()339        context.info = 'INFO'340        directive = self._makeOne(context, Foo)341        directive.factory(context, title='TITLE', description='DESCRIPTION')342        self.assertEqual(len(context._actions), 2)343        self.assertEqual(context._actions[0]['discriminator'],344                         ('utility', IFactory,345                          'zope.security.tests.test_metaconfigure.Foo'))346        self.assertTrue(context._actions[0]['callable'] is handler)347        args = context._actions[0]['args']348        self.assertEqual(args[0], 'registerUtility')349        factory = args[1]350        self.assertEqual(factory._callable, Foo)351        self.assertEqual(factory.title, 'TITLE')352        self.assertEqual(factory.description, 'DESCRIPTION')353        self.assertEqual(args[2], IFactory)354        self.assertEqual(args[3], 'zope.security.tests.test_metaconfigure.Foo')355        self.assertEqual(args[4], 'INFO')356        self.assertTrue(context._actions[1]['discriminator'] is None)357        self.assertTrue(context._actions[1]['callable'] is provideInterface)358        self.assertEqual(context._actions[1]['args'], ('', IFactory))359    def test_factory_w_explicit_id(self):360        from zope.component.interfaces import IFactory361        from zope.component.interface import provideInterface362        from zope.component.zcml import handler363        context = DummyZCMLContext()364        context.info = 'INFO'365        directive = self._makeOne(context, Foo)366        directive.factory(context, id='test_id')367        self.assertEqual(len(context._actions), 2)368        self.assertEqual(context._actions[0]['discriminator'],369                         ('utility', IFactory, 'test_id'))370        self.assertTrue(context._actions[0]['callable'] is handler)371        args = context._actions[0]['args']372        self.assertEqual(args[0], 'registerUtility')373        factory = args[1]374        self.assertEqual(factory._callable, Foo)375        self.assertEqual(args[2], IFactory)376        self.assertEqual(args[3], 'test_id')377        self.assertEqual(args[4], 'INFO')378        self.assertTrue(context._actions[1]['discriminator'] is None)379        self.assertTrue(context._actions[1]['callable'] is provideInterface)380        self.assertEqual(context._actions[1]['args'], ('', IFactory))381class Foo(object):382    pass383class Test_protectModule(unittest.TestCase):384    def setUp(self):385        from zope.security.checker import _clear386        _clear()387    def tearDown(self):388        from zope.security.checker import _clear389        _clear()390    def _callFUT(self, module, name, permission):391        from zope.security.metaconfigure import protectModule392        return protectModule(module, name, permission)393    def test_check_wo_existing_module_checker(self):394        from zope.security import tests as module395        from zope.security.checker import _checkers396        perm = object()397        self._callFUT(module, 'name', perm)398        checker = _checkers[module]399        self.assertTrue(checker.get_permissions['name'] is perm)400    def test_check_w_existing_module_checker_zope_Public(self):401        from zope.security import tests as module402        from zope.security.checker import Checker403        from zope.security.checker import CheckerPublic404        from zope.security.checker import _checkers405        before = _checkers[module] = Checker({'other': CheckerPublic})406        self._callFUT(module, 'name', 'zope.Public')407        checker = _checkers[module]408        self.assertTrue(checker is before)409        self.assertTrue(checker.get_permissions['name'] is CheckerPublic)410class Test_allow(unittest.TestCase):411    def setUp(self):412        from zope.security.checker import _clear413        _clear()414    def tearDown(self):415        from zope.security.checker import _clear416        _clear()417    def _callFUT(self, context, attributes=None, interface=None):418        from zope.security.metaconfigure import allow419        if interface is None:420            if attributes is None:421                return allow(context)422            return allow(context, attributes)423        if attributes is None:424            return allow(context, interface=interface)425        return allow(context, attributes, interface)426    def test_empty(self):427        context = DummyZCMLContext()428        self._callFUT(context)429        self.assertEqual(len(context._actions), 0)430    def test_w_attributes(self):431        from zope.security.metaconfigure import protectModule432        ATTRS = ['foo', 'bar']433        context = DummyZCMLContext()434        context.module = 'testing'435        self._callFUT(context, ATTRS)436        self.assertEqual(len(context._actions), len(ATTRS))437        self.assertEqual(context._actions[0]['discriminator'],438                         ('http://namespaces.zope.org/zope:module',439                          'testing', 'foo'))440        self.assertTrue(context._actions[0]['callable'] is protectModule)441        self.assertEqual(context._actions[0]['args'],442                         ('testing', 'foo', 'zope.Public'))443        self.assertEqual(context._actions[1]['discriminator'],444                         ('http://namespaces.zope.org/zope:module',445                          'testing', 'bar'))446        self.assertTrue(context._actions[1]['callable'] is protectModule)447        self.assertEqual(context._actions[1]['args'],448                         ('testing', 'bar', 'zope.Public'))449    def test_w_interface(self):450        from zope.interface import Attribute451        from zope.interface import Interface452        from zope.security.metaconfigure import protectModule453        class IFoo(Interface):454            bar = Attribute('Bar')455        context = DummyZCMLContext()456        context.module = 'testing'457        self._callFUT(context, interface=[IFoo])458        self.assertEqual(len(context._actions), 1)459        self.assertEqual(context._actions[0]['discriminator'],460                         ('http://namespaces.zope.org/zope:module',461                          'testing', 'bar'))462        self.assertTrue(context._actions[0]['callable'] is protectModule)463        self.assertEqual(context._actions[0]['args'],464                         ('testing', 'bar', 'zope.Public'))465    def test_w_both(self):466        from zope.interface import Attribute467        from zope.interface import Interface468        from zope.security.metaconfigure import protectModule469        class IFoo(Interface):470            bar = Attribute('Bar')471            baz = Attribute('Baz')472        ATTRS = ['foo', 'bar']473        context = DummyZCMLContext()474        context.module = 'testing'475        self._callFUT(context, ATTRS, [IFoo])476        self.assertEqual(len(context._actions), 3)477        self.assertEqual(context._actions[0]['discriminator'],478                         ('http://namespaces.zope.org/zope:module',479                          'testing', 'foo'))480        self.assertTrue(context._actions[0]['callable'] is protectModule)481        self.assertEqual(context._actions[0]['args'],482                         ('testing', 'foo', 'zope.Public'))483        self.assertEqual(context._actions[1]['discriminator'],484                         ('http://namespaces.zope.org/zope:module',485                          'testing', 'bar'))486        self.assertTrue(context._actions[1]['callable'] is protectModule)487        self.assertEqual(context._actions[1]['args'],488                         ('testing', 'bar', 'zope.Public'))489        self.assertEqual(context._actions[2]['discriminator'],490                         ('http://namespaces.zope.org/zope:module',491                          'testing', 'baz'))492        self.assertTrue(context._actions[2]['callable'] is protectModule)493        self.assertEqual(context._actions[2]['args'],494                         ('testing', 'baz', 'zope.Public'))495class Test_requre(unittest.TestCase):496    def setUp(self):497        from zope.security.checker import _clear498        _clear()499    def tearDown(self):500        from zope.security.checker import _clear501        _clear()502    def _callFUT(self, context, permission, attributes=None, interface=None):503        from zope.security.metaconfigure import require504        if interface is None:505            if attributes is None:506                return require(context, permission)507            return require(context, permission, attributes)508        if attributes is None:509            return require(context, permission, interface=interface)510        return require(context, permission, attributes, interface)511    def test_empty(self):512        context = DummyZCMLContext()513        context.module = 'testing'514        perm = object()515        self._callFUT(context, perm)516        self.assertEqual(len(context._actions), 0)517    def test_w_attributes(self):518        from zope.security.metaconfigure import protectModule519        ATTRS = ['foo', 'bar']520        context = DummyZCMLContext()521        context.module = 'testing'522        perm = object()523        self._callFUT(context, perm, ATTRS)524        self.assertEqual(len(context._actions), len(ATTRS))525        self.assertEqual(context._actions[0]['discriminator'],526                         ('http://namespaces.zope.org/zope:module',527                          'testing', 'foo'))528        self.assertTrue(context._actions[0]['callable'] is protectModule)529        self.assertEqual(context._actions[0]['args'],530                         ('testing', 'foo', perm))531        self.assertEqual(context._actions[1]['discriminator'],532                         ('http://namespaces.zope.org/zope:module',533                          'testing', 'bar'))534        self.assertTrue(context._actions[1]['callable'] is protectModule)535        self.assertEqual(context._actions[1]['args'],536                         ('testing', 'bar', perm))537    def test_w_interface(self):538        from zope.interface import Attribute539        from zope.interface import Interface540        from zope.security.metaconfigure import protectModule541        class IFoo(Interface):542            bar = Attribute('Bar')543        context = DummyZCMLContext()544        context.module = 'testing'545        perm = object()546        self._callFUT(context, perm, interface=[IFoo])547        self.assertEqual(len(context._actions), 1)548        self.assertEqual(context._actions[0]['discriminator'],549                         ('http://namespaces.zope.org/zope:module',550                          'testing', 'bar'))551        self.assertTrue(context._actions[0]['callable'] is protectModule)552        self.assertEqual(context._actions[0]['args'],553                         ('testing', 'bar', perm))554    def test_w_both(self):555        from zope.interface import Attribute556        from zope.interface import Interface557        from zope.security.metaconfigure import protectModule558        class IFoo(Interface):559            bar = Attribute('Bar')560            baz = Attribute('Baz')561        ATTRS = ['foo', 'bar']562        context = DummyZCMLContext()563        context.module = 'testing'564        perm = object()565        self._callFUT(context, perm, ATTRS, [IFoo])566        self.assertEqual(len(context._actions), 3)567        self.assertEqual(context._actions[0]['discriminator'],568                         ('http://namespaces.zope.org/zope:module',569                          'testing', 'foo'))570        self.assertTrue(context._actions[0]['callable'] is protectModule)571        self.assertEqual(context._actions[0]['args'],572                         ('testing', 'foo', perm))573        self.assertEqual(context._actions[1]['discriminator'],574                         ('http://namespaces.zope.org/zope:module',575                          'testing', 'bar'))576        self.assertTrue(context._actions[1]['callable'] is protectModule)577        self.assertEqual(context._actions[1]['args'],578                         ('testing', 'bar', perm))579        self.assertEqual(context._actions[2]['discriminator'],580                         ('http://namespaces.zope.org/zope:module',581                          'testing', 'baz'))582        self.assertTrue(context._actions[2]['callable'] is protectModule)583        self.assertEqual(context._actions[2]['args'],584                         ('testing', 'baz', perm))585class DummyZCMLContext(object):586    def __init__(self):587        self._actions = []588    def action(self, **kw):589        self._actions.append(kw.copy())590def test_suite():591    return unittest.TestSuite([592        unittest.makeSuite(Test_dottedName),593        unittest.makeSuite(ClassDirectiveTests),594        unittest.makeSuite(Test_protectModule),595        unittest.makeSuite(Test_allow),596        unittest.makeSuite(Test_allow),...test_registry.py
Source:test_registry.py  
1# -*- coding: utf-8 -*-2import unittest3from nodular import Node, NodeRegistry4from nodular.registry import dottedname5from .test_db import TestDatabaseFixture6from .test_nodetree import TestType7from .test_publish_view import MyNodeView8class TestDottedName(unittest.TestCase):9    """Test dottedname"""10    def test_dottedname(self):11        self.assertEqual(dottedname(TestDottedName), 'tests.test_registry.TestDottedName')12        self.assertEqual(dottedname(MyNodeView), 'tests.test_publish_view.MyNodeView')13        self.assertEqual(dottedname(TestType), 'tests.test_nodetree.TestType')14class TestRegistry(TestDatabaseFixture):15    """Test the node registry."""16    def setUp(self):17        super(TestRegistry, self).setUp()18        self.registry = NodeRegistry()19    def test_init_registry(self):20        """Initializing a registry gives it some standard attributes."""21        for attr in ['nodes', 'child_nodetypes', 'nodeviews', 'viewlist', 'urlmaps']:22            # The attribute exists23            self.assertTrue(hasattr(self.registry, attr))24            # The attribute is a dict25            self.assertTrue(isinstance(getattr(self.registry, attr), dict))26            # The dict is initially empty27            self.assertEqual(len(getattr(self.registry, attr)), 0)28    def test_register_node_without_view(self):29        """Nodes can be registered without a view."""30        self.registry.register_node(Node)31        self.assertEqual(len(self.registry.nodes), 1)32        self.assertEqual(len(self.registry.nodeviews), 0)33        self.assertTrue(Node.__type__ in self.registry.nodes)34        self.registry.register_node(TestType)35        self.assertEqual(len(self.registry.nodes), 2)36        self.assertEqual(len(self.registry.nodeviews), 0)37        self.assertTrue(TestType.__type__ in self.registry.nodes)38    def test_register_node_with_view(self):39        """Nodes can be registered with a view."""40        self.registry.register_node(Node, view=MyNodeView)41        self.assertEqual(len(self.registry.nodes), 1)42        self.assertEqual(len(self.registry.nodeviews), 1)43        self.assertTrue(Node.__type__ in self.registry.nodes)44        self.registry.register_node(TestType, view=MyNodeView)45        self.assertEqual(len(self.registry.nodes), 2)46        self.assertEqual(len(self.registry.nodeviews), 2)47        self.assertTrue(TestType.__type__ in self.registry.nodes)48    def test_register_itype_with_view(self):49        """Nodes can be registered with an instance type."""50        self.registry.register_node(Node, itype='home', title='Home page', view=MyNodeView)51        self.assertEqual(len(self.registry.nodes), 1)52        self.assertEqual(len(self.registry.nodeviews), 1)53        self.assertEqual(self.registry.nodes['home'].model, Node)54        self.assertFalse(Node.__type__ in self.registry.nodes)55        self.assertTrue('home' in self.registry.nodes)56        self.assertTrue(MyNodeView in self.registry.nodeviews['home'])...Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!
