def test_post_proforma_with_proforma_entries(self): customer = CustomerFactory.create() provider = ProviderFactory.create() SubscriptionFactory.create() url = reverse('proforma-list') provider_url = build_absolute_test_url(reverse('provider-detail', [provider.pk])) customer_url = build_absolute_test_url(reverse('customer-detail', [customer.pk])) data = { 'provider': provider_url, 'customer': customer_url, 'series': None, 'number': None, 'currency': 'RON', 'transaction_xe_rate': 1, 'proforma_entries': [{ "description": "Page views", "unit_price": 10.0, "quantity": 20 }] } response = self.client.post(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_201_CREATED
def setUp(self): admin_user = AdminUserFactory.create() self.client.force_authenticate(user=admin_user) self.complete_data = { "customer_reference": "123456", "first_name": "Bruce", "last_name": "Wayne", "company": "Wayne Enterprises", "email": "*****@*****.**", "address_1": "Batcave St.", "address_2": "Some other address info", "city": "Gotham", "state": "SomeState", "zip_code": "1111", "country": "US", "phone": "+40000000000", "currency": "USD", "extra": "What is there more to say?", "sales_tax_number": "RO5555555", "sales_tax_name": "VAT", "sales_tax_percent": '3.00', "payment_due_days": 5, "consolidated_billing": False, "meta": {'water': ['plants', '5']}, "payment_methods": build_absolute_test_url(reverse('payment-method-list', kwargs={'customer_pk': 1})), "transactions": build_absolute_test_url(reverse('transaction-list', kwargs={'customer_pk': 1})) }
def test_post_invoice_with_invoice_entries_without_transaction_xe_rate( transaction_currency, authenticated_api_client ): customer = CustomerFactory.create() provider = ProviderFactory.create() SubscriptionFactory.create() url = reverse('invoice-list') provider_url = build_absolute_test_url(reverse('provider-detail', [provider.pk])) customer_url = build_absolute_test_url(reverse('customer-detail', [customer.pk])) request_data = { 'provider': provider_url, 'customer': customer_url, 'series': None, 'number': None, 'currency': 'RON', 'transaction_currency': transaction_currency, 'invoice_entries': [{ "description": "Page views", "unit_price": 10.0, "quantity": 20}] } response = authenticated_api_client.post(url, data=request_data, format='json') assert response.status_code == status.HTTP_201_CREATED, response.data invoice = Invoice.objects.get(id=response.data['id']) invoice_definition.check_response(invoice, response.data, request_data) assert response.data['invoice_entries'] # content already checked in previous assert
def test_post_proforma_with_proforma_entries(self): customer = CustomerFactory.create() provider = ProviderFactory.create() SubscriptionFactory.create() url = reverse('proforma-list') provider_url = build_absolute_test_url(reverse('provider-detail', [provider.pk])) customer_url = build_absolute_test_url(reverse('customer-detail', [customer.pk])) data = { 'provider': provider_url, 'customer': customer_url, 'series': None, 'number': None, 'currency': text_type('RON'), 'transaction_xe_rate': 1, 'proforma_entries': [{ "description": text_type("Page views"), "unit_price": 10.0, "quantity": 20 }] } response = self.client.post(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_201_CREATED
def test_payment_processors_list(self): url = reverse('payment-processor-list') response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertIn( { "name": "triggered", "type": "triggered", "allowed_currencies": ['RON', 'USD'], "url": build_absolute_test_url( reverse('payment-processor-detail', ['triggered'])) }, response.data) self.assertIn( { "name": "manual", "type": "manual", "allowed_currencies": [], "url": build_absolute_test_url( reverse('payment-processor-detail', ['manual'])) }, response.data)
def test_post_invoice_without_invoice_entries(authenticated_api_client, customer, provider): SubscriptionFactory.create() url = reverse('invoice-list') provider_url = build_absolute_test_url( reverse('provider-detail', [provider.pk])) customer_url = build_absolute_test_url( reverse('customer-detail', [customer.pk])) request_data = { 'provider': provider_url, 'customer': customer_url, 'series': None, 'number': None, 'currency': 'RON', 'invoice_entries': [] } response = authenticated_api_client.post(url, data=request_data, format='json') assert response.status_code == status.HTTP_201_CREATED, response.data invoice = Invoice.objects.get(id=response.data['id']) invoice_definition.check_response(invoice, response.data, request_data)
def test_post_invoice_without_invoice_entries(self): customer = CustomerFactory.create() provider = ProviderFactory.create() SubscriptionFactory.create() url = reverse('invoice-list') provider_url = build_absolute_test_url( reverse('provider-detail', [provider.pk])) customer_url = build_absolute_test_url( reverse('customer-detail', [customer.pk])) data = { 'provider': provider_url, 'customer': customer_url, 'series': "", 'number': "", 'currency': 'RON', 'invoice_entries': [] } response = self.client.post(url, data=data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) invoice = get_object_or_None(Invoice, id=response.data["id"]) self.assertTrue(invoice) self.assertEqual( response.data, { "id": response.data["id"], "series": invoice.series, "number": invoice.number, "provider": provider_url, "customer": customer_url, "archived_provider": '{}', "archived_customer": '{}', "due_date": None, "issue_date": None, "paid_date": None, "cancel_date": None, "sales_tax_name": invoice.sales_tax_name, "sales_tax_percent": str(invoice.sales_tax_percent), "currency": "RON", "transaction_currency": invoice.transaction_currency, "transaction_xe_rate": None, "transaction_xe_date": invoice.transaction_xe_date, "state": invoice.state, "proforma": None, "invoice_entries": [], "pdf_url": None, "total": invoice.total, "total_in_transaction_currency": invoice.total, "transactions": [] })
def test_post_proforma_without_proforma_entries(self): customer = CustomerFactory.create() provider = ProviderFactory.create() SubscriptionFactory.create() url = reverse('proforma-list') provider_url = build_absolute_test_url(reverse('provider-detail', [provider.pk])) customer_url = build_absolute_test_url(reverse('customer-detail', [customer.pk])) data = { 'provider': provider_url, 'customer': customer_url, 'series': "", 'number': "", 'currency': 'RON', 'proforma_entries': [] } response = self.client.post(url, data=data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) proforma = get_object_or_None(Proforma, id=response.data["id"]) self.assertTrue(proforma) self.assertEqual(response.data, { "id": response.data["id"], "series": "ProformaSeries", "number": None, "provider": provider_url, "customer": customer_url, "archived_provider": '{}', "archived_customer": '{}', "due_date": None, "issue_date": None, "paid_date": None, "cancel_date": None, "sales_tax_name": "VAT", "sales_tax_percent": "1.00", "currency": "RON", "transaction_currency": proforma.transaction_currency, "transaction_xe_rate": (str(proforma.transaction_xe_rate) if proforma.transaction_xe_rate else None), "transaction_xe_date": proforma.transaction_xe_date, "pdf_url": None, "state": "draft", "invoice": None, "proforma_entries": [], "total": 0, "total_in_transaction_currency": 0, "transactions": [] })
def test_post_proforma_without_proforma_entries(self): customer = CustomerFactory.create() provider = ProviderFactory.create() SubscriptionFactory.create() url = reverse('proforma-list') provider_url = build_absolute_test_url(reverse('provider-detail', [provider.pk])) customer_url = build_absolute_test_url(reverse('customer-detail', [customer.pk])) data = { 'provider': provider_url, 'customer': customer_url, 'currency': text_type('RON'), 'proforma_entries': [] } response = self.client.post(url, data=data) assert response.status_code == status.HTTP_201_CREATED, response.data proforma = get_object_or_None(Proforma, id=response.data["id"]) assert proforma assert response.data == { "id": response.data["id"], "series": "ProformaSeries", "number": None, "provider": provider_url, "customer": customer_url, "archived_provider": '{}', "archived_customer": '{}', "due_date": None, "issue_date": None, "paid_date": None, "cancel_date": None, "sales_tax_name": "VAT", "sales_tax_percent": "1.00", "currency": text_type("RON"), "transaction_currency": proforma.transaction_currency, "transaction_xe_rate": (str(proforma.transaction_xe_rate) if proforma.transaction_xe_rate else None), "transaction_xe_date": proforma.transaction_xe_date, "pdf_url": None, "state": "draft", "invoice": None, "proforma_entries": [], "total": 0, "total_in_transaction_currency": 0, "transactions": [] }
def test_get_proforma(self, mocked_settings): ProformaFactory.reset_sequence(1) upload_path = '%s/documents/' % settings.MEDIA_ROOT proforma = ProformaFactory.create(pdf=PDF.objects.create(upload_path=upload_path)) proforma.generate_pdf() url = reverse('proforma-detail', kwargs={'pk': proforma.pk}) for show_pdf_storage_url, pdf_url in [ (True, build_absolute_test_url(proforma.pdf.url)), (False, build_absolute_test_url(reverse('pdf', args=[proforma.pdf.pk]))) ]: mocked_settings.SILVER_SHOW_PDF_STORAGE_URL = show_pdf_storage_url response = self.client.get(url) provider_url = build_absolute_test_url(reverse('provider-detail', [proforma.provider.pk])) customer_url = build_absolute_test_url(reverse('customer-detail', [proforma.customer.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, { "id": proforma.pk, "series": "ProformaSeries", "number": proforma.number, "provider": provider_url, "customer": customer_url, "archived_provider": '{}', "archived_customer": '{}', "due_date": None, "issue_date": None, "paid_date": None, "cancel_date": None, "sales_tax_name": "VAT", "sales_tax_percent": '1.00', "currency": text_type("RON"), "transaction_currency": proforma.transaction_currency, "transaction_xe_rate": ("%.4f" % proforma.transaction_xe_rate if proforma.transaction_xe_rate else None), "transaction_xe_date": proforma.transaction_xe_date, "pdf_url": pdf_url, "state": "draft", "invoice": None, "proforma_entries": [], "total": 0, "total_in_transaction_currency": 0, "transactions": [] })
def test_get_proforma(self, mocked_settings): ProformaFactory.reset_sequence(1) upload_path = '%s/documents/' % settings.MEDIA_ROOT proforma = ProformaFactory.create(pdf=PDF.objects.create(upload_path=upload_path)) proforma.generate_pdf() url = reverse('proforma-detail', kwargs={'pk': proforma.pk}) for show_pdf_storage_url, pdf_url in [ (True, build_absolute_test_url(proforma.pdf.url)), (False, build_absolute_test_url(reverse('pdf', args=[proforma.pdf.pk]))) ]: mocked_settings.SILVER_SHOW_PDF_STORAGE_URL = show_pdf_storage_url response = self.client.get(url) provider_url = build_absolute_test_url(reverse('provider-detail', [proforma.provider.pk])) customer_url = build_absolute_test_url(reverse('customer-detail', [proforma.customer.pk])) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, { "id": proforma.pk, "series": "ProformaSeries", "number": proforma.number, "provider": provider_url, "customer": customer_url, "archived_provider": '{}', "archived_customer": '{}', "due_date": None, "issue_date": None, "paid_date": None, "cancel_date": None, "sales_tax_name": "VAT", "sales_tax_percent": '1.00', "currency": "RON", "transaction_currency": proforma.transaction_currency, "transaction_xe_rate": ("%.4f" % proforma.transaction_xe_rate if proforma.transaction_xe_rate else None), "transaction_xe_date": proforma.transaction_xe_date, "pdf_url": pdf_url, "state": "draft", "invoice": None, "proforma_entries": [], "total": 0, "total_in_transaction_currency": 0, "transactions": [] })
def test_get_provider(self): ProviderFactory.reset_sequence(1) provider = ProviderFactory.create() url = reverse('provider-detail', kwargs={'pk': provider.pk}) response = self.client.get(url) assert response.status_code == 200 self_url = build_absolute_test_url(url) expected = { 'id': provider.pk, 'url': self_url, 'name': provider.name, 'company': provider.company, 'flow': provider.flow, 'invoice_series': provider.invoice_series, 'invoice_starting_number': provider.invoice_starting_number, 'proforma_series': provider.proforma_series, 'proforma_starting_number': provider.proforma_starting_number, 'email': provider.email, 'address_1': provider.address_1, 'address_2': provider.address_2, 'city': provider.city, 'state': provider.state, 'zip_code': provider.zip_code, 'country': provider.country, 'extra': provider.extra, 'meta': {u'something': [1, 2]}, } assert response.data == expected
def test_pay_proforma_with_provided_date(self): provider = ProviderFactory.create() customer = CustomerFactory.create() proforma = ProformaFactory.create(provider=provider, customer=customer) proforma.issue() url = reverse('proforma-state', kwargs={'pk': proforma.pk}) data = { 'state': 'paid', 'paid_date': '2014-05-05' } response = self.client.put(url, data=json.dumps(data), content_type='application/json') proforma.refresh_from_db() assert response.status_code == status.HTTP_200_OK due_date = timezone.now().date() + timedelta(days=PAYMENT_DUE_DAYS) invoice_url = build_absolute_test_url(reverse('invoice-detail', [proforma.related_document.pk])) mandatory_content = { 'issue_date': timezone.now().date().strftime('%Y-%m-%d'), 'due_date': due_date.strftime('%Y-%m-%d'), 'paid_date': '2014-05-05', 'state': 'paid', 'invoice': invoice_url } assert response.status_code == status.HTTP_200_OK assert all(item in list(response.data.items()) for item in mandatory_content.items()) invoice = Invoice.objects.all()[0] assert proforma.related_document == invoice assert invoice.related_document == proforma
def test_pay_proforma_with_provided_date(self): provider = ProviderFactory.create() customer = CustomerFactory.create() proforma = ProformaFactory.create(provider=provider, customer=customer) proforma.issue() url = reverse('proforma-state', kwargs={'pk': proforma.pk}) data = {'state': 'paid', 'paid_date': '2014-05-05'} response = self.client.put(url, data=json.dumps(data), content_type='application/json') proforma.refresh_from_db() assert response.status_code == status.HTTP_200_OK due_date = timezone.now().date() + timedelta(days=PAYMENT_DUE_DAYS) invoice_url = build_absolute_test_url( reverse('invoice-detail', [proforma.related_document.pk])) mandatory_content = { 'issue_date': timezone.now().date().strftime('%Y-%m-%d'), 'due_date': due_date.strftime('%Y-%m-%d'), 'paid_date': '2014-05-05', 'state': 'paid', 'invoice': invoice_url } assert response.status_code == status.HTTP_200_OK assert all(item in list(response.data.items()) for item in mandatory_content.items()) invoice = Invoice.objects.all()[0] assert proforma.related_document == invoice assert invoice.related_document == proforma
def test_get_provider(self): ProviderFactory.reset_sequence(1) provider = ProviderFactory.create() url = reverse('provider-detail', kwargs={'pk': provider.pk}) response = self.client.get(url) assert response.status_code == 200 self_url = build_absolute_test_url(url) expected = { 'id': provider.pk, 'url': self_url, 'name': provider.name, 'company': provider.company, 'flow': provider.flow, 'invoice_series': provider.invoice_series, 'invoice_starting_number': provider.invoice_starting_number, 'proforma_series': provider.proforma_series, 'proforma_starting_number': provider.proforma_starting_number, 'email': provider.email, 'address_1': provider.address_1, 'address_2': provider.address_2, 'city': provider.city, 'state': provider.state, 'zip_code': provider.zip_code, 'country': provider.country, 'extra': provider.extra, 'meta': { 'something': [1, 2] }, } assert response.data == expected
def test_encoding(self): now = timezone.now().replace(microsecond=0) payment_method = PaymentMethodFactory.create(added_at=now) factory = APIRequestFactory() url = reverse('payment-method-detail', kwargs={ 'payment_method_id': payment_method.pk, 'customer_pk': payment_method.customer.pk }) request = factory.get(url, format='json') serializer = PaymentMethodSerializer(payment_method, context={'request': request}) self_url = build_absolute_test_url(url) transactions_url = build_absolute_test_url( reverse('payment-method-transaction-list', kwargs={ "payment_method_id": payment_method.pk, "customer_pk": payment_method.customer.pk })) customer_url = build_absolute_test_url( reverse('customer-detail', [payment_method.customer.pk])) expected_data = OrderedDict([ ('url', self_url), ('transactions', transactions_url), ('customer', customer_url), ('payment_processor_name', manual_processor), ('payment_processor', OrderedDict([("type", ManualProcessor.type), ("name", manual_processor), ("allowed_currencies", []), ("url", build_absolute_test_url( reverse('payment-processor-detail', ['manual'])))])), ('added_at', payment_method.added_at), ('verified', False), ('canceled', False), ('valid_until', None), ('display_info', None), ]) json = JSONRenderer().render(serializer.data) self.assertEqual(json, JSONRenderer().render(expected_data))
def test_encoding(self): now = timezone.now().replace(microsecond=0) payment_method = PaymentMethodFactory.create(added_at=now) factory = APIRequestFactory() url = reverse('payment-method-detail', kwargs={'payment_method_id': payment_method.pk, 'customer_pk': payment_method.customer.pk}) request = factory.get(url, format='json') serializer = PaymentMethodSerializer(payment_method, context={ 'request': request }) self_url = build_absolute_test_url(url) transactions_url = build_absolute_test_url( reverse('payment-method-transaction-list', [payment_method.pk, payment_method.customer.pk]) ) customer_url = build_absolute_test_url( reverse('customer-detail', [payment_method.customer.pk]) ) expected_data = OrderedDict([ ('url', self_url), ('transactions', transactions_url), ('customer', customer_url), ('payment_processor_name', manual_processor), ('payment_processor', OrderedDict([ ("type", ManualProcessor.type), ("name", manual_processor), ("allowed_currencies", []), ("url", build_absolute_test_url(reverse('payment-processor-detail', ['manual']))) ])), ('added_at', payment_method.added_at), ('verified', False), ('canceled', False), ('valid_until', None), ('display_info', None), ]) json = JSONRenderer().render(serializer.data) self.assertEqual(json, JSONRenderer().render(expected_data))
def test_payment_processors_detail(self): url = reverse('payment-processor-detail', kwargs={ 'processor_name': 'manual' }) response = self.client.get(url, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, { 'name': u'manual', 'type': u'manual', 'allowed_currencies': [], 'url': build_absolute_test_url(reverse('payment-processor-detail', ['manual'])) })
def test_put_provider_correctly(self): ProviderFactory.reset_sequence(1) provider = ProviderFactory.create() url = reverse('provider-detail', kwargs={'pk': provider.pk}) new_data = { 'id': provider.pk, 'name': 'TestProvider', 'company': 'TheNewCompany', 'email': '*****@*****.**', 'address_1': 'address', 'city': 'City', 'zip_code': '1', 'country': 'RO', 'flow': 'proforma', 'invoice_series': 'NewSeries', 'invoice_starting_number': 1, 'proforma_series': 'ProformaSeries', 'proforma_starting_number': 1 # TODO: add new meta JSON value } response = self.client.put(url, data=new_data) assert response.status_code == status.HTTP_200_OK self_url = build_absolute_test_url(url) assert response.data == { 'id': provider.pk, 'url': self_url, 'name': new_data['name'], 'company': new_data['company'], 'flow': provider.flow, 'email': new_data['email'], 'address_1': new_data['address_1'], 'address_2': provider.address_2, 'city': new_data['city'], 'state': provider.state, 'zip_code': new_data['zip_code'], 'country': new_data['country'], 'extra': provider.extra, 'flow': new_data['flow'], 'invoice_series': new_data['invoice_series'], 'invoice_starting_number': new_data['invoice_starting_number'], 'proforma_series': new_data['proforma_series'], 'proforma_starting_number': new_data['proforma_starting_number'], 'meta': provider.meta, }
def test_put_provider_correctly(self): ProviderFactory.reset_sequence(1) provider = ProviderFactory.create() url = reverse('provider-detail', kwargs={'pk': provider.pk}) new_data = { 'id': provider.pk, 'name': 'TestProvider', 'company': 'TheNewCompany', 'email': '*****@*****.**', 'address_1': 'address', 'city': 'City', 'zip_code': '1', 'country': 'RO', 'flow': 'proforma', 'invoice_series': 'NewSeries', 'invoice_starting_number': 1, 'proforma_series': 'ProformaSeries', 'proforma_starting_number': 1 # TODO: add new meta JSON value } response = self.client.put(url, data=new_data) assert response.status_code == status.HTTP_200_OK self_url = build_absolute_test_url(url) assert response.data == { 'id': provider.pk, 'url': self_url, 'name': new_data['name'], 'company': new_data['company'], 'flow': provider.flow, 'email': new_data['email'], 'address_1': new_data['address_1'], 'address_2': provider.address_2, 'city': new_data['city'], 'state': provider.state, 'zip_code': new_data['zip_code'], 'country': new_data['country'], 'extra': provider.extra, 'flow': new_data['flow'], 'invoice_series': new_data['invoice_series'], 'invoice_starting_number': new_data['invoice_starting_number'], 'proforma_series': new_data['proforma_series'], 'proforma_starting_number': new_data['proforma_starting_number'], 'meta': provider.meta, }
def _transaction_data(self, transaction): transaction.refresh_from_db() payment_method = transaction.payment_method customer = transaction.customer provider = transaction.provider proforma = transaction.proforma invoice = transaction.invoice with patch('silver.utils.payments._get_jwt_token') as mocked_token: mocked_token.return_value = 'token' return OrderedDict([ ('id', str(transaction.uuid)), ('url', reverse('transaction-detail', kwargs={ 'customer_pk': customer.id, 'transaction_uuid': transaction.uuid })), ('customer', reverse('customer-detail', args=[customer.pk])), ('provider', reverse('provider-detail', args=[provider.pk])), ('amount', str(Decimal('0.00') + transaction.amount)), ('currency', str(transaction.currency)), ('state', str(transaction.state)), ('proforma', reverse('proforma-detail', args=[proforma.pk])), ('invoice', reverse('invoice-detail', args=[invoice.pk])), ('can_be_consumed', transaction.can_be_consumed), ('payment_processor', payment_method.payment_processor), ('payment_method', reverse('payment-method-detail', kwargs={ 'customer_pk': customer.id, 'payment_method_id': payment_method.id })), ('pay_url', build_absolute_test_url(get_payment_url(transaction, None))), ('valid_until', None), ('updated_at', transaction.updated_at.isoformat()[:-6] + 'Z'), ('created_at', transaction.created_at.isoformat()[:-6] + 'Z'), ('fail_code', transaction.fail_code), ('refund_code', transaction.refund_code), ('cancel_code', transaction.cancel_code) ])
def test_patch_provider(self): ProviderFactory.reset_sequence(1) provider = ProviderFactory.create() url = reverse('provider-detail', kwargs={'pk': provider.pk}) new_data = { 'company': 'TheNewCompany', # The changed field 'address_1': 'Address11', 'flow': 'proforma', 'invoice_series': 'InvoiceSeries', 'invoice_starting_number': 1, 'proforma_series': 'ProformaSeries', 'proforma_starting_number': 1, 'city': 'City1', 'zip_code': '1', 'country': u'AL', } response = self.client.patch(url, data=new_data) assert response.status_code == 200 self_url = build_absolute_test_url(url) assert response.data == { 'id': provider.pk, 'url': self_url, 'name': provider.name, 'company': new_data['company'], 'flow': new_data['flow'], 'invoice_series': new_data['invoice_series'], 'invoice_starting_number': new_data['invoice_starting_number'], 'proforma_series': new_data['proforma_series'], 'proforma_starting_number': new_data['proforma_starting_number'], 'email': provider.email, 'address_1': new_data['address_1'], 'address_2': provider.address_2, 'city': new_data['city'], 'state': provider.state, 'zip_code': new_data['zip_code'], 'country': new_data['country'], 'extra': provider.extra, 'meta': provider.meta, }
def test_patch_provider(self): ProviderFactory.reset_sequence(1) provider = ProviderFactory.create() url = reverse('provider-detail', kwargs={'pk': provider.pk}) new_data = { 'company': 'TheNewCompany', # The changed field 'address_1': 'Address11', 'flow': 'proforma', 'invoice_series': 'InvoiceSeries', 'invoice_starting_number': 1, 'proforma_series': 'ProformaSeries', 'proforma_starting_number': 1, 'city': 'City1', 'zip_code': '1', 'country': 'AL', } response = self.client.patch(url, data=new_data) assert response.status_code == 200 self_url = build_absolute_test_url(url) assert response.data == { 'id': provider.pk, 'url': self_url, 'name': provider.name, 'company': new_data['company'], 'flow': new_data['flow'], 'invoice_series': new_data['invoice_series'], 'invoice_starting_number': new_data['invoice_starting_number'], 'proforma_series': new_data['proforma_series'], 'proforma_starting_number': new_data['proforma_starting_number'], 'email': provider.email, 'address_1': new_data['address_1'], 'address_2': provider.address_2, 'city': new_data['city'], 'state': provider.state, 'zip_code': new_data['zip_code'], 'country': new_data['country'], 'extra': provider.extra, 'meta': provider.meta, }
def test_get_metered_feature_list(self): MeteredFeatureFactory.create_batch(40) url = reverse('metered-feature-list') response = self.client.get(url) full_url = build_absolute_test_url(url) assert response.status_code == status.HTTP_200_OK assert response._headers['link'] == \ ('Link', '<' + full_url + '?page=2>; rel="next", ' + '<' + full_url + '?page=1>; rel="first", ' + '<' + full_url + '?page=2> rel="last"') response = self.client.get(url + '?page=2') assert response.status_code == status.HTTP_200_OK assert response._headers['link'] == \ ('Link', '<' + full_url + '>; rel="prev", ' + '<' + full_url + '?page=1>; rel="first", ' + '<' + full_url + '?page=2> rel="last"')
def test_get_metered_feature_list(self): MeteredFeatureFactory.create_batch(40) url = reverse('metered-feature-list') response = self.client.get(url) full_url = build_absolute_test_url(url) assert response.status_code == status.HTTP_200_OK assert response._headers['link'] == \ ('Link', '<' + full_url + '?page=2>; rel="next", ' + '<' + full_url + '?page=1>; rel="first", ' + '<' + full_url + '?page=2> rel="last"') response = self.client.get(url + '?page=2') assert response.status_code == status.HTTP_200_OK assert response._headers['link'] == \ ('Link', '<' + full_url + '>; rel="prev", ' + '<' + full_url + '?page=1>; rel="first", ' + '<' + full_url + '?page=2> rel="last"')
def reverse(*args, **kwargs): return build_absolute_test_url(_reverse(*args, **kwargs))
def _get_expected_data(self, document, transactions=None): kind = force_text(document.kind.lower()) transactions = [{ u'id': u'%s' % transaction.uuid, u'url': build_absolute_test_url(reverse('transaction-detail', [transaction.customer.pk, transaction.uuid])), u'customer': build_absolute_test_url(reverse('customer-detail', [transaction.customer.id])), u'provider': build_absolute_test_url(reverse('provider-detail', [transaction.provider.id])), u'invoice': build_absolute_test_url(reverse('invoice-detail', [transaction.invoice.id])), u'proforma': build_absolute_test_url(reverse('proforma-detail', [transaction.proforma.id])), u'payment_processor': transaction.payment_processor, u'refund_code': transaction.refund_code, u'fail_code': transaction.fail_code, u'cancel_code': transaction.cancel_code, u'can_be_consumed': transaction.can_be_consumed, u'created_at': FREEZED_TIME, u'state': transaction.state, u'valid_until': transaction.valid_until, u'updated_at': FREEZED_TIME, u'currency': u'%s' % transaction.currency, u'amount': u'%.2f' % transaction.amount, u'payment_method': build_absolute_test_url(reverse('payment-method-detail', [transaction.customer.pk, transaction.payment_method.pk])), u'pay_url': build_absolute_test_url(reverse('payment', ['token'])) } for transaction in transactions or []] return { u'id': document.pk, u'url': build_absolute_test_url(reverse(kind + '-detail', [document.pk])), u'kind': kind, u'series': document.series, u'number': document.number, u'provider': build_absolute_test_url(reverse('provider-detail', [document.provider.id])), u'customer': build_absolute_test_url(reverse('customer-detail', [document.customer.id])), u'due_date': force_text(document.due_date) if document.due_date else None, u'issue_date': force_text(document.issue_date) if document.issue_date else None, u'paid_date': document.paid_date, u'cancel_date': document.cancel_date, u'sales_tax_name': document.sales_tax_name, u'sales_tax_percent': u'%.2f' % document.sales_tax_percent, u'currency': document.currency, u'transaction_currency': document.transaction_currency, u'state': document.state, u'total': document.total, u'pdf_url': build_absolute_test_url(document.pdf.url) if (document.pdf and document.pdf.url) else None, u'transactions': transactions, u'total_in_transaction_currency': document.total_in_transaction_currency }
def _get_expected_data(self, document, transactions=None): kind = force_str(document.kind.lower()) transactions = [{ u'id': u'%s' % transaction.uuid, u'url': build_absolute_test_url( reverse('transaction-detail', [transaction.customer.pk, transaction.uuid])), u'customer': build_absolute_test_url( reverse('customer-detail', [transaction.customer.id])), u'provider': build_absolute_test_url( reverse('provider-detail', [transaction.provider.id])), u'invoice': build_absolute_test_url( reverse('invoice-detail', [transaction.invoice.id])), u'proforma': build_absolute_test_url( reverse('proforma-detail', [transaction.proforma.id])), u'payment_processor': transaction.payment_processor, u'refund_code': transaction.refund_code, u'fail_code': transaction.fail_code, u'cancel_code': transaction.cancel_code, u'can_be_consumed': transaction.can_be_consumed, u'created_at': FREEZED_TIME, u'state': transaction.state, u'valid_until': transaction.valid_until, u'updated_at': FREEZED_TIME, u'currency': u'%s' % transaction.currency, u'amount': u'%.2f' % transaction.amount, u'payment_method': build_absolute_test_url( reverse( 'payment-method-detail', [transaction.customer.pk, transaction.payment_method.pk])), u'pay_url': build_absolute_test_url(reverse('payment', ['token'])) } for transaction in transactions or []] return { u'id': document.pk, u'url': build_absolute_test_url(reverse(kind + '-detail', [document.pk])), u'kind': kind, u'series': document.series, u'number': document.number, u'provider': build_absolute_test_url( reverse('provider-detail', [document.provider.id])), u'customer': build_absolute_test_url( reverse('customer-detail', [document.customer.id])), u'due_date': force_str(document.due_date) if document.due_date else None, u'issue_date': force_str(document.issue_date) if document.issue_date else None, u'paid_date': document.paid_date, u'cancel_date': document.cancel_date, u'sales_tax_name': document.sales_tax_name, u'sales_tax_percent': u'%.2f' % document.sales_tax_percent, u'currency': document.currency, u'transaction_currency': document.transaction_currency, u'state': document.state, u'total': document.total, u'pdf_url': build_absolute_test_url(document.pdf.url) if (document.pdf and document.pdf.url) else None, u'transactions': transactions, u'total_in_transaction_currency': document.total_in_transaction_currency }
def test_get_invoice(self, mocked_settings): InvoiceFactory.reset_sequence(1) TransactionFactory.reset_sequence(1) customer = CustomerFactory.create() invoice = InvoiceFactory.create(customer=customer, state=Invoice.STATES.ISSUED) invoice.generate_pdf() with mute_signals(pre_save): transactions = [ TransactionFactory.create( state=state, invoice=invoice, payment_method=PaymentMethodFactory(customer=customer)) for state in Transaction.States.as_list() if state not in [ Transaction.States.Canceled, Transaction.States.Refunded, Transaction.States.Failed ] ] expected_transactions = [{ "id": str(transaction.uuid), "url": build_absolute_test_url( reverse('transaction-detail', [transaction.customer.pk, transaction.uuid])), "customer": build_absolute_test_url( reverse('customer-detail', [transaction.customer.id])), "provider": build_absolute_test_url( reverse('provider-detail', [transaction.provider.id])), "amount": "%.2f" % transaction.amount, "currency": "RON", "state": transaction.state, "proforma": build_absolute_test_url( reverse('proforma-detail', [transaction.proforma.id])), "invoice": build_absolute_test_url( reverse('invoice-detail', [transaction.invoice.id])), "can_be_consumed": transaction.can_be_consumed, "payment_processor": transaction.payment_processor, "payment_method": build_absolute_test_url( reverse( 'payment-method-detail', [transaction.customer.pk, transaction.payment_method.pk])), "pay_url": (build_absolute_test_url(reverse('payment', ['token'])) if transaction.state == Transaction.States.Initial else None), } for transaction in transactions] with patch('silver.utils.payments._get_jwt_token') as mocked_token: mocked_token.return_value = 'token' url = reverse('invoice-detail', kwargs={'pk': invoice.pk}) for show_pdf_storage_url, pdf_url in [ (True, build_absolute_test_url(invoice.pdf.url)), (False, build_absolute_test_url(reverse('pdf', args=[invoice.pdf.pk]))) ]: mocked_settings.SILVER_SHOW_PDF_STORAGE_URL = show_pdf_storage_url response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_response = { "id": invoice.pk, "series": "InvoiceSeries", "number": 1, "provider": build_absolute_test_url( reverse('provider-detail', [invoice.provider.pk])), "customer": build_absolute_test_url( reverse('customer-detail', [invoice.customer.pk])), "archived_provider": '{}', "archived_customer": '{}', "due_date": None, "issue_date": invoice.issue_date.strftime('%Y-%m-%d'), "paid_date": None, "cancel_date": None, "sales_tax_name": "VAT", "sales_tax_percent": '1.00', "currency": "RON", "transaction_currency": invoice.transaction_currency, "transaction_xe_rate": ("%.4f" % invoice.transaction_xe_rate if invoice.transaction_xe_rate else None), "transaction_xe_date": invoice.transaction_xe_date, "state": "issued", "proforma": build_absolute_test_url( reverse('proforma-detail', [invoice.related_document.pk])), "invoice_entries": [], "pdf_url": pdf_url, "total": 0 } for field in expected_response: self.assertEqual( expected_response[field], response.data[field], msg=("Expected %s, actual %s for field %s" % (expected_response[field], response.data[field], field))) self.assertEqual(len(response.data["transactions"]), len(expected_transactions)) for actual_transaction in response.data["transactions"]: expected_transaction = [ transaction for transaction in expected_transactions if transaction["id"] == actual_transaction["id"] ] self.assertTrue(expected_transaction) expected_transaction = expected_transaction[0] for field in expected_transaction: self.assertEqual( expected_transaction[field], actual_transaction[field], msg=("Expected %s, actual %s for field %s" % (expected_transaction[field], actual_transaction[field], field)))
def test_get_invoice(self, mocked_settings): InvoiceFactory.reset_sequence(1) TransactionFactory.reset_sequence(1) customer = CustomerFactory.create() invoice = InvoiceFactory.create(customer=customer, state=Invoice.STATES.ISSUED) invoice.generate_pdf() with mute_signals(pre_save): transactions = [ TransactionFactory.create( state=state, invoice=invoice, payment_method=PaymentMethodFactory(customer=customer) ) for state in Transaction.States.as_list() if state not in [Transaction.States.Canceled, Transaction.States.Refunded, Transaction.States.Failed] ] expected_transactions = [{ "id": str(transaction.uuid), "url": build_absolute_test_url(reverse('transaction-detail', [transaction.customer.pk, transaction.uuid])), "customer": build_absolute_test_url(reverse('customer-detail', [transaction.customer.id])), "provider": build_absolute_test_url(reverse('provider-detail', [transaction.provider.id])), "amount": "%.2f" % transaction.amount, "currency": "RON", "state": transaction.state, "proforma": build_absolute_test_url(reverse('proforma-detail', [transaction.proforma.id])), "invoice": build_absolute_test_url(reverse('invoice-detail', [transaction.invoice.id])), "can_be_consumed": transaction.can_be_consumed, "payment_processor": transaction.payment_processor, "payment_method": build_absolute_test_url(reverse('payment-method-detail', [transaction.customer.pk, transaction.payment_method.pk])), "pay_url": (build_absolute_test_url(reverse('payment', ['token'])) if transaction.state == Transaction.States.Initial else None), } for transaction in transactions] with patch('silver.utils.payments._get_jwt_token') as mocked_token: mocked_token.return_value = 'token' url = reverse('invoice-detail', kwargs={'pk': invoice.pk}) for show_pdf_storage_url, pdf_url in [ (True, build_absolute_test_url(invoice.pdf.url)), (False, build_absolute_test_url(reverse('pdf', args=[invoice.pdf.pk]))) ]: mocked_settings.SILVER_SHOW_PDF_STORAGE_URL = show_pdf_storage_url response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) expected_response = { "id": invoice.pk, "series": "InvoiceSeries", "number": 1, "provider": build_absolute_test_url(reverse('provider-detail', [invoice.provider.pk])), "customer": build_absolute_test_url(reverse('customer-detail', [invoice.customer.pk])), "archived_provider": '{}', "archived_customer": '{}', "due_date": None, "issue_date": invoice.issue_date.strftime('%Y-%m-%d'), "paid_date": None, "cancel_date": None, "sales_tax_name": "VAT", "sales_tax_percent": '1.00', "currency": "RON", "transaction_currency": invoice.transaction_currency, "transaction_xe_rate": ("%.4f" % invoice.transaction_xe_rate if invoice.transaction_xe_rate else None), "transaction_xe_date": invoice.transaction_xe_date, "state": "issued", "proforma": build_absolute_test_url(reverse('proforma-detail', [invoice.related_document.pk])), "invoice_entries": [], "pdf_url": pdf_url, "total": 0 } for field in expected_response: self.assertEqual(expected_response[field], response.data[field], msg=("Expected %s, actual %s for field %s" % ( expected_response[field], response.data[field], field))) self.assertEqual(len(response.data["transactions"]), len(expected_transactions)) for actual_transaction in response.data["transactions"]: expected_transaction = [ transaction for transaction in expected_transactions if transaction["id"] == actual_transaction["id"] ] self.assertTrue(expected_transaction) expected_transaction = expected_transaction[0] for field in expected_transaction: self.assertEqual( expected_transaction[field], actual_transaction[field], msg=("Expected %s, actual %s for field %s" % ( expected_transaction[field], actual_transaction[field], field) ) )