3de6b326afd900779c7d7e8360bd51e17d6a4ac0
[hermes] / package / test / unit / test_gravatar.py
1 from org.maemo.hermes.engine.gravatar.service import Service
2 from org.maemo.hermes.engine.friend import Friend 
3 import unittest
4
5 class FakeContact():
6     def __init__(self, addr):
7         self.urls = addr
8     def get_emails(self):
9         return self.urls
10     def get_name(self):
11         return self.urls[0]
12
13 known_address = "maemohermes@wendt.se"
14     
15 class TestGravatarService(unittest.TestCase):
16     
17     def setUp(self):
18         self._setUp('', '')
19         
20         
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/'})
24         
25         self.testee.pre_process_contact(contact);
26         self.testee.process_friends()
27         friend = self.testee.process_contact(contact)
28         assert isinstance(friend, Friend)
29         
30         
31     def test_that_process_contact_returns_None_for_unknown_contact(self):
32         contact = FakeContact([known_address])
33         self._fake_server_response({})
34         
35         self.testee.pre_process_contact(contact);
36         self.testee.process_friends()
37         friend = self.testee.process_contact(contact)
38         assert friend is None
39         
40         
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/"})
44         
45         self.testee.pre_process_contact(self.multiple_contact)
46         self.testee.process_friends()
47         self.testee.process_contact(self.multiple_contact)
48         
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
58         
59         
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)
65             map[hash] = map[key]
66         self._server_response = map
67     
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
72
73     def _setUp(self, email, key):
74         self.testee = Service(email, key)
75         
76         self.existing_address = 'fredrik@wendt.se'
77         self.existing_contact = FakeContact([self.existing_address])
78         self.existing_friend = Friend("Fredrik Wendt")
79         
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")
83         
84         self.multiple_contact = FakeContact([self.existing_address, self.missing_address])
85         self.multiple_friend = Friend("Another Person")
86
87     
88 if __name__ == '__main__':
89     unittest.main()