def test_get_customer_list(self): CustomerFactory.create_batch(40) 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._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_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_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_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_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_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_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_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"') for subscription_data in response.data: subscription = Subscription.objects.get(id=subscription_data['id']) assert subscription_data == spec_subscription(subscription)
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_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_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, [])
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_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_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 = ['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_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_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_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_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_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 asserted_response = 'A proforma can be canceled only if it is in issued state.' assert response.data == {'detail': asserted_response} assert Invoice.objects.count() == 0
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
def test_cancel_invoice_in_draft_state(self): provider = ProviderFactory.create() customer = CustomerFactory.create() invoice = InvoiceFactory.create(provider=provider, customer=customer) 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_add_transaction_with_amount_different_from_document(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.invoice 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': [ u"Transaction amount is different from it's " u"document's total_in_transaction_currency." ] } self.assertEqual(response.data, expected_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_create_one_without_required_fields(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) valid_until = datetime.now().replace(microsecond=0) data = {'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_illegal_state_change_when_in_paid_state(self): provider = ProviderFactory.create() customer = CustomerFactory.create() proforma = ProformaFactory.create(provider=provider, customer=customer) proforma.issue() proforma.pay() 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() == 1
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)
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_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_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'], invoice.transaction_currency)
def test_add_transaction_with_unrelated_documents(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) invoice = InvoiceFactory.create(customer=customer) invoice.issue() invoice.save() proforma = ProformaFactory.create(customer=customer) proforma.issue() proforma.save() 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'Invoice and proforma are not related.'] } self.assertEqual(response.data, expected_data) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_pay_proforma_when_in_paid_state(self): provider = ProviderFactory.create() customer = CustomerFactory.create() proforma = ProformaFactory.create(provider=provider, customer=customer) proforma.issue() proforma.pay() proforma.save() url = reverse('proforma-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': 'A proforma can be paid only if it is in issued state.' } assert Invoice.objects.count() == 1
def test_create_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 subscription = Subscription.objects.get(id=response.data['id']) assert response.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) expected = self._transaction_data(transaction) with patch('silver.utils.payments._get_jwt_token') as mocked_token: mocked_token.return_value = 'token' 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_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().replace(microsecond=0) 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_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().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_put_detail_ignore_customer_change(self): other_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 }) response = self.client.get(url, format='json') expected_data = deepcopy(response.data) data = response.data data['customer'] = reverse('customer-detail', request=response.wsgi_request, kwargs={'customer_pk': other_customer.pk}) response = self.client.put(url, data=data, format='json') self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertEqual(response.data, expected_data)
def test_list_transactions(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) transaction_1 = TransactionFactory.create(payment_method=payment_method) expected_t1 = self._transaction_data(transaction_1) transaction_2 = TransactionFactory.create(payment_method=payment_method) expected_t2 = self._transaction_data(transaction_2) with patch('silver.utils.payments._get_jwt_token') as mocked_token: mocked_token.return_value = 'token' url = reverse('transaction-list', kwargs={'customer_pk': customer.pk}) response = self.client.get(url, format='json') self.assertEqual(response.data[1], expected_t1) self.assertEqual(response.data[0], expected_t2)
def test_filter_min_max_amount(self): customer = CustomerFactory.create() payment = PaymentFactory.create(customer=customer, amount=100) payment_method_ok = PaymentMethodFactory.create( payment_processor='someprocessor', customer=customer) transaction = TransactionFactory.create( payment_method=payment_method_ok, payment=payment) transaction_data = self._transaction_data(customer, payment, payment_method_ok, transaction) urls = [ reverse('payment-method-transaction-list', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method_ok.pk }), reverse('transaction-list', kwargs={'customer_pk': customer.pk}) ] for url in urls: url_with_filterable_data = url + '?min_amount=10' url_no_output = url + '?min_amount=150' response = self.client.get(url_with_filterable_data, 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, []) url_with_filterable_data = url + '?max_amount=1050' url_no_output = url + '?max_amount=10' response = self.client.get(url_with_filterable_data, 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_add_transaction(self): customer = CustomerFactory.create() payment_method = PaymentMethodFactory.create(customer=customer) payment = PaymentFactory.create(customer=customer) valid_until = datetime.now() payment_method_url = reverse('payment-method-detail', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method.id }) payment_url = reverse('payment-detail', kwargs={ 'customer_pk': customer.pk, 'payment_pk': payment.pk }) 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 }), 'payment': reverse('payment-detail', kwargs={ 'customer_pk': customer.pk, 'payment_pk': payment.pk }), 'valid_until': valid_until } response = self.client.post(url, format='json', data=data).data self.assertEqual(response['payment_method'], payment_method_url) self.assertEqual(response['payment'], payment_url) self.assertEqual(response['valid_until'][:-1], valid_until.isoformat()) self.assertEqual(response['is_usable'], False)
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)
def test_transaction_list(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) expected_t1 = self._transaction_data(customer, payment, payment_method, transaction_1) transaction_2 = TransactionFactory.create( payment_method=payment_method, payment=payment) expected_t2 = self._transaction_data(customer, payment, payment_method, transaction_2) url = reverse('transaction-list', kwargs={'customer_pk': customer.pk}) response = self.client.get(url, format='json') self.assertEqual(response.data[0], expected_t1) self.assertEqual(response.data[1], expected_t2)
def test_create_post_subscription_description(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}) test_description = 'test description' response = self.client.post(url, json.dumps({ "plan": plan_url, "start_date": '2014-11-19', "description": test_description, }), content_type='application/json') assert response.status_code == status.HTTP_201_CREATED assert response.data["description"] == test_description
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) entry = EntryFactory.create(unit_price=25.00) invoice = InvoiceFactory.create( series="pytest", customer=customer, invoice_entries=[entry], state='issued', ) transaction = AuthorizeNetTransactionFactory.create( state=Transaction.States.Initial, data={ 'id': '1235', 'status': None, 'authorizenet_id': None, }, payment_method=payment_method, amount=25.00, invoice=invoice) assert transaction.state == transaction.States.Initial payment_processor = get_instance(transaction.payment_processor) status = payment_processor.process_transaction(transaction) trx = Transaction.objects.all().first() assert Transaction.objects.all().count() > 0 assert trx.data.get('status') != "Null response." assert status == True assert transaction.state in [ transaction.States.Pending, transaction.States.Settled ]
def test_process_transaction_with_credit_card_is_success(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.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 in [ transaction.States.Pending, transaction.States.Settled ] assert transaction.data.get('status') != 0 # 0 implies the API sandbox is in test mode assert transaction.data.get('authorizenet_id') != 0
def _create_default_payment_method(self): # 0 for easy asserting. customer = CustomerFactory(sales_tax_percent=0, currency='USD', first_name="Captain", last_name="Hook") # Create customer payment method payment_method = PaymentMethodFactory.create( payment_processor=triggered_processor, customer=customer, canceled=False, verified=True, data={ # Wait until payment day X to begin retry attempts 'attempt_retries_after': 2, # Stop attempts on day X 'stop_retry_attempts': 5, }) return customer, payment_method
def test_post_proforma_without_proforma_entries(self): customer = CustomerFactory.create() provider = ProviderFactory.create() SubscriptionFactory.create() url = reverse('proforma-list') data = { 'provider': 'http://testserver/providers/%s/' % provider.pk, 'customer': 'http://testserver/customers/%s/' % customer.pk, '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": response.data["id"], "series": "ProformaSeries", "number": None, "provider": "http://testserver/providers/%s/" % provider.pk, "customer": "http://testserver/customers/%s/" % customer.pk, "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_issue_invoice_with_custom_issue_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'} response = self.client.put(url, data=json.dumps(data), content_type='application/json') self.assertEqual(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' } self.assertEqual(response.status_code, status.HTTP_200_OK) self.assertTrue(all(item in response.data.items() for item in mandatory_content.iteritems())) self.assertNotEqual(response.data.get('archived_provider', {}), {}) self.assertNotEqual(response.data.get('archived_customer', {}), {})
def test_get_transaction_details(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) expected_t1 = OrderedDict([ ('url', reverse('transaction-detail', kwargs={ 'customer_pk': customer.pk, 'transaction_uuid': transaction_1.uuid })), ('payment_method', reverse('payment-method-detail', kwargs={ 'customer_pk': customer.pk, 'payment_method_id': payment_method.id })), ('payment', reverse('payment-detail', kwargs={ 'customer_pk': customer.pk, 'payment_pk': transaction_1.payment.pk })), ('is_usable', True), ('pay_url', reverse('pay-transaction', kwargs={'transaction_uuid': transaction_1.uuid})), ('valid_until', None), ]) url = reverse('transaction-detail', kwargs={ 'customer_pk': customer.pk, 'transaction_uuid': transaction_1.uuid }) response = self.client.get(url, format='json') self.assertEqual(response.data, dict(expected_t1))
def test_filter_payment_method(self): customer = CustomerFactory.create() payment = PaymentFactory.create(customer=customer) payment_method = PaymentMethodFactory.create( payment_processor='someprocessor', customer=customer) transaction1 = TransactionFactory.create(payment_method=payment_method, payment=payment) transaction_data_1 = self._transaction_data(customer, payment, payment_method, transaction1) transaction2 = TransactionFactory.create(payment_method=payment_method, payment=payment) transaction_data_2 = self._transaction_data(customer, payment, payment_method, 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 customer(self): c = CustomerFactory.create(consolidated_billing=False, sales_tax_percent=Decimal('0.00')) c.meta = { "cardNumber": "4111111111111111", "expirationDate": "2020-12", "cardCode": "123", } c.save() auth = 'AuthorizeNetTriggered' null = 'manual' pm = PaymentMethodFactory.create(customer=c, verified=True, canceled=False, display_info="pytest", payment_processor=auth) pm.save() return c
def test_issue_proforma_with_default_dates(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'} 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', {}) != {} assert Invoice.objects.count() == 0
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)