1 from org.maemo.hermes.engine.facebook.service import Service
2 from org.maemo.hermes.engine.facebook.api import FacebookApi
3 from org.maemo.hermes.engine.names import canonical
4 from org.maemo.hermes.engine.friend import Friend
10 def __init__(self, name, addr, id=None):
13 self.id = id or FakeContact.id_counter
14 FakeContact.id_counter = FakeContact.id_counter + 1;
19 def get_identifiers(self):
20 return [canonical(self.name)]
22 class FakeFacebookApi():
25 def get_friends(self):
26 return FakeFacebookApi.friends
29 def authorise(self, authorise_url, access_token_url, args = None):
32 def request(self, url, args = None):
33 if url == 'https://graph.facebook.com/me':
34 return '{"id":"1234567","name":"Maemo Hermes"}'
36 elif url == 'https://graph.facebook.com/me/friends':
37 return '{"data":[{"id":"1","name":"J Smith","website":"http:\/\/www.example.org"},{"id":"2","name":"P Barnum"}]}'
40 raise Exception("Unknown URL: %s" % (url))
43 class TestFacebookService(unittest.TestCase):
46 self.testee = Service("facebook", FakeFacebookApi())
49 def test_that_get_friends_to_create_contacts_for_works(self):
50 def run_test(expected_length):
51 self._fake_server_response([{'id':'123456','name':'Facebook Junkie', 'birthday':'now'}])
53 friends = self.testee.get_friends_to_create_contacts_for()
54 assert len(friends) == expected_length
56 # default is to NOT create contacts
57 self.testee = Service("facebook", FakeFacebookApi())
60 # passing False explicitly
61 self.testee = Service("facebook", FakeFacebookApi(), False)
64 # passing True to constructor
65 self.testee = Service("facebook", FakeFacebookApi(), True)
69 def test_that_gftccf_returns_friends_with_birth_date(self):
70 self.testee = Service("facebook", FakeFacebookApi(), True)
72 props_with_bday = {'id':'123456','name':'Facebook Junkie', 'birthday':bday}
73 props_without = {'id':'123457','name':'Johnny Secret'}
74 self._fake_server_response([props_with_bday, props_without])
77 assert len(self.testee.get_friends()) == 2
78 friends = self.testee.get_friends_to_create_contacts_for()
79 assert len(friends) == 1
80 assert friends[0].has_birthday_date()
81 assert friends[0].get_birthday_date() == bday
84 def test_that_process_contact_returns_friend_object_for_known_contact(self):
85 known_url = 'http://www.facebook.com/profile.php?id=123456'
86 known_contact = FakeContact('Facebook Junkie', [known_url])
87 self._fake_server_response([{'id':'123456','name':'Facebook Junkie'}])
89 self.testee.process_friends()
90 result = self.testee.process_contact(known_contact)
91 assert isinstance(result, Friend)
94 def test_that_process_contact_returns_None_for_unknown_contact(self):
95 known_contact = FakeContact('Facebook Junkie', [])
96 self._fake_server_response([])
98 self.testee.process_friends()
99 result = self.testee.process_contact(known_contact)
100 assert result is None
103 def test_main_flow_one_match_by_url_one_by_name(self):
105 self.existing_address = 'http://www.facebook.com/profile.php?id=123456'
106 self.existing_contact = FakeContact("Facebook Person", [self.existing_address])
107 existing_fake = {'id':'123456','name':'Name Doesnt Match but URL Does'}
109 self.other_address = 'http://twitter.com/not/correct/site'
110 self.other_contact = FakeContact("Twitter Person", [self.other_address])
111 other_fake = {'id':'123','name':self.other_contact.get_name()}
113 self.none_contact = FakeContact("No URLson", [])
115 fake_data = [existing_fake, other_fake]
116 self._fake_server_response(fake_data)
119 self._run_service([self.existing_contact, self.other_contact, self.none_contact])
122 friends = self.testee.get_friends()
123 contacts = self.testee.get_contacts_with_match()
124 assert len(friends) == 2
125 assert len(contacts) == 2
126 assert self.other_contact in contacts
127 assert self.other_contact == contacts[self.other_contact].get_contact()
128 assert self.existing_contact in contacts
129 assert self.existing_contact == contacts[self.existing_contact].get_contact()
130 assert self.none_contact not in contacts
133 def test_name_collision_avoided_by_previous_matching(self):
135 contact_do_match = FakeContact(name, ["http://www.facebook.com/profile.php?id=123"], 1);
136 contact_no_match = FakeContact(name, [None], 2)
138 data = [{'id':'123','name':name}]
139 self._fake_server_response(data)
141 self._run_service([contact_no_match, contact_do_match])
143 assert len(self.testee.get_unmatched_friends()) == 0
144 matchers = self.testee.get_contacts_with_match().keys()
145 assert len(matchers) == 1
146 assert matchers[0].id == 1
149 def test_name_collision_avoided_only_one_person_matched(self):
151 contact_do_match = FakeContact(name, ["Contact 1"]);
152 contact_no_match = FakeContact(name, ["Contact 2"])
154 data = [{'id':'123','name':name}]
155 self._fake_server_response(data)
157 self._run_service([contact_no_match, contact_do_match])
159 matchers = self.testee.get_contacts_with_match().keys()
160 assert len(matchers) == 1
161 assert len(self.testee.get_unmatched_friends()) == 0
164 def _run_service(self, contacts):
165 for contact in contacts:
166 self.testee.pre_process_contact(contact)
167 self.testee.process_friends()
168 for contact in contacts:
169 self.testee.process_contact(contact)
171 def _fake_server_response(self, data):
172 FakeFacebookApi.friends = data
175 class TestFacebookAPI(unittest.TestCase):
178 self.oauth = FakeOAuth()
179 self.api = FacebookApi(self.oauth)
181 def test_authenticate(self):
185 def test_get_user(self):
186 assert self.api.get_user() == 'Maemo Hermes'
189 def test_get_friends(self):
190 friends = self.api.get_friends()
191 assert len(friends) == 2
192 assert friends[0]['name'] == 'J Smith'
193 assert friends[1]['id'] == '2'
196 if __name__ == '__main__':