added url argument to block_for_auth, fixed tests for LinkedIn
authorFredrik Wendt <fredrik@wendt.se>
Tue, 8 Jun 2010 22:59:16 +0000 (23:59 +0100)
committerFredrik Wendt <fredrik@wendt.se>
Tue, 8 Jun 2010 22:59:16 +0000 (23:59 +0100)
Signed-off-by: Fredrik Wendt <fredrik@wendt.se>

package/src/org/maemo/hermes/engine/linkedin/api.py
package/test/integration/test_linkedinapi.py
package/test/manual/test_linkedinapi.py

index 0d9eb24..ceb2c00 100644 (file)
@@ -43,15 +43,13 @@ class LinkedInApi():
         self.consumer = oauth.OAuthConsumer(api_key, secret_key)
         self.sig_method = oauth.OAuthSignatureMethod_HMAC_SHA1()
         
-        self._verify_browser_command()
-
 
     # -----------------------------------------------------------------------
     def authenticate(self, need_auth, block_for_auth):
         need_auth()
         token = self._get_request_token()
         url = self._get_authorize_url(token)
-        verifier = block_for_auth(True)
+        verifier = block_for_auth(True, url)
         self._verify_verifier(token, verifier)
     
     
@@ -158,6 +156,9 @@ class LinkedInApi():
         try:
             token = oauth.OAuthToken.from_string(response)
         except Exception, e:
+            import traceback
+            traceback.print_exc()
+            print response
             raise Exception("Authorization failure - failed to get request token")
         return token
 
@@ -184,8 +185,8 @@ class LinkedInApi():
         connection.request(oauth_request.http_method, self.ACCESS_TOKEN_URL, headers=oauth_request.to_header()) 
         response = connection.getresponse()
         token_str = response.read()
-        if "ouath_problem" in token_str:
-            raise Exception("Authorization failure - failed to get access token")
+        if 'oauth_problem' in token_str:
+            raise Exception("Authorization failure - failed to get access token (" + token_str + ")")
         self._store_access_token_in_gconf(token_str)
         return oauth.OAuthToken.from_string(token_str)
 
@@ -197,15 +198,7 @@ class LinkedInApi():
         except Exception, e:
             import traceback
             traceback.print_exc()
-            raise Exception("LinkedIn authorization failed, try again (" + e + ")")
-
-
-    # -----------------------------------------------------------------------
-    def _verify_browser_command(self):
-        # -- Check the environment is going to work...
-        # FIXME: duplication
-        if (self._gc.get_string('/desktop/gnome/url-handlers/http/command') == 'epiphany %s'):
-            raise Exception('Browser in gconf invalid (see NB#136012). Installation error.')
+            raise Exception("LinkedIn authorization failed, try again")
 
 
     # -----------------------------------------------------------------------
index 0547300..2e38d9f 100644 (file)
@@ -50,22 +50,23 @@ class IntegrationTestLinkedInApi(unittest.TestCase):
         #if True:
             self.testee.authenticate(self._need_auth_cb, self._block_for_auth)
             assert False # should always go to except clause
-        except:
+        except Exception, e:
             authenticate_threw_exception = True
         
+        assert authenticate_threw_exception
         assert self.need_auth_called
         assert self.block_for_auth_called
         assert self.block_for_auth_show_input_field
-        assert authenticate_threw_exception
 
 
     def _need_auth_cb(self):
         self.need_auth_called = True
 
 
-    def _block_for_auth(self, show_input_field):
+    def _block_for_auth(self, show_input_field, url=None):
         self.block_for_auth_called = True
         self.block_for_auth_show_input_field = show_input_field
+        self.block_for_auth_url = url
         return self.verifier
 
     
index ec5b486..bb7f7cf 100644 (file)
@@ -3,6 +3,7 @@ from org.maemo.hermes.engine.linkedin.api import LinkedInApi
 from org.maemo.hermes.engine.names import canonical
 from org.maemo.hermes.engine.friend import Friend
 import unittest, sys
+import httplib
 
 def log(msg):
     sys.__stderr__.write(msg + "\n")
@@ -38,10 +39,10 @@ class TestLinkedInApi(unittest.TestCase):
         # uncomment next line if you need to authorize ...
         #self.testee.authenticate(self._need_auth_cb, self._block_for_auth)
         # ... or uncomment the following four lines - replacing the oauth_token string with whatever you got
-        #self.gconf.set_string(
-        #          LinkedInApi.GCONF_ACCESS_TOKEN, 
-        #          'oauth_token=f89c2b7b-1c12-4f83-a469-838e78901716&oauth_token_secret=60f817af-6437-4015-962f-cc3aefee0264')
-        #self.testee = LinkedInApi(self.gconf)
+        self.gconf.set_string(
+                  LinkedInApi.GCONF_ACCESS_TOKEN, 
+                  'oauth_token=f89c2b7b-1c12-4f83-a469-838e78901716&oauth_token_secret=60f817af-6437-4015-962f-cc3aefee0264')
+        self.testee = LinkedInApi(self.gconf)
         
         # uncomment to see the raw xml feed for all connections
         #xml = self.testee._make_api_request(LinkedInApi.LI_CONN_API_URL)
@@ -62,7 +63,7 @@ class TestLinkedInApi(unittest.TestCase):
         self.need_auth_called = True
 
 
-    def _block_for_auth(self, url):
+    def _block_for_auth(self, verifier, url=None):
         import os, time
         file = "/tmp/verifier"
         verifier = None