077145b8613e9ec966022374c27376e3a08ddd8f
[hermes] / package / test / 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 class TestGravatarService(unittest.TestCase):
14     
15     def setUp(self):
16         self._setUp('maemohermes@wendt.se', 'b14ec179822b')
17         
18     def test_main_flow(self):
19         self.testee.pre_process_contact(self.existing_contact)
20         self.testee.pre_process_contact(self.missing_contact)
21         self.testee.process_friends()
22         self.testee.process_contact(self.existing_contact)
23         self.testee.process_contact(self.missing_contact)
24         
25         friends = self.testee.get_friends()
26         contacts = self.testee.get_contacts_with_match()
27         assert len(friends) == 1
28         assert len(contacts) == 1
29         assert self.missing_contact not in contacts.keys()
30         assert self.existing_contact in contacts.keys()
31         friend = friends[0]
32         assert friend.get_name() == self.existing_contact.get_name()
33         print friend.get_photo_url()
34         
35         
36     def test_that_a_person_with_two_addresses_and_one_gravatar_works(self):
37         self._fake_server_response({self.missing_address: None, self.existing_address: "http://url.to.img/"})
38         
39         self.testee.pre_process_contact(self.multiple_contact)
40         self.testee.process_friends()
41         self.testee.process_contact(self.multiple_contact)
42         
43         friends = self.testee.get_friends()
44         contacts = self.testee.get_contacts_with_match()
45         assert len(friends) == 1
46         assert len(contacts) == 1
47         assert self.multiple_contact in contacts
48         assert self.missing_contact not in contacts
49         assert self.existing_contact not in contacts.keys()
50         assert friends[0].get_name() == self.existing_contact.get_name()
51         
52         
53     def _fake_server_response(self, map):
54         self.testee._get_hash_info_from_server = self._get_hash_info_from_server
55         # in real results the addresses hashed, so we'll add that here, and keep originals for easier debugging/inspection
56         for key in map.keys():
57             hash = self.testee._get_hash_for_address(key)
58             map[hash] = map[key]
59         self._server_response = map
60     
61     def _get_hash_info_from_server(self, args, url):
62         self._server_args = args
63         self._server_url = url
64         return self._server_response
65
66     def _setUp(self, email, key):
67         self.testee = Service(email, key)
68         
69         self.existing_address = 'fredrik@wendt.se'
70         self.existing_contact = FakeContact([self.existing_address])
71         self.existing_friend = Friend("Fredrik Wendt")
72         
73         self.missing_address = 'will_not_ever_exist_i_truly_hope_at_least@wendt.se'
74         self.missing_contact = FakeContact([self.missing_address])
75         self.missing_friend = Friend("Unknown Person")
76         
77         self.multiple_contact = FakeContact([self.existing_address, self.missing_address])
78         self.multiple_friend = Friend("Another Person")
79
80     
81 if __name__ == '__main__':
82     unittest.main()