Esempio n. 1
0
    def test_create_and_subscribe_with_new_address(self):

        res = self.client.post(
            self.create_donation_session_url,
            {
                "paymentTimes": donations.serializers.TYPE_SINGLE_TIME,
                "amount": "200"
            },
        )
        self.assertEqual(res.status_code, 201)
        self.assertIn(self.information_modal_url, res.data["next"])

        self.donation_information_payload["email"] = "*****@*****.**"
        res = self.client.post(
            self.send_donation_url,
            {
                **self.donation_information_payload, "subscribed2022": True
            },
        )
        payment = Payment.objects.get()
        self.assertEqual(res.status_code, 200)
        self.assertIn(front_url("payment_page", args=(payment.pk, )),
                      res.data["next"])

        self.assertTrue(payment.meta.get("subscribed_2022"))

        # simulate correct payment
        complete_payment(payment)
        donation_notification_listener(payment)

        p2 = Person.objects.exclude(pk=self.p1.pk).get()
        self.assertIn(Person.NEWSLETTER_2022, p2.newsletters)
    def test_can_make_a_loan_when_logged_in(self, on_commit):
        self.factory.user = self.p1.role
        self.factory.session = {}

        res = self.ask_amount_view(self.get("/amount"))
        self.assertEqual(res.status_code, 200)

        res = self.ask_amount_view(self.post("/amount", {"amount": "40000"}))
        self.assertRedirects(res, "/success/", fetch_redirect_response=False)

        res = self.personal_information_view(self.get("/info"))
        self.assertEqual(res.status_code, 200)

        res = self.personal_information_view(
            self.post("/info", self.loan_information_payload)
        )
        self.assertRedirects(res, "/success/", fetch_redirect_response=False)

        res = self.contract_view(self.get("/contract/"))
        self.assertEqual(res.status_code, 200)

        res = self.contract_view(self.post("/contract/", data={"acceptance": "Y"}))
        # no other payment
        payment = Payment.objects.get()
        self.assertRedirects(
            res,
            front_url("payment_page", args=(payment.pk,)),
            fetch_redirect_response=False,
        )

        res = self.client.get(reverse("payment_return", args=(payment.pk,)))
        self.assertEqual(res.status_code, 200)

        self.p1.refresh_from_db()

        # assert fields have been saved on model
        for f in [
            "first_name",
            "last_name",
            "location_address1",
            "location_address2",
            "location_zip",
            "location_city",
            "location_country",
        ]:
            self.assertEqual(getattr(self.p1, f), self.loan_information_payload[f])

        # fake systempay webhook
        complete_payment(payment)

        loan_notification_listener(payment)

        on_commit.assert_called_once()
        partial = on_commit.call_args[0][0]

        self.assertEqual(partial.func, generate_and_send_contract)
        self.assertEqual(partial.args, (payment.id,))
Esempio n. 3
0
    def test_create_person_when_using_new_address(self):

        res = self.client.post(
            self.create_donation_session_url,
            {
                "paymentTimes": donations.serializers.TYPE_SINGLE_TIME,
                "amount": "200"
            },
        )
        self.assertEqual(res.status_code, 201)
        self.assertIn(self.information_modal_url, res.data["next"])

        self.donation_information_payload["email"] = "*****@*****.**"
        res = self.client.post(self.send_donation_url,
                               self.donation_information_payload)

        payment = Payment.objects.get()
        self.assertEqual(res.status_code, 200)
        self.assertIn(front_url("payment_page", args=(payment.pk, )),
                      res.data["next"])

        # simulate correct payment
        complete_payment(payment)
        donation_notification_listener(payment)

        p2 = Person.objects.exclude(pk=self.p1.pk).get()
        # assert fields have been saved on model
        self.assertEqual(p2.first_name,
                         self.donation_information_payload["firstName"])
        self.assertEqual(p2.last_name,
                         self.donation_information_payload["lastName"])
        self.assertEqual(
            p2.location_address1,
            self.donation_information_payload["locationAddress1"],
        )
        self.assertEqual(
            p2.location_address2,
            self.donation_information_payload["locationAddress2"],
        )
        self.assertEqual(p2.location_city,
                         self.donation_information_payload["locationCity"])
        self.assertEqual(
            p2.location_country,
            self.donation_information_payload["locationCountry"],
        )

        # assert user fields have been saved on payment
        self.assertEqual(payment.first_name,
                         self.donation_information_payload["firstName"])
        self.assertEqual(payment.last_name,
                         self.donation_information_payload["lastName"])
        self.assertEqual(payment.phone_number,
                         self.donation_information_payload["contactPhone"])

        self.assertNotIn(Person.NEWSLETTER_2022, p2.newsletters)
Esempio n. 4
0
    def test_allocation_createdmeta_on_payment(self):
        payment = create_payment(
            person=self.p1,
            type=DonsConfig.PAYMENT_TYPE,
            price=10000,
            mode=SystemPayPaymentMode.id,
            meta={"allocations": json.dumps({str(self.group.pk): 10000})},
        )

        complete_payment(payment)
        notify_status_change(payment)

        self.assertTrue(Operation.objects.exists())
        operation = Operation.objects.get()

        self.assertEqual(operation.amount, 10000)
        self.assertEqual(operation.group, self.group)
