def test_401_update_actor_casting_assistant(self): actor = Actor('malek', 23, 'Male') actor.insert() res = self.client().patch('/actor/' + str(actor.id), json={'age': 23}, headers=settingup_auth('casting_assistant')) self.assertEqual(res.status_code, 401)
def test_401_delete_actor_fail(self): actor = Actor('mark', 23, 'Male') actor.insert() res = self.client().delete('/actors/' + str(actor.id), headers=setup_auth('')) self.assertEqual(res.status_code, 401)
def setUp(self): """Define test variables and initialize the app""" self.app = create_app() self.client = self.app.test_client self.executive_producer_jwt = os.environ.get('EXECUTIVE_PRODUCER') self.casting_manager_jwt = os.environ.get('CASTING_MANAGER') self.casting_assistant_jwt = os.environ.get('CASTING_ASSISTANT') self.executive_producer = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(self.executive_producer_jwt) } self.casting_manager = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(self.casting_manager_jwt) } self.casting_assistant = { "Content-Type": "application/json", "Authorization": "Bearer {}".format(self.casting_assistant_jwt) } self.new_actor = Actor(name="Test_Actor", age=45, gender='Male') self.new_movie = Movie(title='Test_Movie', release_date='2016-12-06') # binds the app to the current context with self.app.app_context(): self.db = SQLAlchemy() self.db.init_app(self.app) self.db.create_all
def create_actor(payload): try: request_body = request.get_json() if 'name' not in request_body \ or 'date_of_birth' not in request_body: raise KeyError if request_body['name'] == '' \ or request_body['date_of_birth'] == '': raise ValueError full_name = '' if 'full_name' in request_body: full_name = request_body["full_name"] new_actor = Actor(request_body['name'], full_name, request_body['date_of_birth']) new_actor.insert() return jsonify({ "success": True, "created_actor_id": new_actor.id }), 201 except (TypeError, KeyError, ValueError): abort(422) except Exception: abort(500)
def create_actors(jwt): ''' Creates an actor in the database. Requieres [create:actors] permission. ''' body = request.get_json() name = body.get('name', None) age = body.get('age', None) gender = body.get('gender', None) picture_link = body.get('picture_link', None) bio = body.get('bio', None) if name is None: abort(400) elif age is None: abort(400) elif gender is None: abort(400) elif bio is None: abort(400) try: actor = Actor(name=name, age=age, gender=gender, picture_link=picture_link, bio=bio) actor.insert() return jsonify({'success': True, 'created': actor.id}) except Exception as e: print(e) abort(422)
def setUp(self): """Define test variables and initialize app.""" self.app = app self.client = self.app.test_client self.database_name = "capstone_test" self.database = f"postgresql://*****:*****@localhost/" self.database_path = self.database + self.database_name # setup_db(self.app, self.database_path) # binds the app to the current context with self.app.app_context(): self.app.config["SQLALCHEMY_DATABASE_URI"] = self.database_path self.app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False self.db = db self.db.app = self.app # self.db.init_app(self.app) # create all tables self.db.drop_all() self.db.create_all() with self.app.app_context(): for i in range(10): new_actor = Actor(name='ahmed', age=30, gender='male') new_movie = Movie(title='new movie', start_time='2015-5-6') new_actor.insert() new_movie.insert()
def create_actor(payload): body = request.get_json() if body is None: abort(401) name = body.get("name") age = body.get("age") gender = body.get("gender") movies_ids = body.get("movies_ids", []) if len(movies_ids) == 0: abort(401) try: actor = Actor(name=name, age=age, gender=gender) actor.insert() for movie_id in movies_ids: act_on = Act(movie_id=movie_id, actor_id=actor.id) act_on.insert() return jsonify({"success": True, "created_id": actor.id}) except Exception: abort(422)
def add_actor(token): data = request.get_json() actor_name = data.get('name', None) actor_age = data.get('age', None) actor_gender = data.get('gender', None) if not actor_name or not actor_age or not actor_gender: abort(400) existing_actor = Actor.query.filter( Actor.name == actor_name, Actor.age == actor_age, Actor.gender == actor_gender).one_or_none() if existing_actor: abort(422) try: actor_data = Actor(name=actor_name, age=actor_age, gender=actor_gender) actor_data.insert() return jsonify({ 'success': True, 'actor': actor_data.format() }), 201 except Exception as error: print(error) abort(422)
def add_actor(payload): # Extract relevant details from request. req_data = request.get_json() name = req_data['name'] age = req_data['age'] gender = req_data['gender'] # Check if age is integer if type(age) is int: pass else: abort(422) # Create new newActor = Actor( name=name, age=age, gender=gender ) # Save try: newActor.insert() except: abort(500) finally: # Return actor = newActor.format() return jsonify({ 'success': True, 'actors': actor })
def create_actor(): name, age, sex = get_validated_body_actor() try: actor = Actor(name=name, age=age, sex=sex) actor.insert() except Exception: abort(500, "Internal Error") return jsonify({"success": True, "actor": actor.json()}), 200
def test_403_delete_actor_casting_assistant(self): actor = Actor(name='Maha', age=10, gendar='Female') actor.insert() actorId = actor.id res = self.client().delete(f'/actors/{actor.id}', headers=self.casting_assistant) data = json.loads(res.data) self.assertEqual(res.status_code, 403) self.assertEqual(data['success'], False)
def test_drop_actor_casting_director(self): actor = Actor('malek', 23, 'Male') actor.insert() res = self.client().delete('/actor/' + str(actor.id), headers=settingup_auth('casting_director')) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True) self.assertEqual(int(data['deleted']), actor.id)
def new_actor(jwt): try: data = request.get_json('actor') actor = Actor(name=data['name'], age=data['age'], gender=data['gender']) actor.insert() except Exception: abort(400) return jsonify({'success': True, 'actors': [actor.format()]})
def test_012_delete_actor_by_ID(self): new_actor = Actor(name='one for delete', age='40', gender='female') new_actor.insert() response = self.client().delete( '/actors/{}'.format(new_actor.id), headers={"Authorization": "Bearer {}".format(self.producer_token)}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertEqual(data['success'], True)
def test_update_actor_executive_producer(self): actor = Actor('malek', 23, 'Male') actor.insert() res = self.client().patch('/actor/' + str(actor.id), json={'age': 23}, headers=settingup_auth('executive_producer')) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True) self.assertEqual(actor.age, 23)
def test_update_actor_casting_director(self): actor = Actor('malek', 23, 'Male') actor.insert() res = self.client().patch('/actor/' + str(actor.id), json={'age': 23}, headers=settingup_auth('casting_director')) data = json.loads(res.data) self.assertEqual(res.status_code, 401) self.assertEqual(data['success'], False) self.assertEqual(actor.get_actor()['age'], 23)
def test_422_post_actors_fail(self): actor = Actor('mark', 23, 'Male') actor.insert() res = self.client().post('/actors', json={}, headers=setup_auth('casting_director')) data = json.loads(res.data) self.assertEqual(res.status_code, 422) self.assertEqual(data['success'], False)
def test_delete_actor_executive_producer(self): actor = Actor('mark', 23, 'Male') actor.insert() res = self.client().delete('/actors/' + str(actor.id), headers=setup_auth('executive_producer')) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True) self.assertEqual(int(data['deleted']), actor.id)
def fill_database(): actor_data_1 = Actor(name='new actor_1', age=99, gender="men") actor_data_2 = Actor(name='new actor_2', age=99, gender="men") movie_data_1 = Movie(title='forsaj 9', release_date="2021") movie_data_2 = Movie(title="forsaj 10", release_date="2022") all_data = [actor_data_1, actor_data_2, movie_data_1, movie_data_2] for data in all_data: data.insert()
def test_patch_actor_casting_director(self): actor = Actor('mark', 23, 'Male') actor.insert() res = self.client().patch('/actors/' + str(actor.id), json={'age': 25}, headers=setup_auth('casting_director')) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True) self.assertEqual(actor.get_actor()['age'], 25)
def post_actor(payload): data = request.get_json() try: actor = Actor(name=data['name'], gender=data['gender'], age=data['age']) actor.insert() return jsonify({'success': True, 'actor': actor.format()}), 200 except: abort(422)
def create_actor(payload): print('payload', payload) body = request.get_json() req_name = body.get('name') req_age = body.get('age') req_gender = body.get('gender') actor = Actor(name=req_name, age=req_age, gender=req_gender) actor.insert() return jsonify({'success': True, 'movie': actor.long()})
def create_actor(jwt): data = request.get_json() name = data.get('name') age = data.get('age') gender = data.get('gender') try: actor = Actor(name=name, age=age, gender=gender) actor.insert() return jsonify({ 'success': True, }) except Exception: abort(422)
def add_actor(payload): try: body = request.get_json() name_requested = body.get("name", None) age_requested = body.get("age", None) gender_requested = body.get("gender", None) actor = Actor(name=name_requested, age=age_requested, gender=gender_requested) actor.insert() return jsonify({"success": True, "actor_created": actor.format()}) except: abort(422)
def add_actor(): body = request.get_json() try: new_actor = Actor(body['name'], body['age'], body['gender']) new_actor.create() return jsonify({ "success": True, "status_code": 200, "actor": new_actor.formatted() }) except exc.SQLAlchemyError: abort(409)
def create_actor(jwt): try: data = request.get_json() actor = Actor(name=data.get('name'), age=data.get('age'), gender=data.get('gender')) actor.insert() return jsonify({'success': True, 'actors': actor.format()}), 200 except BaseException: abort(422)
def test_update_actor_casting_director(self): actor = Actor(name='Maha', age=50, gendar='Female') actor.insert() actorId = actor.id res = self.client().patch(f'/actors/{actor.id}', json={ 'name': 'EDIT name', 'age': 10, 'gendar': 'female' }, headers=self.casting_director) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True)
def test_delete_actor(self): new_actor = Actor( name="Njoud", gender="female", age=25 ) new_actor.insert() res = self.client().delete(f'/actors/{new_actor.id}', headers={ 'Authorization': executive_producer_token }) data = json.loads(res.data) self.assertEqual(res.status_code, 200) self.assertEqual(data['success'], True)
def post_actor(payload): if request.method == "POST": body = request.get_json() name = body.get('name', None) age = body.get('age', None) gender = body.get('gender', None) actor = Actor(name=name, age=age, gender=gender) actor.insert() return jsonify({ 'success': True, 'created_actor': actor.name, 'total_actors': len(Actor.query.all()) })
def add_actor(jwt): body = request.get_json() req_name = body.get('name') req_age = body.get('age') req_gender = body.get('gender') if not req_name: abort(422) actor = Actor(name=req_name, age=req_age, gender=req_gender) actor.insert() return jsonify({ 'success': True, 'actors': actor.format() })