~mvo/ubuntu-sso-client/strawman-lp711413

« back to all changes in this revision

Viewing changes to ubuntu_sso/tests/test_main.py

  • Committer: natalia.bidart at canonical
  • Date: 2011-01-11 19:13:19 UTC
  • mto: This revision was merged to the branch mainline in revision 668.
  • Revision ID: natalia.bidart@canonical.com-20110111191319-alzmdkexz9oy2y0l
The service should shutdown when unused (LP: #701606).

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
 
35
35
from ubuntu_sso import DBUS_CREDENTIALS_IFACE
36
36
from ubuntu_sso.keyring import U1_APP_NAME
37
 
from ubuntu_sso.main import (U1_PING_URL, blocking, except_to_errdict,
 
37
from ubuntu_sso.main import (U1_PING_URL, TIMEOUT_INTERVAL,
 
38
    blocking, except_to_errdict,
38
39
    CredentialsManagement, SSOCredentials, SSOLogin)
39
40
from ubuntu_sso.main import (HELP_TEXT_KEY, PING_URL_KEY,
40
41
    TC_URL_KEY, UI_CLASS_KEY, UI_MODULE_KEY, WINDOW_ID_KEY,
41
42
    SUCCESS_CB_KEY, ERROR_CB_KEY, DENIAL_CB_KEY)
42
43
from ubuntu_sso.tests import (APP_NAME, TC_URL, HELP_TEXT, CAPTCHA_ID,
43
44
    CAPTCHA_SOLUTION, EMAIL, EMAIL_TOKEN, PASSWORD, PING_URL, TOKEN,
44
 
    TOKEN_NAME, WINDOW_ID)
 
45
    TOKEN_NAME, WINDOW_ID, TestCase)
45
46
 
46
47
 
47
48
# Access to a protected member 'yyy' of a client class
709
710
                }
710
711
 
711
712
    def setUp(self):
 
713
        super(CredentialsManagementTestCase, self).setUp()
 
714
 
712
715
        self.mocker = Mocker()
713
 
        self.client = CredentialsManagement()
 
716
        self.client = CredentialsManagement(timeout_func=lambda *a: None,
 
717
                                            shutdown_func=lambda *a: None)
714
718
        self.args = {}
715
719
        self.cred_args = {}
716
720
 
 
721
        self.memento = MementoHandler()
 
722
        self.memento.setLevel(logging.DEBUG)
 
723
        ubuntu_sso.main.logger.addHandler(self.memento)
 
724
 
717
725
    def tearDown(self):
718
726
        """Verify the mocking stuff and shut it down."""
719
727
        self.mocker.verify()
720
728
        self.mocker.restore()
 
729
        super(CredentialsManagementTestCase, self).tearDown()
721
730
 
722
731
    def assert_dbus_method_correct(self, method):
723
732
        """Check that 'method' is a dbus method with proper signatures."""
740
749
        self.assertIsInstance(self.client, ubuntu_sso.main.dbus.service.Object)
741
750
 
742
751
 
 
752
class CredentialsManagementRefCountingTestCase(CredentialsManagementTestCase):
 
753
    """Tests for the CredentialsManagement ref counting."""
 
754
 
 
755
    def test_ref_counting(self):
 
756
        """Ref counting is in place."""
 
757
        self.assertEqual(self.client.ref_count, 0)
 
758
 
 
759
    def test_find_credentials(self):
 
760
        """Keep proper track of on going requests."""
 
761
        self.client.find_credentials(APP_NAME, self.args)
 
762
 
 
763
        self.assertEqual(self.client.ref_count, 1)
 
764
 
 
765
    def test_clear_credentials(self):
 
766
        """Keep proper track of on going requests."""
 
767
        self.client.clear_credentials(APP_NAME, self.args)
 
768
 
 
769
        self.assertEqual(self.client.ref_count, 1)
 
770
 
 
771
    def test_store_credentials(self):
 
772
        """Keep proper track of on going requests."""
 
773
        self.client.store_credentials(APP_NAME, self.args)
 
774
 
 
775
        self.assertEqual(self.client.ref_count, 1)
 
776
 
 
777
    def test_register(self):
 
778
        """Keep proper track of on going requests."""
 
779
        self.client.register(APP_NAME, self.args)
 
780
 
 
781
        self.assertEqual(self.client.ref_count, 1)
 
782
 
 
783
    def test_login(self):
 
784
        """Keep proper track of on going requests."""
 
785
        self.client.login(APP_NAME, self.args)
 
786
 
 
787
        self.assertEqual(self.client.ref_count, 1)
 
788
 
 
789
    def test_several_requests(self):
 
790
        """Requests can be nested."""
 
791
        self.client.login(APP_NAME, self.args)
 
792
        self.client.clear_credentials(APP_NAME, self.args)
 
793
        self.client.find_credentials(APP_NAME, self.args)
 
794
        self.client.register(APP_NAME, self.args)
 
795
        self.client.store_credentials(APP_NAME, self.args)
 
796
 
 
797
        self.assertEqual(self.client.ref_count, 5)
 
798
 
 
799
    def test_credentials_found(self):
 
800
        """Ref counter is decreased when a signal is sent."""
 
801
        self.client.ref_count = 3
 
802
        self.client.CredentialsFound(APP_NAME, TOKEN)
 
803
 
 
804
        self.assertEqual(self.client.ref_count, 2)
 
805
 
 
806
    def test_credentials_not_found(self):
 
807
        """Ref counter is decreased when a signal is sent."""
 
808
        self.client.ref_count = 3
 
