def test_update(self): payment = PaymentFactory(value=10.50) self.assertEqual(Payment.objects.first().value, 10.50) payment.value = 15 payment.save() self.assertEqual(Payment.objects.first().value, 15.00)
def test_payment_list(self): payment = PaymentFactory() payment2 = PaymentFactory() url = reverse('payment') response = self.client.get(url, format='json') self.assertEqual(response.status_code, 200) self.assertEqual(len(response.data), 2) self.assertListEqual([ { 'id': payment.id, 'from_account': payment.account.pk, 'to_account': payment.related_account.pk, 'amount': payment.amount, 'direction': payment.direction, }, { 'id': payment2.id, 'from_account': payment2.account.pk, 'to_account': payment2.related_account.pk, 'amount': payment2.amount, 'direction': payment2.direction, }, ], response.data)
def test_amount_due(self): contract = ContractFactory(amount=150, interest_rate=0) self.assertIsInstance(contract, Contract) PaymentFactory.create_batch(2, contract=contract, value=10) self.assertEqual(contract.amount_due, 130)
def test_list_success(self): contract = ContractFactory() PaymentFactory.create_batch(3, contract=contract) self.assertEqual(Payment.objects.count(), 3) self.set_user(contract.client) path = self.get_path() response = self.client.get(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(len(response.data.get("results")), 3, msg=response.data)
def test_send_pse_order_approved(self, send_mail): """ Test task send the email successfully when: - the order is approved - the payment method is pse """ payment = PaymentFactory(payment_type='PSE') order = OrderFactory(status=Order.ORDER_APPROVED_STATUS, payment=payment) email = order.calendar.activity.organizer.user.email assistants = AssistantFactory.create_batch(1, order=order) send_mail.return_value = [{ 'email': email, 'status': 'sent', 'reject_reason': None }] task = SendNewEnrollmentEmailTask() task_id = task.delay(order.id) context = { **self.get_context_data(order, assistants), 'status': 'Aprobada', 'payment_type': 'PSE', 'card_type': dict(Payment.CARD_TYPE)[payment.card_type], 'coupon_amount': None, } self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=email, status='sent', data=context, template_name='payments/email/new_enrollment.html').exists())
def get_context(self): view = Mock(student=self.another_student) request = mock.MagicMock() request.data = {} return { 'view': view, 'status': Order.ORDER_APPROVED_STATUS, 'request': request, 'payment': PaymentFactory(), }
def test_delete_success(self): payment = PaymentFactory(value=15) self.assertEqual(Payment.objects.count(), 1) self.set_user(payment.contract.client) path = self.get_path(id_detail=payment.id) response = self.client.delete(path, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT, msg=response.data) self.assertEqual(Payment.objects.count(), 0)
class PaymentSerializerTest(APITestCase): """ Class for testing the PaymentSerializer serializer """ def setUp(self): self.payment = PaymentFactory() def test_read(self): """ Test the serializer's data returned """ content = { 'date': self.payment.date.isoformat()[:-6] + 'Z', 'payment_type': self.payment.get_payment_type_display(), 'card_type': self.payment.get_card_type_display(), 'last_four_digits': str(self.payment.last_four_digits), } serializer = PaymentSerializer(self.payment) self.assertTrue( all(item in serializer.data.items() for item in content.items()))
def test_send_mail_failed(self, send_mail): """ Test the task when the send_mail fails """ payment = PaymentFactory(payment_type='CC', card_type='visa') order = OrderFactory(status=Order.ORDER_APPROVED_STATUS, payment=payment) send_mail.side_effect = Exception('Connection failed') task = SendPaymentEmailTask() task_id = task.delay(order.id) self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=order.student.user.email, status='error', reject_reason='Connection failed').exists())
def test_put_fail(self): payment = PaymentFactory(value=10.5) self.assertEqual(Payment.objects.count(), 1) self.set_user(payment.contract.client) self.assertEqual(Payment.objects.first().value, 10.5) data = {"value": 15} path = self.get_path(id_detail=payment.id) response = self.client.put(path, data=data, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST, msg=response.data) self.assertEqual(Payment.objects.count(), 1) self.assertEqual(Payment.objects.first().contract, payment.contract) self.assertEqual(Payment.objects.first().value, 10.5)
def test_send_pse_order_with_coupon_declined(self, send_mail): """ Test task send the email successfully when: - the order is declined - the payment method is pse - the order has a coupon """ student = StudentFactory() redeem = RedeemFactory(student=student, used=True) payment = PaymentFactory(payment_type='PSE') order = OrderFactory(status=Order.ORDER_DECLINED_STATUS, payment=payment, student=student, coupon=redeem.coupon) email = order.student.user.email assistants = AssistantFactory.create_batch(1, order=order) transaction_error = 'ERROR' send_mail.return_value = [{ 'email': email, 'status': 'sent', 'reject_reason': None }] task = SendPaymentEmailTask() task_id = task.delay(order.id, transaction_error=transaction_error) context = { **self.get_context_data(order, assistants), 'status': 'Declinada', 'payment_type': 'PSE', 'card_type': dict(Payment.CARD_TYPE)[payment.card_type], 'coupon_amount': redeem.coupon.coupon_type.amount, 'error': transaction_error, } self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=email, status='sent', data=context, template_name='payments/email/payment_declined.html').exists())
def test_send_creditcard_with_coupon_order_approved(self, send_mail): """ Test task send the email successfully when: - the order is approved - the payment method is credit card - the order has a coupon """ student = StudentFactory() redeem = RedeemFactory(student=student, used=True) payment = PaymentFactory(payment_type='CC', card_type='visa') order = OrderFactory(status=Order.ORDER_APPROVED_STATUS, payment=payment, student=student, coupon=redeem.coupon) email = order.student.user.email assistants = AssistantFactory.create_batch(1, order=order) send_mail.return_value = [{ 'email': email, 'status': 'sent', 'reject_reason': None }] task = SendPaymentEmailTask() task_id = task.delay(order.id) context = { **self.get_context_data(order, assistants), 'status': 'Aprobada', 'payment_type': 'Crédito', 'card_type': 'VISA', 'coupon_amount': redeem.coupon.coupon_type.amount, } self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=email, status='sent', data=context, template_name='payments/email/payment_approved.html').exists())
def test_send_creditcard_order_without_coupon_declined(self, send_mail): """ Test task send the email successfully when: - the order is declined - the payment method is credit card - the order doesn't have a coupon """ payment = PaymentFactory(payment_type='CC', card_type='visa') order = OrderFactory(status=Order.ORDER_DECLINED_STATUS, payment=payment) email = order.student.user.email assistants = AssistantFactory.create_batch(1, order=order) transaction_error = 'ERROR' send_mail.return_value = [{ 'email': email, 'status': 'sent', 'reject_reason': None }] task = SendPaymentEmailTask() task_id = task.delay(order.id, transaction_error=transaction_error) context = { **self.get_context_data(order, assistants), 'status': 'Declinada', 'payment_type': 'Crédito', 'card_type': 'VISA', 'coupon_amount': None, 'error': transaction_error, } self.assertTrue(EmailTaskRecord.objects.filter( task_id=task_id, to=email, status='sent', data=context, template_name='payments/email/payment_declined.html').exists())
def test_put_success(self): payment = PaymentFactory(value=10.5) self.assertEqual(Payment.objects.count(), 1) self.set_user(payment.contract.client) self.assertEqual(Payment.objects.first().value, 10.5) data = { "contract_id": payment.contract.id, "value": 15, "date": payment.date } path = self.get_path(id_detail=payment.id) response = self.client.put(path, data=data, HTTP_AUTHORIZATION=self.auth) self.assertEqual(response.status_code, status.HTTP_200_OK, msg=response.data) self.assertEqual(Payment.objects.count(), 1) self.assertEqual(Payment.objects.first().contract, payment.contract) self.assertEqual(Payment.objects.first().value, 15)
def setUp(self): self.payment = PaymentFactory()
def test_create(self): PaymentFactory() self.assertEqual(Payment.objects.count(), 1)
def test_delete(self): payment = PaymentFactory() self.assertEqual(Payment.objects.count(), 1) payment.delete() self.assertEqual(Payment.objects.count(), 0)
def setUp(self): self.payu_callback_url = reverse('payments:notification') self.payment = PaymentFactory() self.order = OrderFactory(payment=self.payment, status=Order.ORDER_PENDING_STATUS)