def setUp(self):
        super(TestFacebookBatchResponse, self).setUp()

        self.req1 = FacebookRequest(endpoint='123', method=METHOD_GET)
        self.req2 = FacebookRequest(endpoint='123',
                                    method=METHOD_POST,
                                    params={'foo': 'bar'})
        self.batch_request = FakeFacebookBatchRequest(
            requests=[self.req1, self.req2])
        self.response = FacebookResponse(request=self.batch_request,
                                         body=json.dumps([
                                             {
                                                 'headers': {},
                                                 'code': 200,
                                                 'body': {
                                                     'foo': 'bar'
                                                 }
                                             },
                                             {
                                                 'headers': {},
                                                 'code': 200,
                                                 'body': {
                                                     'success': True
                                                 }
                                             },
                                         ]),
                                         http_status_code=200)
    def test_contains_file(self):
        request_with_file = FacebookRequest(
            endpoint='',
            params={
                'message':
                'Awesome Photo',
                'source':
                FacebookFile(path='{base_path}/foo.txt'.format(
                    base_path=os.path.dirname(os.path.abspath(__file__))), )
            })
        request = FacebookRequest(endpoint='')

        self.assertTrue(request_with_file.contain_files())
        self.assertFalse(request.contain_files())
 def test_post_params(self):
     expected_post_params = {'foo': 'bar'}
     request = FacebookRequest(endpoint='',
                               method=METHOD_POST,
                               params=expected_post_params)
     self.assertFalse(request.params)
     self.assertDictEqual(request.post_params, expected_post_params)
Beispiel #4
0
 def test_send_request_with_file(self):
     file_to_upload = FacebookFile(path='{base_path}/foo.txt'.format(
         base_path=os.path.dirname(os.path.abspath(__file__))), )
     request = FacebookRequest(
         endpoint='photos',
         method='POST',
         params={
             'message': 'Awesome Photo',
             'source': file_to_upload,
         },
         access_token='fake_token',
     )
     self.client = FakeFacebookClient(fake_response=self.fake_response, )
     self.client.send_request(request)
     self.assertDictEqual(
         {
             'method': 'POST',
             'url': 'https://graph.facebook.com/v2.5/photos',
             'headers': {},
             'params': {
                 'access_token': 'fake_token'
             },
             'files': [('source',
                        ('foo.txt', file_to_upload, 'text/plain'))],
             'timeout': 60,
             'data': {
                 'message': 'Awesome Photo'
             },
         }, self.client.send_kwargs)
Beispiel #5
0
    def request(
            self,
            method,  # type: Text
            endpoint,  # type: Text
            access_token=None,  # type: Optional[Text]
            params=None,  # type: Optional[Dict]
            headers=None,  # type: Optional[Dict]
            graph_version=None,  # type: Optional[Text]
            timeout=None,  # type: Optional[int]
    ):
        # type: (...) -> FacebookRequest
        access_token = access_token or getattr(self, 'default_access_token',
                                               None)
        graph_version = graph_version or self.default_graph_version

        return FacebookRequest(
            app=self.app,
            method=method,
            access_token=access_token,
            endpoint=endpoint,
            params=params,
            headers=headers,
            graph_version=graph_version,
            timeout=timeout,
        )
    def _send_request_with_client_params(self,
                                         endpoint,
                                         params,
                                         access_token=None):
        """
        :type endpoint: str
        :type params: dict
        :type access_token: str

        :return FacebookResponse
        """
        params.update(self._get_client_params())

        access_token = access_token or self.app.access_token()

        self.last_request = FacebookRequest(
            app=self.app,
            access_token=str(access_token),
            method='GET',
            endpoint=endpoint,
            params=params,
            graph_version=self.graph_version,
        )

        return self.client.send_request(self.last_request)
Beispiel #7
0
    def debug_token(self, access_token):  # type: (Union[Text, AccessToken]) -> Dict
        """
        https://developers.facebook.com/docs/graph-api/reference/v2.8/debug_token

        :param access_token:

        :raise FacebookSDKException

        :return: the token metadata
        """
        params = {
            'input_token': str(access_token)
        }

        self.last_request = FacebookRequest(
            app=self.app,
            access_token=self.app.access_token(),
            method='GET',
            endpoint='/debug_token',
            params=params,
            graph_version=self.graph_version,
        )

        response = self.client.send_request(self.last_request)

        return response.json_body
Beispiel #8
0
 def test_encoded_body(self):
     request = FacebookRequest(
         endpoint='',
         method=METHOD_POST,
         params={'foo': 'bar'}
     )
     self.assertFalse(request.params)
     self.assertEqual(request.url_encode_body, 'foo=bar')
