Blame view

src/core/pyros_django/user_manager/tests.py 7.03 KB
1cffbf1c   Etienne Pallier   moved pyros_djang...
1
2
3
4
5
6
7
8
9
from django.test import TestCase
from common.models import *
from django.contrib.auth.models import User



class UserManagerTests(TestCase):

    def setUp(self):
05f66454   ALEXIS-PC\alexis   adding and updati...
10
11
12
        UserLevel.objects.create(name="Visitor",desc="Visitor description",priority=0,quota=0.0)
        UserLevel.objects.create(name="PI",desc="PI description",priority=5,quota=100.0)
        UserLevel.objects.create(name="SysAdmin",desc="SysAdmin description",priority=7,quota=1000.0)
1cffbf1c   Etienne Pallier   moved pyros_djang...
13
14
15
16
17
18
19
20
21
22
23
        Country.objects.create()
        self.assertEqual(PyrosUser.objects.count(), 0, "There should be no User")
        path = "/user_manager/creation_validate"
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze",
                                           "first_name": "toto", "last_name": "titi", "tel": "0123456789",
                                           "laboratory": "IRAP", "address": "ici"})
        self.assertTrue("success" in response.context.keys(), "There should be a success")
        self.assertEqual(PyrosUser.objects.count(), 1, "Theroue shld be one User")

    def test_creation(self):
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
05f66454   ALEXIS-PC\alexis   adding and updati...
24
        self.assertEqual(UserLevel.objects.count(), 3, "There should be 3 UserLevel")
1cffbf1c   Etienne Pallier   moved pyros_djang...
25
26
27

        self.assertEqual(PyrosUser.objects.all()[0].first_name, 'toto')
        self.assertEqual(PyrosUser.objects.all()[0].email, 'toto@titi.fr')
05f66454   ALEXIS-PC\alexis   adding and updati...
28
        self.assertEqual(PyrosUser.objects.all()[0].user_level.filter(name="Visitor").count(),1,"There should be one UserLevel (=Visitor)")
1cffbf1c   Etienne Pallier   moved pyros_djang...
29
30
31

    def test_login(self):
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
05f66454   ALEXIS-PC\alexis   adding and updati...
32
        self.assertEqual(UserLevel.objects.count(), 3, "There should be 3 UserLevel")
1cffbf1c   Etienne Pallier   moved pyros_djang...
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49

        # Activate user
        # La variable qui régit l'activation d'un compte est contenue dans pyrosUsers 
        # et s'appelle is_active, il suffit de passer cette variable à True
        current_user = PyrosUser.objects.all()[0]
        current_user.is_active=True
        current_user.save()
        self.assertEqual(PyrosUser.objects.all()[0].is_active, True, "user should be active")

        # Log user
        path = "/user_manager/login"
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"})
        self.assertTrue(response.context.get("success"))
        self.assertIn('_auth_user_id', self.client.session, "The user should be logged in")

    def test_login_not_active(self):
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
05f66454   ALEXIS-PC\alexis   adding and updati...
50
        self.assertEqual(UserLevel.objects.count(), 3, "There should be 3 UserLevel")
1cffbf1c   Etienne Pallier   moved pyros_djang...
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81

        # Activate user
        # La variable qui régit l'activation d'un compte est contenue dans pyrosUsers
        # et s'appelle is_active, il suffit de passer cette variable à True

        self.assertEqual(PyrosUser.objects.all()[0].is_active, False, "user should not be active")

        # Log user
        path = "/user_manager/login"
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"})

        self.assertFalse(response.context.get("success"))
        self.assertNotIn('_auth_user_id', self.client.session, "The user should be logged in")

    def test_wrong_email(self):
        path = "/user_manager/login"
        response = self.client.post(path, {"email": "toto@tti.fr", "password": "aze"})
        self.assertIn("error", response.context.keys(), "There should be an error")
        self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user")

    def test_wrong_password(self):
        path = "/user_manager/login"
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "azee"})
        self.assertIn("error", response.context.keys(), "There should be an error")
        self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user")

    def test_logout(self):
        self.client.login(username="toto@titi.fr", password="aze")
        path = "/user_manager/logout"
        self.client.get(path)
        self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user")
05f66454   ALEXIS-PC\alexis   adding and updati...
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137

    def test_get_user_priority(self):
        user = PyrosUser.objects.all()[0]
        # add PI role to user
        UserLevel.objects.get(name="PI").pyros_users.add(user)
        self.assertEqual(user.get_priority(),UserLevel.objects.get(name="PI").priority,"The priority should be equal to PI's priority")

    def test_wrong_get_user_priority(self):
        user = PyrosUser.objects.all()[0]
        # add PI role to user
        UserLevel.objects.get(name="PI").pyros_users.add(user)
        self.assertNotEqual(user.get_priority(),UserLevel.objects.get(name="Visitor").priority,"The priority shouldn't be equal to Visitor's priority")

    def test_get_roles_str(self):
        user = PyrosUser.objects.all()[0]
        # add PI role to user
        UserLevel.objects.get(name="PI").pyros_users.add(user)
        roles_str = user.get_roles_str()
        for role in user.user_level.all():
            self.assertIn(role.name,roles_str,f"The role {role} should be in the str representation")
    
    def test_wrong_get_roles_str(self):
        user = PyrosUser.objects.all()[0]
        # add PI role to user
        UserLevel.objects.get(name="PI").pyros_users.add(user)
        roles_str = user.get_roles_str()
        self.assertNotIn(UserLevel.objects.get(name="SysAdmin").name,roles_str,"The role SysAdmin shouldn't be in the str representation")

    def test_max_priority_desc(self):
        user = PyrosUser.objects.all()[0]
        # add PI role to user
        UserLevel.objects.get(name="PI").pyros_users.add(user)
        self.assertEqual(user.get_max_priority_desc(),UserLevel.objects.get(name="PI").desc,"The desc of user_level should be 'PI description' ")

    def test_wrong_max_priority_desc(self):
        user = PyrosUser.objects.all()[0]
        # add PI role to user
        UserLevel.objects.get(name="PI").pyros_users.add(user)
        # add SysAdmin role to user (has a greatier priority than PI and Visitor roles)
        UserLevel.objects.get(name="SysAdmin").pyros_users.add(user)
        self.assertNotEqual(user.get_max_priority_desc(),UserLevel.objects.get(name="PI").desc,"The desc of user_level shouldn't be 'PI description' ")

        
    def test_max_priority_quota(self):
        user = PyrosUser.objects.all()[0]
        # add PI role to user
        UserLevel.objects.get(name="PI").pyros_users.add(user)
        self.assertEqual(user.get_max_priority_quota(),UserLevel.objects.get(name="PI").quota,"The quota of user_level should be 100.0 ")

    def test_wrong_max_priority_quota(self):
        user = PyrosUser.objects.all()[0]
        # add PI role to user
        UserLevel.objects.get(name="PI").pyros_users.add(user)
        # add SysAdmin role to user (has a greatier priority than PI and Visitor roles)
        UserLevel.objects.get(name="SysAdmin").pyros_users.add(user)
        self.assertNotEqual(user.get_max_priority_quota(),UserLevel.objects.get(name="PI").desc,"The quota of user_level shouldn't be 1000.0 ")