updated and cleaned up Gravatar
[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_several_things(self):
22         contact = FakeContact([known_address])
23         self._fake_server_response({known_address: 'http://image.exists.here/',
24                                     'extra@here': 'http://nowhere'})
25         
26         self.testee.pre_process_contact(contact);
27         self.testee.process_friends()
28         friend = self.testee.process_contact(contact)
29         
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
33         
34         
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/'})
38         
39         self.testee.pre_process_contact(contact);
40         self.testee.process_friends()
41         friend = self.testee.process_contact(contact)
42         assert isinstance(friend, Friend)
43         
44         
45     def test_that_process_contact_returns_None_for_unknown_contact(self):
46         contact = FakeContact([known_address])
47         self._fake_server_response({})
48         
49         self.testee.pre_process_contact(contact);
50         self.testee.process_friends()
51         friend = self.testee.process_contact(contact)
52         assert friend is None
53         
54         
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/"})
58         
59         self.testee.pre_process_contact(self.multiple_contact)
60         self.testee.process_friends()
61         self.testee.process_contact(self.multiple_contact)
62         
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
72         
73         
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)
79             map[hash] = map[key]
80         self._server_response = map
81     
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
86
87     def _setUp(self, email, key):
88         self.testee = Service('gravatar', email, key)
89         
90         self.existing_address = 'fredrik@wendt.se'
91         self.existing_contact = FakeContact([self.existing_address])
92         self.existing_friend = Friend("Fredrik Wendt")
93         
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")
97         
98         self.multiple_contact = FakeContact([self.existing_address, self.missing_address])
99         self.multiple_friend = Friend("Another Person")
100
101     
102 if __name__ == '__main__':
103     unittest.main()