def test_model_add_review_of_non_existing_user(self): with self.app.app_context(): book = self.dummy_book() book.save_to_db() review = ReviewsModel( book.isbn, 1, 5, "This is a extensive review of how this book is so good!") with self.assertRaises(Exception): review.save_to_db()
def test_update_review(self): with self.app.app_context(): self.basic_setup() review = ReviewsModel(self.book.isbn, self.user.id, 3, "test") review.save_to_db() data = {"score": 1, "review": "test review"} res = self.client.put(f"/api/review/{self.user.id}/{self.book.isbn}", data=data, headers={ "Authorization": 'Basic ' + base64.b64encode((self.token + ":").encode('ascii')).decode('ascii') }) self.assertEqual(200, res.status_code) # Checks all items have been updated self.assertDictEqual(data, {k: v for k, v in json.loads(res.data)["review"].items() if k in data})
def test_model_add_review(self): with self.app.app_context(): book = self.dummy_book() book.save_to_db() user = self.dummy_user() user.save_to_db() review = ReviewsModel( book.isbn, user.id, 5, "This is a extensive review of how this book is so good!") review.save_to_db() self.assertEqual( review, ReviewsModel.find_by_isbn_user_id(review.isbn, review.user_id))
def test_post_review(self): with self.app.app_context(): book = self.dummy_book() book.save_to_db() user = self.dummy_user() user.save_to_db() token = self.login_dummy_user(self.client) parameters = { 'isbn': book.isbn, 'email': user.email, 'score': 5, 'review': "This book is good!" } res = self.client.post( "/api/review", data=parameters, headers={ "Authorization": 'Basic ' + base64.b64encode( (token + ":").encode('ascii')).decode('ascii') }) self.assertEqual(201, res.status_code) self.assertEqual( ReviewsModel.find_by_isbn_user_id(book.isbn, user.id).json(), json.loads(res.data))
def test_model_add_review_with_wrong_score(self): with self.app.app_context(): book = self.dummy_book() book.save_to_db() user = self.dummy_user() user.save_to_db() with self.assertRaises(ValueError): ReviewsModel(book.isbn, user.id, 0, "Wow such book should not exist").save_to_db() with self.assertRaises(ValueError): ReviewsModel(book.isbn, user.id, -5, "Go to hell you BOOK!").save_to_db() with self.assertRaises(ValueError): ReviewsModel(book.isbn, user.id, 10, "This shit is like crack!").save_to_db()
def test_delete_review(self): with self.app.app_context(): self.basic_setup() review = ReviewsModel(self.book.isbn, self.user.id, 3, "test") review.save_to_db() res = self.client.delete( f"/api/review/{self.user.id}/{self.book.isbn}", headers={ "Authorization": 'Basic ' + base64.b64encode( (self.token + ":").encode('ascii')).decode('ascii') }) self.assertEqual(200, res.status_code) self.assertEqual( None, ReviewsModel.find_by_isbn_user_id(self.book.isbn, self.user.id))
def test_model_update_several_score(self): with self.app.app_context(): self.basic_setup() user2 = UsersModel("test2", "test2") user2.hash_password("test2") user2.save_to_db() review = ReviewsModel(self.book.isbn, self.user.id, 2) review.save_to_db() review2 = ReviewsModel(self.book.isbn, user2.id, 4) review2.save_to_db() data = {"score": 3} review.update_from_db(data) self.assertEqual(data["score"], review.score) self.assertEqual((data["score"] + review2.score)/2, ScoresModel.find_by_isbn(self.book.isbn).score)
def post(self): data = parse_review() with lock: user = UsersModel.find_by_email(data['email']) check_user_and_book(user, data['isbn']) if data['score'] < 1 or data['score'] > 5: return { "message": f"{data['score']} is not a valid value for score. Score must be an integer ranging " f"from 1 to 5." }, 418 if ReviewsModel.find_by_isbn_user_id(data.isbn, user.id) is not None: return { "message": "Given user already posted a review. Did you meant to update it?" }, 403 data['user_id'] = user.id del data['email'] try: review = ReviewsModel(**data) review.save_to_db() except Exception as e: return {"message": str(e)}, 500 return review.json(), 201
def test_model_score_remove_non_existing(self): with self.app.app_context(): book = self.dummy_book() book.save_to_db() user = self.dummy_user() user.save_to_db() review = ReviewsModel( book.isbn, user.id, 5, "This is a extensive review of how this book is so good!") with self.assertRaises(Exception): ScoresModel.remove_review(review)
def test_model_update_all_parameters(self): with self.app.app_context(): self.basic_setup() review = ReviewsModel(self.book.isbn, self.user.id, 2) review.save_to_db() data = {"score": 3, "review": "test review"} review.update_from_db(data) # Checks all items have been updated self.assertDictEqual(data, {k: v for k, v in review.json().items() if k in data}) self.assertEqual(data["score"], ScoresModel.find_by_isbn(self.book.isbn).score)
def test_model_score_add(self): with self.app.app_context(): book = self.dummy_book() book.save_to_db() user = self.dummy_user() user.save_to_db() review = ReviewsModel( book.isbn, user.id, 5, "This is a extensive review of how this book is so good!") ScoresModel.add_review(review) score = ScoresModel.find_by_isbn(review.isbn) self.assertEqual(review.score, score.score)
def test_delete_review_other_user(self): with self.app.app_context(): self.basic_setup() user2 = UsersModel("test2", "test2") user2.hash_password("test2") user2.save_to_db() review = ReviewsModel(self.book.isbn, user2.id, 3, "test") review.save_to_db() res = self.client.delete( f"/api/review/{user2.id}/{self.book.isbn}", headers={ "Authorization": 'Basic ' + base64.b64encode( (self.token + ":").encode('ascii')).decode('ascii') }) self.assertEqual(401, res.status_code) # Checks not modified self.assertEqual( review, ReviewsModel.find_by_isbn_user_id(self.book.isbn, user2.id))
def test_model_add_review_duplicate(self): with self.app.app_context(): book = self.dummy_book() book.save_to_db() user = self.dummy_user() user.save_to_db() review = ReviewsModel( book.isbn, user.id, 5, "This is a extensive review of how this book is so good!") review.save_to_db() second_review = ReviewsModel( book.isbn, user.id, 5, "Omo! This book is so good I want to rate it twice.") with self.assertRaises(Exception): second_review.save_to_db()
def test_model_update_one_score(self): with self.app.app_context(): self.basic_setup() review = ReviewsModel(self.book.isbn, self.user.id, 2) review.save_to_db() data = {"score": 3} review.update_from_db(data) self.assertEqual(data["score"], review.score) self.assertEqual(data["score"], ScoresModel.find_by_isbn(self.book.isbn).score)
def put(self, user_id, isbn): data = parse_review(False) with lock: user = UsersModel.find_by_id(user_id) check_user_and_book(user, isbn) review = ReviewsModel.find_by_isbn_user_id(isbn, user_id) if review is None: return { "message": "Given user hasn't posted a review yet. Did you meant to post it?" }, 404 try: review.update_from_db(data) except Exception as e: return {"message": str(e)}, 500 return {"review": review.json()}, 200
def delete(self, user_id, isbn): with lock: user = UsersModel.find_by_id(user_id) check_user_and_book(user, isbn, True) review = ReviewsModel.find_by_isbn_user_id(isbn, user_id) if review is None: return { "message": "Given user hasn't posted a review yet. Did you meant to post it?" }, 404 try: review.delete_from_db() except Exception as e: return {"message": str(e)}, 500 return { "message": f"Review with ['user_id': {user_id}, 'isbn': {isbn}] deleted" }, 200
def dummy_review(cls): return ReviewsModel(9780553803716, 1, 5, "This book is so good!")