from django.contrib.auth import get_user_model
from django.test import TestCase
from django.urls import reverse

from rest_framework.test import APIClient
from rest_framework import status

CREATE_USER_URL=reverse('user:create')
TOKEN_URL=reverse('user:token')

def create_user(**params):
    return get_user_model().objects.create_user(**params)


class PublicUserApiTest(TestCase):

    def setUp(self):
        self.client=APIClient()

    def test_create_valid_user_succesful(self):

        payload={
            'email':'test@yahoo.com',
            'name':'test',
            'password':'test021111'
        }    

        res=self.client.post(CREATE_USER_URL,payload)

        self.assertEqual(res.status_code,status.HTTP_201_CREATED)
        user=get_user_model().objects.get(**res.data)
        self.assertTrue(user.check_password(payload['password']))
        self.assertNotIn('password',res.data)

    def test_user_exists(self):

        payload={
            'email':'test2@yahoo.com',
            'name':'test',
            'password':'test021111'
        }    
        create_user(**payload)

        res=self.client.post(CREATE_USER_URL,payload)

        self.assertEqual(res.status_code,status.HTTP_400_BAD_REQUEST)

    def test_password_too_short(self):
        
        payload={
            'email':'test2@yahoo.com',
            'name':'test',
            'password':'tes'
        }
        
        res=self.client.post(CREATE_USER_URL,payload)

        self.assertEqual(res.status_code,status.HTTP_400_BAD_REQUEST)

        user_exist=get_user_model().objects.filter(email=payload['email']).exists()

        self.assertFalse(user_exist)
        
    def test_create_user_token(self):

        payload={
            'email':'test@yahoo.com',
            'password':'test1222'
        }    
        create_user(**payload)

        res=self.client.post(TOKEN_URL,payload)

        self.assertIn('token',res.data)
        self.assertEqual(res.status_code,status.HTTP_200_OK)

    def test_create_token_invalid_credentials(self):

        create_user(email='test@yahoo.com',password='testpasss')

        payload={
            'email':'test@yahoo.com',
            'password':'pass'
        }

        res=self.client.post(TOKEN_URL,payload)

        self.assertNotIn('token',res.data)
        self.assertEqual(res.status_code,status.HTTP_400_BAD_REQUEST)

    def test_create_token_user_doesnot_exist(self):

        payload={
            'email':'test@yahoo.com',
            'password':'pass'
        }    

        res=self.client.post(TOKEN_URL,payload)
        self.assertNotIn('token',res.data)
        self.assertEqual(res.status_code,status.HTTP_400_BAD_REQUEST)

    def test_create_token_missing_field(self):

        payload={
            'email':'test@yahoo.com',
            'password':''
        }   
        res=self.client.post(TOKEN_URL,payload)
        self.assertNotIn('token',res.data)
        self.assertEqual(res.status_code,status.HTTP_400_BAD_REQUEST)







        
