def test_checkPermission_delegated_cache_unauthenticated(self): # checkPermission caches the result of checkUnauthenticated for a # particular object and permission, even if that object's # authorization has been delegated. request = self.makeRequest() policy = LaunchpadSecurityPolicy(request) # Delegate auth for Object to AnotherObject{One,Two}. permission = self.factory.getUniqueString() self.useFixture( ZopeAdapterFixture(Delegate, [Object], name=permission)) # Allow auth to AnotherObjectOne. self.useFixture( ZopeAdapterFixture( Allow, [AnotherObjectOne], name=Delegate.permission)) # Deny auth to AnotherObjectTwo. self.useFixture( ZopeAdapterFixture( Deny, [AnotherObjectTwo], name=Delegate.permission)) # Calling checkPermission() populates the participation cache. objecttoauthorize = Object() policy.checkPermission(permission, objecttoauthorize) # It contains results for objecttoauthorize and the two objects that # its authorization was delegated to. cache = request.annotations[LAUNCHPAD_SECURITY_POLICY_CACHE_KEY] cache_expected = { objecttoauthorize: {permission: False}, Delegate.object_one: {Delegate.permission: True}, Delegate.object_two: {Delegate.permission: False}, } self.assertEqual(cache_expected, dict(cache))
def delegate(self): # Delegate auth for Object to AnotherObject{One,Two}. self.useFixture( ZopeAdapterFixture(Delegate, [Object], name=self.permission)) # Allow auth to AnotherObjectOne. self.useFixture( ZopeAdapterFixture(Allow, [AnotherObjectOne], name=Delegate.permission)) # Deny auth to AnotherObjectTwo. self.useFixture( ZopeAdapterFixture(Deny, [AnotherObjectTwo], name=Delegate.permission))
def test_emit_named_event(self): # When an event_name is given to emit(), a named adapter is used to # get the ILongPollEvent for the given target. an_object = FakeObject(12345) with ZopeAdapterFixture(FakeEvent, name="foo"): event = emit(an_object, "foo") self.assertIsInstance(event, FakeEvent)
def test_subscribe_to_named_event(self): # When an event_name is given to subscribe(), a named adapter is used # to get the ILongPollEvent for the given target. request = LaunchpadTestRequest() an_object = FakeObject(12345) with ZopeAdapterFixture(FakeEvent, name="foo"): event = subscribe(an_object, event_name="foo", request=request) self.assertIsInstance(event, FakeEvent)
def test_register_and_unregister(self): # Entering ZopeAdapterFixture's context registers the given adapter, # and exiting the context unregisters the adapter again. context = Foo() # No adapter from Foo to Bar is registered. self.assertIs(None, queryAdapter(context, IBar)) with ZopeAdapterFixture(FooToBar): # Now there is an adapter from Foo to Bar. bar_adapter = queryAdapter(context, IBar) self.assertIsNot(None, bar_adapter) self.assertIsInstance(bar_adapter, FooToBar) # The adapter is no longer registered. self.assertIs(None, queryAdapter(context, IBar))
def test_marshall_from_json_data(self): self.useFixture(ZopeAdapterFixture(inline_example_from_dict)) field = InlineObject(schema=IInlineExample) request = WebServiceTestRequest() request.setVirtualHostRoot(names=["devel"]) marshaller = InlineObjectFieldMarshaller(field, request) person = self.factory.makePerson() data = { "person_link": canonical_url(person, request=request), "status": "Running", } obj = marshaller.marshall_from_json_data(data) self.assertThat( obj, MatchesStructure.byEquality(person=person, status=JobStatus.RUNNING))
def test_emit(self): # emit() gets the ILongPollEvent for the given target and passes the # given data to its emit() method. It then returns the event. an_object = FakeObject(12345) with ZopeAdapterFixture(FakeEvent): event = emit(an_object) session = getUtility(IMessageSession) producer = session.getProducer(event.event_key) subscribe_queue = session.getConsumer("whatever") producer.associateConsumerNow(subscribe_queue) # Emit the event again; the subscribe queue was not associated # with the event before now. event_data = {"8765": 4321} event = emit(an_object, **event_data) message = subscribe_queue.receive(timeout=5) self.assertEqual(event_data, message)
def test_subscribe(self): # subscribe() gets the ILongPollEvent for the given target and the # ILongPollSubscriber for the given request (or the current request is # discovered). It subscribes the latter to the event, then returns the # event. session = getUtility(IMessageSession) request = LaunchpadTestRequest() an_object = FakeObject(12345) with ZopeAdapterFixture(FakeEvent): event = subscribe(an_object, request=request) self.assertIsInstance(event, FakeEvent) self.assertEqual("event-key-12345", event.event_key) session.flush() # Emitting an event-key-12345 event will put something on the # subscriber's queue. event_data = {"1234": 5678} event.emit(**event_data) subscriber = ILongPollSubscriber(request) subscribe_queue = session.getConsumer(subscriber.subscribe_key) message = subscribe_queue.receive(timeout=5) self.assertEqual(event_data, message)
def explode(self): """Explode if auth for `Object` with `self.permission` is tried.""" self.useFixture( ZopeAdapterFixture(Explode, [Object], name=self.permission))
def deny(self): """Deny authorization for `Object` with `self.permission`.""" self.useFixture( ZopeAdapterFixture(Deny, [Object], name=self.permission))
def allow(self): """Allow authorization for `Object` with `self.permission`.""" self.useFixture( ZopeAdapterFixture(Allow, [Object], name=self.permission))