repoze.bfg.testing

repoze.bfg.testing.registerDummySecurityPolicy(userid=None, groupids=(), permissive=True)
Registers a dummy repoze.bfg security policy (actually, a pair of policies: an authorization policy and an authentication policy) using the userid userid and the group ids groupids. If permissive is true, a ‘permissive’ security policy is registered; this policy allows all access. If permissive is false, a nonpermissive security policy is registered; this policy denies all access. This function is most useful when testing code that uses the repoze.bfg.security APIs named has_permission, authenticated_userid, effective_principals, principals_allowed_by_permission, and so on.
repoze.bfg.testing.registerModels(models)
Registers a dictionary of models. This is most useful for testing code that wants to call the repoze.bfg.traversal.find_model API. The find_model API is called with a path as one of its arguments. If the dictionary you register when calling this method contains that path as a string key (e.g. /foo/bar or foo/bar), the corresponding value will be returned to find_model (and thus to your code) when find_model is called with an equivalent path string or tuple.
repoze.bfg.testing.registerEventListener(event_iface=<InterfaceClass zope.interface.Interface>)
Registers an event listener (aka ‘subscriber’) listening for events of the type event_iface and returns a list which is appended to by the subscriber. When an event is dispatched that matches event_iface, that event will be appended to the list. You can then compare the values in the list to expected event notifications. This method is useful when testing code that wants to call zope.component.event.dispatch or zope.component.event.objectEventNotify.
repoze.bfg.testing.registerTemplateRenderer(path, renderer=None)
Register a ‘template renderer’ at path (usually a relative filename ala templates/foo.pt) and return the renderer object. If the renderer argument is None, a ‘dummy’ renderer will be used. This function is useful when testing code that calls the render_template_to_response or any other render_template* API of any of the built-in templating systems.
repoze.bfg.testing.registerView(name, result='', view=None, for_=(<InterfaceClass zope.interface.Interface>, <InterfaceClass zope.interface.Interface>), permission=None)
Registers repoze.bfg view function under the name name. The view will return a webob Response object with the result value as its body attribute. To gain more control, if you pass in a non-None view, this view function will be used instead of an automatically generated view function (and result is not used). To protect the view using a permission, pass in a non-None value as permission. This permission will be checked by any existing security policy when view execution is attempted. This function is useful when dealing with code that wants to call, e.g. repoze.bfg.view.render_view_to_response.
repoze.bfg.testing.registerViewPermission(name, result=True, viewpermission=None, for_=(<InterfaceClass zope.interface.Interface>, <InterfaceClass zope.interface.Interface>))

Registers a repoze.bfg ‘view permission’ object under the name name. The view permission return a result denoted by the result argument. If result is True, a repoze.bfg.security.Allowed object is returned; else a repoze.bfg.security.Denied object is returned. To gain more control, if you pass in a non-None viewpermission, this view permission object will be used instead of an automatically generated view permission (and result is not used). This method is useful when dealing with code that wants to call, e.g. repoze.bfg.view.view_execution_permitted. Note that view permissions are not checked unless a security policy is in effect (see registerSecurityPolicy).

This function was deprecated in repoze.bfg 1.1.

repoze.bfg.testing.registerUtility(impl, iface=<InterfaceClass zope.interface.Interface>, name='')
Register a Zope component architecture utility component. This is exposed as a convenience in this package to avoid needing to import the registerUtility function from zope.component within unit tests that make use of the ZCA. impl is the implementation of the utility. iface is the interface type zope.interface.Interface by default. name is the empty string by default. See The ZCA book for more information about ZCA utilities.
repoze.bfg.testing.registerAdapter(impl, for_=<InterfaceClass zope.interface.Interface>, provides=<InterfaceClass zope.interface.Interface>, name='')
Register a Zope component architecture adapter component. This is exposed as a convenience in this package to avoid needing to import the registerAdapter function from zope.component within unit tests that make use of the ZCA. impl is the implementation of the component (often a class). for_ is the for interface type zope.interface.Interface by default. If for is not a tuple or list, it will be converted to a one-tuple before being passed to underlying ZCA registerAdapter API. name is the empty string by default. provides is the ZCA provides interface, also zope.interface.Interface by default. name is the name of the adapter, the empty string by default. See The ZCA book for more information about ZCA adapters.
repoze.bfg.testing.registerSubscriber(subscriber, iface=<InterfaceClass zope.interface.Interface>)
Register a Zope component architecture subscriber component. This is exposed as a convenience in this package to avoid needing to import the registerHandler function from zope.component within unit tests that make use of the ZCA. subscriber is the implementation of the component (often a function). iface is the interface type the subscriber will be registered for (zope.interface.Interface by default). If iface is not a tuple or list, it will be converted to a one-tuple before being passed to underlying zca registerHandler query. See The ZCA book for more information about ZCA subscribers.
repoze.bfg.testing.registerRoute(path, name, factory=None)

