def test_init_with_user_custom_names(self): user = User.objects.create( username='******', email='*****@*****.**', first_name='First', last_name='Last', ) form = PayFastForm(initial={ 'amount': 100, 'item_name': 'Example item', }, user=user) self.assertEqual( { 'amount': 100, 'email_address': '*****@*****.**', 'item_name': 'Example item', 'm_payment_id': '1', 'merchant_id': '10000100', 'merchant_key': '46f0cd694581a', 'name_first': 'example_user First Name', 'name_last': 'example_user Last Name', 'notify_url': notify_url(), }, form.initial) self.assertEqual(user, form.order.user)
def test_notify(self): notify_data = self._create_order() # the server sends a notification response = self.client.post(notify_url(), notify_data) self.assertEqual(response.status_code, 200) self.assertTrue(self.signal_handler.called) order = _order() self.assertEqual(order.request_ip, u'127.0.0.1') self.assertEqual(order.debug_info, u'') self.assertEqual(order.trusted, True)
def test_notify(self): notify_data = self._create_order() order = _order() # the server sends a notification response = self.client.post(notify_url(), notify_data) self.assertEqual(response.status_code, 200, response.content) self.assertEqual(self.notify_handler_orders, [order]) order = _order() self.assertEqual(order.request_ip, '127.0.0.1') self.assertEqual(order.debug_info, '') self.assertEqual(order.trusted, True)
def test_init(self): form = PayFastForm(initial={ 'amount': 100, 'item_name': 'Example item', }) self.assertEqual( { 'amount': 100, 'item_name': 'Example item', 'm_payment_id': '1', 'merchant_id': '10000100', 'merchant_key': '46f0cd694581a', 'notify_url': notify_url(), }, form.initial) self.assertIsNone(form.order.user)
def test_untrusted_ip(self): """ The notify handler rejects notification attempts from untrusted IP address. """ notify_data = self._create_order() # the server sends a notification response = self.client.post(notify_url(), notify_data, REMOTE_ADDR='127.0.0.2') self.assertEqual(response.status_code, 404) self.assertFalse(self.signal_handler.called) order = _order() self.assertEqual(order.request_ip, u'127.0.0.2') self.assertEqual(order.debug_info, u'__all__: untrusted ip: 127.0.0.2') self.assertEqual(order.trusted, False)
def test_invalid_request(self): form = PayFastForm(initial={'amount': 100, 'item_name': 'foo'}) response = self.client.post(notify_url(), {'m_payment_id': form.order.pk}) self.assertEqual(response.status_code, 404) self.assertFalse(self.signal_handler.called) order = _order() self.assertEqual(order.request_ip, u'127.0.0.1') self.assertEqual( set(order.debug_info.split(u'|')), { u'amount_gross: Amount is not the same: {} != None'.format( # Django 1.8 returns more precise DecimalField values. u'100' if django.VERSION < (1, 8) else u'100.00'), u'item_name: This field is required.', u'merchant_id: This field is required.', }) self.assertEqual(order.trusted, False)
def test_invalid_request(self): form = PayFastForm(initial={'amount': 100, 'item_name': 'foo'}) notify_data = {'m_payment_id': form.order.m_payment_id} notify_data['signature'] = NotifyForm._calculate_itn_signature( notify_data) response = self.client.post(notify_url(), notify_data) expected_amount = ('100' if django.VERSION < (1, 8) else '100.00' if django.VERSION < (2, 0) else '100') self._assertBadRequest( response, { 'amount_gross': [{ 'code': '', 'message': ('Amount is not the same: {} != None'.format( expected_amount)) }], 'item_name': [{ 'code': 'required', 'message': 'This field is required.' }], 'merchant_id': [{ 'code': 'required', 'message': 'This field is required.' }], }) self.assertEqual(self.notify_handler_orders, []) order = _order() self.assertEqual(order.request_ip, '127.0.0.1') self.assertEqual( set(order.debug_info.split('|')), { 'amount_gross: Amount is not the same: {} != None'.format( '100' if django.VERSION < (1, 8) else # Django 1.8+ returns more precise DecimalField values '100.00' if django.VERSION < (2, 0) else # Django 2.0+ returns less precise DecimalField values again. '100'), 'item_name: This field is required.', 'merchant_id: This field is required.', }) self.assertEqual(order.trusted, False)
def test_untrusted_ip(self): """ The notify handler rejects notification attempts from untrusted IP address. """ notify_data = self._create_order() # the server sends a notification response = self.client.post(notify_url(), notify_data, REMOTE_ADDR='127.0.0.2') self._assertBadRequest(response, { '__all__': [{ 'code': '', 'message': 'untrusted ip: 127.0.0.2' }], }) self.assertEqual(self.notify_handler_orders, []) order = _order() self.assertEqual(order.request_ip, '127.0.0.2') self.assertEqual(order.debug_info, '__all__: untrusted ip: 127.0.0.2') self.assertEqual(order.trusted, False)
def test_non_existing_order(self): response = self.client.post(notify_url(), {}) self.assertEqual(response.status_code, 404) self.assertFalse(self.signal_handler.called) self.assertQuerysetEqual(PayFastOrder.objects.all(), [])