import os
import unittest
import json
from flask_sqlalchemy import SQLAlchemy
import logging
import sys
from flaskapp import create_app
from models import setup_db, Movies, Roles, Actors


class CastingAgencyUnitTest(unittest.TestCase):
    """This class represents the unit test case"""
    def setUp(self):
        """Define test variables and initialize app."""
        self.app = create_app()
        self.client = self.app.test_client
        self.database_name = "castingagency_test"
        self.database_path = "postgresql://{}/{}".format('udacity:udacity@localhost:5432', self.database_name)
        setup_db(self.app, self.database_path)
        self.tokenCastAssistant = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlJCbVJDamJXcldadktmWlFPS0k4SyJ9.eyJpc3MiOiJodHRwczovL2ZzbmRlci5ldS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWY2Yjk1NDU1ZGY0YmEwMDY5MWRjNDhkIiwiYXVkIjoiY2FzdGluZ2FnZW5jeWFwaSIsImlhdCI6MTYwMjQxNzg4NiwiZXhwIjoxNjAyNTA0Mjg2LCJhenAiOiIydFRIdk5HUWxoSW9zcERyRkttVGVQeXhKcjFIRE9zRiIsImd0eSI6InBhc3N3b3JkIiwicGVybWlzc2lvbnMiOlsiZ2V0OmFjdG9ycyIsImdldDptb3ZpZXMiXX0.ZANEOvoNCbzlDg0bjxspha53Un_zbpCR-vh6L_At2MjHz8r6f0pGZbXXnQDM5vGtkH35G1ydTV6oTyVDftOdZf36CVelZ8DT18iMDGczkTqz7l-JPmaJ7hxgqQGiI-GL4K84Sm-MegXEuinldDaLSiOhPeZ-hEsiIK6WEZxaU9S8eifXZvI_i8c1EywhU_Ya4Y_jaXZPWqhyEeH3WYUi_AQc5GLlAdbqKgQBAUlqxoUioznCiPq7H2Aussxuwb0Z0jrAAmviXhfFuY6XaSOz6tZ_USrGO3vcrtOKfpyS_9CxTRJmFsUggWO54O8JOYtzQ5o_aQFdHc_5gbo3Eoueqw'
        self.tokenCastDirector = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlJCbVJDamJXcldadktmWlFPS0k4SyJ9.eyJpc3MiOiJodHRwczovL2ZzbmRlci5ldS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWY2Yjk1NmRhNmFmNjQwMDcxZDNmODEzIiwiYXVkIjoiY2FzdGluZ2FnZW5jeWFwaSIsImlhdCI6MTYwMjQxODAxNCwiZXhwIjoxNjAyNTA0NDE0LCJhenAiOiIydFRIdk5HUWxoSW9zcERyRkttVGVQeXhKcjFIRE9zRiIsImd0eSI6InBhc3N3b3JkIiwicGVybWlzc2lvbnMiOlsiZGVsZXRlOmFjdG9ycyIsImdldDphY3RvcnMiLCJnZXQ6bW92aWVzIiwicGF0Y2g6YWN0b3JzIiwicGF0Y2g6bW92aWVzIiwicGF0Y2g6cm9sZXMiLCJwb3N0OmFjdG9ycyJdfQ.Oqf_YgPDW2WajbwCgSA2ufIPdhcbAqVIHJNslRfpSDR9vsqdKhpdcHpZQ6L61V_SMDNKM0ZIW-SP6X-gR6F91RA4mOMqQCrejcuZua_7zGqKmnQrkqPMBfpr2ObqkbB2HYsjk1M0s3Htl2qUKdwIzP1_Z1N_LCXI35DhtpSe850JmpH5mqC1zrF1wdaL9gHLkX3XPlEniiaPu1zcp9uv85seXyzR8wnABNcrnj73WzMMErnEE-iTZj_apPgOco_hZiZ1fi2qVUMkt788muAcTgMliY5pMu_l_48lxxZf6EPIEKHyGTgiQqzFfAMIXxtwt-PI2kB_k1Jcft_ESLyWQQ'
        self.tokenExecutiveProducer = 'eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlJCbVJDamJXcldadktmWlFPS0k4SyJ9.eyJpc3MiOiJodHRwczovL2ZzbmRlci5ldS5hdXRoMC5jb20vIiwic3ViIjoiYXV0aDB8NWY4MmYyNTIxMGVlNWEwMDY4MmEzZTA3IiwiYXVkIjoiY2FzdGluZ2FnZW5jeWFwaSIsImlhdCI6MTYwMjQxODA0MCwiZXhwIjoxNjAyNTA0NDQwLCJhenAiOiIydFRIdk5HUWxoSW9zcERyRkttVGVQeXhKcjFIRE9zRiIsImd0eSI6InBhc3N3b3JkIiwicGVybWlzc2lvbnMiOlsiZGVsZXRlOmFjdG9ycyIsImRlbGV0ZTptb3ZpZXMiLCJnZXQ6YWN0b3JzIiwiZ2V0Om1vdmllcyIsInBhdGNoOmFjdG9ycyIsInBhdGNoOm1vdmllcyIsInBhdGNoOnJvbGVzIiwicG9zdDphY3RvcnMiLCJwb3N0Om1vdmllcyIsInBvc3Q6cm9sZXMiXX0.YxOTgvM1ZvmdY9lGJ6lYcjAKWmoLFQuKXOd2GAr0Im2z763nn-OMyRnxMRItnGAd60GRDAclkCfZge_VO-fKeDp37J_dzfUNhYeXbhwNL-vZ9_anT9isv7IkO81HS2J0crcRrHlPnGP2WBMDhLJMCHVOg_EfEj7hXOCANDIytnv9YlkrxethAsME_nIYCAHjHXn8quJLxz9eByqKs93cdZf5HTQjibfg3i-2O0m62qwoqFiQZPf98nwExnfnDqd-zvweZC8UwzagBxYyo09SgFVg7KwdkSuztCfOV9pGnLCLKeZAg0kAGiFl78h3KghYz9V1S1Fq10yfB4q13b3gjw'

        # binds the app to the current context
        with self.app.app_context():
            self.db = SQLAlchemy()
            self.db.init_app(self.app)
            # create all tables
            self.db.create_all()    
    def tearDown(self):
        """Executed after reach test"""
        pass

    """
    Tests for API Endpoints
    """
    def test_get_movies_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_movies_pass")
        res = self.client().get('/movies',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_get_movies_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_movies_fails")
        res = self.client().get('/movies')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])            
    def test_get_movies_pagination_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_movies_pagination_pass")
        res = self.client().get('/movies?page=1',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_get_movies_pagination_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_movies_pagination_fail")
        res = self.client().get('/movies?page=1000',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],404) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])   
    def test_get_movies_actors_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_movies_actors_pass")
        res = self.client().get('/movies/1/actors',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_get_movies_actors_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_movies_actors_fails")
        res = self.client().get('/movies/1/actors')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])  
    def test_get_actors_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_actors_pass")
        res = self.client().get('/actors',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_get_actors_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_actors_fails")
        res = self.client().get('/actors')
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])            
    def test_get_actors_pagination_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_actors_pagination_pass")
        res = self.client().get('/actors?page=1',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_get_actors_pagination_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_actors_pagination_pass")
        res = self.client().get('/actors?page=1000',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)

        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],404) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])        

    def test_search_movies_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_search_movies")
        res = self.client().post('/movies/search', json={'searchTerm': 'The'},headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_search_movies_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_search_movies")
        res = self.client().post('/movies/search', json={'searchTerm': 'The'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])  
    def test_search_actors_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_search_actors_pass")
        res = self.client().post('/actors/search', json={'searchName': 'M'},headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_search_actors_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_search_actors_fail")
        res = self.client().post('/actors/search', json={'searchName': 'M'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 

    def test_create_movie_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_movies_pass")
        res = self.client().post('/movies', json={'name': 'New Movie','timestamp': 1523443804, 'genres': ['Comedy','Romantic']},headers={'Authorization': f'Bearer {self.tokenExecutiveProducer}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 201)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_create_movie_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_movie_fail")
        res = self.client().post('/movies', json={'name': 'New Movie'},headers={'Authorization': f'Bearer {self.tokenExecutiveProducer}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],422) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 
    def test_create_movie_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_movie_fail")
        res = self.client().post('/movies', json={'name': 'New Movie'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])   
    def test_create_actor_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_actor_pass")
        res = self.client().post('/actors', json={'name': 'New Actor','gender': 'Male', 'age': 26},headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 201)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_create_actor_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_actor_fail")
        res = self.client().post('/actors', json={'name': 'New Actor'},headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],422) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 
    def test_create_actor_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_actor_fail")
        res = self.client().post('/actors', json={'name': 'New Actor'},headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])
    def test_create_role_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_role_pass")
        res = self.client().post('/roles', json={'name': 'New Role','type': 'Lead Actor', 'movie': 1},headers={'Authorization': f'Bearer {self.tokenExecutiveProducer}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 201)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_create_role_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_role_fail")
        res = self.client().post('/roles', json={'name': 'New Role'},headers={'Authorization': f'Bearer {self.tokenExecutiveProducer}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],422) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 
    def test_create_role_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_create_role_fail")
        res = self.client().post('/roles', json={'name': 'New Role'},headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])     

    def test_read_movie_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_read_movies_pass")
        res = self.client().get('/movies/1',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_read_movie_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_read_movies_pass")
        res = self.client().get('/movies/1000',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data) 
        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],404) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])   
    def test_read_movie_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_read_movies_pass")
        res = self.client().get('/movies/1')
        data = json.loads(res.data) 
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 
    def test_read_actor_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_read_actors_pass")
        res = self.client().get('/actors/1',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_read_actor_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_read_actors_fail")
        res = self.client().get('/actors/1000',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data) 
        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],404) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 
    def test_read_actor_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_read_actors_fail")
        res = self.client().get('/actors/1')
        data = json.loads(res.data) 
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])         

    def test_update_movie_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_movies_pass")
        res = self.client().patch('/movies/1', json={'name': 'The ring'},headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_update_movie_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_movies_fail")
        res = self.client().patch('/movies/1', json={'wrongname': 'Witched'},headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)   
        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],422) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])   
    def test_update_movie_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_movies_fail")
        res = self.client().patch('/movies/1', json={'wrongname': 'Witched'},headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)   
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 
    def test_update_actor_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_actor_pass")
        res = self.client().patch('/actors/1', json={'name': 'Young Actor'},headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_update_actor_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_actor_fail")
        res = self.client().patch('/actors/1', json={'wrongname': 'New Actor'},headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],422) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 
    def test_update_actor_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_actor_fail")
        res = self.client().patch('/actors/1', json={'wrongname': 'New Actor'},headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance']) 
    def test_update_role_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_role_pass")
        res = self.client().patch('/roles/1', json={'name': 'The Master'},headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
    def test_update_role_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_role_fail")
        res = self.client().patch('/roles/1', json={'wrongname': 'New Role'},headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 422)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],422) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])
    def test_update_role_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_update_role_fail")
        res = self.client().patch('/roles/1', json={'wrongname': 'New Role'},headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)    
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])
    
    def test_delete_movie_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_delete_movie_pass")
        movies = Movies.query.all()
        mid = movies[-1].id

        res = self.client().delete(f'/movies/{mid}',headers={'Authorization': f'Bearer {self.tokenExecutiveProducer}'})
        data = json.loads(res.data)

        movie = Movies.query.get(mid)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
        self.assertEqual(movie,None)
    def test_delete_movie_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_delete_movie_fail")
        res = self.client().delete('/movies/200',headers={'Authorization': f'Bearer {self.tokenExecutiveProducer}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],404) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])
    def test_delete_movie_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_delete_movie_fail")
        res = self.client().delete('/movies/1',headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])
    def test_delete_actor_pass(self):
        log = logging.getLogger("TestLog")
        log.debug("test_delete_actor_pass")
        actors = Actors.query.all()
        mid = actors[-1].id

        res = self.client().delete(f'/actors/{mid}',headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)

        actor = Actors.query.get(mid)

        self.assertEqual(res.status_code, 200)
        self.assertEqual(data['success'],True)
        self.assertTrue(data['data'])
        self.assertEqual(actor,None)
    def test_delete_movie_fail(self):
        log = logging.getLogger("TestLog")
        log.debug("test_delete_actor_fail")
        res = self.client().delete('/actors/200',headers={'Authorization': f'Bearer {self.tokenCastDirector}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 404)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],404) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])    
    def test_delete_movie_fail_auth(self):
        log = logging.getLogger("TestLog")
        log.debug("test_delete_actor_fail")
        res = self.client().delete('/actors/1',headers={'Authorization': f'Bearer {self.tokenCastAssistant}'})
        data = json.loads(res.data)
        self.assertEqual(res.status_code, 401)
        self.assertEqual(data['success'],False)
        self.assertEqual(data['status'],401) 
        self.assertTrue(data['type'])
        self.assertTrue(data['title'])
        self.assertTrue(data['detail'])
        self.assertTrue(data['instance'])      

# Make the tests conveniently executable
if __name__ == "__main__":
    logging.basicConfig(stream=sys.stderr, level=logging.DEBUG)
    unittest.main()