Beispiel #9
0
 def test_post_request_has_access_token(self):
     access_token = 'fake_token'
     request = FacebookRequest(
         endpoint='',
         access_token=access_token,
         method=METHOD_POST,
     )
     self.assertTrue('access_token' in request.params)
     self.assertEqual(request.params.get('access_token'), access_token)
Beispiel #10
0
 def test_prepare_request_with_ascii_encoded_params(self):
     request = FacebookRequest(
         endpoint='events',
         method='POST',
         params={
             'foo': u'ánother boRken param',
         },
     )
     request_params = self.client._prepareRequest(request=request)
     self.assertEqual(request_params.get('method'), request.method)
 def test_files_to_upload(self):
     facebook_file = FacebookFile(path='{base_path}/foo.txt'.format(
         base_path=os.path.dirname(os.path.abspath(__file__))), )
     request = FacebookRequest(endpoint='',
                               params={
                                   'message': 'Awesome Photo',
                                   'source': facebook_file
                               })
     files_to_upload = request.files_to_upload()[0]
     expected = ('source', ('foo.txt', facebook_file, 'text/plain'))
     self.assertEqual(files_to_upload, expected)
Beispiel #12
0
 def setUp(self):
     self.req1 = FacebookRequest(
         endpoint='123',
         method=METHOD_GET,
         headers={'Conent-Type': 'application/json'},
     )
     self.req2 = FacebookRequest(
         endpoint='123',
         method=METHOD_POST,
         params={'foo': 'bar'},
     )
     self.req3 = FacebookRequest(
         access_token='other_token',
         endpoint='123',
         method=METHOD_DELETE,
     )
     self.req4 = FacebookRequest(
         endpoint='123',
         method=METHOD_GET,
         params={'fields': 'name, description'},
         headers={'Conent-Type': 'application/json'},
     )
    def test_extract_files_from_params(self):
        request = FacebookRequest(
            endpoint='',
            params={
                'message':
                'Awesome Photo',
                'source':
                FacebookFile(path='{base_path}/foo.txt'.format(
                    base_path=os.path.dirname(os.path.abspath(__file__))), )
            })

        self.assertFalse('source' in request._params)
        self.assertIn('source', request.files)
Beispiel #14
0
    def setUp(self):
        super(TestFacebookClient, self).setUp()
        self.request = FacebookRequest(endpoint='me', method='GET')
        self.batch_request = FacebookBatchRequest(access_token='fake_token',
                                                  requests=[self.request])
        self.fake_response = FakeResponse(
            status_code=200,
            content=
            '{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}',
        )
        self.fake_batch_response = FakeResponse(
            status_code=200,
            content='[{"code":"123","body":"Foo"}]',
        )

        self.client = FakeFacebookClient(fake_response=self.fake_response)
 def setUp(self):
     super(TestFacebookResponsePagination, self).setUp()
     self.paginable_body = {
         'data': [{
             'images': [
                 {
                     'height': 960,
                     'source':
                     'https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xtf1/t31.0-8/1290121r2_1173774305979756_75759041928559429558_o.jpg',
                     'width': 1280
                 },
                 {
                     'height': 720,
                     'source':
                     'https://fbcdn-sphotos-a-a.akamaihd.net/hphotos-ak-xfp1/v/t1.0-9/12495117_1173774305979756_4579041928559429558_n.jpg?oh=f4066e6b8bb9c6db1aea2e07d30f6bd8&oe=588D2B3D&__gda__=1485370477_c221b76e4a897ced0894126abc1553f5',
                     'width': 960
                 },
                 {
                     'height': 600,
                     'source':
                     'https://fbcdn-photos-a-a.akamaihd.net/hphotos-ak-xtf1/t31.0-0/p600x600/12901212_1173774305979756_75790419428559429558_o.jpg',
                     'width': 800
                 },
             ]
         }],
         'paging': {
             'cursors': {
                 'before':
                 'TVRFM016YzNORE13TlRrM09UYzFOam94TkRVNU5EUTRNVGczT2pNNU5EQTRPVFkwTURZAME56ZA3pOZAz09',
                 'after':
                 'TVRBeU1EWTNOVFUyTmpVeE1Ea3hOREU2TVRRek16Y3dPREl4TVRvek9UUXdPRGsyTkRBMk5EYzRNelk9'
             },
             'next':
             'https://graph.facebook.com/v2.7/me/photos?access_token=foo_token&pretty=0&fields=images&limit=25&after=TVRBeU1EWTNOVFUyTmpVeE1Ea3hOREU2TVRRek16Y3dPREl4TVRvek9UUXdPRGsyTkRBMk5EYzRNelk9',
             'previous':
             'https://graph.facebook.com/v2.7/me/photos?access_token=foo_token&pretty=0&fields=images&limit=25&before=TVRFM016YzNORE13TlRrM09UYzFOam94TkRVNU5EUTRNVGczT2pNNU5EQTRPVFkwTURZAME56ZA3pOZAz09',
         }
     }
     self.response = FacebookResponse(request=FacebookRequest(
         endpoint='/me/photos',
         method='GET',
         params={'foo': 'bar'},
         access_token='foo_token',
     ),
                                      body=json.dumps(self.paginable_body),
                                      http_status_code=200)
    def setUp(self):
        super(TestFacebookClient, self).setUp()
        self.request = FacebookRequest(endpoint='me', method='GET')
        self.batch_request = FacebookBatchRequest(
            access_token='fake_token',
            requests=[self.request]
        )
        self.fake_response = FakeResponse(
            status_code=200,
            content='{"data":[{"id":"123","name":"Foo"},{"id":"1337","name":"Bar"}]}',
            headers='{"Content-Type": "application/json; charset=UTF-8","x-fb-trace-id": "1234"}',
        )
        self.fake_batch_response = FakeResponse(
            status_code=200,
            content='[{"code":"123","body":"Foo"}]',
            headers='{"Content-Type": "application/json; charset=UTF-8","x-fb-trace-id": "1234"}',
        )

        self.client = FakeFacebookClient(fake_response=self.fake_response)
