tests.py 3.75 KB
from django.test import TestCase
from common.models import *
from django.contrib.auth.models import User



class UserManagerTests(TestCase):

    def setUp(self):
        UserLevel.objects.create()
        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")
        self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel")

        self.assertEqual(PyrosUser.objects.all()[0].first_name, 'toto')
        self.assertEqual(PyrosUser.objects.all()[0].email, 'toto@titi.fr')

    def test_login(self):
        self.assertEqual(Country.objects.count(), 1, "There should be 1 Country")
        self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel")

        # 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")
        self.assertEqual(UserLevel.objects.count(), 1, "There should be 1 UserLevel")

        # 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")