Ejemplo n.º 1
0
 def setUp(self):
     yield super(TestBillingApi, self).setUp()
     self.api_url = "http://localhost:9090/"
     self.billing_api = BillingApi(self.api_url)
Ejemplo n.º 2
0
 def setUp(self):
     yield super(TestBillingApi, self).setUp()
     self.api_url = "http://localhost:9090/"
     self.billing_api = BillingApi(self.api_url)
Ejemplo n.º 3
0
class TestBillingApi(VumiTestCase):
    @inlineCallbacks
    def setUp(self):
        yield super(TestBillingApi, self).setUp()
        self.api_url = "http://localhost:9090/"
        self.billing_api = BillingApi(self.api_url)

    def _mk_response(self,
                     code=200,
                     phrase='OK',
                     headers={},
                     delivered_body='{}'):
        response = Response(('HTTP', 1, 1), code, phrase, mkheaders(headers),
                            None)

        response.delivered_body = delivered_body
        return response

    @inlineCallbacks
    def test_create_transaction_request(self):
        hrm = HttpRequestMock(self._mk_response())
        self.patch(billing_worker, 'http_request_full',
                   hrm.dummy_http_request_full)

        kwargs = {
            'account_number': "test-account",
            'message_id': 'msg-id-1',
            'tag_pool_name': "pool1",
            'tag_name': "1234",
            'message_direction': "Inbound",
            'session_created': False,
        }
        yield self.billing_api.create_transaction(**kwargs)
        self.assertEqual(hrm.request.uri, "%stransactions" % (self.api_url, ))
        self.assertEqual(hrm.request.bodyProducer.body,
                         json.dumps(kwargs, cls=JSONEncoder))

    @inlineCallbacks
    def test_create_transaction_response(self):
        delivered_body = {
            "id": 1,
            "account_number": "test-account",
            "tag_pool_name": "pool1",
            "tag_name": "1234",
            "message_direction": "Inbound",
            "message_cost": 80,
            "session_created": False,
            "session_cost": 30,
            "markup_percent": decimal.Decimal('10.0'),
            "credit_amount": -35,
            "credit_factor": decimal.Decimal('0.4'),
            "created": "2013-10-30T10:42:51.144745+02:00",
            "last_modified": "2013-10-30T10:42:51.144745+02:00",
            "status": "Completed"
        }
        response = self._mk_response(
            delivered_body=json.dumps(delivered_body, cls=JSONEncoder))

        hrm = HttpRequestMock(response)
        self.patch(billing_worker, 'http_request_full',
                   hrm.dummy_http_request_full)

        kwargs = {
            'account_number': "test-account",
            'message_id': 'msg-id-1',
            'tag_pool_name': "pool1",
            'tag_name': "1234",
            'message_direction': "Inbound",
            'session_created': False,
        }
        result = yield self.billing_api.create_transaction(**kwargs)
        self.assertEqual(result, delivered_body)

    @inlineCallbacks
    def test_create_transaction_error(self):
        response = self._mk_response(code=500,
                                     phrase="Internal Server Error",
                                     delivered_body="")

        hrm = HttpRequestMock(response)
        self.patch(billing_worker, 'http_request_full',
                   hrm.dummy_http_request_full)

        kwargs = {
            'account_number': "test-account",
            'message_id': 'msg-id-1',
            'tag_pool_name': "pool1",
            'tag_name': "1234",
            'message_direction': "Inbound",
            'session_created': False,
        }
        d = self.billing_api.create_transaction(**kwargs)
        yield self.assertFailure(d, BillingError)
Ejemplo n.º 4
0
class TestBillingApi(VumiTestCase):

    @inlineCallbacks
    def setUp(self):
        yield super(TestBillingApi, self).setUp()
        self.api_url = "http://localhost:9090/"
        self.billing_api = BillingApi(self.api_url)

    def _mk_response(self, code=200, phrase='OK', headers={},
                     delivered_body='{}'):
        response = Response(('HTTP', 1, 1), code, phrase,
                            mkheaders(headers), None)

        response.delivered_body = delivered_body
        return response

    @inlineCallbacks
    def test_create_transaction_request(self):
        hrm = HttpRequestMock(self._mk_response())
        self.patch(billing_worker, 'http_request_full',
                   hrm.dummy_http_request_full)

        kwargs = {
            'account_number': "test-account",
            'message_id': 'msg-id-1',
            'tag_pool_name': "pool1",
            'tag_name': "1234",
            'message_direction': "Inbound",
            'session_created': False,
        }
        yield self.billing_api.create_transaction(**kwargs)
        self.assertEqual(hrm.request.uri, "%stransactions" % (self.api_url,))
        self.assertEqual(hrm.request.bodyProducer.body,
                         json.dumps(kwargs, cls=JSONEncoder))

    @inlineCallbacks
    def test_create_transaction_response(self):
        delivered_body = {
            "id": 1,
            "account_number": "test-account",
            "tag_pool_name": "pool1",
            "tag_name": "1234",
            "message_direction": "Inbound",
            "message_cost": 80,
            "session_created": False,
            "session_cost": 30,
            "markup_percent": decimal.Decimal('10.0'),
            "credit_amount": -35,
            "credit_factor": decimal.Decimal('0.4'),
            "created": "2013-10-30T10:42:51.144745+02:00",
            "last_modified": "2013-10-30T10:42:51.144745+02:00",
            "status": "Completed"
        }
        response = self._mk_response(
            delivered_body=json.dumps(delivered_body, cls=JSONEncoder))

        hrm = HttpRequestMock(response)
        self.patch(billing_worker, 'http_request_full',
                   hrm.dummy_http_request_full)

        kwargs = {
            'account_number': "test-account",
            'message_id': 'msg-id-1',
            'tag_pool_name': "pool1",
            'tag_name': "1234",
            'message_direction': "Inbound",
            'session_created': False,
        }
        result = yield self.billing_api.create_transaction(**kwargs)
        self.assertEqual(result, delivered_body)

    @inlineCallbacks
    def test_create_transaction_error(self):
        response = self._mk_response(code=500, phrase="Internal Server Error",
                                     delivered_body="")

        hrm = HttpRequestMock(response)
        self.patch(billing_worker, 'http_request_full',
                   hrm.dummy_http_request_full)

        kwargs = {
            'account_number': "test-account",
            'message_id': 'msg-id-1',
            'tag_pool_name': "pool1",
            'tag_name': "1234",
            'message_direction': "Inbound",
            'session_created': False,
        }
        d = self.billing_api.create_transaction(**kwargs)
        yield self.assertFailure(d, BillingError)