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_cancel_invoice_with_provided_date(authenticated_api_client): provider = ProviderFactory.create() customer = CustomerFactory.create() invoice = InvoiceFactory.create(provider=provider, customer=customer) invoice.issue() url = reverse('invoice-state', kwargs={'pk': invoice.pk}) data = {'state': 'canceled', 'cancel_date': '2014-10-10'} response = authenticated_api_client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_200_OK due_date = timezone.now().date() + timedelta( days=settings.SILVER_DEFAULT_DUE_DAYS) mandatory_content = { 'issue_date': timezone.now().date().strftime('%Y-%m-%d'), 'due_date': due_date.strftime('%Y-%m-%d'), 'cancel_date': '2014-10-10', 'state': 'canceled' } assert response.status_code == status.HTTP_200_OK assert all(item in list(response.data.items()) for item in mandatory_content.items())
def test_cancel_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': 'canceled', 'cancel_date': '2014-10-10'} response = self.client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_200_OK due_date = timezone.now().date() + timedelta(days=PAYMENT_DUE_DAYS) mandatory_content = { 'issue_date': timezone.now().date().strftime('%Y-%m-%d'), 'due_date': due_date.strftime('%Y-%m-%d'), 'cancel_date': '2014-10-10', 'state': 'canceled' } assert response.status_code == status.HTTP_200_OK assert all(item in list(response.data.items()) for item in mandatory_content.items()) assert Invoice.objects.count() == 0
def test_put_provider_without_required_field(self): """ .. note:: The test does not verify each required field, because the test test_create_provider_without_required_fields does this and since the creation will fail the update will fail too. This is more of a sanity test, to check if the correct view is called and if it does what's supposed to do for at least one field. """ provider = ProviderFactory.create() url = reverse('provider-detail', kwargs={'pk': provider.pk}) new_data = { 'id': provider.pk, 'email': '*****@*****.**', 'address_1': 'address', 'city': 'City', 'zip_code': '1', 'country': 'RO', 'flow': 'invoice', 'invoice_series': 'NSeries', 'invoice_starting_number': 2, } response = self.client.put(url, data=new_data) assert response.status_code == status.HTTP_400_BAD_REQUEST assert response.data == {'name': ['This field is required.']}
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_issue_proforma_with_custom_issue_date_and_due_date(self): provider = ProviderFactory.create() customer = CustomerFactory.create() proforma = ProformaFactory.create(provider=provider, customer=customer) url = reverse('proforma-state', kwargs={'pk': proforma.pk}) data = { 'state': 'issued', 'issue_date': '2014-01-01', 'due_date': '2014-01-20' } response = self.client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_200_OK mandatory_content = { 'issue_date': '2014-01-01', 'due_date': '2014-01-20', 'state': 'issued' } assert response.status_code == status.HTTP_200_OK assert all(item in list(response.data.items()) for item in mandatory_content.items()) assert response.data.get('archived_provider', {}) != {} assert response.data.get('archived_customer', {}) != {} assert Invoice.objects.count() == 0 proforma = get_object_or_None(Proforma, pk=1)
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_issue_invoice_with_custom_issue_date_and_due_date( authenticated_api_client): provider = ProviderFactory.create() customer = CustomerFactory.create() invoice = InvoiceFactory.create(provider=provider, customer=customer) url = reverse('invoice-state', kwargs={'pk': invoice.pk}) data = { 'state': 'issued', 'issue_date': '2014-01-01', 'due_date': '2014-01-20' } response = authenticated_api_client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_200_OK mandatory_content = { 'issue_date': '2014-01-01', 'due_date': '2014-01-20', 'state': 'issued' } assert response.status_code == status.HTTP_200_OK assert all(item in list(response.data.items()) for item in mandatory_content.items()) assert response.data.get('archived_provider', {}) != {} assert response.data.get('archived_customer', {}) != {}
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_delete_provider(self): provider = ProviderFactory.create() url = reverse('provider-detail', kwargs={'pk': provider.pk}) response = self.client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT
def test_create_plan(self): url = reverse('plan-list') ProductCodeFactory.create_batch(2) feature1_pc = ProductCode.objects.all()[0].value plan_pc = ProductCode.objects.all()[1].value provider = ProviderFactory.create() provider_url = reverse('provider-detail', kwargs={'pk': provider.pk}) response = self.client.post(url, json.dumps({ "name": "Hydrogen", "interval": "month", "interval_count": 1, "amount": 149.99, "currency": "USD", "trial_period_days": 15, "generate_after": 86400, "enabled": True, "private": False, "product_code": plan_pc, 'metered_features': [{ 'name': 'Page Views', 'unit': '100k', 'price_per_unit': 0.01, 'included_units': 0, 'product_code': feature1_pc }, { 'name': 'VIP Support', 'price_per_unit': 49.99, 'unit': 1, 'included_units': 1, 'product_code': "1234" }], 'provider': provider_url }), content_type='application/json') assert response.status_code == status.HTTP_201_CREATED
def test_pay_proforma_when_in_draft_state(self): provider = ProviderFactory.create() customer = CustomerFactory.create() proforma = ProformaFactory.create(provider=provider, customer=customer) url = reverse('proforma-state', kwargs={'pk': proforma.pk}) data = {'state': 'paid'} response = self.client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_403_FORBIDDEN assert response.data == {'detail': 'A proforma can be paid only if it is in issued state.'} assert Invoice.objects.count() == 0
def test_illegal_state_change_when_in_draft_state(authenticated_api_client): provider = ProviderFactory.create() customer = CustomerFactory.create() invoice = InvoiceFactory.create(provider=provider, customer=customer) url = reverse('invoice-state', kwargs={'pk': invoice.pk}) data = {'state': 'illegal-state'} response = authenticated_api_client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_403_FORBIDDEN assert response.data == {'detail': 'Illegal state value.'}
def test_illegal_state_change_when_in_draft_state(self): provider = ProviderFactory.create() customer = CustomerFactory.create() proforma = ProformaFactory.create(provider=provider, customer=customer) url = reverse('proforma-state', kwargs={'pk': proforma.pk}) data = {'state': 'illegal-state'} response = self.client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_403_FORBIDDEN assert response.data == {'detail': 'Illegal state value.'} assert Invoice.objects.count() == 0
def test_pay_invoice_when_in_draft_state(authenticated_api_client): provider = ProviderFactory.create() customer = CustomerFactory.create() invoice = InvoiceFactory.create(provider=provider, customer=customer) url = reverse('invoice-state', kwargs={'pk': invoice.pk}) data = {'state': 'paid'} response = authenticated_api_client.put(url, data=json.dumps(data), content_type='application/json') assert response.status_code == status.HTTP_403_FORBIDDEN assert response.data == { 'detail': 'An invoice can be paid only if it is in issued state.' }
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_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_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 provider(db): return ProviderFactory.create()
def provider(db): from silver.fixtures.factories import ProviderFactory return ProviderFactory.create()