~super-friends/friends/13.10

« back to all changes in this revision

Viewing changes to friends/tests/test_facebook.py

  • Committer: Tarmac
  • Author(s): Robert Bruce Park
  • Date: 2013-07-22 20:34:58 UTC
  • mfrom: (224.1.12 simplify-contacts)
  • Revision ID: tarmac-20130722203458-dwbs94fsik5iydt0
Vast simplification of contact logic.

Approved by Ken VanDine, PS Jenkins bot, Robert Bruce Park.

Show diffs side-by-side

added added

removed removed

Lines of Context:
26
26
import unittest
27
27
import shutil
28
28
 
29
 
from gi.repository import GLib
 
29
from gi.repository import GLib, EDataServer
30
30
from pkg_resources import resource_filename
31
31
 
32
32
from friends.protocols.facebook import Facebook
33
33
from friends.tests.mocks import FakeAccount, FakeSoupMessage, LogMock
34
34
from friends.tests.mocks import TestModel, mock
35
 
from friends.tests.mocks import EDSBookClientMock, EDSSource, EDSRegistry
 
35
from friends.tests.mocks import EDSBookClientMock, EDSRegistry
36
36
from friends.errors import ContactsError, FriendsError, AuthorizationError
37
37
from friends.utils.cache import JsonCache
38
38
 
57
57
    def test_features(self):
58
58
        # The set of public features.
59
59
        self.assertEqual(Facebook.get_features(),
60
 
            ['contacts', 'delete', 'home', 'like', 'receive', 'search', 'send',
61
 
             'send_thread', 'unlike', 'upload', 'wall'])
 
60
                         ['contacts', 'delete', 'delete_contacts', 'home',
 
61
                          'like', 'receive', 'search', 'send', 'send_thread',
 
62
                          'unlike', 'upload', 'wall'])
62
63
 
63
64
    @mock.patch('friends.utils.authentication.manager')
64
65
    @mock.patch('friends.utils.authentication.Accounts')
474
475
            params=dict(access_token='face'))
475
476
        unpublish.assert_called_once_with('post_id')
476
477
 
477
 
    @mock.patch('friends.utils.http.Soup.Message',
478
 
                FakeSoupMessage('friends.tests.data', 'facebook-contacts.dat'))
479
 
    @mock.patch('friends.protocols.facebook.Facebook._login',
480
 
                return_value=True)
481
 
    def test_fetch_contacts(self, *mocks):
482
 
        # Receive the users friends.
483
 
        results = self.protocol._fetch_contacts()
484
 
        self.assertEqual(len(results), 8)
485
 
        self.assertEqual(results[7]['name'], 'John Smith')
486
 
        self.assertEqual(results[7]['id'], '444444')
 
478
    @mock.patch('friends.protocols.facebook.Downloader')
 
479
    def test_contacts(self, downloader):
 
480
        downloader().get_json.return_value = dict(
 
481
            name='Joe Blow', username='jblow', link='example.com', gender='male')
 
482
        downloader.reset_mock()
 
483
        self.protocol._get_access_token = mock.Mock(return_value='broken')
 
484
        follow = self.protocol._follow_pagination = mock.Mock(
 
485
            return_value=[dict(id='contact1'), dict(id='contact2')])
 
486
        prev = self.protocol._previously_stored_contact = mock.Mock(return_value=False)
 
487
        push = self.protocol._push_to_eds = mock.Mock()
 
488
        self.assertEqual(self.protocol.contacts(), 2)
 
489
        follow.assert_called_once_with(
 
490
            params={'access_token': 'broken', 'limit': 1000},
 
491
            url='https://graph.facebook.com/me/friends',
 
492
            limit=1000)
 
493
        self.assertEqual(
 
494
            prev.call_args_list,
 
495
            [mock.call('contact1'), mock.call('contact2')])
 
496
        self.assertEqual(
 
497
            downloader.call_args_list,
 
498
            [mock.call(url='https://graph.facebook.com/contact1',
 
499
                       params={'access_token': 'broken'}),
 
500
             mock.call(url='https://graph.facebook.com/contact2',
 
501
                       params={'access_token': 'broken'})])
 
502
        self.assertEqual(
 
503
            push.call_args_list,
 
504
            [mock.call({
 
505
                'X-FOLKS-WEB-SERVICES-IDS': {
 
506
                    'jabber': '-contact1@chat.facebook.com',
 
507
                    'facebook-id': 'contact1',
 
508
                    'remote-full-name': 'Joe Blow'},
 
509
                'X-GENDER': 'male',
 
510
                'facebook-name': 'Joe Blow',
 
511
                'facebook-id': 'contact1',
 
512
                'X-URIS': 'example.com',
 
513
                'facebook-nick': 'jblow'}),
 
514
             mock.call({
 
515
                 'X-FOLKS-WEB-SERVICES-IDS': {
 
516
                     'jabber': '-contact2@chat.facebook.com',
 
517
                     'facebook-id': 'contact2',
 
518
                     'remote-full-name': 'Joe Blow'},
 
519
                 'X-GENDER': 'male',
 
520
                 'facebook-name': 'Joe Blow',
 
521
                 'facebook-id': 'contact2',
 
522
                 'X-URIS': 'example.com',
 
523
                 'facebook-nick': 'jblow'})])
