def test_new_list(self, mock_requests): mock_requests.register_uri( 'GET', ("https://api.chartmogul.com/v1/invoices" "?external_id=INV0001&customer_uuid=cus_f466e33d-ff2b-4a11-8f85-417eb02157a7"), request_headers={'Authorization': 'Basic dG9rZW46c2VjcmV0'}, headers={'Content-Type': 'application/json'}, status_code=200, text=newInvoiceListExample ) config = Config("token", "secret") # is actually checked in mock result = Invoice.all(config, customer_uuid='cus_f466e33d-ff2b-4a11-8f85-417eb02157a7', external_id='INV0001').get() self.assertEqual(mock_requests.call_count, 1, "expected call") cu = [] cu.append('cus_f466e33d-ff2b-4a11-8f85-417eb02157a7') ei = [] ei.append('inv0001') self.assertEqual(mock_requests.last_request.qs, {'customer_uuid': cu,'external_id': ei}) # Struct too complex to do 1:1 comparison self.assertTrue(isinstance(result, Invoice._many)) self.assertEqual(len(result.invoices), 1) self.assertEqual(result.invoices[0].customer_uuid, 'cus_f466e33d-ff2b-4a11-8f85-417eb02157a7') self.assertEqual(result.current_page, 1) self.assertEqual(result.total_pages, 1)
def test_retrieve_invoice(self, mock_requests): mock_requests.register_uri('GET', ( "https://api.chartmogul.com/v1/invoices/inv_22910fc6-c931-48e7-ac12-90d2cb5f0059" ), request_headers={ 'Authorization': 'Basic dG9rZW46c2VjcmV0' }, headers={ 'Content-Type': 'application/json' }, status_code=200, text=retrieveInvoiceExample) config = Config("token", "secret") # is actually checked in mock result = Invoice.retrieve( config, uuid='inv_22910fc6-c931-48e7-ac12-90d2cb5f0059').get() self.assertEqual(mock_requests.call_count, 1, "expected call") # Struct too complex to do 1:1 comparison self.assertTrue(isinstance(result, Invoice)) self.assertEqual(result.uuid, 'inv_22910fc6-c931-48e7-ac12-90d2cb5f0059')
def test_delete(self, mock_requests): mock_requests.register_uri( 'DELETE', ("https://api.chartmogul.com/v1/invoices" "/inv_f466e33d-ff2b-4a11-8f85-417eb02157a7"), request_headers={'Authorization': 'Basic dG9rZW46c2VjcmV0'}, status_code=204) config = Config("token", "secret") # is actually checked in mock result = Invoice.destroy( config, uuid='inv_f466e33d-ff2b-4a11-8f85-417eb02157a7').get() self.assertEqual(mock_requests.call_count, 1, "expected call") self.assertEqual(mock_requests.last_request.qs, {}) self.assertTrue(result is None)
def test_delete_not_found(self, mock_requests): mock_requests.register_uri( 'DELETE', ("https://api.chartmogul.com/v1/invoices" "/inv_f466e33d-ff2b-4a11-8f85-417eb02157a7"), request_headers={'Authorization': 'Basic dG9rZW46c2VjcmV0'}, headers={'Content-Type': 'application/json'}, status_code=404, json={'error': 'Invoice not found'}) config = Config("token", "secret") # is actually checked in mock with self.assertRaises(APIError) as context: result = Invoice.destroy( config, uuid='inv_f466e33d-ff2b-4a11-8f85-417eb02157a7').get() self.assertEqual(mock_requests.call_count, 1, "expected call")
def _create_invoice(result): ds_uuid, customer, plan = result return Invoice.create(config, uuid=customer.uuid, data={ "invoices": [{ "external_id": "INV0001", "date": datetime(2015, 11, 1, 0, 0, 0), "currency": "USD", "due_date": datetime(2015, 11, 15, 0, 0, 0), "line_items": [{ "type": "subscription", "subscription_external_id": "sub_0001", "plan_uuid": plan.uuid, "service_period_start": datetime(2015, 11, 1, 0, 0, 0), "service_period_end": datetime(2015, 12, 1, 0, 0, 0), "amount_in_cents": 5000, "quantity": 1, "discount_code": "PSO86", "discount_amount_in_cents": 1000, "tax_amount_in_cents": 900 }], "transactions": [{ "date": datetime(2015, 11, 5, 0, 14, 23), "type": "payment", "result": "successful" }] }] })
def test_create(self, mock_requests): mock_requests.register_uri( 'POST', "https://api.chartmogul.com/v1/import/customers/UUID/invoices", request_headers={'Authorization': 'Basic dG9rZW46c2VjcmV0'}, status_code=200, json=responseData) config = Config("token", "secret") # is actually checked in mock result = Invoice.create(config, uuid="UUID", data=requestData).get() self.assertEqual(mock_requests.call_count, 1, "expected call") self.assertEqual(mock_requests.last_request.qs, {}) self.assertEqual(mock_requests.last_request.json(), requestSerialized) # Struct too complex to do 1:1 comparison self.assertTrue(isinstance(result, Invoice._many)) self.assertEqual(len(result.invoices), 1)
def test_delete_all(self, mock_requests): mock_requests.register_uri( 'DELETE', ("https://api.chartmogul.com/v1/data_sources" "/ds_f466e33d-ff2b-4a11-8f85-417eb02157a7/customers" "/cus_f466e33d-ff2b-4a11-8f85-417eb02157a7/invoices"), request_headers={'Authorization': 'Basic dG9rZW46c2VjcmV0'}, headers={'Content-Type': 'application/json'}, status_code=204) config = Config("token", "secret") # is actually checked in mock result = Invoice.destroy_all( config, data_source_uuid='ds_f466e33d-ff2b-4a11-8f85-417eb02157a7', customer_uuid='cus_f466e33d-ff2b-4a11-8f85-417eb02157a7').get() self.assertEqual(mock_requests.call_count, 1, "expected call") self.assertEqual(mock_requests.last_request.qs, {}) self.assertTrue(result is None)
def test_list_has_customer_uuid(self, mock_requests): responseData['customer_uuid'] = 'UUID' mock_requests.register_uri( 'GET', "https://api.chartmogul.com/v1/import/customers/UUID/invoices", request_headers={'Authorization': 'Basic dG9rZW46c2VjcmV0'}, status_code=200, json=responseData) config = Config("token", "secret") # is actually checked in mock result = Invoice.all(config, uuid="UUID").get() self.assertEqual(mock_requests.call_count, 1, "expected call") self.assertEqual(mock_requests.last_request.qs, {}) # Struct too complex to do 1:1 comparison self.assertTrue(isinstance(result, Invoice._many)) self.assertEqual(len(result.invoices), 1) self.assertTrue(isinstance(result.invoices[0], Invoice)) self.assertEqual(result.invoices[0].uuid, "inv_565c73b2-85b9-49c9-a25e-2b7df6a677c9") self.assertEqual(result.customer_uuid, 'UUID')
def _delete_invoice(result): return Invoice.destroy(config, uuid=result.invoices[0].uuid)