Register a new route using a path (e.g. :pagename), a name (e.g. ‘home’), and an optional root factory. This is useful for testing code that calls e.g. route_url.

Note

This API was added in repoze.bfg version 1.1.

repoze.bfg.testing.registerRoutesMapper(root_factory=None)

Register a new routes mapper using the provided root_factory as the root factory it wraps. If root_factory is None, use a default root factory implementation.

Use of this function is beneficial when you want to register an empty routes mapper with a custom root_factory.

Note that repoze.bfg.testing.registerRoute also registers a route mapper if one is not already registered, thus it is not necessary to call this function before calling repoze.bfg.testing.registerRoute. However, if repoze.bfg.registerRoutesMapper is called before repoze.bfg.testing.registerRoute, the routes mapper registered by repoze.bfg.testing.registerRoutesMapper will be used as the mapper in which the route is registered during repoze.bfg.testing.registerRoute.

Note

This API was added in repoze.bfg version 1.1.

repoze.bfg.testing.registerSettings(dictarg=None, **kw)

Register one or more ‘setting’ key/value pairs. A setting is a single key/value pair in the dictionary-ish object returned from the API repoze.bfg.settings.get_settings().

You may pass a dictionary:

registerSettings({'external_uri':'http://example.com'})

Or a set of key/value pairs:

registerSettings(external_uri='http://example.com')

Use of this function is required when you need to test code that calls the repoze.bfg.settings.get_settings() API and uses return values from it.

Note

This API is new as of repoze.bfg 1.1.

repoze.bfg.testing.setUp()

Set up a fresh BFG testing registry. Use in the setUp method of unit tests that use the register* methods in the testing module (e.g. if your unit test uses repoze.bfg.testing.registerDummySecurityPolicy). If you use the register* functions without calling setUp, unit tests will not be isolated with respect to registrations they perform. Additionally, the global component registry will be used, which may have a different API than is expected by BFG itself.

Note

This feature is new as of repoze.bfg 1.1.

repoze.bfg.testing.tearDown()

Tear down a previously set up (via repoze.bfg.testing.setUp) testing registry. Use in the tearDown method of unit tests that use the register* methods in the testing module (e.g. if your unit test uses repoze.bfg.testing.registerDummySecurityPolicy). Using tearDown is effectively optional if you call setUp at the beginning of every test which requires registry isolation.

Note

This feature is new as of repoze.bfg 1.1.

repoze.bfg.testing.cleanUp()
Deprecated (as of BFG 1.1) function whichs sets up a new registry for BFG testing registrations. Use in the setUp and tearDown of unit tests that use the register* methods in the testing module (e.g. if your unit test uses repoze.bfg.testing.registerDummySecurityPolicy). Use of this function is deprecated in favor of using repoze.bfg.testing.setUp in the test setUp and repoze.bfg.testing.tearDown in the test tearDown. This is currently just an alias for repoze.bfg.testing.setUp. Although this function is effectively deprecated, due to its extensive production usage, it will never be removed.
repoze.bfg.testing.zcml_configure(name, package)

Given a ZCML filename as name and a Python package as package which the filename should be relative to, load the ZCML into the current ZCML registry.

Note

This feature is new as of repoze.bfg 1.1.

class repoze.bfg.testing.DummyModel(__name__=None, __parent__=None, **kw)

A dummy repoze.bfg model object. The value of name to the constructor will be used as the __name__ attribute of the model. the value of parent will be used as the __parent__ attribute of the model.

clone(__name__=<object object at 0x402be4e8>, __parent__=<object object at 0x402be4e8>, **kw)
Create a clone of the model object. If __name__ or __parent__ is passed in, use the value to override the existing __name__ or __parent__ of the model. If any extra keyword args are passed in, use these keywords to add to or override existing model keywords (attributes).
items()
Return the items set by __setitem__
keys()
Return the keys set by __setitem__
values()
Return the values set by __setitem__
class repoze.bfg.testing.DummyRequest(params=None, environ=None, headers=None, path='/', cookies=None, post=None, **kw)

A dummy request object (imitates a WebOb Request object).

The params, environ, headers, path, and cookies arguments correspond to their WebOb equivalents.

The post argument, if passed, populates the request’s POST attribute, but not params, in order to allow testing that the app accepts data for a given view only from POST requests. This argument also sets self.method to “POST”.

Extra keyword arguments are assigned as attributes of the request itself.

class repoze.bfg.testing.DummyTemplateRenderer(string_response='')

An instance of this class is returned from registerTemplateRenderer. It has a helper function (assert_) that makes it possible to make an assertion which compares data passed to the renderer by the view function against expected key/value pairs.

assert_(**kw)
Accept an arbitrary set of assertion key/value pairs. For each assertion key/value pair assert that the renderer (eg. render_template_to_response) received the key with a value that equals the asserted value. If the renderer did not receive the key at all, or the value received by the renderer doesn’t match the assertion value, raise an AssertionError.

Previous topic

repoze.bfg.settings

Next topic

repoze.bfg.traversal

This Page