Beispiel #17
0
    def _send_request_with_client_params(self, endpoint, params, access_token=None):
        # type: (Text, Dict, Optional[Union[Text, AccessToken]]) -> FacebookResponse
        """
        :return FacebookResponse
        """
        params.update(self._get_client_params())

        access_token = access_token or self.app.access_token()

        self.last_request = FacebookRequest(
            app=self.app,
            access_token=str(access_token),
            method='GET',
            endpoint=endpoint,
            params=params,
            graph_version=self.graph_version,
        )

        return self.client.send_request(self.last_request)
Beispiel #18
0
    def request(self,
                method,
                endpoint,
                access_token=None,
                params=None,
                headers=None,
                graph_version=None):
        access_token = access_token or getattr(self, 'default_access_token',
                                               None)
        graph_version = graph_version or self.default_graph_version

        return FacebookRequest(
            app=self.app,
            method=method,
            access_token=access_token,
            endpoint=endpoint,
            params=params,
            headers=headers,
            graph_version=graph_version,
        )
Beispiel #19
0
    def test_batch_request_with_files(self):
        request = FacebookRequest(
            endpoint='/foo',
            method='POST',
            params={
                'message': 'foo',
                'source': FacebookFile(
                    path='{base_path}/foo.txt'.format(
                        base_path=os.path.dirname(os.path.abspath(__file__))
                    ),
                )
            }
        )
        batch_request = FacebookBatchRequest(
            access_token='fake_token',
            requests=[request],
        )
        expected_batch = '[{"body": "message=foo", "attached_files": "%(attached_files)s", "name": "0", "relative_url": "/v2.5/foo", "headers": {}, "method": "POST"}]' % dict(attached_files=batch_request.requests[0].get('attached_files'))
        batch_request.prepare_batch_request()

        self.assertEqual(sorted(batch_request.post_params['batch']), sorted(expected_batch))
 def test_endpoint_with_distinct_access_tokens(self):
     with self.assertRaises(FacebookSDKException):
         FacebookRequest(
             endpoint='/foo?access_token=foo_token',
             access_token='bar_token',
         )
Beispiel #21
0
 def test_endpoint_with_access_token(self):
     request = FacebookRequest(endpoint='/foo?access_token=foo_token')
     self.assertEqual(request.url, force_slash_prefix(DEFAULT_GRAPH_VERSION) + '/foo')
     self.assertEqual(request.access_token, 'foo_token')
Beispiel #22
0
 def test_default_attributes(self):
     request = FacebookRequest(endpoint='')
     self.assertIsInstance(request.headers, dict)
     self.assertIsInstance(request.params, dict)
     self.assertEqual(request.graph_version, DEFAULT_GRAPH_VERSION)
Beispiel #23
0
 def test_empty_endpoint_url(self):
     request = FacebookRequest(endpoint='')
     self.assertEqual(request.url, force_slash_prefix(DEFAULT_GRAPH_VERSION) + '/')