474
475
params=dict(access_token='face'))
475
476
unpublish.assert_called_once_with('post_id')
477
@mock.patch('friends.utils.http.Soup.Message',
478
FakeSoupMessage('friends.tests.data', 'facebook-contacts.dat'))
479
@mock.patch('friends.protocols.facebook.Facebook._login',
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',
495
[mock.call('contact1'), mock.call('contact2')])
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'})])
505
'X-FOLKS-WEB-SERVICES-IDS': {
506
'jabber': '-contact1@chat.facebook.com',
507
'facebook-id': 'contact1',
508
'remote-full-name': 'Joe Blow'},
510
'facebook-name': 'Joe Blow',
511
'facebook-id': 'contact1',
512
'X-URIS': 'example.com',
513
'facebook-nick': 'jblow'}),
515
'X-FOLKS-WEB-SERVICES-IDS': {
516
'jabber': '-contact2@chat.facebook.com',
517
'facebook-id': 'contact2',
518
'remote-full-name': 'Joe Blow'},
520
'facebook-name': 'Joe Blow',
521
'facebook-id': 'contact2',
522
'X-URIS': 'example.com',
523
'facebook-nick': 'jblow'})])
488
525
def test_create_contact(self, *mocks):
489
526
# Receive the users friends.
490
bare_contact = {'name': 'Lucy Baron',
492
'username': 'lucy.baron5',
493
'link': 'http:www.facebook.com/lucy.baron5'}
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',
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')
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)
538
@mock.patch('friends.utils.base.Base._get_eds_source',
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(
551
595
self.protocol._push_to_eds,
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
561
result = self.protocol._create_eds_source()
562
self.assertEqual(result, 'test-source-uid')
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)
571
def test_successful_get_eds_source(self, *mocks):
573
def get_display_name(self):
574
return 'test-facebook-contacts'
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()
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,
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())
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,
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())