Commit 4078466e69318660721e9f11e5f5850f0b248d9b
1 parent
6b16badb
Exists in
dev
Refactorisation tests user_manager
Showing
1 changed file
with
9 additions
and
48 deletions
Show diff stats
src/user_manager/tests.py
@@ -2,43 +2,32 @@ from django.test import TestCase | @@ -2,43 +2,32 @@ from django.test import TestCase | ||
2 | from common.models import * | 2 | from common.models import * |
3 | from django.contrib.auth.models import User | 3 | from django.contrib.auth.models import User |
4 | 4 | ||
5 | -from pprint import pprint | 5 | + |
6 | 6 | ||
7 | class UserManagerTests(TestCase): | 7 | class UserManagerTests(TestCase): |
8 | 8 | ||
9 | def setUp(self): | 9 | def setUp(self): |
10 | UserLevel.objects.create() | 10 | UserLevel.objects.create() |
11 | Country.objects.create() | 11 | Country.objects.create() |
12 | - | ||
13 | - def test_creation(self): | ||
14 | - self.assertEqual(Country.objects.count(), 1, "There should be 1 Country") | ||
15 | - self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel") | ||
16 | self.assertEqual(PyrosUser.objects.count(), 0, "There should be no User") | 12 | self.assertEqual(PyrosUser.objects.count(), 0, "There should be no User") |
17 | - #print("country is", Country.objects.count()) | ||
18 | - print("country id is", Country.objects.all()[0].id) | ||
19 | path = "/user_manager/creation_validate" | 13 | path = "/user_manager/creation_validate" |
20 | response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze", | 14 | response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze", |
21 | "first_name": "toto", "last_name": "titi", "tel": "0123456789", | 15 | "first_name": "toto", "last_name": "titi", "tel": "0123456789", |
22 | "laboratory": "IRAP", "address": "ici"}) | 16 | "laboratory": "IRAP", "address": "ici"}) |
23 | self.assertTrue("success" in response.context.keys(), "There should be a success") | 17 | self.assertTrue("success" in response.context.keys(), "There should be a success") |
24 | - self.assertEqual(PyrosUser.objects.count(), 1, "There should be one User") | ||
25 | - #self.assertEqual(PyrosUser.objects.count(), 1, "There should be one PyrosUser") | 18 | + self.assertEqual(PyrosUser.objects.count(), 1, "Theroue shld be one User") |
19 | + | ||
20 | + def test_creation(self): | ||
21 | + self.assertEqual(Country.objects.count(), 1, "There should be 1 Country") | ||
22 | + self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel") | ||
23 | + | ||
26 | self.assertEqual(PyrosUser.objects.all()[0].first_name, 'toto') | 24 | self.assertEqual(PyrosUser.objects.all()[0].first_name, 'toto') |
27 | self.assertEqual(PyrosUser.objects.all()[0].email, 'toto@titi.fr') | 25 | self.assertEqual(PyrosUser.objects.all()[0].email, 'toto@titi.fr') |
28 | 26 | ||
29 | def test_login(self): | 27 | def test_login(self): |
30 | self.assertEqual(Country.objects.count(), 1, "There should be 1 Country") | 28 | self.assertEqual(Country.objects.count(), 1, "There should be 1 Country") |
31 | self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel") | 29 | self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel") |
32 | - self.assertEqual(PyrosUser.objects.count(), 0, "There should be no User") | ||
33 | - | ||
34 | - # Create user | ||
35 | - path = "/user_manager/creation_validate" | ||
36 | - response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze", | ||
37 | - "first_name": "toto", "last_name": "titi", "tel": "0123456789", | ||
38 | - "laboratory": "IRAP", "address": "ici"}) | ||
39 | - self.assertTrue("success" in response.context.keys(), "There should be a success") | ||
40 | - self.assertEqual(PyrosUser.objects.count(), 1, "There should be one User") | ||
41 | - | 30 | + |
42 | # Activate user | 31 | # Activate user |
43 | # La variable qui régit l'activation d'un compte est contenue dans pyrosUsers | 32 | # La variable qui régit l'activation d'un compte est contenue dans pyrosUsers |
44 | # et s'appelle is_active, il suffit de passer cette variable à True | 33 | # et s'appelle is_active, il suffit de passer cette variable à True |
@@ -50,67 +39,39 @@ class UserManagerTests(TestCase): | @@ -50,67 +39,39 @@ class UserManagerTests(TestCase): | ||
50 | # Log user | 39 | # Log user |
51 | path = "/user_manager/login" | 40 | path = "/user_manager/login" |
52 | response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"}) | 41 | response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"}) |
53 | - | ||
54 | - """ Note that we use .has_key() because the login phase change the httpresponse context """ | ||
55 | - #print(response.context) | ||
56 | self.assertTrue(response.context.get("success")) | 42 | self.assertTrue(response.context.get("success")) |
57 | self.assertIn('_auth_user_id', self.client.session, "The user should be logged in") | 43 | self.assertIn('_auth_user_id', self.client.session, "The user should be logged in") |
58 | 44 | ||
59 | def test_login_not_active(self): | 45 | def test_login_not_active(self): |
60 | self.assertEqual(Country.objects.count(), 1, "There should be 1 Country") | 46 | self.assertEqual(Country.objects.count(), 1, "There should be 1 Country") |
61 | self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel") | 47 | self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel") |
62 | - self.assertEqual(PyrosUser.objects.count(), 0, "There should be no User") | ||
63 | - | ||
64 | - # Create user | ||
65 | - path = "/user_manager/creation_validate" | ||
66 | - response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze", | ||
67 | - "first_name": "toto", "last_name": "titi", "tel": "0123456789", | ||
68 | - "laboratory": "IRAP", "address": "ici"}) | ||
69 | - self.assertTrue("success" in response.context.keys(), "There should be a success") | ||
70 | - self.assertEqual(PyrosUser.objects.count(), 1, "There should be one User") | ||
71 | 48 | ||
72 | # Activate user | 49 | # Activate user |
73 | # La variable qui régit l'activation d'un compte est contenue dans pyrosUsers | 50 | # La variable qui régit l'activation d'un compte est contenue dans pyrosUsers |
74 | # et s'appelle is_active, il suffit de passer cette variable à True | 51 | # et s'appelle is_active, il suffit de passer cette variable à True |
75 | - current_user = PyrosUser.objects.all()[0] | ||
76 | - current_user.save() | 52 | + |
77 | self.assertEqual(PyrosUser.objects.all()[0].is_active, False, "user should not be active") | 53 | self.assertEqual(PyrosUser.objects.all()[0].is_active, False, "user should not be active") |
78 | 54 | ||
79 | # Log user | 55 | # Log user |
80 | path = "/user_manager/login" | 56 | path = "/user_manager/login" |
81 | response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"}) | 57 | response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"}) |
82 | 58 | ||
83 | - """ Note that we use .has_key() because the login phase change the httpresponse context """ | ||
84 | - # print(response.context) | ||
85 | self.assertFalse(response.context.get("success")) | 59 | self.assertFalse(response.context.get("success")) |
86 | self.assertNotIn('_auth_user_id', self.client.session, "The user should be logged in") | 60 | self.assertNotIn('_auth_user_id', self.client.session, "The user should be logged in") |
87 | 61 | ||
88 | def test_wrong_email(self): | 62 | def test_wrong_email(self): |
89 | - path = "/user_manager/creation_validate" | ||
90 | - response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze", | ||
91 | - "first_name": "toto", "last_name": "titi", "tel": "0123456789", | ||
92 | - "laboratory": "IRAP", "address": "ici"}) | ||
93 | - | ||
94 | path = "/user_manager/login" | 63 | path = "/user_manager/login" |
95 | response = self.client.post(path, {"email": "toto@tti.fr", "password": "aze"}) | 64 | response = self.client.post(path, {"email": "toto@tti.fr", "password": "aze"}) |
96 | self.assertIn("error", response.context.keys(), "There should be an error") | 65 | self.assertIn("error", response.context.keys(), "There should be an error") |
97 | self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user") | 66 | self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user") |
98 | 67 | ||
99 | def test_wrong_password(self): | 68 | def test_wrong_password(self): |
100 | - path = "/user_manager/creation_validate" | ||
101 | - response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze", | ||
102 | - "first_name": "toto", "last_name": "titi", "tel": "0123456789", | ||
103 | - "laboratory": "IRAP", "address": "ici"}) | ||
104 | path = "/user_manager/login" | 69 | path = "/user_manager/login" |
105 | response = self.client.post(path, {"email": "toto@titi.fr", "password": "azee"}) | 70 | response = self.client.post(path, {"email": "toto@titi.fr", "password": "azee"}) |
106 | self.assertIn("error", response.context.keys(), "There should be an error") | 71 | self.assertIn("error", response.context.keys(), "There should be an error") |
107 | self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user") | 72 | self.assertNotIn('_auth_user_id', self.client.session, "There shouldn't be an authentified user") |
108 | 73 | ||
109 | def test_logout(self): | 74 | def test_logout(self): |
110 | - path = "/user_manager/creation_validate" | ||
111 | - response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze", "password_confirm": "aze", | ||
112 | - "first_name": "toto", "last_name": "titi", "tel": "0123456789", | ||
113 | - "laboratory": "IRAP", "address": "ici"}) | ||
114 | self.client.login(username="toto@titi.fr", password="aze") | 75 | self.client.login(username="toto@titi.fr", password="aze") |
115 | path = "/user_manager/logout" | 76 | path = "/user_manager/logout" |
116 | self.client.get(path) | 77 | self.client.get(path) |