def test_create_onceoff_integrity_hash(self, mock_rave_settings): """Ensure the correct hash is returned for a onceoff payment""" mock_rave_settings.PUBLIC_KEY = "test" mock_rave_settings.SECRET_KEY = "test" payment_type = PaymentTypeModelFactory( amount=10, currency="USD", custom_logo="http://example.com/eg.png", custom_title="test", pay_button_text="test", payment_options="card", ) user = UserFactory(first_name="test", last_name="test", email="*****@*****.**") txref = "12345" redirect_url = "test" expected_response = ( "8d5aa47bc2b280aeb0815ef7ace05c87bd25d69276cbc12ac82c5cfe7a9d5d52") actual_response = create_integrity_hash(payment_type=payment_type, user=user, txref=txref, redirect_url=redirect_url) self.assertEqual(expected_response, actual_response)
def test_create_plan_integrity_hash(self, mock_rave_settings): """Ensure the correct hash is returned for a plan""" mock_rave_settings.PUBLIC_KEY = "test" mock_rave_settings.SECRET_KEY = "test" payment_type = PaymentTypeModelFactory( amount=10, currency="USD", custom_logo="http://example.com/eg.png", custom_title="test", pay_button_text="test", payment_options="card", payment_plan=1, ) user = UserFactory(first_name="test", last_name="test", email="*****@*****.**") txref = "12345" redirect_url = "test" expected_response = ( "1e2b7754ee03721e2bca37680cc5cb973b41addf446a8b791a8bc97e6eaa652c") actual_response = create_integrity_hash(payment_type=payment_type, user=user, txref=txref, redirect_url=redirect_url) self.assertEqual(expected_response, actual_response)
def test_pay_button_params( self, mock_reverse, mock_rave_settings, mock_timezone, mock_create_integrity_hash, ): """Ensure a json string is returned containing the correct txref, pub_key and integrity_hash """ mock_reverse.return_value = "test" mock_rave_settings.PUBLIC_KEY = "test" mock_timezone.now.return_value.timestamp.return_value = "test" mock_create_integrity_hash.return_value = "test" payment_type = PaymentTypeModelFactory() user = UserFactory() expected_response = ( f'{{"txref": "{payment_type.id}__test__{user.id}"' ', "redirect_url": "test", "pub_key": "test", "integrity_hash": "test"}' ) actual_response = pay_button_params(user=user, payment_type=payment_type) mock_create_integrity_hash.assert_called() mock_reverse.assert_called() self.assertEqual(expected_response, actual_response)
def test_perform_create(self): """Ensure the user and payment_type are gotten from the reference and saved to the Transaction instance""" user = UserFactory() payment_type = PaymentTypeModelFactory() factory = APIRequestFactory() data = { "txRef": f"{payment_type.id}__test__{user.id}", "flwRef": "test", "orderRef": "test", "amount": 10, "charged_amount": 10, "status": "test", } request = factory.post("fake-url", data) request.user = user view = TransactionCreateView() view.request = request serializer = TransactionSerializer(data=data) serializer.is_valid() view.perform_create(serializer=serializer) self.assertEqual(TransactionModel.objects.count(), 1) transaction = TransactionModel.objects.first() self.assertEqual(transaction.user.id, user.id) self.assertEqual(transaction.payment_type.id, payment_type.id)
def test_get_context_data(self): """Ensure a transaction is added to the context only if a valid user and reference is provided""" factory = RequestFactory() user = UserFactory() transaction = TransactionModelFactory(user=user) request = factory.get("test") request.user = user view = TransactionDetailView() view.request = request view.kwargs = {"reference": transaction.reference} context_data = view.get_context_data() self.assertEqual(transaction, context_data["transaction"]) view.kwargs = {"reference": "invalid"} context_data = view.get_context_data() self.assertIsNone(context_data["transaction"])
def handle(self, *args, **kwargs): get_user_model().objects.all().delete() UserFactory(username="******", is_staff=True, is_superuser=True) PaymentTypeModel.objects.all().delete() PaymentTypeModelFactory( description="Once off Purchase", custom_title="Purchase this item", currency="USD", pay_button_text="Buy Now", ) PaymentTypeModelFactory( description="Subscription Plan", custom_title="Sign Up to this plan", currency="USD", pay_button_text="Sign Up", payment_plan=123, ) print("\nData imported")
def test_validate_reference(self): """Ensure the serializer raises an exception for an invalid payment_type_id or user_id """ payment_type = PaymentTypeModelFactory() user = UserFactory() expected_response = f"{payment_type.id}__test__{user.id}" actual_response = TransactionSerializer.validate_reference( self=None, value=expected_response) self.assertEqual(expected_response, actual_response) with self.assertRaises(ValidationError) as e: TransactionSerializer.validate_reference( self=None, value=f"123__test__{user.id}") self.assertEqual(e.exception.detail[0], "Payment type does not exist") with self.assertRaises(ValidationError) as e: TransactionSerializer.validate_reference( self=None, value=f"{payment_type.id}__test__123") self.assertEqual(e.exception.detail[0], "User does not exist")