def test_post_invoice_with_invoice_entries(self): CustomerFactory.create() ProviderFactory.create() SubscriptionFactory.create() url = reverse('invoice-list') data = { 'provider': 'http://testserver/providers/1/', 'customer': 'http://testserver/customers/1/', 'series': None, 'number': None, 'currency': 'RON', 'invoice_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 test_edit_patch_customer(self): CustomerFactory.create() changed_data = self.complete_data.copy() unchanged_fields = [ 'emails', 'zip_code', 'company', 'payment_due_days' ] ignore_fields = ['url', 'id', 'subscriptions', 'payments'] for field in unchanged_fields: changed_data.pop(field) url = reverse('customer-detail', kwargs={'pk': 1}) 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_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_get_transaction(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) transaction = TransactionFactory.create(payment_method=payment_method) invoice = transaction.invoice proforma = transaction.proforma provider = invoice.provider expected = OrderedDict([ ('id', unicode(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', unicode(Decimal('0.00') + transaction.amount)), ('currency', unicode(transaction.currency)), ('currency_rate_date', None), ('state', unicode(transaction.state)), ('proforma', reverse('proforma-detail', args=[proforma.pk])), ('invoice', reverse('invoice-detail', args=[invoice.pk])), ('can_be_consumed', transaction.can_be_consumed), ('payment_method', reverse('payment-method-detail', kwargs={'customer_pk': customer.id, 'payment_method_id': payment_method.id})), ('pay_url', reverse('pay-transaction', kwargs={'transaction_uuid': transaction.uuid})), ('valid_until', None) ]) url = reverse('transaction-detail', kwargs={'customer_pk': customer.pk, 'transaction_uuid': transaction.uuid}) response = self.client.get(url, format='json') self.assertEqual(response.data, dict(expected))
def test_add_transaction_with_documents_for_a_different_customer(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) proforma = ProformaFactory.create() proforma.state = proforma.STATES.ISSUED proforma.create_invoice() proforma.refresh_from_db() invoice = proforma.invoice valid_until = datetime.now() 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_add_transaction_with_unrelated_documents(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) invoice = InvoiceFactory.create(customer=customer) proforma = ProformaFactory.create(customer=customer) valid_until = datetime.now() 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'Invoice and proforma are not related.'] } self.assertEqual(response.data, expected_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
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
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_cancel_invoice_with_provided_date(self): provider = ProviderFactory.create() customer = CustomerFactory.create() invoice = InvoiceFactory.create(provider=provider, customer=customer) invoice.issue() invoice.save() url = reverse('invoice-state', kwargs={'pk': 1}) 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 response.data.items() for item in mandatory_content.iteritems())
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 response.data.items() for item in mandatory_content.iteritems()) 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() proforma.save() 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) 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': 'http://testserver/invoices/%s/' % proforma.invoice.pk } assert response.status_code == status.HTTP_200_OK assert all(item in response.data.items() for item in mandatory_content.iteritems()) invoice = Invoice.objects.all()[0] assert proforma.invoice == invoice assert invoice.proforma == proforma invoice = get_object_or_None(Invoice, proforma=proforma)
def test_pay_invoice_with_provided_date(self): provider = ProviderFactory.create() customer = CustomerFactory.create() invoice = InvoiceFactory.create(provider=provider, customer=customer) invoice.issue() invoice.save() url = reverse('invoice-state', kwargs={'pk': invoice.pk}) data = { 'state': 'paid', 'paid_date': '2014-05-05' } 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'), 'paid_date': '2014-05-05', 'state': 'paid' } assert response.status_code == status.HTTP_200_OK assert all(item in response.data.items() for item in mandatory_content.iteritems())
def test_process_transaction_with_credit_card(self): customer = CustomerFactory.create() customer.meta = self.customer.meta payment_method = AuthorizeNetRecurringPaymentMethodFactory.create( payment_processor='AuthorizeNetTriggered', customer=customer ) transaction = AuthorizeNetTransactionFactory.create( state=Transaction.States.Initial, data={ 'id': '1235', 'status': None, 'authorizenet_id': None, }, payment_method=payment_method, ) assert transaction.state == transaction.States.Initial payment_processor = get_instance(transaction.payment_processor) status = payment_processor.process_transaction(transaction) assert status == True assert transaction.state == transaction.States.Pending
def test_issue_proforma_with_custom_issue_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'} 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': '2014-01-01', 'due_date': due_date.strftime('%Y-%m-%d'), '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_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 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_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_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_get_subscription_list(self): customer = CustomerFactory.create() SubscriptionFactory.create_batch(40, 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._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_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 ' u'least one document (invoice or proforma).' ] } self.assertEqual(response.data, expected_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_issue_invoice_with_custom_issue_date_and_due_date(self): provider = ProviderFactory.create() customer = CustomerFactory.create() InvoiceFactory.create(provider=provider, customer=customer) url = reverse('invoice-state', kwargs={'pk': 1}) 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 response.data.items() for item in mandatory_content.iteritems()) assert response.data.get('archived_provider', {}) != {} assert response.data.get('archived_customer', {}) != {} invoice = get_object_or_None(Invoice, pk=1)
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_filter_disabled(self): customer = CustomerFactory.create() payment = PaymentFactory.create(customer=customer, amount=100) payment_method = PaymentMethodFactory.create( payment_processor='someprocessor', customer=customer) transaction = TransactionFactory.create(payment_method=payment_method, payment=payment, disabled=False) transaction_data = self._transaction_data(customer, payment, payment_method, transaction) 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_not_disabled = url + '?disabled=False' url_no_output = url + '?disabled=True' response = self.client.get(url_not_disabled, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data[0], transaction_data) response = self.client.get(url_no_output, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, [])
def test_issue_invoice_with_default_dates(self): provider = ProviderFactory.create() customer = CustomerFactory.create() InvoiceFactory.create(provider=provider, customer=customer) url = reverse('invoice-state', kwargs={'pk': 1}) data = {'state': 'issued'} 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'), 'state': 'issued' } assert response.status_code == status.HTTP_200_OK assert all(item in response.data.items() for item in mandatory_content.iteritems()) assert response.data.get('archived_provider', {}) != {} assert response.data.get('archived_customer', {}) != {} invoice = get_object_or_None(Invoice, pk=1)
def create_basic_plan(self, start_date, end_date, interval=Plan.INTERVALS.YEAR): self.provider = ProviderFactory.create(flow=Provider.FLOWS.INVOICE) self.customer = CustomerFactory.create( consolidated_billing=False, sales_tax_percent=Decimal('0.00')) self.currency = 'USD' self.plan = PlanFactory.create( interval=interval, interval_count=1, generate_after=0, enabled=True, provider=self.provider, product_code=ProductCodeFactory(value="yearly-seat-plan"), amount=Decimal('10.00'), prebill_plan=False, currency=self.currency, trial_period_days=None, cycle_billing_duration=dt.timedelta(days=1), ) self.plan.save() # Create the prorated subscription self.subscription = SubscriptionFactory.create(plan=self.plan, start_date=start_date, customer=self.customer) self.subscription.activate() self.subscription.save()
def test_process_create_customer_profile(self): customer = CustomerFactory.create() customer.meta = self.customer.meta customer_data = CustomerData.objects.create(customer=customer, data={ 'id': '1235', }) payment_method = AuthorizeNetRecurringPaymentMethodFactory.create( payment_processor='AuthorizeNetTriggered', customer=customer, ) transaction = AuthorizeNetTransactionFactory.create( state=Transaction.States.Initial, data={ 'id': '1235', }, payment_method=payment_method) payment_processor = get_instance(transaction.payment_processor) resp = payment_processor.create_customer_profile(customer) import os print(os.environ) assert resp == True tok = payment_processor.client_token(customer) customer_data = CustomerData.objects.get(customer=customer) assert tok == customer_data.get('payment_id')
def test_issue_invoice_with_custom_issue_date_and_due_date(self): 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 = 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', {}) != {}
def test_get_subscription_list(self): customer = CustomerFactory.create() SubscriptionFactory.create_batch(40, 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._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_cancel_proforma_with_provided_date(self): provider = ProviderFactory.create() customer = CustomerFactory.create() proforma = ProformaFactory.create(provider=provider, customer=customer) proforma.issue() proforma.save() 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 response.data.items() for item in mandatory_content.iteritems()) assert Invoice.objects.count() == 0
def test_fetch_transaction_status_with_null_tx_id(self): customer = CustomerFactory.create() customer.meta = self.customer.meta entry = EntryFactory.create(unit_price=25.00) invoice = InvoiceFactory.create( series="pytest", customer=customer, invoice_entries=[entry], state='issued', ) payment_method = AuthorizeNetRecurringPaymentMethodFactory.create( payment_processor='AuthorizeNetTriggered', customer=customer) transaction = AuthorizeNetTransactionFactory.create( invoice=invoice, state=Transaction.States.Pending, data={ 'id': '1235', 'status': None, 'authorizenet_id': None, }, payment_method=payment_method, ) assert str(transaction.data.get('authorizenet_id')) == "None" payment_processor = get_instance(transaction.payment_processor) status = payment_processor.fetch_transaction_status(transaction) assert status == False
def test_process_transaction_from_customer_profile(self): # TODO: return customer = CustomerFactory.create() customer.meta = self.customer.meta customer_data = CustomerData.objects.create(customer=customer, data={ 'id': '1235', }) payment_method = AuthorizeNetRecurringPaymentMethodFactory.create( payment_processor='AuthorizeNetTriggered', customer=customer, ) transaction = AuthorizeNetTransactionFactory.create( state=Transaction.States.Initial, data={ 'id': '1235', }, payment_method=payment_method) payment_processor = get_instance(transaction.payment_processor) resp = payment_processor.execute_transaction(transaction, charge_profile=True) assert resp == True tok = payment_processor.client_token(customer) customer_data = CustomerData.objects.get(customer=customer) assert tok == customer_data.get('payment_id')
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 response.data.items() for item in mandatory_content.iteritems()) invoice = Invoice.objects.all()[0] assert proforma.related_document == invoice assert invoice.related_document == proforma
def test_modify_one(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) payment = PaymentFactory.create(customer=customer) transaction_1 = TransactionFactory.create( payment_method=payment_method, payment=payment) valid_until = datetime.now() url = reverse('transaction-detail', kwargs={ 'customer_pk': customer.id, 'transaction_uuid': transaction_1.uuid }) data = { 'payment': reverse('payment-detail', kwargs={ 'customer_pk': customer.id, 'payment_pk': payment_method.id }), 'valid_until': valid_until } response = self.client.put(url, format='json', data=data) self.assertEqual(response.data['detail'], 'Method "PUT" not allowed.') response = self.client.post(url, format='json', data=data) self.assertEqual(response.data['detail'], 'Method "POST" not allowed.') response = self.client.patch(url, format='json', data=data) self.assertEqual(response.data['detail'], 'Method "PATCH" not allowed.')
def test_filter_payment_method(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create( payment_processor='someprocessor', 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_method=someprocessor' url_no_output = url + '?payment_method=Random' response = self.client.get(url_method_someprocessor, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data[0], transaction_data_1) self.assertEqual(response.data[1], 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_create_one_without_required_fields(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) payment = PaymentFactory.create(customer=customer) transaction = TransactionFactory.create(payment_method=payment_method, payment=payment) valid_until = datetime.now() url = reverse('transaction-detail', kwargs={ 'customer_pk': customer.id, 'transaction_uuid': transaction.uuid }) data = { 'payment': reverse('payment-detail', kwargs={ 'customer_pk': customer.id, 'payment_pk': payment.id }), 'valid_until': valid_until } url = reverse('payment-method-transaction-list', kwargs={ 'customer_pk': customer.id, 'payment_method_id': payment_method.id }) response = self.client.post(url, format='json', data=data) self.assertEqual(response.data['payment_method'], ['This field is required.'])
def test_edit_put_customer(self): customer = CustomerFactory.create() changed_data = self.complete_data.copy() unchanged_fields = ['email', 'address_2'] 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.put(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_issue_proforma_with_custom_issue_date(self): provider = ProviderFactory.create() customer = CustomerFactory.create() ProformaFactory.create(provider=provider, customer=customer) url = reverse('proforma-state', kwargs={'pk': 1}) data = {'state': 'issued', 'issue_date': '2014-01-01'} 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': '2014-01-01', 'due_date': due_date.strftime('%Y-%m-%d'), 'state': 'issued' } assert response.status_code == status.HTTP_200_OK assert all(item in response.data.items() for item in mandatory_content.iteritems()) 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_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.invoice 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'], unicode(Decimal('0.00') + invoice.transaction_total)) self.assertEqual(response.data['invoice'], invoice_url) self.assertEqual(response.data['proforma'], proforma_url) self.assertEqual(response.data['currency'], invoice.transaction_currency)
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_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_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_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, [])
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_pay_invoice_when_in_draft_state(self): provider = ProviderFactory.create() customer = CustomerFactory.create() InvoiceFactory.create(provider=provider, customer=customer) url = reverse('invoice-state', kwargs={'pk': 1}) 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': '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, '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): CustomerFactory.create() ProviderFactory.create() SubscriptionFactory.create() url = reverse('proforma-list') data = { 'provider': 'http://testserver/providers/1/', 'customer': 'http://testserver/customers/1/', 'series': "", 'number': "", 'currency': 'RON', 'proforma_entries': [] } response = self.client.post(url, data=data) assert response.status_code == status.HTTP_201_CREATED assert response.data == { "id": 1, "series": "ProformaSeries", "number": 1, "provider": "http://testserver/providers/1/", "customer": "http://testserver/customers/1/", "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", 'pdf_url': None, "state": "draft", "invoice": None, "proforma_entries": [], "total": Decimal('0.00'), }
def test_cancel_proforma_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': 'canceled'} 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 canceled only if it is in issued state.'} assert Invoice.objects.count() == 0
def test_post_invoice_with_invoice_entries(self): CustomerFactory.create() ProviderFactory.create() SubscriptionFactory.create() url = reverse('invoice-list') data = { 'provider': 'http://testserver/providers/1/', 'customer': 'http://testserver/customers/1/', 'series': None, 'number': None, 'currency': 'RON', 'invoice_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 test_illegal_state_change_when_in_draft_state(self): provider = ProviderFactory.create() customer = CustomerFactory.create() InvoiceFactory.create(provider=provider, customer=customer) url = reverse('invoice-state', kwargs={'pk': 1}) 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.'}
def test_edit_patch_customer(self): CustomerFactory.create() changed_data = self.complete_data.copy() unchanged_fields = ['email', 'zip_code', 'company', 'payment_due_days'] for field in unchanged_fields: changed_data.pop(field) url = reverse('customer-detail', kwargs={'pk': 1}) 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 ['url', 'id', 'subscriptions']: 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_create_post_subscription(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-12-07', "start_date": '2014-11-19' }), content_type='application/json') assert response.status_code == status.HTTP_201_CREATED
def test_illegal_state_change_when_in_issued_state(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': '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_cancel_invoice_in_canceled_state(self): provider = ProviderFactory.create() customer = CustomerFactory.create() invoice = InvoiceFactory.create(provider=provider, customer=customer) invoice.issue() invoice.cancel() url = reverse('invoice-state', kwargs={'pk': invoice.pk}) data = {'state': 'canceled'} 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': 'An invoice can be canceled only if it is in issued state.' }
def test_not_allowed_methods(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) transaction_1 = TransactionFactory.create(payment_method=payment_method) valid_until = datetime.now() url = reverse('transaction-detail', kwargs={'customer_pk': customer.id, 'transaction_uuid': transaction_1.uuid}) data = { 'valid_until': valid_until } response = self.client.put(url, format='json', data=data) self.assertEqual(response.data['detail'], 'Method "PUT" not allowed.') response = self.client.post(url, format='json', data=data) self.assertEqual(response.data['detail'], 'Method "POST" not allowed.')
def test_create_transactions_on_payment_method_verify(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create( payment_processor=triggered_processor, customer=customer, canceled=False, verified=False ) lone_invoice = InvoiceFactory.create( transaction_currency='USD', transaction_xe_rate=Decimal('1.0'), state=Invoice.STATES.ISSUED, customer=customer ) lone_proforma = ProformaFactory.create( transaction_currency='USD', transaction_xe_rate=Decimal('1.0'), state=Proforma.STATES.ISSUED, customer=customer ) paired_proforma = ProformaFactory.create( transaction_currency='USD', transaction_xe_rate=Decimal('1.0'), state=Proforma.STATES.ISSUED, issue_date=timezone.now().date(), customer=customer ) paired_invoice = paired_proforma.create_invoice() self.assertEqual(payment_method.transactions.count(), 0) payment_method.verified = True payment_method.save() self.assertEqual(lone_invoice.transactions.count(), 1) self.assertEqual(lone_proforma.transactions.count(), 1) self.assertEqual(list(paired_invoice.transactions), list(paired_proforma.transactions)) self.assertEqual(paired_invoice.transactions.count(), 1)