Blame view

src/user_manager/tests.py 3.75 KB
3df2d31a   haribo   #3430 : dates are...
1
from django.test import TestCase
ddf59dd4   haribo   Remaniement :
2
from common.models import *
fd99569d   haribo   Date: 22/06/2016
3
from django.contrib.auth.models import User
3df2d31a   haribo   #3430 : dates are...
4

4078466e   Quentin Durand   Refactorisation t...
5

fd99569d   haribo   Date: 22/06/2016
6
7
8
9
10
11

class UserManagerTests(TestCase):

    def setUp(self):
        UserLevel.objects.create()
        Country.objects.create()
4b59e304   Etienne Pallier   bugfix user test_...
12
        self.assertEqual(PyrosUser.objects.count(), 0, "There should be no User")
fd99569d   haribo   Date: 22/06/2016
13
14
15
16
17
        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")
4078466e   Quentin Durand   Refactorisation t...
18
19
20
21
22
23
        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")
        self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel")

4b59e304   Etienne Pallier   bugfix user test_...
24
25
        self.assertEqual(PyrosUser.objects.all()[0].first_name, 'toto')
        self.assertEqual(PyrosUser.objects.all()[0].email, 'toto@titi.fr')
fd99569d   haribo   Date: 22/06/2016
26
27

    def test_login(self):
4b59e304   Etienne Pallier   bugfix user test_...
28
29
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
        self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel")
4078466e   Quentin Durand   Refactorisation t...
30

4b59e304   Etienne Pallier   bugfix user test_...
31
32
33
34
35
36
37
        # 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")
fd99569d   haribo   Date: 22/06/2016
38

4b59e304   Etienne Pallier   bugfix user test_...
39
        # Log user
fd99569d   haribo   Date: 22/06/2016
40
41
        path = "/user_manager/login"
        response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"})
6b16badb   Quentin Durand   Fix unitests + ne...
42
        self.assertTrue(response.context.get("success"))
fd99569d   haribo   Date: 22/06/2016
43
        self.assertIn('_auth_user_id', self.client.session, "The user should be logged in")
6b16badb   Quentin Durand   Fix unitests + ne...
44
45
46
47

    def test_login_not_active(self):
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
        self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel")
6b16badb   Quentin Durand   Fix unitests + ne...
48
49
50
51

        # 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
4078466e   Quentin Durand   Refactorisation t...
52

6b16badb   Quentin Durand   Fix unitests + ne...
53
54
55
56
57
58
        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"})

6b16badb   Quentin Durand   Fix unitests + ne...
59
60
        self.assertFalse(response.context.get("success"))
        self.assertNotIn('_auth_user_id', self.client.session, "The user should be logged in")
fd99569d   haribo   Date: 22/06/2016
61
62

    def test_wrong_email(self):
fd99569d   haribo   Date: 22/06/2016
63
64
65
66
67
68
        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):
fd99569d   haribo   Date: 22/06/2016
69
70
71
72
73
74
        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):
fd99569d   haribo   Date: 22/06/2016
75
76
77
78
        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")