from django.test import TestCase from common.models import * from django.contrib.auth.models import User from pprint import pprint class UserManagerTests(TestCase): def setUp(self): UserLevel.objects.create() Country.objects.create() def test_creation(self): 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(User.objects.count(), 1, "There should be one User") self.assertEqual(PyrosUser.objects.count(), 1, "There should be one PyrosUser") def test_login(self): 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"}) path = "/user_manager/login" response = self.client.post(path, {"email": "toto@titi.fr", "password": "aze"}) """ Note that we use .has_key() because the login phase change the httpresponse context """ self.assertTrue(response.context.has_key("success"), "There should be a success") self.assertIn('_auth_user_id', self.client.session, "The user should be logged in") def test_wrong_email(self): 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"}) 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/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"}) 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): 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.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")