import pytest
from django.urls import reverse

from la_chariotte import settings

pytestmark = pytest.mark.django_db


def test_send_order_confirmation_mail(mailoutbox, simple_grouped_order, client):
    """
    When a user orders, they receive a confirmation email
    """
    item = simple_grouped_order.item_set.first()

    # the user places an order
    order_url = reverse(
        "order:order",
        kwargs={
            "code": simple_grouped_order.code,
        },
    )
    client.post(
        order_url,
        {
            f"quantity_{item.pk}": [4, 0],
            "first_name": "Prénom",
            "last_name": "Nom",
            "phone": "0645632569",
            "email": "test@example.com",
            "note": "",
        },
    )
    assert len(mailoutbox) == 1
    m = mailoutbox[0]
    assert (
        m.subject
        == f"[{settings.PROJECT_NAME}] Votre commande pour « Test grouped order »"
    )
    assert f"{settings.BASE_URL}" in m.alternatives[0][0]
    assert m.alternatives[0][1] == "text/html"
    assert "test@example.com" in mailoutbox[0].to