487
524
 
488
525
    def test_create_contact(self, *mocks):
489
526
        # Receive the users friends.
490
 
        bare_contact = {'name': 'Lucy Baron',
491
 
                        'id': '555555555',
492
 
                        'username': 'lucy.baron5',
493
 
                        'link': 'http:www.facebook.com/lucy.baron5'}
 
527
        bare_contact = {
 
528
            'facebook-id':   '555555555',
 
529
            'facebook-name': 'Lucy Baron',
 
530
            'facebook-nick': 'lucy.baron5',
 
531
            'X-URIS':        'http:www.facebook.com/lucy.baron5',
 
532
            'X-GENDER':      'female',
 
533
            'X-FOLKS-WEB-SERVICES-IDS': {
 
534
                'jabber': '-555555555@chat.facebook.com',
 
535
                'remote-full-name': 'Lucy Baron',
 
536
                'facebook-id': '555555555',
 
537
            }}
494
538
        eds_contact = self.protocol._create_contact(bare_contact)
495
539
        facebook_id_attr = eds_contact.get_attribute('facebook-id')
496
540
        self.assertEqual(facebook_id_attr.get_value(), '555555555')
521
565
        # Finally test to ensure all key value pairs were tested
522
566
        self.assertTrue(test_jabber and test_remote_name and test_facebook_id)
523
567
 
524
 
    @mock.patch('friends.utils.base.Base._get_eds_source',
 
568
    @mock.patch('friends.utils.base.Base._prepare_eds_connections',
525
569
                return_value=True)
526
570
    @mock.patch('gi.repository.EBook.BookClient.new',
527
571
                return_value=EDSBookClientMock())
530
574
                        'id': '555555555',
531
575
                        'username': 'lucy.baron5',
532
576
                        'link': 'http:www.facebook.com/lucy.baron5'}
533
 
        eds_contact = self.protocol._create_contact(bare_contact)
534
577
        self.protocol._address_book = 'test-address-book'
 
578
        client = self.protocol._book_client = mock.Mock()
 
579
        client.add_contact_sync.return_value = True
535
580
        # Implicitely fail test if the following raises any exceptions
536
 
        self.protocol._push_to_eds(eds_contact)
 
581
        self.protocol._push_to_eds(bare_contact)
537
582
 
538
 
    @mock.patch('friends.utils.base.Base._get_eds_source',
539
 
                return_value=None)
