diff --git a/conftest.py b/conftest.py new file mode 100644 index 0000000..92a7127 --- /dev/null +++ b/conftest.py @@ -0,0 +1,3 @@ +pytest_plugins = [ + "la_chariotte.helpers.fixtures", +] diff --git a/la_chariotte/helpers/fixtures.py b/la_chariotte/helpers/fixtures.py new file mode 100644 index 0000000..86dd43c --- /dev/null +++ b/la_chariotte/helpers/fixtures.py @@ -0,0 +1,10 @@ +import pytest + + +@pytest.fixture +def client_log(client, django_user_model): + username = "test@user.fr" + password = "azertypassword" + user = django_user_model.objects.create_user(username=username, password=password) + client.login(username=username, password=password) + return client diff --git a/la_chariotte/order/tests/test_views.py b/la_chariotte/order/tests/test_views.py index a9e0a59..dd4c314 100644 --- a/la_chariotte/order/tests/test_views.py +++ b/la_chariotte/order/tests/test_views.py @@ -1,6 +1,7 @@ import datetime import pytest +from django.contrib import auth from django.urls import reverse from django.utils import timezone @@ -21,11 +22,21 @@ def create_grouped_order(days_before_delivery_date,days_before_deadline,name): class TestGroupedOrderIndexView: - def test_no_grouped_orders(self, client): + def test_anonymous_user_redirection(self, client): + """ + If the user is anonymous, they are redirected to login view + """ + assert auth.get_user(client).is_anonymous + response = client.get(reverse("order:index")) + assert response.status_code == 302 + assert response.url.startswith(reverse("login")) + assert response.url.endswith(reverse("order:index")) + + def test_no_grouped_orders(self, client_log): """ If no grouped order exist, an appropriate message is displayed """ - response = client.get(reverse("order:index")) + response = client_log.get(reverse("order:index")) assert response.status_code == 200 assert "Pas de commande groupée pour l'instant" in response.content.decode() assert len(response.context["grouped_order_list"]["old_grouped_orders"]) == 0 @@ -41,7 +52,7 @@ class TestGroupedOrderIndexView: len(response.context["grouped_order_list"]["incoming_grouped_orders"]) == 0 ) - def test_grouped_orders_in_right_section(self, client): + def test_grouped_orders_in_right_section(self, client_log): """ According to their delivery date and deadline, grouped orders are placed in the correct section : several gr orders """ @@ -113,7 +124,7 @@ class TestGroupedOrderIndexView: == future_grouped_order ) - def test_grouped_orders_in_right_section__with_only_old(self, client): + def test_grouped_orders_in_right_section__with_only_old(self, client_log): """ According to their delivery date and deadline, grouped orders are placed in correct section : only old gr order """ @@ -150,7 +161,7 @@ class TestGroupedOrderIndexView: == old_gr_order ) - def test_grouped_orders_in_right_section__with_only_future(self, client): + def test_grouped_orders_in_right_section__with_only_future(self, client_log): """ According to their delivery date and deadline, grouped orders are placed in correct section : only incoming gr order """ diff --git a/la_chariotte/order/views.py b/la_chariotte/order/views.py index 8c0e6e1..e91c33a 100644 --- a/la_chariotte/order/views.py +++ b/la_chariotte/order/views.py @@ -1,15 +1,18 @@ +from django.contrib.auth.mixins import LoginRequiredMixin from django.http import HttpResponseRedirect from django.shortcuts import get_object_or_404, render -from django.urls import reverse +from django.urls import reverse, reverse_lazy from django.utils import timezone from django.views import generic from .models import GroupedOrder, Item, Order, OrderedItem -class IndexView(generic.ListView): +class IndexView(LoginRequiredMixin, generic.ListView): """Vue de toutes les commandes groupées existantes - plus tard, de toutes les commandes groupées de l'utilisateur connecté""" + """No permissions restriction""" + template_name = "order/index.html" context_object_name = "grouped_order_list" diff --git a/la_chariotte/settings.py b/la_chariotte/settings.py index 9f9afff..3d6b1b7 100644 --- a/la_chariotte/settings.py +++ b/la_chariotte/settings.py @@ -53,6 +53,7 @@ MIDDLEWARE = [ ROOT_URLCONF = "la_chariotte.urls" +LOGIN_URL = "login" LOGIN_REDIRECT_URL = "home" LOGOUT_REDIRECT_URL = "home"