Пример #1
0
 def test_post_pass_header(self):
     client = Client(**PRODUCTION_CONFIG)
     resp_body_dict = {'foo': 'bar'}
     mock_open = mock.mocksignature(client.opener.open)
     mock_resp = mock.Mock()
     mock_resp.read = mock.Mock(return_value=json.dumps(resp_body_dict))
     mock_open.mock.return_value = mock_resp
     resp_header = {'foo': 'bar'}
     with mock.patch.object(client.opener, 'open', new=mock_open), \
          mock.patch('poundpay.client.urllib2') as urllib2_mock:
         resp = client.post('payments', resp_body_dict, resp_header)
     _args, kwargs = urllib2_mock.Request.call_args
     self.assertEqual(kwargs['headers'], resp_header)
     self.assertEqual(resp.json, resp_body_dict)
Пример #2
0
 def test_post_pass_header(self):
     client = Client(**PRODUCTION_CONFIG)
     resp_body_dict = {'foo': 'bar'}
     mock_open = mock.mocksignature(client.opener.open)
     mock_resp = mock.Mock()
     mock_resp.read = mock.Mock(return_value=json.dumps(resp_body_dict))
     mock_open.mock.return_value = mock_resp
     resp_header = {'foo': 'bar'}
     with mock.patch.object(client.opener, 'open', new=mock_open), \
          mock.patch('poundpay.client.urllib2') as urllib2_mock:
         resp = client.post('payments', resp_body_dict, resp_header)
     _args, kwargs = urllib2_mock.Request.call_args
     self.assertEqual(kwargs['headers'], resp_header)
     self.assertEqual(resp.json, resp_body_dict)
Пример #3
0
class TestClientHTTPOperation(unittest.TestCase):

    def setUp(self):
        self.client = Client(**PRODUCTION_CONFIG)
        self.resp_dict = {'foo': 'bar'}
        self.mock_open = mock.Mock()
        self.mock_open.return_value.read.return_value = json.dumps(
            self.resp_dict
            )

    def test_get(self):
        with mock.patch.object(
                self.client.opener, 'open', self.mock_open, mocksignature=True
                ):
            resp = self.client.get('payments')
        self.assertEqual(resp.json, self.resp_dict)
        self.mock_open.return_value.read.assert_called_once_with()

    def test_http_error(self):
        response = mock.Mock()
        response.code = 403
        response.msg = 'Unauthorized'
        response.info.return_value = {}
        response.read.return_value = json.dumps({'message': 'unauthorized yo'})
        response_handler = mock.Mock()
        response_handler.default_open.return_value = response
        self.client.opener.handle_open['default'] = [response_handler]
        with self.assertRaises(ClientException) as exception:
            self.client.post('payments', self.resp_dict)
        exc = exception.exception
        self.assertEqual(
            str(exc.args[0]), ("HTTP Error 403: Unauthorized :: "
                               "{'message': 'unauthorized yo'}")
            )

    def test_http_success(self):
        response = mock.Mock()
        response.code = 200
        response.getcode.return_value = response.code
        response.msg = 'OK'
        response.info.return_value = {}
        response.read.return_value = json.dumps({'hi': 'there'})
        response_handler = mock.Mock()
        response_handler.default_open.return_value = response
        self.client.opener.handle_open['default'] = [response_handler]
        resp = self.client.post('payments', self.resp_dict)
        self.assertEqual(
            str(resp), "[200 OK] - {'hi': 'there'}"
            )

    def test_post(self):
        with mock.patch.object(
                self.client.opener, 'open', self.mock_open, mocksignature=True
                ):
            resp = self.client.post('payments', self.resp_dict)
        self.assertEqual(resp.json, self.resp_dict)

    def test_delete(self):
        self.mock_open.return_value.read.return_value = json.dumps({})
        with mock.patch.object(
                self.client.opener, 'open', self.mock_open, mocksignature=True
                ):
            resp = self.client.delete('payments/sid')
        self.assertEqual(resp.json, {})

    def test_put(self):
        with mock.patch.object(
                self.client.opener, 'open', self.mock_open, mocksignature=True
                ):
            resp = self.client.put('payments/sid', self.resp_dict)
        self.assertEqual(resp.json, self.resp_dict)
Пример #4
0
class TestClientHTTPOperation(unittest.TestCase):
    def setUp(self):
        self.client = Client(**PRODUCTION_CONFIG)
        self.resp_dict = {'foo': 'bar'}
        self.mock_open = mock.Mock()
        self.mock_open.return_value.read.return_value = json.dumps(
            self.resp_dict)

    def test_get(self):
        with mock.patch.object(self.client.opener,
                               'open',
                               self.mock_open,
                               mocksignature=True):
            resp = self.client.get('payments')
        self.assertEqual(resp.json, self.resp_dict)
        self.mock_open.return_value.read.assert_called_once_with()

    def test_http_error(self):
        response = mock.Mock()
        response.code = 403
        response.msg = 'Unauthorized'
        response.info.return_value = {}
        response.read.return_value = json.dumps({'message': 'unauthorized yo'})
        response_handler = mock.Mock()
        response_handler.default_open.return_value = response
        self.client.opener.handle_open['default'] = [response_handler]
        with self.assertRaises(ClientException) as exception:
            self.client.post('payments', self.resp_dict)
        exc = exception.exception
        self.assertEqual(str(exc.args[0]), ("HTTP Error 403: Unauthorized :: "
                                            "{'message': 'unauthorized yo'}"))

    def test_http_success(self):
        response = mock.Mock()
        response.code = 200
        response.getcode.return_value = response.code
        response.msg = 'OK'
        response.info.return_value = {}
        response.read.return_value = json.dumps({'hi': 'there'})
        response_handler = mock.Mock()
        response_handler.default_open.return_value = response
        self.client.opener.handle_open['default'] = [response_handler]
        resp = self.client.post('payments', self.resp_dict)
        self.assertEqual(str(resp), "[200 OK] - {'hi': 'there'}")

    def test_post(self):
        with mock.patch.object(self.client.opener,
                               'open',
                               self.mock_open,
                               mocksignature=True):
            resp = self.client.post('payments', self.resp_dict)
        self.assertEqual(resp.json, self.resp_dict)

    def test_delete(self):
        self.mock_open.return_value.read.return_value = json.dumps({})
        with mock.patch.object(self.client.opener,
                               'open',
                               self.mock_open,
                               mocksignature=True):
            resp = self.client.delete('payments/sid')
        self.assertEqual(resp.json, {})

    def test_put(self):
        with mock.patch.object(self.client.opener,
                               'open',
                               self.mock_open,
                               mocksignature=True):
            resp = self.client.put('payments/sid', self.resp_dict)
        self.assertEqual(resp.json, self.resp_dict)