from django.test import TestCase
from rest_framework.test import APIClient
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework import status


TOKEN_URL = reverse('user:token')
USER_ME = reverse('user:me')

def create_user(**payload):
    user = get_user_model().objects.create_user(**payload)
    return user
    
class PublicUserApiTest(TestCase):
    
    def setUp(self):
        self.client = APIClient()

    def test_create_token_for_user(self):
        """test that a token is created for the user"""
        payload = {'email':'<EMAIL>','password':'<PASSWORD>'}
        create_user(**payload)
        response = self.client.post(TOKEN_URL, payload)
        self.assertIn('token',response.data)

    def test_create_token_invalid_credentials(self):
        """Test that token is not created if invalid credentials are given"""
        create_user(email='<EMAIL>', password='<PASSWORD>')
        payload = {'email': '<EMAIL>', 'password': '<PASSWORD>'}
        response = self.client.post(TOKEN_URL, payload)

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

    def test_create_token_no_user(self):
        """Test that token is not created if user doens't exist"""
        payload = {'email': '<EMAIL>', 'password': '<PASSWORD>'}
        response = self.client.post(TOKEN_URL, payload)

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

    def test_create_token_missing_field(self):
        """Test that email and password are required"""
        response = self.client.post(
            TOKEN_URL, {'email': 'one', 'password': ''})
        self.assertNotIn('token', response.data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
    
        def test_retrive_user_unauthorised(self):
            """test that authentication is required"""
        response = self.client.get(USER_ME)

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
    

from django.test import TestCase
from rest_framework.test import APIClient
from django.contrib.auth import get_user_model
from django.urls import reverse
from rest_framework import status


TOKEN_URL = reverse('user:token')
USER_ME = reverse('user:me')

def create_user(**payload):
    user = get_user_model().objects.create_user(**payload)
    return user
    
class PublicUserApiTest(TestCase):
    
    def setUp(self):
        self.client = APIClient()

    def test_create_token_for_user(self):
        """test that a token is created for the user"""
        payload = {'email':'<EMAIL>','password':'<PASSWORD>'}
        create_user(**payload)
        response = self.client.post(TOKEN_URL, payload)
        self.assertIn('token',response.data)

    def test_create_token_invalid_credentials(self):
        """Test that token is not created if invalid credentials are given"""
        create_user(email='<EMAIL>', password='<PASSWORD>')
        payload = {'email': '<EMAIL>', 'password': '<PASSWORD>'}
        response = self.client.post(TOKEN_URL, payload)

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

    def test_create_token_no_user(self):
        """Test that token is not created if user doens't exist"""
        payload = {'email': '<EMAIL>', 'password': '<PASSWORD>'}
        response = self.client.post(TOKEN_URL, payload)

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

    def test_create_token_missing_field(self):
        """Test that email and password are required"""
        response = self.client.post(
            TOKEN_URL, {'email': 'one', 'password': ''})
        self.assertNotIn('token', response.data)
        self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
    
        def test_retrive_user_unauthorised(self):
            """test that authentication is required"""
        response = self.client.get(USER_ME)

        self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)
    

test that a token is created for the user Test that token is not created if invalid credentials are given Test that token is not created if user doens't exist Test that email and password are required test that authentication is required