tests and test fixtures

This commit is contained in:
Laetitia Getti 2023-04-18 14:26:42 +02:00
parent ca026a4617
commit e16447dcff
5 changed files with 35 additions and 7 deletions

3
conftest.py Normal file
View file

@ -0,0 +1,3 @@
pytest_plugins = [
"la_chariotte.helpers.fixtures",
]

View file

@ -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

View file

@ -1,6 +1,7 @@
import datetime import datetime
import pytest import pytest
from django.contrib import auth
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
@ -21,11 +22,21 @@ def create_grouped_order(days_before_delivery_date,days_before_deadline,name):
class TestGroupedOrderIndexView: 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 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 response.status_code == 200
assert "Pas de commande groupée pour l'instant" in response.content.decode() assert "Pas de commande groupée pour l'instant" in response.content.decode()
assert len(response.context["grouped_order_list"]["old_grouped_orders"]) == 0 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 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 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 == 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 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 == 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 According to their delivery date and deadline, grouped orders are placed in correct section : only incoming gr order
""" """

View file

@ -1,15 +1,18 @@
from django.contrib.auth.mixins import LoginRequiredMixin
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, render 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.utils import timezone
from django.views import generic from django.views import generic
from .models import GroupedOrder, Item, Order, OrderedItem 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é""" """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" template_name = "order/index.html"
context_object_name = "grouped_order_list" context_object_name = "grouped_order_list"

View file

@ -53,6 +53,7 @@ MIDDLEWARE = [
ROOT_URLCONF = "la_chariotte.urls" ROOT_URLCONF = "la_chariotte.urls"
LOGIN_URL = "login"
LOGIN_REDIRECT_URL = "home" LOGIN_REDIRECT_URL = "home"
LOGOUT_REDIRECT_URL = "home" LOGOUT_REDIRECT_URL = "home"