~robru/friends/simplify-contacts

« back to all changes in this revision

Viewing changes to friends/tests/test_facebook.py

  • Committer: Robert Bruce Park
  • Date: 2013-07-23 08:17:16 UTC
  • Revision ID: robert.park@canonical.com-20130723081716-chhwxomauc3x5o5n
More explicit arguments for _push_to_eds rather than an obscure dict.

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, EDataServer
 
29
from gi.repository import GLib, EDataServer, EBookContacts
30
30
from pkg_resources import resource_filename
31
31
 
32
32
from friends.protocols.facebook import Facebook
501
501
                       params={'access_token': 'broken'})])
502
502
        self.assertEqual(
503
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'})])
 
504
            [mock.call(gender='male', jabber='-contact1@chat.facebook.com',
 
505
                       nick='jblow', link='example.com', name='Joe Blow',
 
506
                       uid='contact1'),
 
507
             mock.call(gender='male', jabber='-contact2@chat.facebook.com',
 
508
                       nick='jblow', link='example.com', name='Joe Blow',
 
509
                       uid='contact2')])
524
510
 
525
511
    def test_create_contact(self, *mocks):
526
512
        # Receive the users friends.
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
 
            }}
538
 
        eds_contact = self.protocol._create_contact(bare_contact)
 
513
        eds_contact = self.protocol._create_contact(
 
514
            uid='555555555',
 
515
            name='Lucy Baron',
 
516
            nick='lucy.baron5',
 
517
            gender='female',
 
518
            link='http:www.facebook.com/lucy.baron5',
 
519
            jabber='-555555555@chat.facebook.com')
539
520
        facebook_id_attr = eds_contact.get_attribute('facebook-id')
540
521
        self.assertEqual(facebook_id_attr.get_value(), '555555555')
541
 
        facebook_name_attr = eds_contact.get_attribute('facebook-name')
542
 
        self.assertEqual(facebook_name_attr.get_value(), 'Lucy Baron')
543
522
        web_service_addrs = eds_contact.get_attribute('X-FOLKS-WEB-SERVICES-IDS')
544
523
        params= web_service_addrs.get_params()
545
 
 
546
 
        self.assertEqual(len(params), 3)
547
 
 
548
 
        test_jabber = False
549
 
        test_remote_name = False
550
 
        test_facebook_id = False
551
 
 
552
 
        for p in params:
553
 
            if p.get_name() == 'jabber':
554
 
                self.assertEqual(len(p.get_values()), 1)
555
 
                self.assertEqual(p.get_values()[0], '-555555555@chat.facebook.com')
556
 
                test_jabber = True
557
 
            if p.get_name() == 'remote-full-name':
558
 
                self.assertEqual(len(p.get_values()), 1)
559
 
                self.assertEqual(p.get_values()[0], 'Lucy Baron')
560
 
                test_remote_name = True
561
 
            if p.get_name() == 'facebook-id':
562
 
                self.assertEqual(len(p.get_values()), 1)
563
 
                self.assertEqual(p.get_values()[0], '555555555')
564
 
                test_facebook_id = True
565
 
        # Finally test to ensure all key value pairs were tested
566
 
        self.assertTrue(test_jabber and test_remote_name and test_facebook_id)
 
524
        self.assertEqual(len(params), 5)
 
525
 
 
526
        # Can't compare the vcard string directly because it is sorted randomly...
 
527
        vcard = eds_contact.to_string(
 
528
            EBookContacts.VCardFormat(1)).replace('\r\n ', '')
 
529
        self.assertIn(
 
530
            'social-networking-attributes.X-URIS:http:www.facebook.com/lucy.baron5',
 
531
            vcard)
 
532
        self.assertIn(
 
533
            'social-networking-attributes.X-GENDER:female',
 
534
            vcard)
 
535
        self.assertIn(
 
536
            'social-networking-attributes.facebook-id:555555555',
 
537
            vcard)
 
538
        self.assertIn(
 
539
            'FN:Lucy Baron',
 
540
            vcard)
 
541
        self.assertIn(
 
542
            'NICKNAME:lucy.baron5',
 
543
            vcard)
 
544
        self.assertIn(
 
545
            'social-networking-attributes.X-FOLKS-WEB-SERVICES-IDS;',
 
546
            vcard)
 
547
        self.assertIn(
 
548
            'remote-full-name="Lucy Baron"',
 
549
            vcard)
 
550
        self.assertIn(
 
551
            'facebook-id=555555555',
 
552
            vcard)
 
553
        self.assertIn(
 
554
            'jabber="-555555555@chat.facebook.com"',
 
555
            vcard)
 
556
        self.assertIn(
 
557
            'facebook-nick="lucy.baron5"',
 
558
            vcard)
567
559
 
568
560
    @mock.patch('friends.utils.base.Base._prepare_eds_connections',
569
561
                return_value=True)
571
563
                return_value=EDSBookClientMock())
572
564
    def test_successfull_push_to_eds(self, *mocks):
573
565
        bare_contact = {'name': 'Lucy Baron',
574
 
                        'id': '555555555',
575
 
                        'username': 'lucy.baron5',
 
566
                        'uid': '555555555',
 
567
                        'nick': 'lucy.baron5',
576
568
                        'link': 'http:www.facebook.com/lucy.baron5'}
577
569
        self.protocol._address_book = 'test-address-book'
578
570
        client = self.protocol._book_client = mock.Mock()
579
571
        client.add_contact_sync.return_value = True
580
572
        # Implicitely fail test if the following raises any exceptions
581
 
        self.protocol._push_to_eds(bare_contact)
 
573
        self.protocol._push_to_eds(**bare_contact)
582
574
 
583
575
    @mock.patch('friends.utils.base.Base._prepare_eds_connections',
584
576
                return_value=None)
585
577
    def test_unsuccessfull_push_to_eds(self, *mocks):
586
578
        bare_contact = {'name': 'Lucy Baron',
587
 
                        'id': '555555555',
588
 
                        'username': 'lucy.baron5',
 
579
                        'uid': '555555555',
 
580
                        'nick': 'lucy.baron5',
589
581
                        'link': 'http:www.facebook.com/lucy.baron5'}
590
582
        self.protocol._address_book = 'test-address-book'
591
583
        client = self.protocol._book_client = mock.Mock()
593
585
        self.assertRaises(
594
586
            ContactsError,
595
587
            self.protocol._push_to_eds,
596
 
            bare_contact,
 
588
            **bare_contact
597
589
            )
598
590
 
599
591
    @mock.patch('gi.repository.EBook.BookClient.connect_sync',