Esempio n. 5
0
def update_payment_from_transaction(payment, transaction):
    if transaction.status == SystemPayTransaction.STATUS_COMPLETED:
        if payment.status == Payment.STATUS_CANCELED:
            logger.error(
                f"La transaction Systempay {transaction.pk} a été mise à jour alors que le paiement {payment.pk} était annulé"
            )
            return

        if transaction.is_refund:
            refund_payment(payment)
        else:
            complete_payment(payment)

    if (
        transaction.status == SystemPayTransaction.STATUS_REFUSED
        and payment.status == Payment.STATUS_WAITING
    ):
        refuse_payment(payment)
Esempio n. 6
0
    def test_can_donate_while_logged_in(self, on_commit):
        self.client.force_login(self.p1.role)

        res = self.client.get(self.amount_url)
        self.assertEqual(res.status_code, 200)

        res = self.client.post(
            self.create_donation_session_url,
            {
                "paymentTimes": donations.serializers.TYPE_SINGLE_TIME,
                "amount": "200",
            },
        )
        self.assertEqual(res.status_code, 201)
        self.assertIn(self.information_modal_url, res.data["next"])

        res = self.client.get(self.information_modal_url)
        self.assertEqual(res.status_code, 200)

        res = self.client.post(self.send_donation_url,
                               self.donation_information_payload)
        # no other payment
        payment = Payment.objects.get()
        self.assertEqual(res.status_code, 200)
        self.assertIn(front_url("payment_page", args=(payment.pk, )),
                      res.data["next"])

        res = self.client.get(reverse("payment_return", args=(payment.pk, )))
        self.assertRedirects(
            res,
            "https://lafranceinsoumise.fr/remerciement-don/",
            fetch_redirect_response=False,
        )

        self.p1.refresh_from_db()

        # assert fields have been saved on model
        self.assertEqual(self.p1.first_name,
                         self.donation_information_payload["firstName"])
        self.assertEqual(self.p1.last_name,
                         self.donation_information_payload["lastName"])
        self.assertEqual(
            self.p1.location_address1,
            self.donation_information_payload["locationAddress1"],
        )
        self.assertEqual(
            self.p1.location_address2,
            self.donation_information_payload["locationAddress2"],
        )
        self.assertEqual(self.p1.location_city,
                         self.donation_information_payload["locationCity"])
        self.assertEqual(
            self.p1.location_country,
            self.donation_information_payload["locationCountry"],
        )

        # check person is not unsubscribed
        self.assertIn(Person.NEWSLETTER_2022, self.p1.newsletters)

        # fake systempay webhook
        complete_payment(payment)
        donation_notification_listener(payment)
        on_commit.assert_called_once()
Esempio n. 7
0
    def test_can_donate_with_allocation(self):
        self.client.force_login(self.p1.role)
        session = self.client.session

        res = self.client.get(self.amount_url)
        self.assertEqual(res.status_code, 200)

        res = self.client.post(
            self.create_donation_session_url,
            {
                "paymentTimes":
                donations.serializers.TYPE_SINGLE_TIME,
                "amount":
                "20000",
                "allocations": [
                    {
                        "group": str(self.group.pk),
                        "amount": 10000
                    },
                    {
                        "group": str(self.other_group.pk),
                        "amount": 5000
                    },
                ],
            },
        )

        self.assertEqual(res.status_code, 201)
        self.assertIn(self.information_modal_url, res.data["next"])

        self.assertEqual(
            session["_donation_"]["allocations"],
            json.dumps([
                {
                    "group": str(self.group.pk),
                    "amount": 10000
                },
                {
                    "group": str(self.other_group.pk),
                    "amount": 5000
                },
            ]),
        )

        res = self.client.get(self.information_modal_url)
        self.assertEqual(res.status_code, 200)

        res = self.client.post(
            self.send_donation_url,
            {
                **self.donation_information_payload,
                "allocations": [
                    {
                        "group": str(self.group.pk),
                        "amount": 10000
                    },
                    {
                        "group": str(self.other_group.pk),
                        "amount": 5000
                    },
                ],
            },
        )
        # no other payment
        payment = Payment.objects.get()
        self.assertEqual(res.status_code, 200)
        self.assertIn(front_url("payment_page", args=(payment.pk, )),
                      res.data["next"])

        self.assertIn("allocations", payment.meta)
        self.assertEqual(
            payment.meta["allocations"],
            json.dumps({
                str(self.group.pk): 10000,
                str(self.other_group.pk): 5000
            }),
        )

        complete_payment(payment)
        donation_notification_listener(payment)

        self.assertTrue(
            Operation.objects.filter(group=self.group,
                                     amount=10000,
                                     payment=payment))
        self.assertTrue(
            Operation.objects.filter(group=self.other_group,
                                     amount=5000,
                                     payment=payment))