1
0
mirror of https://gitlab.com/animath/si/plateforme.git synced 2025-06-21 01:58:23 +02:00

Add tests for Hello Asso payments using a fake endpoint

Signed-off-by: Emmy D'Anello <emmy.danello@animath.fr>
This commit is contained in:
Emmy D'Anello
2024-02-25 17:24:52 +01:00
parent 92408b359b
commit 83300ad4b7
5 changed files with 377 additions and 2 deletions

View File

@ -6,7 +6,7 @@ from django.contrib.contenttypes.models import ContentType
from django.contrib.sites.models import Site
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.management import call_command
from django.test import TestCase
from django.test import LiveServerTestCase, override_settings, TestCase
from django.urls import reverse
from registration.models import CoachRegistration, Payment, StudentRegistration
@ -875,6 +875,208 @@ class TestPayment(TestCase):
self.assertFalse(payment.valid)
@override_settings(HELLOASSO_TEST_ENDPOINT=True, ROOT_URLCONF="tfjm.helloasso.test_urls")
class TestHelloAssoPayment(LiveServerTestCase):
"""
Tests that are relative to a HelloAsso
"""
def setUp(self):
self.superuser = User.objects.create_superuser(
username="admin",
email="admin@example.com",
password="admin",
)
self.tournament = Tournament.objects.create(
name="France",
place="Here",
price=21,
)
self.team = Team.objects.create(
name="Super team",
trigram="AAA",
access_code="azerty",
)
self.user = User.objects.create(
first_name="Toto",
last_name="Toto",
email="toto@example.com",
password="toto",
)
StudentRegistration.objects.create(
user=self.user,
team=self.team,
student_class=12,
address="1 Rue de Rivoli",
zip_code=75001,
city="Paris",
school="Earth",
give_contact_to_animath=True,
email_confirmed=True,
)
self.coach = User.objects.create(
first_name="Coach",
last_name="Coach",
email="coach@example.com",
password="coach",
)
CoachRegistration.objects.create(
user=self.coach,
team=self.team,
address="1 Rue de Rivoli",
zip_code=75001,
city="Paris",
)
self.team.participation.tournament = self.tournament
self.team.participation.valid = True
self.team.participation.save()
self.client.force_login(self.user)
Site.objects.update(domain=self.live_server_url.replace("http://", ""))
def test_create_checkout_intent(self):
with self.settings(HELLOASSO_TEST_ENDPOINT_URL=self.live_server_url):
payment = Payment.objects.get(registrations=self.user.registration, final=False)
checkout_intent = payment.create_checkout_intent()
self.assertIsNotNone(checkout_intent)
self.assertEqual(checkout_intent['metadata'], {
'payment_id': payment.pk,
'users': [
{
'user_id': self.user.pk,
'first_name': self.user.first_name,
'last_name': self.user.last_name,
'email': self.user.email,
}
],
'final': False,
'tournament_id': self.tournament.pk,
})
self.assertNotIn('order', checkout_intent)
checkout_intent_fetched = payment.get_checkout_intent()
self.assertEqual(checkout_intent, checkout_intent_fetched)
# Don't create a new checkout intent if one already exists
checkout_intent_new = payment.create_checkout_intent()
self.assertEqual(checkout_intent, checkout_intent_new)
payment.refresh_from_db()
self.assertEqual(payment.checkout_intent_id, checkout_intent['id'])
self.assertFalse(payment.valid)
def test_helloasso_payment_success(self):
"""
Simulates the redirection to Hello Asso and the return for a successful payment.
"""
with self.settings(HELLOASSO_TEST_ENDPOINT_URL=self.live_server_url):
payment = Payment.objects.get(registrations=self.user.registration, final=False)
self.assertIsNone(payment.checkout_intent_id)
self.assertFalse(payment.valid)
response = self.client.get(reverse('registration:update_payment', args=(payment.pk,)))
self.assertEqual(response.status_code, 200)
response = self.client.get(reverse('registration:payment_hello_asso', args=(payment.pk,)),
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.redirect_chain[-1],
(reverse('participation:team_detail', args=(self.team.pk,)), 302))
self.assertIn("type=return", response.redirect_chain[1][0])
self.assertIn("code=succeeded", response.redirect_chain[1][0])
payment.refresh_from_db()
self.assertIsNotNone(payment.checkout_intent_id)
self.assertTrue(payment.valid)
checkout_intent = payment.get_checkout_intent()
self.assertIn('order', checkout_intent)
def test_helloasso_payment_refused(self):
"""
Simulates the redirection to Hello Asso and the return for a refused payment.
"""
with self.settings(HELLOASSO_TEST_ENDPOINT_URL=self.live_server_url):
payment = Payment.objects.get(registrations=self.user.registration, final=False)
checkout_intent = payment.create_checkout_intent()
self.assertFalse(payment.valid)
response = self.client.get(reverse('registration:update_payment', args=(payment.pk,)))
self.assertEqual(response.status_code, 200)
response = self.client.get(checkout_intent['redirectUrl'] + "?refused", follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.redirect_chain[-1],
(reverse('registration:update_payment', args=(payment.pk,)), 302))
self.assertIn("type=return", response.redirect_chain[0][0])
self.assertIn("code=refused", response.redirect_chain[0][0])
payment.refresh_from_db()
self.assertFalse(payment.valid)
checkout_intent = payment.get_checkout_intent()
self.assertNotIn('order', checkout_intent)
def test_helloasso_payment_error(self):
"""
Simulates the redirection to Hello Asso and the return for an errored payment.
"""
with self.settings(HELLOASSO_TEST_ENDPOINT_URL=self.live_server_url):
payment = Payment.objects.get(registrations=self.user.registration, final=False)
checkout_intent = payment.create_checkout_intent()
self.assertFalse(payment.valid)
response = self.client.get(reverse('registration:update_payment', args=(payment.pk,)))
self.assertEqual(response.status_code, 200)
response = self.client.get(checkout_intent['redirectUrl'] + "?error", follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.redirect_chain[-1],
(reverse('registration:update_payment', args=(payment.pk,)), 302))
self.assertIn("type=error", response.redirect_chain[0][0])
self.assertIn("error=", response.redirect_chain[0][0])
payment.refresh_from_db()
self.assertFalse(payment.valid)
checkout_intent = payment.get_checkout_intent()
self.assertNotIn('order', checkout_intent)
def test_anonymous_payment(self):
"""
Test to make a successful payment from an anonymous user, authenticated by token.
"""
self.client.logout()
with self.settings(HELLOASSO_TEST_ENDPOINT_URL=self.live_server_url):
payment = Payment.objects.get(registrations=self.user.registration, final=False)
self.assertIsNone(payment.checkout_intent_id)
self.assertFalse(payment.valid)
response = self.client.get(reverse('registration:payment_hello_asso', args=(payment.pk,)),
follow=True)
self.assertRedirects(response,
f"{reverse('login')}?next="
f"{reverse('registration:payment_hello_asso', args=(payment.pk,))}")
response = self.client.get(
reverse('registration:payment_hello_asso', args=(payment.pk,)) + "?token=" + payment.token,
follow=True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.redirect_chain[-1], (reverse('index'), 302))
self.assertIn("type=return", response.redirect_chain[1][0])
self.assertIn("code=succeeded", response.redirect_chain[1][0])
payment.refresh_from_db()
self.assertIsNotNone(payment.checkout_intent_id)
self.assertTrue(payment.valid)
checkout_intent = payment.get_checkout_intent()
self.assertIn('order', checkout_intent)
class TestAdmin(TestCase):
def setUp(self) -> None:
self.user = User.objects.create_superuser(