def test_delete_actors_ca(self): '''Test delete_actors as Casting Assistant ''' actor = Actor(name='Test Actor', age=40, gender='M') actor.insert() res = self.client().delete(f'/actors/{actor.id}', headers={'Authorization': bearer('ca')}) self.assertEqual(res.status_code, 401) actor.delete()
def add_actor(): try: data = json.loads(request.data) actor = Actor(**data) actor.insert() return jsonify({'success': True, 'actor': actor.format()}), 201 except Exception as error: raise error
def test_delete_actors_cd(self): '''Test delete_actors as Casting Director ''' actor = Actor(name='Test Actor', age=40, gender='M') actor.insert() res = self.client().delete(f'/actors/{actor.id}', headers={'Authorization': bearer('cd')}) self.assertEqual(res.status_code, 200) self.assertIsInstance(res.json['deleted'], dict) self.assertEqual(res.json['deleted']['id'], actor.id)
def create_actor(payload): try: actor = Actor(name=request.json['name'], age=request.json['age'], gender=request.json['gender']) actor.insert() return jsonify({'success': True, 'actors': [actor.long()]}), 200 except Exception as e: print(e) abort(422)
def test_patch_actors_ca(self): '''Test patch_actors as Casting Assistant ''' actor = Actor(name='Test Actor', age=40, gender='M') actor.insert() res = self.client().patch(f'/actors/{actor.id}', json={ 'name': 'Test Actorrrr', 'age': 45, 'gender': 'F' }, headers={'Authorization': bearer('ca')}) self.assertEqual(res.status_code, 401) actor.delete()
def test_post_movie_actors_ca(self): '''Test post_actors as Casting Assistant ''' movie = Movie(title='Test Movie', release_date='2020-01-15') movie.insert() actor = Actor(name='Test Actor', age=40, gender='M') actor.insert() res = self.client().post(f'/movies/{movie.id}/actors', json={'actor': { 'id': actor.id }}, headers={'Authorization': bearer('ca')}) self.assertEqual(res.status_code, 401) actor.delete() movie.delete()
def test_post_movie_actors_ep(self): '''Test post_actors as Executive Producer ''' movie = Movie(title='Test Movie', release_date='2020-01-15') movie.insert() actor = Actor(name='Test Actor', age=40, gender='M') actor.insert() res = self.client().post(f'/movies/{movie.id}/actors', json={'actor': { 'id': actor.id }}, headers={'Authorization': bearer('ep')}) self.assertEqual(res.status_code, 200) self.assertIsInstance(res.json["movie"], dict) actor.delete() movie.delete()
def test_delete_movie_actors_cd(self): '''Test delete_actors as Casting Director ''' movie = Movie(title='Test Movie', release_date='2020-01-15') movie.insert() actor = Actor(name='Test Actor', age=40, gender='M') actor.insert() movie.actors.append(actor) movie.update() res = self.client().delete(f'/movies/{movie.id}/actors', json={'actor': { 'id': actor.id }}, headers={'Authorization': bearer('cd')}) self.assertEqual(res.status_code, 200) self.assertIsInstance(res.json["movie"], dict) movie.delete() actor.delete()
def test_patch_actors_cd(self): '''Test patch_actors as Casting Director ''' actor = Actor(name='Test Actor', age=40, gender='M') actor.insert() res = self.client().patch(f'/actors/{actor.id}', json={ 'name': 'Test Actorrrr', 'age': 45, 'gender': 'F' }, headers={'Authorization': bearer('cd')}) self.assertEqual(res.status_code, 200) self.assertIsInstance(res.json["actor"], dict) actor = Actor.query.get(actor.id) self.assertEqual(actor.name, 'Test Actorrrr') self.assertEqual(actor.age, 45) self.assertEqual(actor.gender, 'F') actor.delete()
class ActorsTestCase(unittest.TestCase): """This class represents the Actors test case """ def setUp(self): """Define test variables and initialize app""" self.app = app self.client = self.app.test_client database_path = os.getenv('TEST_DATABASE_URI') setup_db(self.app, database_path) self.actor_id = '' with self.app.app_context(): self.db = SQLAlchemy() self.db.init_app(self.app) self.db.create_all() # Add an actor self.actor = Actor(name="Mugerwa Fred", dob='1996-05-07', gender="male") self.actor.insert() self.actor_id = self.actor.id def tearDown(self): with self.app.app_context(): for movie in Movie.query.all(): movie.delete() self.db.session.query(Actor).delete() self.db.session.commit() def test_get_actors_with_assistant_token(self): response = self.client().get( '/api/v1/actors', headers={'Authorization': f'Bearer {assistant_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['actors'][0].keys(), mock_data.actor.keys()) self.assertEqual(data['total-actors'], 1) def test_get_actors_with_director_token(self): response = self.client().get( '/api/v1/actors', headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['actors'][0].keys(), mock_data.actor.keys()) self.assertEqual(data['total-actors'], 1) def test_get_actors_with_producer_token(self): response = self.client().get( '/api/v1/actors', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['actors'][0].keys(), mock_data.actor.keys()) self.assertEqual(data['total-actors'], 1) def test_get_actors_with_invalid_page_number(self): page = 100 # This page doesn't exist response = self.client().get( f'/api/v1/actors?page={page}', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 404) self.assertEqual(data, mock_data.not_found_error_response) def test_get_actor_with_assistant_token(self): actor = Actor.query.get(self.actor_id) response = self.client().get( f'/api/v1/actors/{self.actor_id}', headers={'Authorization': f'Bearer {assistant_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['actor']['name'], actor.name) self.assertEqual(data['actor']['age'], actor.get_age()) self.assertEqual(data['actor']['gender'], actor.gender) def test_get_actor_with_director_token(self): actor = Actor.query.get(self.actor_id) response = self.client().get( f'/api/v1/actors/{self.actor_id}', headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['actor']['name'], actor.name) self.assertEqual(data['actor']['age'], actor.get_age()) self.assertEqual(data['actor']['gender'], actor.gender) def test_get_actor_with_producer_token(self): actor = Actor.query.get(self.actor_id) response = self.client().get( f'/api/v1/actors/{self.actor_id}', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['actor']['name'], actor.name) self.assertEqual(data['actor']['age'], actor.get_age()) self.assertEqual(data['actor']['gender'], actor.gender) def test_get_actor_with_invalid_actor_id(self): actor_id = 0 # This actor ID doesn't exist response = self.client().get( f'/api/v1/actors/{actor_id}', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 404) self.assertEqual(data, mock_data.not_found_error_response) def test_add_actor_with_assistant_token(self): actor = { 'name': 'Jane Vanfon', 'dob': '1999-01-01', 'gender': 'female' } response = self.client().post( '/api/v1/actors', content_type='application/json', data=json.dumps(actor), headers={'Authorization': f'Bearer {assistant_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 403) self.assertEqual(data, mock_data.forbidden_error_response) def test_add_actor_with_director_token(self): actor = { 'name': 'Jane Vanfon', 'dob': '1999-01-01', 'gender': 'female' } response = self.client().post( '/api/v1/actors', content_type='application/json', data=json.dumps(actor), headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 201) self.assertTrue(data['success']) self.assertEqual(data['actor']['name'], actor['name']) self.assertIsInstance(data['actor']['age'], int) self.assertEqual(data['actor']['gender'], actor['gender']) def test_add_actor_with_producer_token(self): actor = { 'name': 'Jane Vanfon', 'dob': '1999-01-01', 'gender': 'female' } response = self.client().post( '/api/v1/actors', content_type='application/json', data=json.dumps(actor), headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 201) self.assertTrue(data['success']) self.assertEqual(data['actor']['name'], actor['name']) self.assertIsInstance(data['actor']['age'], int) self.assertEqual(data['actor']['gender'], actor['gender']) def test_edit_actor_with_assistant_token(self): actor = {'name': 'James Peters'} response = self.client().patch( f'/api/v1/actors/{self.actor_id}', content_type='application/json', data=json.dumps(actor), headers={'Authorization': f'Bearer {assistant_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 403) self.assertEqual(data, mock_data.forbidden_error_response) def test_edit_actor_with_director_token(self): actor = {'name': 'James Peters'} expected_actor = Actor.query.get(self.actor_id) response = self.client().patch( f'/api/v1/actors/{self.actor_id}', content_type='application/json', data=json.dumps(actor), headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['actor']['name'], expected_actor.name) self.assertEqual(data['actor']['age'], expected_actor.get_age()) self.assertEqual(data['actor']['gender'], expected_actor.gender) def test_edit_actor_with_producer_token(self): actor = {'name': 'James Peters'} expected_actor = Actor.query.get(self.actor_id) response = self.client().patch( f'/api/v1/actors/{self.actor_id}', content_type='application/json', data=json.dumps(actor), headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['actor']['name'], expected_actor.name) self.assertEqual(data['actor']['age'], expected_actor.get_age()) self.assertEqual(data['actor']['gender'], expected_actor.gender) def test_edit_actor_with_invalid_actor_id_in_request(self): actor = {'name': 'James Peters'} actor_id = 0 # This actor ID doesn't exist response = self.client().patch( f'/api/v1/actors/{actor_id}', content_type='application/json', data=json.dumps(actor), headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 422) self.assertEqual(data, mock_data.unprocessable_error_response) def test_delete_actor_with_assistant_token(self): response = self.client().delete( f'/api/v1/actors/{self.actor_id}', headers={'Authorization': f'Bearer {assistant_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 403) self.assertEqual(data, mock_data.forbidden_error_response) def test_delete_actor_with_director_token(self): response = self.client().delete( f'/api/v1/actors/{self.actor_id}', headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['deleted'], self.actor_id) def test_delete_actor_with_producer_token(self): response = self.client().delete( f'/api/v1/actors/{self.actor_id}', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['deleted'], self.actor_id) def test_delete_actor_with_invalid_actor_id(self): actor_id = 0 # This actor ID doesn't exist response = self.client().delete( f'/api/v1/actors/{actor_id}', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 422) self.assertEqual(data, mock_data.unprocessable_error_response)