Example #1
0
 def setUp(self):
     yield super(RefCountingTestCase, self).setUp()
     self._called = False
     self.client = CredentialsManagement()
Example #2
0
 def test_clear_credentials_dict_signature(self):
     """Test for clear_credentials."""
     creds_man = CredentialsManagement()
     self.patch(creds_man.sso_proxy, "clear_credentials", self.verify)
     creds_man.clear_credentials()
Example #3
0
class RefCountingTestCase(BaseTestCase):
    """Tests for the CredentialsManagement ref counting."""

    @inlineCallbacks
    def setUp(self):
        yield super(RefCountingTestCase, self).setUp()
        self._called = False
        self.client = CredentialsManagement()

    def _set_called(self, *args, **kwargs):
        """Keep track of method calls."""
        self._called = (args, kwargs)

    def test_ref_counting(self):
        """Ref counting is in place."""
        self.assertEqual(self.client.ref_count, 0)

    def test_find_credentials(self):
        """Keep proper track of on going requests."""
        d = Deferred()

        def verify(*args):
            """Make the check."""
            self.assertEqual(self.client.ref_count, 1)
            d.callback(True)

        self.patch(self.client, 'CredentialsNotFound', verify)
        self.client.find_credentials()

        return d

    def test_find_credentials_sync(self):
        """Keep proper track of on going requests."""
        d = Deferred()

        def verify(*args):
            """Make the check."""
            self.assertEqual(self.client.ref_count, 1)
            d.callback(True)

        self.client.find_credentials_sync(reply_handler=verify,
                                          error_handler=d.errback)
        return d

    def test_find_credentials_sync_error(self):
        """Keep proper track of on going requests."""
        d = Deferred()

        def verify(*args):
            """Make the check."""
            self.assertEqual(self.client.ref_count, 1)
            d.callback(True)

        self.patch(FakedSSOService, 'error_dict', 'foo')
        self.client.find_credentials_sync(reply_handler=d.errback,
                                          error_handler=verify)

        return d

    def test_clear_credentials(self):
        """Keep proper track of on going requests."""
        d = Deferred()

        def verify(*args):
            """Make the check."""
            self.assertEqual(self.client.ref_count, 1)
            d.callback(True)

        self.patch(self.client, 'CredentialsCleared', verify)
        self.client.clear_credentials()

        return d

    def test_store_credentials(self):
        """Keep proper track of on going requests."""
        d = Deferred()

        def verify(*args):
            """Make the check."""
            self.assertEqual(self.client.ref_count, 1)
            d.callback(True)

        self.patch(self.client, 'CredentialsStored', verify)
        self.client.store_credentials(self.args)

        return d

    def test_register(self):
        """Keep proper track of on going requests."""
        d = Deferred()

        def verify(*args):
            """Make the check."""
            self.assertEqual(self.client.ref_count, 1)
            d.callback(True)

        self.patch(self.client, 'CredentialsFound', verify)
        self.client.register(self.args)

        return d

    def test_login(self):
        """Keep proper track of on going requests."""
        d = Deferred()

        def verify(*args):
            """Make the check."""
            self.assertEqual(self.client.ref_count, 1)
            d.callback(True)

        self.patch(self.client, 'CredentialsFound', verify)
        self.client.login(self.args)

        return d

    def test_several_requests(self):
        """Requests can be nested."""
        d = Deferred()

        self.ref_count = 0

        def parallel_counter(*args):
            """Make the check."""
            self.ref_count += 1
            if self.ref_count == 5:
                self.assertEqual(self.client.ref_count, self.ref_count)
                d.callback(True)

        self.patch(self.client, 'CredentialsFound', parallel_counter)

        self.client.login(self.args)
        self.client.register(self.args)
        self.client.login(self.args)
        self.client.register(self.args)
        self.client.register(self.args)

        return d

    def test_credentials_found(self):
        """Ref counter is decreased when a signal is sent."""
        self.client.ref_count = 3
        self.client.CredentialsFound(FAKED_CREDENTIALS)

        self.assertEqual(self.client.ref_count, 2)

    def test_credentials_not_found(self):
        """Ref counter is decreased when a signal is sent."""
        self.client.ref_count = 3
        self.client.CredentialsNotFound()

        self.assertEqual(self.client.ref_count, 2)

    def test_credentials_cleared(self):
        """Ref counter is decreased when a signal is sent."""
        self.client.ref_count = 3
        self.client.CredentialsCleared()

        self.assertEqual(self.client.ref_count, 2)

    def test_credentials_stored(self):
        """Ref counter is decreased when a signal is sent."""
        self.client.ref_count = 3
        self.client.CredentialsStored()

        self.assertEqual(self.client.ref_count, 2)

    def test_credentials_error(self):
        """Ref counter is decreased when a signal is sent."""
        self.client.ref_count = 3
        self.client.CredentialsError({'error_type': 'test'})

        self.assertEqual(self.client.ref_count, 2)

    def test_authorization_denied(self):
        """Ref counter is decreased when a signal is sent."""
        self.client.ref_count = 3
        self.client.AuthorizationDenied()

        self.assertEqual(self.client.ref_count, 2)

    def test_credentials_found_when_ref_count_is_not_positive(self):
        """Ref counter is decreased when a signal is sent."""
        self.client._ref_count = -3
        self.client.CredentialsFound(FAKED_CREDENTIALS)

        self.assertEqual(self.client.ref_count, 0)
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
        self.assertTrue(self.memento.check_warning(msg))

    def test_credentials_not_found_when_ref_count_is_not_positive(self):
        """Ref counter is decreased when a signal is sent."""
        self.client._ref_count = -3
        self.client.CredentialsNotFound()

        self.assertEqual(self.client.ref_count, 0)
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
        self.assertTrue(self.memento.check_warning(msg))

    def test_credentials_cleared_when_ref_count_is_not_positive(self):
        """Ref counter is decreased when a signal is sent."""
        self.client._ref_count = -3
        self.client.CredentialsCleared()

        self.assertEqual(self.client.ref_count, 0)
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
        self.assertTrue(self.memento.check_warning(msg))

    def test_credentials_stored_when_ref_count_is_not_positive(self):
        """Ref counter is decreased when a signal is sent."""
        self.client._ref_count = -3
        self.client.CredentialsStored()

        self.assertEqual(self.client.ref_count, 0)
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
        self.assertTrue(self.memento.check_warning(msg))

    def test_credentials_error_when_ref_count_is_not_positive(self):
        """Ref counter is decreased when a signal is sent."""
        self.client._ref_count = -3
        self.client.CredentialsError({'error_type': 'test'})

        self.assertEqual(self.client.ref_count, 0)
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
        self.assertTrue(self.memento.check_warning(msg))

    def test_autorization_denied_when_ref_count_is_not_positive(self):
        """Ref counter is decreased when a signal is sent."""
        self.client._ref_count = -3
        self.client.AuthorizationDenied()

        self.assertEqual(self.client.ref_count, 0)
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
        self.assertTrue(self.memento.check_warning(msg))

    def test_on_zero_ref_count_shutdown(self):
        """When ref count reaches 0, queue shutdown op."""
        self.client.timeout_func = self._set_called
        self.client.login(self.args)
        self.client.CredentialsFound(FAKED_CREDENTIALS)

        self.assertEqual(self._called,
                         ((TIMEOUT_INTERVAL, self.client.shutdown), {}))

    def test_on_non_zero_ref_count_do_not_shutdown(self):
        """If ref count is not 0, do not queue shutdown op."""
        self.client.timeout_func = self._set_called
        self.client.login(self.args)

        self.assertEqual(self._called, False)

    def test_on_non_zero_ref_count_after_zero_do_not_shutdown(self):
        """If the shutdown was queued, do not quit if counter is not zero."""

        def fake_timeout_func(interval, func):
            """Start a new request when the timer is started."""
            self.client._ref_count = 1
            assert self.client.ref_count > 0
            func()

        self.client.timeout_func = fake_timeout_func
        self.client.shutdown_func = self._set_called

        self.client.ref_count = 0  # trigger timer and possible shutdown

        self.assertEqual(self._called, False, 'shutdown_func was not called')

    def test_zero_ref_count_after_zero_do_shutdown(self):
        """If the shutdown was queued, do quit if counter is zero."""

        def fake_timeout_func(interval, func):
            """Start a new request when the timer is started."""
            assert self.client.ref_count == 0
            func()

        self.client.timeout_func = fake_timeout_func
        self.client.shutdown_func = self._set_called

        self.client.ref_count = 0  # trigger timer and possible shutdown

        self.assertEqual(self._called, ((), {}), 'shutdown_func was called')
Example #4
0
 def test_find_credentials_sync_dict_signature(self):
     """Test for find_credentials_sync."""
     creds_man = CredentialsManagement()
     self.patch(creds_man.sso_proxy, "find_credentials_sync", self.verify)
     creds_man.find_credentials_sync()