~didrocks/ubuntuone-client/dont-suffer-zg-crash

« back to all changes in this revision

Viewing changes to tests/platform/linux/test_dbus.py

  • Committer: Bazaar Package Importer
  • Author(s): Rodney Dawes
  • Date: 2011-01-25 16:42:52 UTC
  • mto: This revision was merged to the branch mainline in revision 64.
  • Revision ID: james.westby@ubuntu.com-20110125164252-rl1pybasx1nsqgoy
Tags: upstream-1.5.3
ImportĀ upstreamĀ versionĀ 1.5.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
from ubuntu_sso.main import SSOCredentials
32
32
 
33
33
from contrib.testing.testcase import (
34
 
    BaseTwistedTestCase, FakeMain, FAKED_CREDENTIALS,
35
 
    MementoHandler)
 
34
    BaseTwistedTestCase, FakeMain, FAKED_CREDENTIALS)
36
35
 
37
 
from ubuntuone.clientdefs import APP_NAME
38
36
from ubuntuone.storageprotocol.sharersp import NotifyShareHolder
39
37
from ubuntuone.syncdaemon import states, config
40
38
from ubuntuone.platform.linux.dbus_interface import (
52
50
    NM_STATE_CONNECTED, NM_STATE_DISCONNECTED
53
51
)
54
52
from ubuntuone.platform.linux import ExternalInterface, get_udf_path
 
53
from ubuntuone.syncdaemon import action_queue
55
54
 
