def test_get_customer_list(self): CustomerFactory.create_batch(settings.API_PAGE_SIZE * 2) url = reverse('customer-list') response = self.client.get(url) full_url = None for field in response.data: full_url = field.get('url', None) if full_url: break if full_url: domain = full_url.split('/')[2] full_url = full_url.split(domain)[0] + domain + url assert response.status_code == status.HTTP_200_OK assert response['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['link'] == \ ('<' + full_url + '>; rel="prev", ' + '<' + full_url + '?page=1>; rel="first", ' + '<' + full_url + '?page=2> rel="last"')
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_add_transaction_with_amount_greater_than_what_should_be_charged(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) proforma = ProformaFactory.create(customer=customer, state=Proforma.STATES.ISSUED, issue_date=timezone.now().date()) proforma.create_invoice() invoice = proforma.related_document valid_until = datetime.now().replace(microsecond=0) url = reverse('payment-method-transaction-list', kwargs={'customer_pk': customer.pk, 'payment_method_id': payment_method.pk}) invoice_url = reverse('invoice-detail', args=[invoice.pk]) proforma_url = reverse('proforma-detail', args=[proforma.pk]) data = { 'payment_method': reverse('payment-method-detail', kwargs={'customer_pk': customer.pk, 'payment_method_id': payment_method.id}), 'valid_until': valid_until, 'amount': invoice.total_in_transaction_currency + 1, 'invoice': invoice_url, 'proforma': proforma_url } response = self.client.post(url, format='json', data=data) expected_data = { 'non_field_errors': ["Amount is greater than the amount that should be charged in " "order to pay the billing document."] } self.assertEqual(response.data, expected_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
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_edit_patch_customer(self): customer = CustomerFactory.create() changed_data = self.complete_data.copy() unchanged_fields = [ 'email', 'zip_code', 'company', 'phone', 'payment_due_days' ] ignore_fields = [ 'url', 'id', 'subscriptions', 'payment_methods', 'transactions' ] for field in unchanged_fields: changed_data.pop(field) url = reverse('customer-detail', kwargs={'customer_pk': customer.pk}) response = self.client.patch(url, data=json.dumps(changed_data), content_type='application/json') self.assertEqual(response.status_code, status.HTTP_200_OK) for e in ignore_fields: response.data.pop(e) for field in response.data: if field not in unchanged_fields: self.assertEqual(response.data[field], self.complete_data[field])
def test_invoice_currency_used_for_transaction_currency(self): customer = CustomerFactory.create(currency=None) invoice = InvoiceFactory.create(customer=customer, currency='EUR', transaction_currency=None) self.assertEqual(invoice.transaction_currency, 'EUR')
def test_add_transaction_with_documents_for_a_different_customer(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) proforma = ProformaFactory.create() proforma.issue() proforma.create_invoice() proforma.refresh_from_db() invoice = proforma.related_document valid_until = datetime.now().replace(microsecond=0) url = reverse('payment-method-transaction-list', kwargs={'customer_pk': customer.pk, 'payment_method_id': payment_method.pk}) invoice_url = reverse('invoice-detail', args=[invoice.pk]) proforma_url = reverse('proforma-detail', args=[proforma.pk]) data = { 'payment_method': reverse('payment-method-detail', kwargs={'customer_pk': customer.pk, 'payment_method_id': payment_method.id}), 'valid_until': valid_until, 'amount': 200.0, 'invoice': invoice_url, 'proforma': proforma_url } response = self.client.post(url, format='json', data=data) expected_data = { 'non_field_errors': [u"Customer doesn't match with the one in documents."] } self.assertEqual(response.data, expected_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_get_subscription_list(self): customer = CustomerFactory.create() SubscriptionFactory.create_batch(settings.API_PAGE_SIZE * 2, customer=customer) url = reverse('subscription-list', kwargs={'customer_pk': customer.pk}) response = self.client.get(url) full_url = None for field in response.data: full_url = field.get('url', None) if full_url: break if full_url: domain = full_url.split('/')[2] full_url = full_url.split(domain)[0] + domain + url assert response.status_code == status.HTTP_200_OK assert response['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['link'] == \ ('<' + full_url + '>; rel="prev", ' + '<' + full_url + '?page=1>; rel="first", ' + '<' + full_url + '?page=2> rel="last"') for subscription_data in response.data: subscription = Subscription.objects.get(id=subscription_data['id']) assert subscription_data == spec_subscription(subscription)
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_get_subscription_list_reference_filter(self): customer = CustomerFactory.create() subscriptions = SubscriptionFactory.create_batch(3, customer=customer) url = reverse('subscription-list', kwargs={'customer_pk': customer.pk}) references = [subscription.reference for subscription in subscriptions] reference = '?reference=' + references[0] response = self.client.get(url + reference) assert len(response.data) == 1 assert response.status_code == status.HTTP_200_OK reference = '?reference=' + ','.join(references) response = self.client.get(url + reference) assert len(response.data) == 3 assert response.status_code == status.HTTP_200_OK reference = '?reference=' + ','.join(references[:-1]) + ',invalid' response = self.client.get(url + reference) assert len(response.data) == 2 assert response.status_code == status.HTTP_200_OK for subscription_data in response.data: subscription = Subscription.objects.get(id=subscription_data['id']) assert subscription_data == spec_subscription(subscription)
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_proforma_currency_used_for_transaction_currency(self): customer = CustomerFactory.create(currency=None) proforma = ProformaFactory.create(customer=customer, currency='EUR', transaction_currency=None) self.assertEqual(proforma.transaction_currency, 'EUR')
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_add_transaction_without_documents(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) valid_until = datetime.now().replace(microsecond=0) url = reverse('payment-method-transaction-list', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method.pk }) data = { 'payment_method': reverse('payment-method-detail', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method.id }), 'valid_until': valid_until, 'amount': 200.0, } response = self.client.post(url, format='json', data=data) expected_data = { 'non_field_errors': [ u'The transaction must have at least one billing document ' u'(invoice or proforma).' ] } self.assertEqual(response.data, expected_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
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_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_add_transaction_without_currency_and_amount(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) entries = DocumentEntryFactory.create_batch(2) proforma = ProformaFactory.create(customer=customer, state=Proforma.STATES.ISSUED, issue_date=timezone.now().date(), currency='USD', transaction_currency='RON', transaction_xe_rate=Decimal('0.25'), proforma_entries=entries) proforma.create_invoice() invoice = proforma.related_document valid_until = datetime.now().replace(microsecond=0) + timedelta( minutes=30) url = reverse('payment-method-transaction-list', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method.pk }) payment_method_url = reverse('payment-method-detail', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method.id }) invoice_url = reverse('invoice-detail', args=[invoice.pk]) proforma_url = reverse('proforma-detail', args=[proforma.pk]) data = { 'payment_method': reverse('payment-method-detail', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method.id }), 'valid_until': valid_until, 'invoice': invoice_url, 'proforma': proforma_url } response = self.client.post(url, format='json', data=data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data['payment_method'], payment_method_url) self.assertEqual(response.data['valid_until'][:-1], valid_until.isoformat()) self.assertEqual(response.data['can_be_consumed'], True) self.assertEqual(response.data['amount'], force_str(invoice.total_in_transaction_currency)) self.assertEqual(response.data['invoice'], invoice_url) self.assertEqual(response.data['proforma'], proforma_url) self.assertEqual(response.data['currency'], invoice.transaction_currency)
def test_delete_customer(self): customer = CustomerFactory.create() url = reverse('customer-detail', kwargs={'customer_pk': customer.pk}) response = self.client.delete(url) assert response.status_code == status.HTTP_204_NO_CONTENT assert Customer.objects.all().count() == 0
def test_get_listing(self): PaymentMethodFactory.create(customer=CustomerFactory.create()) payment_method = self.create_payment_method(customer=self.customer) url = reverse('payment-method-list', kwargs={'customer_pk': self.customer.pk}) self.assert_get_data(url, [payment_method])
def test_get_customer_detail(self): customer = CustomerFactory.create() url = reverse('customer-detail', kwargs={'customer_pk': customer.pk}) response = self.client.get(url) self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertNotEqual(response.data, []) self.assertEqual(response.data['phone'], customer.phone)
def test_get_detail(self): PaymentMethodFactory.create(customer=CustomerFactory.create()) payment_method = self.create_payment_method(customer=self.customer) url = reverse('payment-method-detail', kwargs={ 'customer_pk': self.customer.pk, 'payment_method_id': payment_method.pk }) self.assert_get_data(url, payment_method)
def test_get_subscription_detail_unexisting(self): customer = CustomerFactory.create() url = reverse('subscription-detail', kwargs={'subscription_pk': 42, 'customer_pk': customer.pk}) response = self.client.get(url) assert response.status_code == status.HTTP_404_NOT_FOUND assert response.data == {u'detail': u'Not found.'}
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_add_transaction(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) entry = DocumentEntryFactory(quantity=1, unit_price=200) proforma = ProformaFactory.create(customer=customer, proforma_entries=[entry]) proforma.issue() proforma.create_invoice() proforma.refresh_from_db() invoice = proforma.related_document payment_method_url = reverse('payment-method-detail', kwargs={'customer_pk': customer.pk, 'payment_method_id': payment_method.id}) invoice_url = reverse('invoice-detail', args=[invoice.pk]) proforma_url = reverse('proforma-detail', args=[proforma.pk]) url = reverse('payment-method-transaction-list', kwargs={'customer_pk': customer.pk, 'payment_method_id': payment_method.pk}) valid_until = datetime.now().replace(microsecond=0) + timedelta(minutes=30) currency = invoice.transaction_currency data = { 'payment_method': reverse('payment-method-detail', kwargs={'customer_pk': customer.pk, 'payment_method_id': payment_method.id}), 'amount': invoice.total_in_transaction_currency, 'invoice': invoice_url, 'proforma': proforma_url, 'valid_until': valid_until, 'currency': currency, } response = self.client.post(url, format='json', data=data) self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.data['payment_method'], payment_method_url) self.assertEqual(response.data['valid_until'][:-1], valid_until.isoformat()) self.assertEqual(response.data['can_be_consumed'], True) self.assertEqual(response.data['amount'], force_text(invoice.total_in_transaction_currency)) self.assertEqual(response.data['invoice'], invoice_url) self.assertEqual(response.data['proforma'], proforma_url) self.assertEqual(response.data['currency'], currency) self.assertTrue(Transaction.objects.filter(uuid=response.data['id']))
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_filter_payment_method(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create( payment_processor=triggered_processor, customer=customer ) transaction1 = TransactionFactory.create( payment_method=payment_method ) transaction_data_1 = self._transaction_data(transaction1) transaction2 = TransactionFactory.create( payment_method=payment_method ) transaction_data_2 = self._transaction_data(transaction2) urls = [ reverse( 'payment-method-transaction-list', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method.pk}), reverse( 'transaction-list', kwargs={'customer_pk': customer.pk})] for url in urls: url_method_someprocessor = ( url + '?payment_processor=' + triggered_processor ) url_no_output = url + '?payment_processor=Random' with patch('silver.utils.payments._get_jwt_token') as mocked_token: mocked_token.return_value = 'token' response = self.client.get(url_method_someprocessor, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) transaction1.refresh_from_db() transaction_data_1['updated_at'] = response.data[1]['updated_at'] transaction1.refresh_from_db() transaction_data_2['updated_at'] = response.data[0]['updated_at'] self.assertEqual(response.data[1], transaction_data_1) self.assertEqual(response.data[0], transaction_data_2) response = self.client.get(url_no_output, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, [])
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_create_post_subscription_with_invalid_trial_end(self): plan = PlanFactory.create() customer = CustomerFactory.create() plan_url = reverse('plan-detail', kwargs={'pk': plan.pk}) url = reverse('subscription-list', kwargs={'customer_pk': customer.pk}) response = self.client.post(url, json.dumps({ "plan": plan_url, "trial_end": '2014-11-07', "start_date": '2014-11-19' }), content_type='application/json') assert response.status_code == status.HTTP_400_BAD_REQUEST