def test_approved_payment(self): class TestForm(TrustCommerceForm): def get_result(self): return { 'status': 'approved', 'auth_code': '123456', 'transaction_id': '023-0089505847', 'avs_code': '0', 'is_success': True, } def get_token(self): return "1234" request = RequestFactory().get("/payment/?token=1234") request.session = {} response = payment( request, template_name="trustcommerce/payment.html", customer_id="Customer", password="******", form_class=TestForm, ) self.assertEqual(response.status_code, 200) self.assertFalse("token" in response.context_data["form"].initial) self.assertEqual(response.context_data["result"]["status"], "approved") self.assertEqual(response.context_data["result"]["avs_code"], "0") self.assertEqual(response.context_data["result"]["is_success"], True) self.assertEqual(response.context_data["result"]["transaction_id"], "023-0089505847") self.assertEqual(response.context_data["result"]["auth_code"], "123456")
def test_decline_payment(self): class TestForm(TrustCommerceForm): def complete_transaction(self, token): return "transid=023-0090523841\nstatus=decline\ndeclinetype=decline\navs=0" def get_token(self): return "1234" def verify_request(self): return True request = RequestFactory().get("/payment/?token=1234") request.session = {} response = payment( request, template_name="trustcommerce/payment.html", customer_id="Customer", password="******", form_class=TestForm, ) self.assertEqual(response.status_code, 200) # Check that new token is generated after decline initial = response.context_data["form"].initial self.assertEqual(initial["token"], "1234") # Check result status in template self.assertEqual(response.context_data["result"]["status"], "decline") self.assertEqual(response.context_data["result"]["avs_code"], "0") self.assertEqual(response.context_data["result"]["is_success"], False) self.assertEqual(response.context_data["result"]["transaction_id"], "023-0090523841") self.assertEqual(response.context_data["result"]["auth_code"], None) self.assertEqual(response.context_data["result"]["error_message"], "The credit card was declined")
def test_hash_fields(self): """ Custom fields that aren't user-configurable should be added to the form as hidden inputs. """ class TestForm(TrustCommerceForm): def get_token(self): return "1234" request = RequestFactory().get("/payment/") request.session = {} response = payment( request, template_name="trustcommerce/payment.html", customer_id="Customer", password="******", hash_fields=dict( verify="y", cycle="1m", start="3d", authnow="y", ), form_class=TestForm, ) self.assertEqual(response.status_code, 200) form = response.context_data["form"] self.assertEqual(form.fields["verify"].initial, "y") self.assertEqual(form.fields["cycle"].initial, "1m") self.assertEqual(form.fields["start"].initial, "3d") self.assertEqual(form.fields["authnow"].initial, "y")
def test_bad_data(self): class TestForm(TrustCommerceForm): def complete_transaction(self, token): return "status=baddata\nerror=badformat\noffenders=exp\n" def get_token(self): return "1234" def verify_request(self): return True request = RequestFactory().get("/payment/?token=1234") request.session = {} response = payment( request, template_name="trustcommerce/payment.html", customer_id="Customer", password="******", form_class=TestForm, ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context_data["form"].errors, {'exp': [u'Enter a valid value.']}) self.assertEqual(response.context_data["result"]["error_message"], "A field was improperly formatted, such as non-digit characters in a number field")
def test_token_service_error(self): class TestForm(TrustCommerceForm): def get_result(self): return None def get_token(self): raise ServiceError() request = RequestFactory().get("/payment/") request.session = {} response = payment( request, template_name="trustcommerce/payment.html", customer_id="Customer", password="******", form_class=TestForm, ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context_data["form"].initial, { 'action': 'sale', 'returnurl': 'http://testserver/payment/', }) self.assertEqual(response.context_data["service_error"], True)
def test_error_payment(self): class TestForm(TrustCommerceForm): def complete_transaction(self, token): return "transid=023-0095189057\nstatus=error\nerrortype=linkfailure" def get_token(self): return "1234" def verify_request(self): return True request = RequestFactory().get("/payment/?token=1234") request.session = {} response = payment( request, template_name="trustcommerce/payment.html", customer_id="Customer", password="******", form_class=TestForm, ) self.assertEqual(response.status_code, 200) # Check that new token is generated after error initial = response.context_data["form"].initial self.assertEqual(initial["token"], "1234") # Check error status in the template self.assertEqual(response.context_data["service_error"], True)
def test_double_process(self): """ Don't process a token more than once. This process depends on storing the previous token in the session. It's a bit simplistic, but works for now. """ class TestForm(TrustCommerceForm): def complete_transaction(self, token): return "status=baddata\nerror=badformat\noffenders=exp\n" def get_token(self): return "1234" def verify_request(self): return True request = RequestFactory().get("/payment/?token=1234") request.session = {} response = payment( request, template_name="trustcommerce/payment.html", customer_id="Customer", password="******", form_class=TestForm, ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context_data["form"].errors, {'exp': [u'Enter a valid value.']}) self.assertEqual(request.session[TOKEN_KEY], "1234") response = payment( request, template_name="trustcommerce/payment.html", customer_id="Customer", password="******", form_class=TestForm, ) self.assertEqual(response.status_code, 200) self.assertEqual(response.context_data["form"].errors, {})