kpsul/kfet/tests/test_statistic.py

84 lines
2.9 KiB
Python
Raw Permalink Normal View History

from unittest.mock import patch
2016-08-02 10:40:46 +02:00
from django.contrib.auth.models import Permission, User
from django.test import Client, TestCase
from kfet.models import Account, Article, ArticleCategory
class TestStats(TestCase):
@patch("gestioncof.signals.messages")
def test_user_stats(self, mock_messages):
"""
Checks that we can get the stat-related pages without any problem.
"""
# We setup two users and an article. Only the first user is part of the
# team.
user = User.objects.create(username="Foobar")
user.set_password("foobar")
user.save()
Account.objects.create(trigramme="FOO", cofprofile=user.profile)
2019-12-24 17:13:27 +01:00
perm = Permission.objects.get(
codename="is_team", content_type__app_label="kfet"
)
user.user_permissions.add(perm)
user2 = User.objects.create(username="Barfoo")
user2.set_password("barfoo")
user2.save()
Account.objects.create(trigramme="BAR", cofprofile=user2.profile)
article = Article.objects.create(
name="article", category=ArticleCategory.objects.create(name="C")
)
# Each user have its own client
client = Client()
client.login(username="Foobar", password="foobar")
client2 = Client()
client2.login(username="Barfoo", password="barfoo")
2019-10-05 02:25:05 +02:00
# 1. FOO should be able to get these pages but BAR receives a 404
user_urls = [
"/k-fet/accounts/FOO/stat/operations/list",
"/k-fet/accounts/FOO/stat/operations?{}".format(
"&".join(
[
2020-09-16 19:19:29 +02:00
"scale-name=day",
"scale-n_steps=7",
"scale-last=True",
"format=json",
]
)
),
"/k-fet/accounts/FOO/stat/balance/list",
"/k-fet/accounts/FOO/stat/balance?format=json",
]
for url in user_urls:
resp = client.get(url)
self.assertEqual(200, resp.status_code)
resp2 = client2.get(url)
2019-10-05 02:25:05 +02:00
self.assertEqual(404, resp2.status_code)
# 2. FOO is a member of the team and can get these pages but BAR
# receives a Redirect response
articles_urls = [
"/k-fet/articles/{}/stat/sales/list".format(article.pk),
2020-09-16 19:19:29 +02:00
"/k-fet/articles/{}/stat/sales?{}".format(
article.pk,
"&".join(
[
"scale-name=day",
"scale-n_steps=7",
"scale-last=True",
"format=json",
]
),
),
]
for url in articles_urls:
resp = client.get(url)
self.assertEqual(200, resp.status_code)
resp2 = client2.get(url, follow=True)
self.assertRedirects(resp2, "/gestion/")