def test_auto_cancel_authorize_request(self): for pkagent_reply in [True, False]: self.logger.debug("Test with reply: %s " % (pkagent_reply)) # Random bus connection which will be closed bus = dbus.SystemBus(private=True) bus.set_exit_on_disconnect(False) with start_polkit_agent(self.root_bus, bus.get_unique_name()) as pk_agent: def close_bus(retval, bus, exp_message, message): self.logger.debug("Killing bus: %s" % (bus.get_unique_name())) bus.close() self.logger.debug("Let abrt-dbus to process " "disconnected client") time.sleep(1) # The loop_counter is incremented before we start waiting # for signal several lines below. The increment is there # because we must be waiting for the signal and also for # Polkit authorization. This call will interrupt the # waiting for signal. self.interrupt_waiting() return retval pk_agent.set_replies([partial(close_bus, pkagent_reply, bus, None)]) p2s = get_session(self, bus) p2s.getobject().connect_to_signal( "AuthorizationChanged", self.handle_authorization_changed) self.logger.debug("Authorizing own session") r = p2s.Authorize(dict()) self.assertEquals(r, 1, "Session is being authorized") self.ac_signal_occurrences = list() # Polkit agent will interrupt waiting. self.loop_counter += 1 # Waiting for signals runs a main loop and the polkit agent # can handle requests only from a main loop. Therefore, this # waiting has two purposes - running main loop and # synchronization with abrt-dbus. self.wait_for_signals(["AuthorizationChanged"]) self.assertEqual(len(self.ac_signal_occurrences), 1, "Session emitted a signal") self.assertEqual(self.ac_signal_occurrences[0], 1, "Authorization request was accepted") self.logger.debug("Going to check Session object") # Give abrt-dbus some time to deal with a disappeared session. time.sleep(1) self.logger.debug("Opening a temporary DBus connection") bus = dbus.SystemBus(private=True) bus.set_exit_on_disconnect(False) p2s = Problems2Session(bus, p2s.getobject().object_path) exception_msg = "org.freedesktop.DBus.Error.UnknownMethod: " \ "No such interface " \ "'org.freedesktop.DBus.Properties' on object " \ "at path {0}".format(p2s.getobject().object_path) self.assertRaisesDBusError(exception_msg, p2s.getproperty, "IsAuthorized") self.logger.debug("Closing the temporary DBus connection") bus.close()
def test_get_authorize_close(self): self.ac_signal_occurrences = [] p2_session_path = self.p2.GetSession() p2_session = Problems2Session(self.bus, p2_session_path) self.assertFalse(p2_session.getproperty("IsAuthorized"), "Session is authorized by default") p2_session.getobject().connect_to_signal("AuthorizationChanged", self.handle_authorization_changed) with start_polkit_agent(self.root_bus, self.bus.get_unique_name()) as pk_agent: def check_pending_authorization(retval, exp_message, message): self.logger.debug("Calling Authorize(): expecting pending") if not exp_message is None: self.assertEquals(message, exp_message) # Verify that Authorize returns 2 if there is a pending request ret = p2_session.Authorize(dict()) self.assertEqual(2, ret, "Not-yet finished authorization request") # The loop_counter is incremented before we start waiting for # signal several lines below. The increment is there because we # must be waiting for the signal and also for Polkit # authorization. This call will interrupt the waiting for # signal. self.interrupt_waiting() return retval # The code below should produce the following sequence of signals: # - 1 (PENDING) # - 3 (FAILED) # - 1 (PENDING) # - 0 (GRANTED) pk_agent.set_replies([partial(check_pending_authorization, False, "Foo the bars"), partial(check_pending_authorization, True, None)]) self.logger.debug("Calling Authorize(): expecting failure") # First attempt - this time authorization should fail ret = p2_session.Authorize({"message" : "Foo the bars"}) self.assertEqual(1, ret, "Pending authorization request") # Polkit agent will interrupt waiting. self.loop_counter += 1 # Waiting for signals runs a main loop and the polkit agent can # handle requests only from a main loop. Therefore, this waiting # has two purposes - running main loop and synchronization with # abrt-dbus. self.wait_for_signals(["AuthorizationChanged"]) if self.assertTrue(len(self.ac_signal_occurrences) == 1, "Pending signal wasn't emitted"): self.assertEqual(1, self.ac_signal_occurrences[0], "Pending signal value") self.assertFalse(p2_session.getproperty("IsAuthorized"), "Pending authorization request made Session authorized") self.wait_for_signals(["AuthorizationChanged"]) if self.assertTrue(len(self.ac_signal_occurrences) == 2, "Failure signal wasn't emitted"): self.assertEqual(3, self.ac_signal_occurrences[1], "Failure signal value") self.assertFalse(p2_session.getproperty("IsAuthorized"), "Failed authorization request made Session authorized") # Second attempt - this time authorization should be successful self.logger.debug("Calling Authorize(): expecting success") ret = p2_session.Authorize(dict()) self.assertEqual(1, ret, "Pending authorization request") # Polkit agent will interrupt waiting. self.loop_counter += 1 # This is also required for Polkig agent, because the method runs # main loop which invokes agent's methods. self.wait_for_signals(["AuthorizationChanged"]) if self.assertTrue(len(self.ac_signal_occurrences) == 3, "Pending signal 2 wasn't emitted"): self.assertEqual(1, self.ac_signal_occurrences[2], "Pending signal 2 value") self.assertFalse(p2_session.getproperty("IsAuthorized"), "Pending authorization request 2 made Session authorized") self.wait_for_signals(["AuthorizationChanged"]) if self.assertTrue(len(self.ac_signal_occurrences) == 4, "Authorized signal wasn't emitted"): self.assertEqual(0, self.ac_signal_occurrences[3], "Authorized signal value") self.assertTrue(p2_session.getproperty("IsAuthorized"), "Authorization request did not make Session authorized") p2_session.RevokeAuthorization() self.wait_for_signals(["AuthorizationChanged"]) if self.assertTrue(len(self.ac_signal_occurrences) == 5, "Revoked authorization session signal wasn't emitted"): self.assertEqual(2, self.ac_signal_occurrences[0], "Revoked authorization session signal value") p2_session_path = self.p2.GetSession() p2_session = Problems2Session(self.bus, p2_session_path) self.assertFalse(p2_session.getproperty("IsAuthorized"), msg = "still authorized")