示例#1
0
    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()
示例#2
0
    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})
示例#3
0
    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))
示例#4
0
    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))
示例#5
0
    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()
示例#6
0
    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))
示例#7
0
    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)
示例#8
0
    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
示例#9
0
    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)
示例#10
0
    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)
示例#11
0
    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)
示例#12
0
    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))
示例#13
0
    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()
示例#14
0
    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)
示例#15
0
    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
示例#16
0
    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
示例#17
0
 def dummy_review(cls):
     return ReviewsModel(9780553803716, 1, 5, "This book is so good!")