Use pytest for tests (#681)

* use pytest for tests

Eh ouais, il y a que la config qui change. Pytest est implémentable par étapes. Et ça c'est beau.

* rework tests with pytest

* remove unittest custom TestRunner

* Edit doc and CI
This commit is contained in:
thomas girod
2024-06-26 19:10:24 +02:00
committed by GitHub
parent a5cbac1f97
commit d97602e60b
19 changed files with 1268 additions and 1515 deletions

View File

@ -40,14 +40,14 @@ from eboutic.models import Basket
class EbouticTest(TestCase):
@classmethod
def setUpTestData(cls):
cls.barbar = Product.objects.filter(code="BARB").first()
cls.refill = Product.objects.filter(code="15REFILL").first()
cls.cotis = Product.objects.filter(code="1SCOTIZ").first()
cls.eboutic = Counter.objects.filter(name="Eboutic").first()
cls.skia = User.objects.filter(username="skia").first()
cls.subscriber = User.objects.filter(username="subscriber").first()
cls.old_subscriber = User.objects.filter(username="old_subscriber").first()
cls.public = User.objects.filter(username="public").first()
cls.barbar = Product.objects.get(code="BARB")
cls.refill = Product.objects.get(code="15REFILL")
cls.cotis = Product.objects.get(code="1SCOTIZ")
cls.eboutic = Counter.objects.get(name="Eboutic")
cls.skia = User.objects.get(username="skia")
cls.subscriber = User.objects.get(username="subscriber")
cls.old_subscriber = User.objects.get(username="old_subscriber")
cls.public = User.objects.get(username="public")
def get_busy_basket(self, user) -> Basket:
"""
@ -82,7 +82,7 @@ class EbouticTest(TestCase):
return url
def test_buy_with_sith_account(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
self.subscriber.customer.amount = 100 # give money before test
self.subscriber.customer.save()
basket = self.get_busy_basket(self.subscriber)
@ -90,14 +90,12 @@ class EbouticTest(TestCase):
response = self.client.post(reverse("eboutic:pay_with_sith"))
self.assertRedirects(response, "/eboutic/pay/success/")
new_balance = Customer.objects.get(user=self.subscriber).amount
self.assertEqual(float(new_balance), 100 - amount)
self.assertEqual(
'basket_items=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/eboutic',
self.client.cookies["basket_items"].OutputString(),
)
assert float(new_balance) == 100 - amount
expected = 'basket_items=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/eboutic'
assert expected == self.client.cookies["basket_items"].OutputString()
def test_buy_with_sith_account_no_money(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
basket = self.get_busy_basket(self.subscriber)
initial = basket.get_total() - 1 # just not enough to complete the sale
self.subscriber.customer.amount = initial
@ -105,20 +103,19 @@ class EbouticTest(TestCase):
response = self.client.post(reverse("eboutic:pay_with_sith"))
self.assertRedirects(response, "/eboutic/pay/failure/")
new_balance = Customer.objects.get(user=self.subscriber).amount
self.assertEqual(float(new_balance), initial)
self.assertEqual(
'basket_items=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/eboutic',
self.client.cookies["basket_items"].OutputString(),
) # this cookie should be removed after payment
assert float(new_balance) == initial
# this cookie should be removed after payment
expected = 'basket_items=""; expires=Thu, 01 Jan 1970 00:00:00 GMT; Max-Age=0; Path=/eboutic'
assert expected == self.client.cookies["basket_items"].OutputString()
def test_submit_basket(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
self.client.cookies["basket_items"] = """[
{"id": 2, "name": "Cotis 2 semestres", "quantity": 1, "unit_price": 28},
{"id": 4, "name": "Barbar", "quantity": 3, "unit_price": 1.7}
]"""
response = self.client.get(reverse("eboutic:command"))
self.assertEqual(response.status_code, 200)
assert response.status_code == 200
self.assertInHTML(
"<tr><td>Cotis 2 semestres</td><td>1</td><td>28.00 €</td></tr>",
response.content.decode(),
@ -127,42 +124,37 @@ class EbouticTest(TestCase):
"<tr><td>Barbar</td><td>3</td><td>1.70 €</td></tr>",
response.content.decode(),
)
self.assertIn("basket_id", self.client.session)
assert "basket_id" in self.client.session
basket = Basket.objects.get(id=self.client.session["basket_id"])
self.assertEqual(basket.items.count(), 2)
assert basket.items.count() == 2
barbar = basket.items.filter(product_name="Barbar").first()
self.assertIsNotNone(barbar)
self.assertEqual(barbar.quantity, 3)
assert barbar is not None
assert barbar.quantity == 3
cotis = basket.items.filter(product_name="Cotis 2 semestres").first()
self.assertIsNotNone(cotis)
self.assertEqual(cotis.quantity, 1)
self.assertEqual(basket.get_total(), 3 * 1.7 + 28)
assert cotis is not None
assert cotis.quantity == 1
assert basket.get_total() == 3 * 1.7 + 28
def test_submit_empty_basket(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
self.client.cookies["basket_items"] = "[]"
response = self.client.get(reverse("eboutic:command"))
self.assertRedirects(response, "/eboutic/")
def test_submit_invalid_basket(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
max_id = Product.objects.aggregate(res=Max("id"))["res"]
self.client.cookies["basket_items"] = f"""[
{{"id": {max_id + 1}, "name": "", "quantity": 1, "unit_price": 28}}
]"""
response = self.client.get(reverse("eboutic:command"))
self.assertIn(
'basket_items=""',
self.client.cookies["basket_items"].OutputString(),
)
self.assertIn(
"Path=/eboutic",
self.client.cookies["basket_items"].OutputString(),
)
cookie = self.client.cookies["basket_items"].OutputString()
assert 'basket_items=""' in cookie
assert "Path=/eboutic" in cookie
self.assertRedirects(response, "/eboutic/")
def test_submit_basket_illegal_quantity(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
self.client.cookies["basket_items"] = """[
{"id": 4, "name": "Barbar", "quantity": -1, "unit_price": 1.7}
]"""
@ -170,11 +162,11 @@ class EbouticTest(TestCase):
self.assertRedirects(response, "/eboutic/")
def test_buy_subscribe_product_with_credit_card(self):
self.client.login(username="old_subscriber", password="plop")
self.client.force_login(self.old_subscriber)
response = self.client.get(
reverse("core:user_profile", kwargs={"user_id": self.old_subscriber.id})
)
self.assertTrue("Non cotisant" in str(response.content))
assert "Non cotisant" in str(response.content)
self.client.cookies["basket_items"] = """[
{"id": 2, "name": "Cotis 2 semestres", "quantity": 1, "unit_price": 28}
]"""
@ -184,21 +176,21 @@ class EbouticTest(TestCase):
response.content.decode(),
)
basket = Basket.objects.get(id=self.client.session["basket_id"])
self.assertEqual(basket.items.count(), 1)
assert basket.items.count() == 1
response = self.client.get(self.generate_bank_valid_answer())
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content.decode("utf-8") == "Payment successful")
assert response.status_code == 200
assert response.content.decode("utf-8") == "Payment successful"
subscriber = User.objects.get(id=self.old_subscriber.id)
self.assertEqual(subscriber.subscriptions.count(), 2)
assert subscriber.subscriptions.count() == 2
sub = subscriber.subscriptions.order_by("-subscription_end").first()
self.assertTrue(sub.is_valid_now())
self.assertEqual(sub.member, subscriber)
self.assertEqual(sub.subscription_type, "deux-semestres")
self.assertEqual(sub.location, "EBOUTIC")
assert sub.is_valid_now()
assert sub.member == subscriber
assert sub.subscription_type == "deux-semestres"
assert sub.location == "EBOUTIC"
def test_buy_refill_product_with_credit_card(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
# basket contains 1 refill item worth 15€
self.client.cookies["basket_items"] = json.dumps(
[{"id": 3, "name": "Rechargement 15 €", "quantity": 1, "unit_price": 15}]
@ -208,13 +200,13 @@ class EbouticTest(TestCase):
url = self.generate_bank_valid_answer()
response = self.client.get(url)
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content.decode() == "Payment successful")
assert response.status_code == 200
assert response.content.decode() == "Payment successful"
new_balance = Customer.objects.get(user=self.subscriber).amount
self.assertEqual(new_balance, initial_balance + 15)
assert new_balance == initial_balance + 15
def test_alter_basket_after_submission(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
self.client.cookies["basket_items"] = json.dumps(
[{"id": 4, "name": "Barbar", "quantity": 1, "unit_price": 1.7}]
)
@ -227,30 +219,30 @@ class EbouticTest(TestCase):
)
self.client.get(reverse("eboutic:command"))
response = self.client.get(et_answer_url)
self.assertEqual(response.status_code, 500)
self.assertIn(
"Basket processing failed with error: SuspiciousOperation('Basket total and amount do not match'",
response.content.decode("utf-8"),
assert response.status_code == 500
assert (
"Basket processing failed with error: SuspiciousOperation('Basket total and amount do not match'"
in response.content.decode("utf-8"),
)
def test_buy_simple_product_with_credit_card(self):
self.client.login(username="subscriber", password="plop")
self.client.force_login(self.subscriber)
self.client.cookies["basket_items"] = json.dumps(
[{"id": 4, "name": "Barbar", "quantity": 1, "unit_price": 1.7}]
)
self.client.get(reverse("eboutic:command"))
et_answer_url = self.generate_bank_valid_answer()
response = self.client.get(et_answer_url)
self.assertTrue(response.status_code == 200)
self.assertTrue(response.content.decode("utf-8") == "Payment successful")
assert response.status_code == 200
assert response.content.decode("utf-8") == "Payment successful"
selling = (
Selling.objects.filter(customer=self.subscriber.customer)
.order_by("-date")
.first()
)
self.assertEqual(selling.payment_method, "CARD")
self.assertEqual(selling.quantity, 1)
self.assertEqual(selling.unit_price, self.barbar.selling_price)
self.assertEqual(selling.counter.type, "EBOUTIC")
self.assertEqual(selling.product, self.barbar)
assert selling.payment_method == "CARD"
assert selling.quantity == 1
assert selling.unit_price == self.barbar.selling_price
assert selling.counter.type == "EBOUTIC"
assert selling.product == self.barbar