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_that_process_contact_returns_friend_object_if_contact_is_known(self):
22 contact = FakeContact([known_address])
23 self._fake_server_response({known_address: 'http://image.exists.here/'})
25 self.testee.pre_process_contact(contact);
26 self.testee.process_friends()
27 friend = self.testee.process_contact(contact)
28 assert isinstance(friend, Friend)
31 def test_that_process_contact_returns_None_for_unknown_contact(self):
32 contact = FakeContact([known_address])
33 self._fake_server_response({})
35 self.testee.pre_process_contact(contact);
36 self.testee.process_friends()
37 friend = self.testee.process_contact(contact)
41 def test_that_a_person_with_two_addresses_and_one_gravatar_works(self):
42 self._fake_server_response({self.missing_address: None,
43 self.existing_address: "http://url.to.img/"})
45 self.testee.pre_process_contact(self.multiple_contact)
46 self.testee.process_friends()
47 self.testee.process_contact(self.multiple_contact)
49 friends = self.testee.get_friends()
50 contacts = self.testee.get_contacts_with_match()
51 assert len(friends) == 1
52 assert len(contacts) == 1
53 assert self.multiple_contact in contacts
54 assert self.missing_contact not in contacts
55 assert self.existing_contact not in contacts.keys()
56 assert friends[0].get_name() == self.existing_contact.get_name()
57 assert friends[0].get_contact() == self.multiple_contact
60 def _fake_server_response(self, map):
61 self.testee._get_hash_info_from_server = self._get_hash_info_from_server
62 # in real results the addresses hashed, so we'll add that here, and keep originals for easier debugging/inspection
63 for key in map.keys():
64 hash = self.testee._get_hash_for_address(key)
66 self._server_response = map
68 def _get_hash_info_from_server(self, args, url):
69 self._server_args = args
70 self._server_url = url
71 return self._server_response
73 def _setUp(self, email, key):
74 self.testee = Service(email, key)
76 self.existing_address = 'fredrik@wendt.se'
77 self.existing_contact = FakeContact([self.existing_address])
78 self.existing_friend = Friend("Fredrik Wendt")
80 self.missing_address = 'will_not_ever_exist_i_truly_hope_at_least@wendt.se'
81 self.missing_contact = FakeContact([self.missing_address])
82 self.missing_friend = Friend("Unknown Person")
84 self.multiple_contact = FakeContact([self.existing_address, self.missing_address])
85 self.multiple_friend = Friend("Another Person")
88 if __name__ == '__main__':