56
55
from ubuntuone.syncdaemon.volume_manager import (
57
56
    Share,
62
61
from ubuntuone.platform.linux.tools import DBusClient
63
62
from ubuntuone.syncdaemon.marker import MDMarker
64
63
 
 
64
from ubuntuone.devtools.handlers import MementoHandler
 
65
 
65
66
 
66
67
# OAuth stubs
67
68
class FakeLoginProcessor(object):
97
98
        """
98
99
        self.next_login_cb = (callback, args)
99
100
 
 
101
 
100
102
class FakeDBusInterface(object):
101
103
    """A fake DBusInterface..."""
102
104
 
103
105
    def shutdown(self, with_restart=False):
104
106
        """...that only knows how to go away"""
105
107
 
106
 
    def _request_token(self):
 
108
    def _request_token(self, *args, **kwargs):
107
109
        """Return a token which is a fixed set of credentials."""
108
110
        return FAKED_CREDENTIALS
109
111
 
136
138
        self.dbus_iface = DBusInterface(self.bus, self.main,
137
139
                                        system_bus=self.bus)
138
140
        # token is a fixed set of credentials
139
 
        self.dbus_iface._request_token = lambda: FAKED_CREDENTIALS
 
141
        self.dbus_iface._request_token = lambda *a, **kw: FAKED_CREDENTIALS
140
142
 
141
143
        self.main.external = ExternalInterface(self.main, False, False,
142
144
                                               dbus_iface=self.dbus_iface)
145
147
        self.dbus_iface.connect()
146
148
        self.event_q.push('SYS_INIT_DONE')
147
149
        self.signal_receivers = set()
148
 
        self.action_q.content_queue.set_change_notification_cb(
149
 
            self.dbus_iface.status.emit_content_queue_changed)
150
150
 
151
151
    def tearDown(self):
152
152
        """ Cleanup the test. """
286
286
    def run(self):
287
287
        """Just succeed."""
288
288
        return defer.succeed(None)
 
289
    locked_run = run
289
290
 
290
291
    def to_dict(self):
291
292
        """Just send both values."""
317
318
        """Fake register."""
318
319
        self._called['register'] = (args, kwargs)
319
320
 
 
321
    def find_credentials(self, *args, **kwargs):
 
322
        """Fake find_credentials."""
 
323
        self._called['find_credentials'] = (args, kwargs)
 
324
 
320
325
 
321
326
class FakedBusName(object):
322
327
    """Fake a dbus name."""
553
558
        return push_deferred
554
559
 
555
560
    def test_waiting_content(self):
556
 
        """Test Status.waiting_content and Status.schedule_next.
 
561
        """Test Status.waiting_content.
557
562
 
558
563
        Fake data in the AQ is used.
559
564
        """
 
565
        class FakeContentCommand(FakeCommand, action_queue.Upload):
 
566
            """Fake command that goes in content queue."""
 
567
            def __init__(self, *args):
 
568
                FakeCommand.__init__(self, *args)
 
569
 
560
570
        # prepare the VM so it lies for us
561
571
        share_path = os.path.join(self.shares_dir, 'share')
562
572
        self.main.vm.add_share(Share(path=share_path, volume_id='share_id'))
563
 
        a_path = os.path.join(share_path, "path_a")
564
573
        b_path = os.path.join(share_path, "path_b")
565
574
        c_path = os.path.join(share_path, "path_c")
566
 
        self.fs_manager.create(a_path, "share_id")
567
575
        self.fs_manager.create(b_path, "share_id")
568
576
        self.fs_manager.create(c_path, "share_id")
569
 
        self.fs_manager.set_node_id(a_path, "node_id_a")
570
577
        self.fs_manager.set_node_id(b_path, "node_id_b")
571
578
        self.fs_manager.set_node_id(c_path, "node_id_c")
572
579
        # inject the fake data
573
 
        self.action_q.content_queue._head = FakeCommand("share_id",
574
 
                                                        "node_id_a")
575
 
        self.action_q.content_queue.waiting.extend([
576
 
                FakeCommand("share_id", "node_id_b"),
577
 
                FakeCommand("share_id", "node_id_c"),
578
 
                EmptyCommand()])
 
580
        self.action_q.queue.waiting.extend([
 
581
                FakeContentCommand("share_id", "node_id_b"),
 
582
                FakeContentCommand("share_id", "node_id_c")])
579
583
        # OK, testing time
580
584
        client = DBusClient(self.bus, '/status', DBUS_IFACE_STATUS_NAME)
581
585
        d = defer.Deferred()
582
 
        self.second = False
 
586
 
583
587
        def waiting_handler(result):
584
588
            """Waiting_content reply handler."""
585
 
            self.assertEquals(4, len(result))
586
 
            # the second time we're called, the result should be reversed
587
 
            if self.second:
588
 
                node_a, node_c, node_b, node_d = result
589
 
            else:
590
 
                node_a, node_b, node_c, node_d = result
591
 
 
592
 
            self.assertEquals(a_path, str(node_a['path']))
593
 
            self.assertEquals(b_path, str(node_b['path']))
594
 
            self.assertEquals('share_id', str(node_a['share']))
595
 
            self.assertEquals('share_id', str(node_b['share']))
596
 
            self.assertEquals('node_id_a', str(node_a['node']))
597
 
            self.assertEquals('node_id_b', str(node_b['node']))
598
 
            self.assertEquals(EmptyCommand.__name__, str(node_d['operation']))
599
 
 
600
 
            if self.second:
601
 
                # OK, we're done
602
 
                d.callback(True)
603
 
            else:
604
 
                # the last shall be the first, et cetera.
605
 
                client.call_method('schedule_next', 'share_id', 'node_id_c',
606
 
                                   reply_handler=reschedule_handler,
607
 
                                   error_handler=self.error_handler)
608
 
        def reschedule_handler(result):
609
 
            """Schedule_next reply handler."""
610
 
            self.assertEqual(result, None)
611
 
            self.second = True
612
 
            client.call_method('waiting_content',
613
 
                               reply_handler=waiting_handler,
614
 
                               error_handler=self.error_handler)
 
589
            self.assertEqual(2, len(result))
 
590
            node_b, node_c = result
 
591
 
 
592
            self.assertEqual(b_path, str(node_b['path']))
 
593
            self.assertEqual('share_id', str(node_b['share']))
 
594
            self.assertEqual('node_id_b', str(node_b['node']))
 
595
 
 
596
            # OK, we're done
 
597
            d.callback(True)
 
598
 
615
599
        client.call_method('waiting_content',
616
600
                           reply_handler=waiting_handler,
617
601
                           error_handler=self.error_handler)
622
606
        # prepare the VM so it lies for us
623
607
        share_path = os.path.join(self.shares_dir, 'share')
624
608
        self.main.vm.add_share(Share(path=share_path, volume_id='share_id'))
625
 
        a_path = os.path.join(share_path, "path_a")
626
609
        b_path = os.path.join(share_path, "path_b")
627
610
        c_path = os.path.join(share_path, "path_c")
628
611
        d_path = os.path.join(share_path, "path_d")
629
 
        self.fs_manager.create(a_path, "share_id")
630
612
        self.fs_manager.create(b_path, "share_id")
631
613
        mdidc = self.fs_manager.create(c_path, "share_id")
632
614
        mdidd = self.fs_manager.create(d_path, "share_id")
633
 
        self.fs_manager.set_node_id(a_path, "node_id_a")
634
615
        self.fs_manager.set_node_id(b_path, "node_id_b")
635
616
        self.fs_manager.set_node_id(c_path, "node_id_c")
636
617
        self.fs_manager.set_node_id(d_path, "node_id_d")
642
623
        trash[("share_id", "node_id_d")] = trash[("share_id", "node_id_d")][:2]
643
624
 
644
625
        # inject the fake data
645
 
        self.action_q.meta_queue._head = FakeCommand("share_id", "node_id_a")
646
 
        self.action_q.meta_queue.waiting.extend([
 
626
        self.action_q.queue.waiting.extend([
647
627
                FakeCommand("share_id", "node_id_b", u"moƱo"),
648
628
                FakeCommand("share_id", "node_id_c"),
649
629
                FakeCommand("share_id", "node_id_d")])
650
630
        # add a command without node_id and share_id (e.g: GetPublicFiles)
651
 
        self.action_q.meta_queue.waiting.append(EmptyCommand())
 
631
        self.action_q.queue.waiting.append(EmptyCommand())
652
632
 
653
633
        # testing time
654
634
        client = DBusClient(self.bus, '/status', DBUS_IFACE_STATUS_NAME)
655
635
        d = defer.Deferred()
656
636
        def waiting_handler(result):
657
637
            """Reply handler."""
658
 
            self.assertEqual(len(result), 5)
659
 
 
660
 
            pl = dict(share_id='share_id', node_id='node_id_a',
661
 
                      other='', path=a_path)
662
 
            self.assertEqual(result[0], ('FakeCommand', pl))
 
638
            self.assertEqual(len(result), 4)
663
639
 
664
640
            pl = dict(share_id='share_id', node_id='node_id_b',
665
641
                      other=u'moƱo', path=b_path)
666
 
            self.assertEqual(result[1], ('FakeCommand', pl))
 
642
            self.assertEqual(result[0], ('FakeCommand', pl))
667
643
 
668
644
            pl = dict(share_id='share_id', node_id='node_id_c',
669
645
                      other='', path=c_path)
670
 
            self.assertEqual(result[2], ('FakeCommand', pl))
 
646
            self.assertEqual(result[1], ('FakeCommand', pl))
671
647
 
672
648
            pl = dict(share_id='share_id', node_id='node_id_d',
673
649
                      other='', path='')
674
 
            self.assertEqual(result[3], ('FakeCommand', pl))
 
650
            self.assertEqual(result[2], ('FakeCommand', pl))
675
651
            d.callback(True)
676
652
        client.call_method('waiting_metadata',
677
653
                           reply_handler=waiting_handler,
690
666
        self.fs_manager.set_node_id(path, "node_id")
691
667
 
692
668
        # inject fake data
693
 
        self.action_q.meta_queue.waiting.append(
 
669
        self.action_q.queue.waiting.append(
694
670
            FakeCommand("share", u"node_id"))
695
671
 
696
672
        d = defer.Deferred()
724
700
        self.fs_manager.set_node_id(path, "node_id")
725
701
 
726
702
        # inject fake data
727
 
        self.action_q.meta_queue.waiting.append(
 
703
        self.action_q.queue.waiting.append(
728
704
            FakeCommand("this share id no longer exists",
729
705
                        "neither does this path id"))
730
706
 
759
735
        self.fs_manager.set_node_id(path, "node_id")
760
736
 
761
737
        # inject fake data
762
 
        self.action_q.content_queue.waiting.append(
 
738
        self.action_q.queue.waiting.append(
763
739
            FakeCommand("share", u"node_id"))
764
740
 
765
741
        d = defer.Deferred()
844
820
                           error_handler=self.error_handler)
845
821
        return d
846
822
 
847
 
    def test_contq_changed(self):
848
 
        """Test the Status.ContentQueueChanged signal."""
849
 
        d = defer.Deferred()
850
 
        match = self.bus.add_signal_receiver(lambda: d.callback(None),
851
 
                                             'ContentQueueChanged')
852
 
        self.signal_receivers.add(match)
853
 
 
854
 
        # inject the fake data
855
 
        self.action_q.content_queue.queue(FakeCommand("share_id", "node_id"))
 
823
    def test_content_queue_added(self):
 
824
        """Test the signal because a command was added to the queue."""
 
825
        class FakeCommand(action_queue.Upload):
 
826
            """Fake to don't need to instantiate everything for a test."""
 
827
            def __init__(self):
 
828
                """Nothing needed :)"""
 
829
 
 
830
        d = defer.Deferred()
 
831
        receiver = self.bus.add_signal_receiver(lambda: d.callback(True),
 
832
                                            signal_name='ContentQueueChanged')
 
833
        self.signal_receivers.add(receiver)
 
834
        self.main.event_q.push('SYS_QUEUE_ADDED', FakeCommand())
 
835
        return d
 
836
 
 
837
    def test_content_queue_removed(self):
 
838
        """Test the signal because a command was removed from the queue."""
 
839
        class FakeCommand(action_queue.Upload):
 
840
            """Fake to don't need to instantiate everything for a test."""
 
841
            def __init__(self):
 
842
                """Nothing needed :)"""
 
843
 
 
844
        d = defer.Deferred()
 
845
        receiver = self.bus.add_signal_receiver(lambda: d.callback(True),
 
846
                                            signal_name='ContentQueueChanged')
 
847
        self.signal_receivers.add(receiver)
 
848
        self.main.event_q.push('SYS_QUEUE_REMOVED', FakeCommand())
856
849
        return d
857
850
 
858
851
    def test_current_downloads(self):
1162
1155
        # helper functions, pylint: disable-msg=C0111
1163
1156
        def fake_create_share(*result):
1164
1157
            # pylint: disable-msg=W0612
1165
 
            node_id, username, name, access_level, marker = result
 
1158
            node_id, username, name, access_level, marker, path = result
1166
1159
            self.assertEquals('node_id', node_id)
1167
1160
            mdobj = self.fs_manager.get_by_path(a_dir)
1168
1161
            self.assertEquals(self.fs_manager.get_abspath("", mdobj.path),
1193
1186
        # helper functions, pylint: disable-msg=C0111
1194
1187
        def fake_create_share(*result):
1195
1188
            # pylint: disable-msg=W0612
1196
 
            node_id, username, name, access_level, marker = result
 
1189
            node_id, username, name, access_level, marker, path = result
1197
1190
            self.assertEquals('node_id', node_id)
1198
1191
            mdobj = self.fs_manager.get_by_path(a_dir)
1199
1192
            self.assertEquals(self.fs_manager.get_abspath("", mdobj.path),
1222
1215
        # helper functions, pylint: disable-msg=C0111
1223
1216
        def aq_create_share(*args):
1224
1217
            self.main.event_q.push('AQ_CREATE_SHARE_OK',
1225
 
                                   share_id='share_id', marker=args[-1])
 
1218
                                   share_id='share_id', marker=args[-2])
1226
1219
        self.main.action_q.create_share = aq_create_share
1227
1220
        self.main.vm.create_share(a_dir, 0, 'share_a_dir', 'View')
1228
1221
        client = DBusClient(self.bus, '/shares', DBUS_IFACE_SHARES_NAME)
1229
1222
        d = defer.Deferred()
1230
1223
        def reply_handler(results):
1231
1224
            """Handle get_shared reply."""
1232
 
            self.assertEquals(1, len(results))
 
1225
            self.assertEqual(1, len(results))
1233
1226
            shared = results[0]
1234
 
            self.assertEquals(a_dir, str(shared['path']))
1235
 
            self.assertEquals('node_id', str(shared['node_id']))
1236
 
            self.assertEquals('share_id', str(shared['volume_id']))
1237
 
            self.assertEquals('View', str(shared['access_level']))
 
1227
            self.assertEqual(a_dir, str(shared['path']))
 
1228
            self.assertEqual('node_id', str(shared['node_id']))
 
1229
            self.assertEqual('share_id', str(shared['volume_id']))
 
1230
            self.assertEqual('View', str(shared['access_level']))
1238
1231
            d.callback(True)
1239
1232
        client.call_method('get_shared',
1240
1233
                           reply_handler=reply_handler,
1249
1242
        # helper functions, pylint: disable-msg=C0111
1250
1243
        def aq_create_share(*args):
1251
1244
            self.main.event_q.push('AQ_CREATE_SHARE_OK',
1252
 
                                   share_id='share_id', marker=args[-1])
 
1245
                                   share_id='share_id', marker=args[-2])
1253
1246
        self.main.action_q.create_share = aq_create_share
1254
1247
        self.main.vm.create_share(a_dir, 0, 'share_a_dir', 'View')
1255
1248
        client = DBusClient(self.bus, '/shares', DBUS_IFACE_SHARES_NAME)
1262
1255
        d = defer.Deferred()
1263
1256
        def reply_handler(results):
1264
1257
            """Handle get_shared reply."""
1265
 
            self.assertEquals(1, len(results))
 
1258
            self.assertEqual(1, len(results))
1266
1259
            shared = results[0]
1267
 
            self.assertEquals('', str(shared['path']))
1268
 
            self.assertEquals('node_id', str(shared['node_id']))
1269
 
            self.assertEquals('share_id', str(shared['volume_id']))
1270
 
            self.assertEquals('View', str(shared['access_level']))
 
1260
            self.assertEqual('', str(shared['path']))
 
1261
            self.assertEqual('node_id', str(shared['node_id']))
 
1262
            self.assertEqual('share_id', str(shared['volume_id']))
 
1263
            self.assertEqual('View', str(shared['access_level']))
1271
1264
            d.callback(True)
1272
1265
        client.call_method('get_shared',
1273
1266
                           reply_handler=reply_handler,
1483
1476
        # helper functions, pylint: disable-msg=C0111
1484
1477
        def fake_create_share(*result):
1485
1478
            # pylint: disable-msg=W0612
1486
 
            node_id, username, name, access_level, marker = result
 
1479
            node_id, username, name, access_level, marker, path = result
1487
1480
            self.assertEquals('node_id', node_id)
1488
1481
            mdobj = self.fs_manager.get_by_path(a_dir)
1489
1482
            self.assertEquals(self.fs_manager.get_abspath("", mdobj.path),
1737
1730
                               error='AN_ERROR', hash='hash')
1738
1731
        return d
1739
1732
 
1740
 
    def test_meta_queue_changed_waiting(self):
1741
 
        """Test the signal because MQ waiting."""
 
1733
    def test_meta_queue_added(self):
 
1734
        """Test the signal because a command was added to the queue."""
1742
1735
        d = defer.Deferred()
1743
1736
        receiver = self.bus.add_signal_receiver(lambda: d.callback(True),
1744
1737
                                                signal_name='MetaQueueChanged')
1745
1738
        self.signal_receivers.add(receiver)
1746
 
        self.main.event_q.push('SYS_META_QUEUE_WAITING')
 
1739
        cmd = action_queue.ListShares   # any "meta" command
 
1740
        self.main.event_q.push('SYS_QUEUE_ADDED', cmd)
1747
1741
        return d
1748
1742
 
1749
 
    def test_meta_queue_changed_done(self):
1750
 
        """Test the signal because MQ done."""
 
1743
    def test_meta_queue_changed(self):
 
1744
        """Test the signal because a command was removed from the queue."""
1751
1745
        d = defer.Deferred()
1752
1746
        receiver = self.bus.add_signal_receiver(lambda: d.callback(True),
1753
1747
                                                signal_name='MetaQueueChanged')
1754
1748
        self.signal_receivers.add(receiver)
1755
 
        self.main.event_q.push('SYS_META_QUEUE_DONE')
 
1749
        cmd = action_queue.ListShares   # any "meta" command
 
1750
        self.main.event_q.push('SYS_QUEUE_REMOVED', cmd)
1756
1751
        return d
1757
1752
 
1758
1753
    def test_status_changed(self):
2493
2488
 
2494
2489
 
2495
2490
class DBusOAuthTestCase(BaseTwistedTestCase):
2496
 
    """Tests the interaction between dbus_interface and ubuntu-sso-client."""
 
2491
    """Tests the interaction between dbus_interface and credentials.
 
2492
 
 
2493
    Check conditions when autconnecting is False.
 
2494
 
 
2495
    """
2497
2496
 
2498
2497
    timeout = 2
 
2498
    method = 'register'
 
2499
    autoconnecting = False
2499
2500
 
2500
2501
    def setUp(self):
2501
2502
        """Init."""
2539
2540
            self.assertEqual(self.dbus_iface._signal_handler, cb)
2540
2541
            self.dbus_iface._deferred.callback(None)
2541
2542
 
2542
 
        self.patch(FakedSSOBackend, 'register', f)
2543
 
        yield self.dbus_iface.connect()
 
2543
        self.patch(FakedSSOBackend, self.method, f)
 
2544
        yield self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2544
2545
 
2545
2546
    @defer.inlineCallbacks
2546
2547
    def test_signals_are_removed_after_connection(self):
2550
2551
            """Just succeed."""
2551
2552
            self.dbus_iface._deferred.callback(None)
2552
2553
 
2553
 
        self.patch(FakedSSOBackend, 'register', f)
2554
 
        yield self.dbus_iface.connect()
 
2554
        self.patch(FakedSSOBackend, self.method, f)
 
2555
        yield self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2555
2556
        self.assertNotIn((DBUS_CREDENTIALS_IFACE, None), self.bus.callbacks)
2556
2557
 
2557
2558
    @defer.inlineCallbacks
2560
2561
 
2561
2562
        def f(*a, **kw):
2562
2563
            """Receive credentials."""
2563
 
            self.dbus_iface._signal_handler(APP_NAME, FAKED_CREDENTIALS,
 
2564
            self.dbus_iface._signal_handler(FAKED_CREDENTIALS,
2564
2565
                                            member='CredentialsFound')
2565
2566
 
2566
 
        self.patch(FakedSSOBackend, 'register', f)
2567
 
        yield self.dbus_iface.connect()
 
2567
        self.patch(FakedSSOBackend, self.method, f)
 
2568
        yield self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2568
2569
        self.assertEqual(self.events, [('SYS_USER_CONNECT',
2569
2570
                                       {'access_token': FAKED_CREDENTIALS})])
2570
2571
 
2574
2575
 
2575
2576
        def f(*a, **kw):
2576
2577
            """Receive error signal."""
2577
 
            self.dbus_iface._signal_handler(APP_NAME, 'Error description',
2578
 
                                            'Detailed error',
 
2578
            self.dbus_iface._signal_handler({'error_type': 'Error description',
 
2579
                                             'error_detail': 'Detailed error'},
2579
2580
                                            member='CredentialsError')
2580
2581
 
2581
 
        self.patch(FakedSSOBackend, 'register', f)
2582
 
        d = self.dbus_iface.connect()
 
2582
        self.patch(FakedSSOBackend, self.method, f)
 
2583
        d = self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2583
2584
        d.addErrback(lambda failure: self.assertEqual(NoAccessToken,
2584
2585
                                                      failure.type))
2585
2586
        yield d
2590
2591
 
2591
2592
        def f(*a, **kw):
2592
2593
            """Receive error signal."""
2593
 
            self.dbus_iface._signal_handler(APP_NAME,
2594
 
                                            member='AuthorizationDenied')
2595
 
 
2596
 
        self.patch(FakedSSOBackend, 'register', f)
2597
 
        d = self.dbus_iface.connect()
 
2594
            self.dbus_iface._signal_handler(member='AuthorizationDenied')
 
2595
 
 
2596
        self.patch(FakedSSOBackend, self.method, f)
 
2597
        d = self.dbus_iface.connect(autoconnecting=self.autoconnecting)
 
2598
        d.addErrback(lambda failure: self.assertEqual(NoAccessToken,
 
2599
                                                      failure.type))
 
2600
        yield d
 
2601
 
 
2602
    @defer.inlineCallbacks
 
2603
    def test_connect_raises_NoAccessToken_if_no_creds(self):
 
2604
        """If no credentials, NoAccessToken if no credentials."""
 
2605
 
 
2606
        def f(*a, **kw):
 
2607
            """Receive error signal."""
 
2608
            self.dbus_iface._signal_handler(member='CredentialsNotFound')
 
2609
 
 
2610
        self.patch(FakedSSOBackend, self.method, f)
 
2611
        d = self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2598
2612
        d.addErrback(lambda failure: self.assertEqual(NoAccessToken,
2599
2613
                                                      failure.type))
2600
2614
        yield d
2608
2622
            """Just fail."""
2609
2623
            raise expected
2610
2624
 
2611
 
        self.patch(FakedSSOBackend, 'register', f)
2612
 
        d = self.dbus_iface.connect()
 
2625
        self.patch(FakedSSOBackend, self.method, f)
 
2626
        d = self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2613
2627
        d.addErrback(lambda failure: self.assertEqual(expected, failure.value))
2614
2628
        yield d
2615
2629
 
2622
2636
            """Just fail."""
2623
2637
            raise expected
2624
2638
 
2625
 
        self.patch(FakedSSOBackend, 'register', f)
2626
 
        d = self.dbus_iface.connect()
 
2639
        self.patch(FakedSSOBackend, self.method, f)
 
2640
        d = self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2627
2641
        d.addErrback(lambda failure: self.assertEqual(expected, failure.value))
2628
2642
        yield d
2629
2643
        self.assertTrue(len(self.memento.records) > 0)
2630
 
        msg = self.memento.records[0].message
 
2644
        record = self.memento.records[1]
 
2645
        msg = record.message
2631
2646
        self.assertIn('connect failed while getting the token', msg)
2632
 
        self.assertIn(expected, self.memento.records[0].exc_info)
 
2647
        self.assertIn(expected, record.exc_info)
2633
2648
 
2634
2649
    def test_oauth_credentials_are_none_at_startup(self):
2635
2650
        """If the oauth_credentials are not passed as param, they are None."""
2644
2659
                    'token_secret': 'faked_token_secret'}
2645
2660
        self.dbus_iface.oauth_credentials = (expected['token'],
2646
2661
                                             expected['token_secret'])
2647
 
        yield self.dbus_iface.connect()
 
2662
        yield self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2648
2663
        self.assertEqual(self.events, [('SYS_USER_CONNECT',
2649
2664
                                       {'access_token': expected})])
2650
2665
 
2659
2674
                                             expected['consumer_secret'],
2660
2675
                                             expected['token'],
2661
2676
                                             expected['token_secret'])
2662
 
        yield self.dbus_iface.connect()
 
2677
        yield self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2663
2678
        self.assertEqual(self.events, [('SYS_USER_CONNECT',
2664
2679
                                       {'access_token': expected})])
2665
2680
 
2669
2684
        self.dbus_iface.oauth_credentials = ('consumer_key',
2670
2685
                                             'consumer_secret',
2671
2686
                                             'token_secret')
2672
 
        yield self.dbus_iface.connect()
 
2687
        yield self.dbus_iface.connect(autoconnecting=self.autoconnecting)
2673
2688
        self.assertEqual(self.events, [])
2674
2689
        msgs = (str(self.dbus_iface.oauth_credentials), 'useless')
2675
2690
        self.assertTrue(self.memento.check_warning(*msgs))
2676
2691
 
2677
 
    def test_only_log_if_app_name_is_not_correct_when_credentials_found(self):
2678
 
        """If the app_name is not ours, just log an INFO message."""
2679
 
 
2680
 
        def f(*a, **kw):
2681
 
            """Receive wrong app_name."""
2682
 
            self.dbus_iface._signal_handler(APP_NAME * 2,
2683
 
                                            member='CredentialsFound')
2684
 
 
2685
 
        self.patch(FakedSSOBackend, 'register', f)
2686
 
        self.dbus_iface._request_token()
2687
 
 
2688
 
        self.assertFalse(self.dbus_iface._deferred.called)
2689
 
        self.assertTrue(self.memento.check_info('CredentialsFound',
2690
 
                                                APP_NAME * 2))
2691
 
 
2692
 
    def test_only_log_if_app_name_is_not_correct_when_credentials_error(self):
2693
 
        """If the app_name is not ours, just log an INFO message."""
2694
 
 
2695
 
        def f(*a, **kw):
2696
 
            """Receive a wrong app_name."""
2697
 
            self.dbus_iface._signal_handler(APP_NAME * 2,
2698
 
                                            member='CredentialsError')
2699
 
 
2700
 
        self.patch(FakedSSOBackend, 'register', f)
2701
 
        self.dbus_iface._request_token()
2702
 
 
2703
 
        self.assertFalse(self.dbus_iface._deferred.called)
2704
 
        self.assertTrue(self.memento.check_info('CredentialsError',
2705
 
                                                APP_NAME * 2))
2706
 
 
2707
 
    def test_only_log_if_app_name_is_not_correct_when_authorization_denied(self):
2708
 
        """If the app_name is not ours, just log an INFO message."""
2709
 
 
2710
 
        def f(*a, **kw):
2711
 
            """Receive a wrong app_name."""
2712
 
            self.dbus_iface._signal_handler(APP_NAME * 2,
2713
 
                                            member='AuthorizationDenied')
2714
 
 
2715
 
        self.patch(FakedSSOBackend, 'register', f)
2716
 
        self.dbus_iface._request_token()
2717
 
 
2718
 
        self.assertFalse(self.dbus_iface._deferred.called)
2719
 
        self.assertTrue(self.memento.check_info('AuthorizationDenied',
2720
 
                                                APP_NAME * 2))
2721
 
 
2722
2692
    def test_signal_handler_remains_generic(self):
2723
2693
        """The signal handler function should be generic."""
2724
2694
        self.dbus_iface._signal_handler()
2725
2695
        # no failure
2726
 
        self.assertTrue(self.memento.check_debug('member: None',
2727
 
                                                 'app_name: None'))
 
2696
        self.assertTrue(self.memento.check_debug('member: None'))
2728
2697
 
2729
2698
        self.dbus_iface._signal_handler(no_member_kwarg='Test')
2730
2699
        # no failure
2731
 
        self.assertTrue(self.memento.check_debug('member: None',
2732
 
                                                 'app_name: None'))
 
2700
        self.assertTrue(self.memento.check_debug('member: None'))
 
2701
 
 
2702
 
 
2703
class DBusOAuthTestCaseRegister(DBusOAuthTestCase):
 
2704
    """Tests the interaction between dbus_interface and credentials.
 
2705
 
 
2706
    Check conditions when autconnecting is True.
 
2707
 
 
2708
    """
 
2709
 
 
2710
    method = 'find_credentials'
 
2711
    autoconnecting = True
2733
2712
 
2734
2713
 
2735
2714
class FolderTests(DBusTwistedTestCase):
2965
2944
        udf = self._create_udf(uuid.uuid4(), 'node_id', suggested_path)
2966
2945
        self.main.vm.add_udf(udf)
2967
2946
        d = defer.Deferred()
2968
 
        def delete_volume(path):
 
2947
        def delete_volume(volume_id, path):
2969
2948
            """Fake delete_volume."""
2970
2949
            self.main.event_q.push("AQ_DELETE_VOLUME_OK", volume_id=udf.id)
2971
2950
        self.main.action_q.delete_volume = delete_volume
2997
2976
        self.main.vm.add_udf(udf)
2998
2977
        d = defer.Deferred()
2999
2978
        # patch delete_volume to fail
3000
 
        def delete_volume(path):
 
2979
        def delete_volume(volume_id, path):
3001
2980
            """Fake delete_volume."""
3002
2981
            self.main.event_q.push("AQ_DELETE_VOLUME_ERROR",
3003
2982
                                   volume_id=udf.volume_id, error="I'm broken")
3131
3110
                      node_id='node_id', accepted=True)
3132
3111
        self.main.vm.add_share(share)
3133
3112
        d = defer.Deferred()
3134
 
        def delete_volume(volume_id):
 
3113
        def delete_volume(volume_id, path):
3135
3114
            """Fake delete_volume."""
3136
3115
            self.main.event_q.push("AQ_DELETE_VOLUME_OK", volume_id=volume_id)
3137
3116
        self.main.action_q.delete_volume = delete_volume
3190
3169
        self.main.vm.add_share(share)
3191
3170
        d = defer.Deferred()
3192
3171
        # patch delete_volume to fail
3193
 
        def delete_volume(volume_id):
 
3172
        def delete_volume(volume_id, path):
3194
3173
            """Fake delete_volume."""
3195
3174
            self.main.event_q.push("AQ_DELETE_VOLUME_ERROR",
3196
3175
                                   volume_id=volume_id, error="I'm broken")