1 from org.maemo.hermes.engine.gravatar.service import Service
2 from org.maemo.hermes.engine.friend import Friend
6 def __init__(self, addr):
13 known_address = "maemohermes@wendt.se"
15 class TestGravatarService(unittest.TestCase):
21 def test_several_things(self):
22 contact = FakeContact([known_address])
23 self._fake_server_response({known_address: 'http://image.exists.here/',
24 'extra@here': 'http://nowhere'})
26 self.testee.pre_process_contact(contact);
27 self.testee.process_friends()
28 friend = self.testee.process_contact(contact)
30 assert isinstance(friend, Friend)
31 assert len(self.testee.get_friends_to_create_contacts_for()) == 0
32 assert len(self.testee.get_unmatched_friends()) == 0 # doesn't matter should always
35 def test_that_process_contact_returns_friend_object_if_contact_is_known(self):
36 contact = FakeContact([known_address])
37 self._fake_server_response({known_address: 'http://image.exists.here/'})
39 self.testee.pre_process_contact(contact);
40 self.testee.process_friends()
41 friend = self.testee.process_contact(contact)
42 assert isinstance(friend, Friend)
45 def test_that_process_contact_returns_None_for_unknown_contact(self):
46 contact = FakeContact([known_address])
47 self._fake_server_response({})
49 self.testee.pre_process_contact(contact);
50 self.testee.process_friends()
51 friend = self.testee.process_contact(contact)
55 def test_that_a_person_with_two_addresses_and_one_gravatar_works(self):
56 self._fake_server_response({self.missing_address: None,
57 self.existing_address: "http://url.to.img/"})
59 self.testee.pre_process_contact(self.multiple_contact)
60 self.testee.process_friends()
61 self.testee.process_contact(self.multiple_contact)
63 friends = self.testee._get_friends()
64 contacts = self.testee._get_contacts_with_match()
65 assert len(friends) == 1
66 assert len(contacts) == 1
67 assert self.multiple_contact in contacts
68 assert self.missing_contact not in contacts
69 assert self.existing_contact not in contacts.keys()
70 assert friends[0].get_name() == self.existing_contact.get_name()
71 assert friends[0].get_contact() == self.multiple_contact
74 def _fake_server_response(self, map):
75 self.testee._get_hash_info_from_server = self._get_hash_info_from_server
76 # in real results the addresses hashed, so we'll add that here, and keep originals for easier debugging/inspection
77 for key in map.keys():
78 hash = self.testee._get_hash_for_address(key)
80 self._server_response = map
82 def _get_hash_info_from_server(self, args, url):
83 self._server_args = args
84 self._server_url = url
85 return self._server_response
87 def _setUp(self, email, key):
88 self.testee = Service('gravatar', email, key)
90 self.existing_address = 'fredrik@wendt.se'
91 self.existing_contact = FakeContact([self.existing_address])
92 self.existing_friend = Friend("Fredrik Wendt")
94 self.missing_address = 'will_not_ever_exist_i_truly_hope_at_least@wendt.se'
95 self.missing_contact = FakeContact([self.missing_address])
96 self.missing_friend = Friend("Unknown Person")
98 self.multiple_contact = FakeContact([self.existing_address, self.missing_address])
99 self.multiple_friend = Friend("Another Person")
102 if __name__ == '__main__':