809
        self.client.CredentialsNotFound(APP_NAME)
 
810
 
 
811
        self.assertEqual(self.client.ref_count, 2)
 
812
 
 
813
    def test_credentials_cleared(self):
 
814
        """Ref counter is decreased when a signal is sent."""
 
815
        self.client.ref_count = 3
 
816
        self.client.CredentialsCleared(APP_NAME)
 
817
 
 
818
        self.assertEqual(self.client.ref_count, 2)
 
819
 
 
820
    def test_credentials_stored(self):
 
821
        """Ref counter is decreased when a signal is sent."""
 
822
        self.client.ref_count = 3
 
823
        self.client.CredentialsStored(APP_NAME)
 
824
 
 
825
        self.assertEqual(self.client.ref_count, 2)
 
826
 
 
827
    def test_credentials_error(self):
 
828
        """Ref counter is decreased when a signal is sent."""
 
829
        self.client.ref_count = 3
 
830
        self.client.CredentialsError(APP_NAME, {'error_type': 'test'})
 
831
 
 
832
        self.assertEqual(self.client.ref_count, 2)
 
833
 
 
834
    def test_authorization_denied(self):
 
835
        """Ref counter is decreased when a signal is sent."""
 
836
        self.client.ref_count = 3
 
837
        self.client.AuthorizationDenied(APP_NAME)
 
838
 
 
839
        self.assertEqual(self.client.ref_count, 2)
 
840
 
 
841
    def test_credentials_found_when_ref_count_is_not_positive(self):
 
842
        """Ref counter is decreased when a signal is sent."""
 
843
        self.client._ref_count = -3
 
844
        self.client.CredentialsFound(APP_NAME, TOKEN)
 
845
 
 
846
        self.assertEqual(self.client.ref_count, 0)
 
847
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
 
848
        self.assertTrue(self.memento.check_warning(msg))
 
849
 
 
850
    def test_credentials_not_found_when_ref_count_is_not_positive(self):
 
851
        """Ref counter is decreased when a signal is sent."""
 
852
        self.client._ref_count = -3
 
853
        self.client.CredentialsNotFound(APP_NAME)
 
854
 
 
855
        self.assertEqual(self.client.ref_count, 0)
 
856
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
 
857
        self.assertTrue(self.memento.check_warning(msg))
 
858
 
 
859
    def test_credentials_cleared_when_ref_count_is_not_positive(self):
 
860
        """Ref counter is decreased when a signal is sent."""
 
861
        self.client._ref_count = -3
 
862
        self.client.CredentialsCleared(APP_NAME)
 
863
 
 
864
        self.assertEqual(self.client.ref_count, 0)
 
865
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
 
866
        self.assertTrue(self.memento.check_warning(msg))
 
867
 
 
868
    def test_credentials_stored_when_ref_count_is_not_positive(self):
 
869
        """Ref counter is decreased when a signal is sent."""
 
870
        self.client._ref_count = -3
 
871
        self.client.CredentialsStored(APP_NAME)
 
872
 
 
873
        self.assertEqual(self.client.ref_count, 0)
 
874
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
 
875
        self.assertTrue(self.memento.check_warning(msg))
 
876
 
 
877
    def test_credentials_error_when_ref_count_is_not_positive(self):
 
878
        """Ref counter is decreased when a signal is sent."""
 
879
        self.client._ref_count = -3
 
880
        self.client.CredentialsError(APP_NAME, {'error_type': 'test'})
 
881
 
 
882
        self.assertEqual(self.client.ref_count, 0)
 
883
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
 
884
        self.assertTrue(self.memento.check_warning(msg))
 
885
 
 
886
    def test_autorization_denied_when_ref_count_is_not_positive(self):
 
887
        """Ref counter is decreased when a signal is sent."""
 
888
        self.client._ref_count = -3
 
889
        self.client.AuthorizationDenied(APP_NAME)
 
890
 
 
891
        self.assertEqual(self.client.ref_count, 0)
 
892
        msg = 'Attempting to decrease ref_count to a negative value (-4).'
 
893
        self.assertTrue(self.memento.check_warning(msg))
 
894
 
 
895
    def test_on_zero_ref_count_shutdown(self):
 
896
        """When ref count reaches 0, queue shutdown op."""
 
897
        self.client.timeout_func = self._set_called
 
898
        self.client.find_credentials(APP_NAME, self.args)
 
899
        self.client.CredentialsFound(APP_NAME, TOKEN)
 
900
 
 
901
        self.assertEqual(self._called,
 
902
                         ((TIMEOUT_INTERVAL, self.client.shutdown_func), {}))
 
903
 
 
904
    def test_on_non_zero_ref_count_do_not_shutdown(self):
 
905
        """If ref count is not 0, do not queue shutdown op."""
 
906
        self.client.timeout_func = self._set_called
 
907
        self.client.find_credentials(APP_NAME, self.args)
 
908
 
 
909
        self.assertEqual(self._called, False)
 
910
 
 
911
 
743
912
class CredentialsManagementFindTestCase(CredentialsManagementTestCase):
744
913
    """Tests for the CredentialsManagement find method."""
745
914
 
989
1158
    """Tests for the CredentialsManagement DBus signals."""
990
1159
 
991
1160
    def setUp(self):
992
 
        self.client = CredentialsManagement()
 
1161
        self.client = CredentialsManagement(timeout_func=lambda *a: None,
 
1162
                                            shutdown_func=lambda *a: None)
993
1163
 
994
1164
        self.memento = MementoHandler()
995
1165
        self.memento.setLevel(logging.DEBUG)