Exemplo n.º 1
0
class Test(unittest.TestCase):
    ''' urllib2.urlopen("https://graph.facebook.com/" + path + "?" +
                                  urllib.urlencode(args), post_data)
               
        urllib.urlencode(post_args)
        
        urllib.urlopen("https://api.facebook.com/method/" + path + "?" +
                              urllib.urlencode(args), post_data)        
                              
        responds with.. <socket._fileobject>
        me url response = {"id":"8","name":"Joe Facebook","first_name":"Joe","last_name":"Facebook","link":"http:\/\/www.facebook.com\/profile.php?id=8","hometown":{"id":"1","name":"AnyTown, Illinois"},"location":{"id":"1","name":"Chicago, Illinois"},"gender":"male","timezone":-6,"locale":"en_US","verified":true,"updated_time":"2011-11-22T05:28:53+0000"}
    '''
    
    def setUp(self):
        # set up the mock return for urllib2.urlopen()
        self.mock_opener = Mock(urllib2.OpenerDirector)
        
        # the open graph we will test with        
        self.graph = GraphAPI("access_token")
        logging.getLogger('facebook_sdk')
        logging.basicConfig(level=logging.DEBUG)
        


    def tearDown(self):
        pass


    def test_get_object_me_raise_error(self):
        ''' test that the MainTypeError is raised when no main type is set on the response '''
        self.mock_opener.open.return_value = FaceBookResponse("dummy","dummy")
        urllib2.install_opener(self.mock_opener)

        self.assertRaises(MainTypeError, self.graph.get_object, "me")

    def test_get_object_me(self):
        # file data that gets responded from facebook.
        
        me_str = "{\"id\":\"123456789\",\"name\":\"Grey Beard\",\"first_name\":\"Grey\",\"last_name\":\"Beard\",\"link\":\"http:\\/\\/www.facebook.com\\/profile.php?id=1\",\"hometown\":{\"id\":\"1\",\"name\":\"Any Town, Illinois\"},\"location\":{\"id\":\"1\",\"name\":\"Chicago, Illinois\"},\"gender\":\"male\",\"timezone\":-6,\"locale\":\"en_US\",\"verified\":\"true\",\"updated_time\":\"2011-11-22T05:28:53+0000\"}"
        
        # mock out the socket._fileobject that gets returned from the open graph. 
        fb_response = FaceBookResponse(me_str, "text")
        
        self.mock_opener.open.return_value = fb_response
        urllib2.install_opener(self.mock_opener)
        
        user = self.graph.get_object("me")
        
        self.assertEquals("123456789", user['id'], 'Ids dont match' )
        self.assertEquals("Grey Beard", user['name'], 'names dont match')
    
    def test_get_object_me_pic(self):
        fb_response = FaceBookResponse("yo", "image", "dummyurl")
        self.mock_opener.open.return_value = fb_response
        urllib2.install_opener(self.mock_opener)
        
        user = self.graph.get_object("me/picture")
        
        self.assertEquals("dummyurl", user['url'], 'urls dont match' )
        self.assertEquals("image/jpeg", user['mime-type'], 'names dont match')
Exemplo n.º 2
0
 def setUp(self):
     # set up the mock return for urllib2.urlopen()
     self.mock_opener = Mock(urllib2.OpenerDirector)
     
     # the open graph we will test with        
     self.graph = GraphAPI("access_token")
     logging.getLogger('facebook_sdk')
     logging.basicConfig(level=logging.DEBUG)