540
 
    @mock.patch('friends.utils.base.Base._create_eds_source',
 
583
    @mock.patch('friends.utils.base.Base._prepare_eds_connections',
541
584
                return_value=None)
542
585
    def test_unsuccessfull_push_to_eds(self, *mocks):
543
586
        bare_contact = {'name': 'Lucy Baron',
544
587
                        'id': '555555555',
545
588
                        'username': 'lucy.baron5',
546
589
                        'link': 'http:www.facebook.com/lucy.baron5'}
547
 
        eds_contact = self.protocol._create_contact(bare_contact)
548
590
        self.protocol._address_book = 'test-address-book'
 
591
        client = self.protocol._book_client = mock.Mock()
 
592
        client.add_contact_sync.return_value = False
549
593
        self.assertRaises(
550
594
            ContactsError,
551
595
            self.protocol._push_to_eds,
552
 
            eds_contact,
 
596
            bare_contact,
553
597
            )
554
598
 
555
 
    @mock.patch('gi.repository.EDataServer.Source.new',
556
 
                return_value=EDSSource('foo', 'bar'))
557
 
    def test_create_eds_source(self, *mocks):
558
 
        regmock = self.protocol._source_registry = mock.Mock()
559
 
        regmock.ref_source = lambda x: x
560
 
 
561
 
        result = self.protocol._create_eds_source()
562
 
        self.assertEqual(result, 'test-source-uid')
563
 
 
564
 
    @mock.patch('gi.repository.EBook.BookClient.new',
 
599
    @mock.patch('gi.repository.EBook.BookClient.connect_sync',
565
600
                return_value=EDSBookClientMock())
566
601
    def test_successful_previously_stored_contact(self, *mocks):
567
 
        result = self.protocol._previously_stored_contact(
568
 
            True, 'facebook-id', '11111')
 
602
        result = self.protocol._previously_stored_contact('11111')
569
603
        self.assertEqual(result, True)
570
604
 
571
 
    def test_successful_get_eds_source(self, *mocks):
572
 
        class FakeSource:
573
 
            def get_display_name(self):
574
 
                return 'test-facebook-contacts'
575
 
            def get_uid(self):
576
 
                return 1345245
577
 
 
578
 
        reg_mock = self.protocol._source_registry = mock.Mock()
579
 
        reg_mock.list_sources.return_value = [FakeSource()]
580
 
        reg_mock.ref_source = lambda x: x
581
 
        self.protocol._address_book = 'test-facebook-contacts'
582
 
        result = self.protocol._get_eds_source()
583
 
        self.assertEqual(result, 1345245)
 
605
    def test_first_run_prepare_eds_connections(self):
 
606
        self.protocol._name = 'testsuite'
 
607
        self.assertIsNone(self.protocol._address_book_name)
 
608
        self.assertIsNone(self.protocol._eds_source_registry)
 
609
        self.assertIsNone(self.protocol._eds_source)
 
610
        self.assertIsNone(self.protocol._book_client)
 
611
        self.protocol._prepare_eds_connections()
 
612
        self.assertEqual(self.protocol._address_book_name,
 
613
                         'friends-testsuite-contacts')
 
614
        self.assertEqual(self.protocol._eds_source.get_display_name(),
 
615
                         'friends-testsuite-contacts')
 
616
        self.assertEqual(self.protocol._eds_source.get_uid(),
 
617
                         'friends-testsuite-contacts')
 
618
        self.protocol.delete_contacts()
 
619
 
 
620
    @mock.patch('gi.repository.EDataServer.SourceRegistry')
 
621
    @mock.patch('gi.repository.EDataServer.Source')
 
622
    @mock.patch('gi.repository.EBook.BookClient')
 
623
    def test_mocked_prepare_eds_connections(self, client, source, registry):
 
624
        self.protocol._name = 'testsuite'
 
625
        self.assertIsNone(self.protocol._address_book_name)
 
626
        self.protocol._prepare_eds_connections()
 
627
        self.protocol._prepare_eds_connections() # Second time harmlessly ignored
 
628
        self.assertEqual(self.protocol._address_book_name,
 
629
                         'friends-testsuite-contacts')
 
630
        registry.new_sync.assert_called_once_with(None)
 
631
        self.assertEqual(self.protocol._eds_source_registry,
 
632
                         registry.new_sync())
 
633
        registry.new_sync().ref_source.assert_called_once_with(
 
634
            'friends-testsuite-contacts')
 
635
        self.assertEqual(self.protocol._eds_source,
 
636
                         registry.new_sync().ref_source())
 
637
        client.connect_sync.assert_called_once_with(
 
638
            registry.new_sync().ref_source(), None)
 
639
        self.assertEqual(self.protocol._book_client,
 
640
                         client.connect_sync())
 
641
 
 
642
    @mock.patch('gi.repository.EDataServer.SourceRegistry')
 
643
    @mock.patch('gi.repository.EDataServer.Source')
 
644
    @mock.patch('gi.repository.EBook.BookClient')
 
645
    def test_create_new_eds_book(self, client, source, registry):
 
646
        self.protocol._name = 'testsuite'
 
647
        self.assertIsNone(self.protocol._address_book_name)
 
648
        registry.new_sync().ref_source.return_value = None
 
649
        registry.reset_mock()
 
650
        self.protocol._prepare_eds_connections()
 
651
        self.protocol._prepare_eds_connections() # Second time harmlessly ignored
 
652
        self.assertEqual(self.protocol._address_book_name,
 
653
                         'friends-testsuite-contacts')
 
654
        registry.new_sync.assert_called_once_with(None)
 
655
        self.assertEqual(self.protocol._eds_source_registry,
 
656
                         registry.new_sync())
 
657
        registry.new_sync().ref_source.assert_called_once_with(
 
658
            'friends-testsuite-contacts')
 
659
        source.new_with_uid.assert_called_once_with(
 
660
            'friends-testsuite-contacts', None)
 
661
        self.assertEqual(self.protocol._eds_source,
 
662
                         source.new_with_uid())
 
663
        source.new_with_uid().set_display_name.assert_called_once_with(
 
664
            'friends-testsuite-contacts')
 
665
        source.new_with_uid().set_parent.assert_called_once_with('local-stub')
 
666
        source.new_with_uid().get_extension.assert_called_once_with(
 
667
            EDataServer.SOURCE_EXTENSION_ADDRESS_BOOK)
 
668
        registry.new_sync().commit_source_sync.assert_called_once_with(
 
669
            source.new_with_uid(), None)
 
670
        client.connect_sync.assert_called_once_with(
 
671
            source.new_with_uid(), None)
 
672
        self.assertEqual(self.protocol._book_client,
 
673
                         client.connect_sync())