2017-04-03 23:55:54 +02:00
|
|
|
from unittest.mock import patch
|
2016-08-02 10:40:46 +02:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
from django.contrib.auth.models import Permission, User
|
|
|
|
from django.test import Client, TestCase
|
2017-04-03 23:55:54 +02:00
|
|
|
|
2017-04-10 11:52:57 +02:00
|
|
|
from kfet.models import Account, Article, ArticleCategory
|
2017-04-03 23:55:54 +02:00
|
|
|
|
|
|
|
|
|
|
|
class TestStats(TestCase):
|
2018-10-06 12:35:49 +02:00
|
|
|
@patch("gestioncof.signals.messages")
|
2017-04-03 23:55:54 +02:00
|
|
|
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-21 16:26:59 +01:00
|
|
|
perm = Permission.objects.get_by_natural_key("is_team", "kfet", "account")
|
2017-04-03 23:55:54 +02:00
|
|
|
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(
|
2018-10-06 12:35:49 +02:00
|
|
|
name="article", category=ArticleCategory.objects.create(name="C")
|
2017-04-03 23:55:54 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
# 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
|
2017-04-03 23:55:54 +02:00
|
|
|
user_urls = [
|
|
|
|
"/k-fet/accounts/FOO/stat/operations/list",
|
|
|
|
"/k-fet/accounts/FOO/stat/operations?{}".format(
|
2018-10-06 12:35:49 +02:00
|
|
|
"&".join(
|
|
|
|
[
|
|
|
|
"scale=day",
|
|
|
|
"types=['purchase']",
|
|
|
|
"scale_args={'n_steps':+7,+'last':+True}",
|
|
|
|
"format=json",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
),
|
2017-04-03 23:55:54 +02:00
|
|
|
"/k-fet/accounts/FOO/stat/balance/list",
|
2018-10-06 12:35:49 +02:00
|
|
|
"/k-fet/accounts/FOO/stat/balance?format=json",
|
2017-04-03 23:55:54 +02:00
|
|
|
]
|
|
|
|
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)
|
2017-04-03 23:55:54 +02:00
|
|
|
|
|
|
|
# 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),
|
2018-10-06 12:35:49 +02:00
|
|
|
"/k-fet/articles/{}/stat/sales".format(article.pk),
|
2017-04-03 23:55:54 +02:00
|
|
|
]
|
|
|
|
for url in articles_urls:
|
|
|
|
resp = client.get(url)
|
|
|
|
self.assertEqual(200, resp.status_code)
|
2017-04-04 17:32:46 +02:00
|
|
|
resp2 = client2.get(url, follow=True)
|
|
|
|
self.assertRedirects(resp2, "/")
|