diff --git a/la_chariotte/helpers/fixtures.py b/la_chariotte/helpers/fixtures.py index 2d5e026..b3a8f40 100644 --- a/la_chariotte/helpers/fixtures.py +++ b/la_chariotte/helpers/fixtures.py @@ -4,6 +4,7 @@ import pytest from django.utils import timezone from la_chariotte.order.models import GroupedOrder, Item +from la_chariotte.tests.utils import create_grouped_order @pytest.fixture @@ -23,6 +24,18 @@ def other_user(django_user_model): return user +@pytest.fixture +def authenticated_user_with_name(django_user_model): + username = "toto@gmail.com" + password = "tata" + first_name = "boule" + last_name = "bill" + user = django_user_model.objects.create_user( + username=username, password=password, first_name=first_name, last_name=last_name + ) + return user + + @pytest.fixture def simple_grouped_order(other_user): date = timezone.now().date() + datetime.timedelta(days=30) @@ -39,6 +52,19 @@ def simple_grouped_order(other_user): return grouped_order +@pytest.fixture +def connected_grouped_order(client, authenticated_user_with_name): + client.force_login(authenticated_user_with_name) + grouped_order = create_grouped_order( + name="Test grouped order", + orga_user=authenticated_user_with_name, + days_before_delivery_date=30, + days_before_deadline=5, + ) + item = Item.objects.create(name="test item", grouped_order=grouped_order, price=2) + return grouped_order + + @pytest.fixture(autouse=True) def password_hasher_setup(settings): # Use a weaker password hasher during tests, for speed diff --git a/la_chariotte/order/models.py b/la_chariotte/order/models.py index 2733fa9..0aa203f 100644 --- a/la_chariotte/order/models.py +++ b/la_chariotte/order/models.py @@ -97,8 +97,6 @@ class GroupedOrder(models.Model): class OrderAuthor(models.Model): """Used when a user orders (with or without an account)""" - # TODO faire le lien avec CustomUser (CustomUser hérite de OrderAuthor), - # pour ensuite préremplir quand on est connecté·e first_name = models.CharField(verbose_name="Prénom") last_name = models.CharField(verbose_name="Nom") phone = models.CharField( diff --git a/la_chariotte/order/templates/order/grouped_order_detail.html b/la_chariotte/order/templates/order/grouped_order_detail.html index 15ad1c4..f8f5660 100644 --- a/la_chariotte/order/templates/order/grouped_order_detail.html +++ b/la_chariotte/order/templates/order/grouped_order_detail.html @@ -152,18 +152,18 @@

+ value="{{ order_author.first_name }}" required>

+ value="{{ order_author.last_name }}" required>

+ value="{{ order_author.phone }}" required>

+ value="{{ order_author.email }}" required>

diff --git a/la_chariotte/order/views/grouped_order.py b/la_chariotte/order/views/grouped_order.py index 4457f09..7a87ff4 100644 --- a/la_chariotte/order/views/grouped_order.py +++ b/la_chariotte/order/views/grouped_order.py @@ -117,12 +117,23 @@ class GroupedOrderDetailView(generic.DetailView): remaining_qty = {item.id: item.get_remaining_nb() for item in items} prices_dict = {item.id: item.price for item in items} + if self.request.user.is_authenticated: + order_author = OrderAuthor.objects.create( + first_name=self.request.user.first_name, + last_name=self.request.user.last_name, + email=self.request.user.username, + ) + order_author.save() + else: + order_author = None + context = super().get_context_data(**kwargs) context.update( { # Used for the js display of total price of an order "prices_dict": json.dumps(prices_dict, cls=DjangoJSONEncoder), "remaining_qty": remaining_qty, + "order_author": order_author, } ) return context diff --git a/la_chariotte/tests/test_order_views_grouped_order.py b/la_chariotte/tests/test_order_views_grouped_order.py index 4eaa729..83f5dfd 100644 --- a/la_chariotte/tests/test_order_views_grouped_order.py +++ b/la_chariotte/tests/test_order_views_grouped_order.py @@ -4,14 +4,14 @@ from io import StringIO import pytest from django.contrib import auth +from django.contrib.auth import get_user from django.forms.utils import to_current_timezone from django.urls import reverse from django.utils import timezone -from icalendar import Calendar +from icalendar import Calendar, vText from la_chariotte.order import models - -from .utils import create_grouped_order, order_items_in_grouped_order +from la_chariotte.tests.utils import create_grouped_order, order_items_in_grouped_order pytestmark = pytest.mark.django_db @@ -215,11 +215,32 @@ class TestJoinGroupedOrderView: response = client.post(join_url, {"code": "123456"}) assert ( - "nous ne trouvons aucune commande avec ce code" in response.content.decode() + "Désolé, nous ne trouvons aucune commande avec ce code" + in response.content.decode() ) class TestGroupedOrderDetailView: + def test_order_item_with_authenticated_user(self, client, connected_grouped_order): + detail_url = reverse( + "order:grouped_order_detail", + kwargs={ + "code": connected_grouped_order.code, + }, + ) + response = client.get(detail_url) + + assert response.status_code == 200 + + order_author = response.context[0]["order_author"] + current_user = get_user(client) + + assert order_author is not None + + assert order_author.first_name == current_user.first_name + assert order_author.email == current_user.username + assert order_author.last_name == current_user.last_name + def test_order_item(self, client, other_user): """ From the OrderDetailView, we order an item using the order form, and it creates an models.Order with an Ordered_item inside @@ -300,7 +321,9 @@ class TestGroupedOrderDetailView: # OrderedItems are not created when the ordered quantity is 0 assert models.OrderedItem.objects.count() == 2 - def test_order_item__no_articles_ordered(self, client, other_user): + def test_order_item__no_articles_ordered( + self, client, authenticated_user_with_name + ): """ From the OrderDetailView, we order without having changed any item quantity. An error is raised. @@ -310,7 +333,7 @@ class TestGroupedOrderDetailView: days_before_delivery_date=5, days_before_deadline=2, name="gr order test", - orga_user=other_user, + orga_user=authenticated_user_with_name, ) item = models.Item.objects.create( name="test item 1", grouped_order=grouped_order, price=1 @@ -336,10 +359,10 @@ class TestGroupedOrderDetailView: order_url, { f"quantity_{item.pk}": [0, 0], - "first_name": "Prénom test", - "last_name": "Nom test", + "first_name": {authenticated_user_with_name.first_name}, + "last_name": {authenticated_user_with_name.last_name}, "phone": "0645632569", - "email": "test@mail.fr", + "email": {authenticated_user_with_name.email}, "note": "test note", }, ) @@ -348,12 +371,14 @@ class TestGroupedOrderDetailView: response.context["error_message"] == "Veuillez commander au moins un produit" ) - assert response.context["author"].first_name == "Prénom test" + assert ( + response.context["author"].first_name + == authenticated_user_with_name.first_name + ) content = response.content.decode() - assert "Prénom test" in content - assert "Nom test" in content - assert "0645632569" in content - assert "test@mail.fr" in content + assert authenticated_user_with_name.first_name in content + assert authenticated_user_with_name.last_name in content + assert authenticated_user_with_name.email in content assert "test note" in content assert not models.Order.objects.first() assert not models.OrderAuthor.objects.first()