def test_delete_movies_ca(self): '''Test delete_movies as Casting Assistant ''' movie = Movie(title='Test Movie', release_date='2020-01-15') movie.insert() res = self.client().delete(f'/movies/{movie.id}', headers={'Authorization': bearer('ca')}) self.assertEqual(res.status_code, 401) movie.delete()
def add_movie(): try: data = json.loads(request.data) movie = Movie(**data) movie.insert() return jsonify({'success': True, 'movie': movie.format()}), 201 except Exception as error: raise error
def create_movie(payload): try: movie = Movie(title=request.json['title'], release_date=request.json['release_date']) movie.insert() return jsonify({'success': True, 'movies': [movie.short()]}), 200 except Exception as e: print(e) abort(401)
def test_delete_movies_ep(self): '''Test delete_movies as Executive Producer ''' movie = Movie(title='Test Movie', release_date='2020-01-15') movie.insert() res = self.client().delete(f'/movies/{movie.id}', headers={'Authorization': bearer('ep')}) self.assertEqual(res.status_code, 200) self.assertIsInstance(res.json['deleted'], dict) self.assertEqual(res.json['deleted']['id'], movie.id)
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_patch_movies_cd(self): '''Test patch_movies as Casting Director ''' movie = Movie(title='Test Movie', release_date='2020-01-15') movie.insert() res = self.client().patch(f'/movies/{movie.id}', json={ 'title': 'Test Movieeee', 'release_date': '1920-01-15' }, headers={'Authorization': bearer('cd')}) self.assertEqual(res.status_code, 200) self.assertIsInstance(res.json['movie'], dict) movie = Movie.query.get(movie.id) self.assertEqual(movie.title, 'Test Movieeee') self.assertEqual(movie.release_date, '1920-01-15') 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()
class MoviesTestCase(unittest.TestCase): """This class represents the Movies 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.movie_id = '' with self.app.app_context(): self.db = SQLAlchemy() self.db.init_app(self.app) self.db.create_all() # Add a movie self.movie = Movie(title='The Hatchet', release_date=date(2020, 12, 11)) self.movie.insert() self.movie_id = self.movie.id def tearDown(self): with self.app.app_context(): for actor in Actor.query.all(): actor.delete() self.db.session.query(Movie).delete() self.db.session.commit() def test_get_movies_with_assistant_token(self): release_date = date(2020, 12, 11) movie = { 'title': 'The Hatchet', 'release_date': release_date.isoformat() } response = self.client().get( '/api/v1/movies?page=1', headers={'Authorization': f'Bearer {assistant_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['movies'][0]['title'], movie['title']) self.assertEqual(data['total-movies'], 1) def test_get_movies_with_director_token(self): release_date = date(2020, 12, 11) movie = { 'title': 'The Hatchet', 'release_date': release_date.isoformat() } response = self.client().get( '/api/v1/movies?page=1', headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['movies'][0]['title'], movie['title']) self.assertEqual(data['total-movies'], 1) def test_get_movies_with_producer_token(self): release_date = date(2020, 12, 11) movie = { 'title': 'The Hatchet', 'release_date': release_date.isoformat() } response = self.client().get( '/api/v1/movies?page=1', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['movies'][0]['title'], movie['title']) self.assertEqual(data['total-movies'], 1) def test_get_movies_with_invalid_page_number(self): page = 100 # This page doesn't exist response = self.client().get( f'/api/v1/movies?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_movie_with_assistant_token(self): response = self.client().get( f'/api/v1/movies/{self.movie_id}', headers={'Authorization': f'Bearer {assistant_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) def test_get_movie_with_director_token(self): response = self.client().get( f'/api/v1/movies/{self.movie_id}', headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) def test_get_movie_with_producer_token(self): response = self.client().get( f'/api/v1/movies/{self.movie_id}', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) def test_get_movie_with_invalid_movie_id(self): movie_id = 0 # invalid movie ID response = self.client().get( f'/api/v1/movies/{movie_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_movie_with_assistant_token(self): release_date = date(2020, 2, 9) movie = { 'title': 'Above The Storm', 'release_date': release_date.isoformat() } response = self.client().post( '/api/v1/movies', content_type='application/json', data=json.dumps(movie), 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_movie_with_director_token(self): release_date = date(2020, 2, 9) movie = { 'title': 'Above The Storm', 'release_date': release_date.isoformat() } response = self.client().post( '/api/v1/movies', content_type='application/json', data=json.dumps(movie), headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 403) self.assertEqual(data, mock_data.forbidden_error_response) def test_add_movie_with_producer_token(self): release_date = date(2020, 2, 9) movie = { 'title': 'Above The Storm', 'release_date': release_date.isoformat() } response = self.client().post( '/api/v1/movies', content_type='application/json', data=json.dumps(movie), headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 201) self.assertTrue(data['success']) self.assertEqual(data['movie']['title'], movie['title']) def test_edit_movie_with_assistant_token(self): release_date = date(2020, 2, 9) movie = {'release_date': release_date.isoformat()} response = self.client().patch( f'/api/v1/movies/{self.movie_id}', content_type='application/json', data=json.dumps(movie), 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_movie_with_director_token(self): release_date = date(2020, 2, 9) movie = {'release_date': release_date.isoformat()} response = self.client().patch( f'/api/v1/movies/{self.movie_id}', content_type='application/json', data=json.dumps(movie), headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['movie']['id'], self.movie_id) def test_edit_movie_with_producer_token(self): release_date = date(2020, 2, 9) movie = {'release_date': release_date.isoformat()} response = self.client().patch( f'/api/v1/movies/{self.movie_id}', content_type='application/json', data=json.dumps(movie), headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue(data['success']) self.assertEqual(data['movie']['id'], self.movie_id) def test_edit_movie_with_invalid_movie_id(self): movie_id = 0 # invalid movie ID release_date = date(2020, 2, 9) movie = {'release_date': release_date.isoformat()} response = self.client().patch( f'/api/v1/movies/{movie_id}', content_type='application/json', data=json.dumps(movie), 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_movie_with_assistant_token(self): response = self.client().delete( f'/api/v1/movies/{self.movie_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_movie_with_director_token(self): response = self.client().delete( f'/api/v1/movies/{self.movie_id}', headers={'Authorization': f'Bearer {director_token}'}) data = json.loads(response.data) print("Response in director delete ", response.data) self.assertEqual(response.status_code, 403) self.assertEqual(data, mock_data.forbidden_error_response) def test_delete_movie_with_producer_token(self): response = self.client().delete( f'/api/v1/movies/{self.movie_id}', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 200) self.assertTrue('success') self.assertEqual(data['deleted'], self.movie_id) def test_delete_movie_with_invalid_id(self): movie_id = 0 # invalid movie_id response = self.client().delete( f'/api/v1/movies/{movie_id}', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) print("Response in delete invalid id", response.data) self.assertEqual(response.status_code, 422) self.assertEqual(data, mock_data.unprocessable_error_response) def test_405_error_response(self): # there is no PATCH /api/v1/movies endpoint response = self.client().patch( '/api/v1/movies', headers={'Authorization': f'Bearer {producer_token}'}) data = json.loads(response.data) self.assertEqual(response.status_code, 405) self.assertEqual(data, mock_data.not_allowed_error_response)