<reponame>ShivanS93/nutrify
from django.urls import reverse

from users.tests.test_setup import TestSetUp

# from users.models import CustomUser


class TestSignUpForms(TestSetUp):
    """
    Testing the User Sign up forms
    """

    def test_login_normal(self):
        """
        Testing the login of a normal user
        """
        test_login_data = {
            "username": "test_user_normal",
            "password": self.<PASSWORD>,
        }
        response = self.client.post(reverse("login"), test_login_data)
        assert response.status_code == 302
        assert response.url == reverse("food-image-upload-view")

    def test_login_not_user(self):
        """
        A user that does not exist cannot login
        """
        test_login_data = {
            "username": "bogus",
            "password": <PASSWORD>,
        }
        response = self.client.post(reverse("login"), test_login_data)
        err = response.context["form"].errors
        assert response.status_code == 200
        assert (
            err["__all__"][0]
            == "Please enter a correct username and password. Note that both fields may be case-sensitive."
        )

    def test_login_wrong_password(self):
        """
        A correct username is given but login fails due to wrong password
        """
        test_login_data = {
            "username": "test_user_normal",
            "password": "<PASSWORD>!",
        }
        response = self.client.post(reverse("login"), test_login_data)
        err = response.context["form"].errors
        assert response.status_code == 200
        assert (
            err["__all__"][0]
            == "Please enter a correct username and password. Note that both fields may be case-sensitive."
        )

    def test_signup_normal(self):
        """
        Testing as normal user sign up and this will be successful
        they will be redirected to the login page
        """
        test_signup_data = {
            "username": "TestSignupUser",
            "email": "<EMAIL>",
            "password1": self.<PASSWORD>,
            "password2": <PASSWORD>,
        }
        response = self.client.post(reverse("signup"), test_signup_data)
        assert response.status_code == 302
        assert response.url == reverse("login")

    def test_signup_user_exists(self):
        """
        Checking signup fails, no redirect and error message is given
        """
        test_signup_data = {
            "username": "test_user_normal",
            "email": "<EMAIL>",
            "password1": self.TEST_PASSWORD + "x",
            "password2": self.TEST_PASSWORD + "x",
        }
        response = self.client.post(reverse("signup"), test_signup_data)
        assert response.status_code == 200
        assert response.context["form"].errors == {
            "username": ["User with this Username already exists."]
        }

        # TODO: write password reset test

    def test_password_reset_exists(self):
        """
        Checking password reset with a valid email
        Set up imagclient?
        """
        # url name = password_change
        # url success = password_change_done
        pass

    def test_password_reset_not_exist(self):
        """
        Checking password reset with a non valid email
        """
        # response = self.client.post(
        #    reverse("password_reset"), {"email": "<EMAIL>"}
        # )
        pass
        # assert response.status_code == 200
        # assert response.context["form"].errors == {"email": ["brah"]}

from django.urls import reverse

from users.tests.test_setup import TestSetUp

# from users.models import CustomUser


class TestSignUpForms(TestSetUp):
    """
    Testing the User Sign up forms
    """

    def test_login_normal(self):
        """
        Testing the login of a normal user
        """
        test_login_data = {
            "username": "test_user_normal",
            "password": self.<PASSWORD>,
        }
        response = self.client.post(reverse("login"), test_login_data)
        assert response.status_code == 302
        assert response.url == reverse("food-image-upload-view")

    def test_login_not_user(self):
        """
        A user that does not exist cannot login
        """
        test_login_data = {
            "username": "bogus",
            "password": <PASSWORD>,
        }
        response = self.client.post(reverse("login"), test_login_data)
        err = response.context["form"].errors
        assert response.status_code == 200
        assert (
            err["__all__"][0]
            == "Please enter a correct username and password. Note that both fields may be case-sensitive."
        )

    def test_login_wrong_password(self):
        """
        A correct username is given but login fails due to wrong password
        """
        test_login_data = {
            "username": "test_user_normal",
            "password": "<PASSWORD>!",
        }
        response = self.client.post(reverse("login"), test_login_data)
        err = response.context["form"].errors
        assert response.status_code == 200
        assert (
            err["__all__"][0]
            == "Please enter a correct username and password. Note that both fields may be case-sensitive."
        )

    def test_signup_normal(self):
        """
        Testing as normal user sign up and this will be successful
        they will be redirected to the login page
        """
        test_signup_data = {
            "username": "TestSignupUser",
            "email": "<EMAIL>",
            "password1": self.<PASSWORD>,
            "password2": <PASSWORD>,
        }
        response = self.client.post(reverse("signup"), test_signup_data)
        assert response.status_code == 302
        assert response.url == reverse("login")

    def test_signup_user_exists(self):
        """
        Checking signup fails, no redirect and error message is given
        """
        test_signup_data = {
            "username": "test_user_normal",
            "email": "<EMAIL>",
            "password1": self.TEST_PASSWORD + "x",
            "password2": self.TEST_PASSWORD + "x",
        }
        response = self.client.post(reverse("signup"), test_signup_data)
        assert response.status_code == 200
        assert response.context["form"].errors == {
            "username": ["User with this Username already exists."]
        }

        # TODO: write password reset test

    def test_password_reset_exists(self):
        """
        Checking password reset with a valid email
        Set up imagclient?
        """
        # url name = password_change
        # url success = password_change_done
        pass

    def test_password_reset_not_exist(self):
        """
        Checking password reset with a non valid email
        """
        # response = self.client.post(
        #    reverse("password_reset"), {"email": "<EMAIL>"}
        # )
        pass
        # assert response.status_code == 200
        # assert response.context["form"].errors == {"email": ["brah"]}
# from users.models import CustomUser Testing the User Sign up forms Testing the login of a normal user A user that does not exist cannot login A correct username is given but login fails due to wrong password Testing as normal user sign up and this will be successful         they will be redirected to the login page Checking signup fails, no redirect and error message is given # TODO: write password reset test Checking password reset with a valid email         Set up imagclient? # url name = password_change # url success = password_change_done Checking password reset with a non valid email # response = self.client.post( #    reverse("password_reset"), {"email": "<EMAIL>"} # ) # assert response.status_code == 200 # assert response.context["form"].errors == {"email": ["brah"]}