Re-implement Facebook service to use OAuth2 and Graph API. This allows
[hermes] / package / test / unit / test_facebook.py
index b766d2f..2b3e958 100644 (file)
@@ -1,4 +1,5 @@
 from org.maemo.hermes.engine.facebook.service import Service
+from org.maemo.hermes.engine.facebook.api import FacebookApi
 from org.maemo.hermes.engine.names import canonical
 from org.maemo.hermes.engine.friend import Friend
 import unittest
@@ -18,38 +19,58 @@ class FakeContact():
     def get_identifiers(self):
         return [canonical(self.name)]
     
+class FakeFacebookApi():
+    friends = []
+    
+    def get_friends(self):
+        return FakeFacebookApi.friends
+    
+class FakeOAuth():
+    def authorise(self, authorise_url, access_token_url, args = None):
+        pass
+    
+    def request(self, url, args = None):
+        if url == 'https://graph.facebook.com/me':
+            return '{"id":"1234567","name":"Maemo Hermes"}'
+        
+        elif url == 'https://graph.facebook.com/me/friends':
+            return '{"data":[{"id":"1","name":"J Smith","website":"http:\/\/www.example.org"},{"id":"2","name":"P Barnum"}]}'
+        
+        else:
+            raise Exception("Unknown URL: %s" % (url))
+        
 
 class TestFacebookService(unittest.TestCase):
     
     def setUp(self):
-        self.testee = Service("facebook", None)
+        self.testee = Service("facebook", FakeFacebookApi())
         
     
     def test_that_get_friends_to_create_contacts_for_works(self):
         def run_test(expected_length):
-            self._fake_server_response([{'uid':'123456','name':'Facebook Junkie', 'birthday_date':'now'}])
+            self._fake_server_response([{'id':'123456','name':'Facebook Junkie', 'birthday':'now'}])
             self._run_service([])
             friends = self.testee.get_friends_to_create_contacts_for()
             assert len(friends) == expected_length
             
         # default is to NOT create contacts
-        self.testee = Service("facebook", None)
+        self.testee = Service("facebook", FakeFacebookApi())
         run_test(0)
         
         # passing False explicitly
-        self.testee = Service("facebook", None, False)
+        self.testee = Service("facebook", FakeFacebookApi(), False)
         run_test(0)
         
         # passing True to constructor
-        self.testee = Service("facebook", None, True)
+        self.testee = Service("facebook", FakeFacebookApi(), True)
         run_test(1)
         
     
     def test_that_gftccf_returns_friends_with_birth_date(self):
-        self.testee = Service("facebook", None, True)
+        self.testee = Service("facebook", FakeFacebookApi(), True)
         bday = '1980-10-15'
-        props_with_bday = {'uid':'123456','name':'Facebook Junkie', 'birthday_date':bday}
-        props_without = {'uid':'123457','name':'Johnny Secret'}
+        props_with_bday = {'id':'123456','name':'Facebook Junkie', 'birthday':bday}
+        props_without = {'id':'123457','name':'Johnny Secret'}
         self._fake_server_response([props_with_bday, props_without])
         self._run_service([])
         
@@ -63,7 +84,7 @@ class TestFacebookService(unittest.TestCase):
     def test_that_process_contact_returns_friend_object_for_known_contact(self):
         known_url = 'http://www.facebook.com/profile.php?id=123456'
         known_contact = FakeContact('Facebook Junkie', [known_url])
-        self._fake_server_response([{'uid':'123456','name':'Facebook Junkie'}])
+        self._fake_server_response([{'id':'123456','name':'Facebook Junkie'}])
         
         self.testee.process_friends()
         result = self.testee.process_contact(known_contact)
@@ -83,11 +104,11 @@ class TestFacebookService(unittest.TestCase):
         # arrange
         self.existing_address = 'http://www.facebook.com/profile.php?id=123456'
         self.existing_contact = FakeContact("Facebook Person", [self.existing_address])
-        existing_fake = {'uid':'123456','name':'Name Doesnt Match but URL Does'}
+        existing_fake = {'id':'123456','name':'Name Doesnt Match but URL Does'}
         
         self.other_address = 'http://twitter.com/not/correct/site'
         self.other_contact = FakeContact("Twitter Person", [self.other_address])
-        other_fake = {'uid':'123','name':self.other_contact.get_name()}
+        other_fake = {'id':'123','name':self.other_contact.get_name()}
         
         self.none_contact = FakeContact("No URLson", [])
         
@@ -114,7 +135,7 @@ class TestFacebookService(unittest.TestCase):
         contact_do_match = FakeContact(name, ["http://www.facebook.com/profile.php?id=123"], 1);
         contact_no_match = FakeContact(name, [None], 2)
         
-        data = [{'uid':'123','name':name}]
+        data = [{'id':'123','name':name}]
         self._fake_server_response(data)
         
         self._run_service([contact_no_match, contact_do_match])
@@ -130,7 +151,7 @@ class TestFacebookService(unittest.TestCase):
         contact_do_match = FakeContact(name, ["Contact 1"]);
         contact_no_match = FakeContact(name, ["Contact 2"])
         
-        data = [{'uid':'123','name':name}]
+        data = [{'id':'123','name':name}]
         self._fake_server_response(data)
         
         self._run_service([contact_no_match, contact_do_match])
@@ -148,12 +169,28 @@ class TestFacebookService(unittest.TestCase):
             self.testee.process_contact(contact)
         
     def _fake_server_response(self, data):
-        self.testee._get_friends_data = self._get_friends_data
-        self._server_response = data
-    
-    def _get_friends_data(self):
-        return self._server_response
+        FakeFacebookApi.friends = data
+
 
+class TestFacebookAPI(unittest.TestCase):
+
+    def setUp(self):
+        self.oauth = FakeOAuth()
+        self.api = FacebookApi(self.oauth)
+
+    def test_authenticate(self):
+        pass
+    
+    
+    def test_get_user(self):
+        assert self.api.get_user() == 'Maemo Hermes'
+       
+        
+    def test_get_friends(self):
+        friends = self.api.get_friends()
+        assert len(friends) == 2
+        assert friends[0]['name'] == 'J Smith'
+        assert friends[1]['id'] == '2'
 
     
 if __name__ == '__main__':