def test_set_hidden_state(self): review = db_review.create( user_id=self.user.id, entity_id="e7aad618-fa86-3983-9e77-405e21796eca", entity_type="release_group", text="Awesome", is_draft=False, license_id=self.license["id"], ) db_review.set_hidden_state(review["id"], is_hidden=True) review = db_review.get_by_id(review["id"]) self.assertEqual(review["is_hidden"], True) db_review.set_hidden_state(review["id"], is_hidden=False) review = db_review.get_by_id(review["id"]) self.assertEqual(review["is_hidden"], False)
def create(review_id, text=None, rating=None): """Creates a new revision for the given review. Args: review_id (uuid): ID of the review. text (str): Updated/New text part of the review. rating (int): Updated/New rating part of the review """ if text is None and rating is None: raise db_exceptions.BadDataException("Text part and rating part of a revision can not be None simultaneously") if rating not in VALID_RATING_VALUES: raise ValueError("{} is not a valid rating value. It must be on the scale 1-5".format(rating)) # Convert ratings to values on a scale 0-100 rating = RATING_SCALE_0_100.get(rating) with db.engine.connect() as connection: connection.execute(sqlalchemy.text(""" INSERT INTO revision(review_id, timestamp, text, rating) VALUES (:review_id, :timestamp, :text, :rating) """), { "review_id": review_id, "timestamp": datetime.now(), "text": text, "rating": rating, }) # Update average rating if rating part of the review has changed review = db_review.get_by_id(review_id) rev_num = get_revision_number(review["id"], review["last_revision"]["id"]) if rev_num > 1: revisions = get(review["id"], limit=2, offset=0) if revisions[0]["rating"] != revisions[1]["rating"]: db_avg_rating.update(review["entity_id"], review["entity_type"]) elif rating is not None: db_avg_rating.update(review["entity_id"], review["entity_type"])
def get_review_or_404(review_id): """Get a review using review ID or raise error 404.""" try: review = db_review.get_by_id(review_id) except db_exceptions.NoDataFoundException: raise NotFound("Can't find a review with ID: {review_id}".format(review_id=review_id)) return review
def get_review_or_404(review_id): """Get a review using review ID or raise error 404""" try: review = db_review.get_by_id(review_id) except db_exceptions.NoDataFoundException: raise NotFound("Can't find a review with ID: {review_id}".format(review_id=review_id)) return review
def test_list_reports(self): db_spam_report.create(self.revision_id, self.user2.id, "This is a report") db_review.update( review_id=self.review["id"], drafted=self.review["is_draft"], text="Updated Review", ) self.review = db_review.get_by_id(self.review["id"]) db_spam_report.create(self.review["last_revision"]["id"], self.user1.id, "This is again a report on the updated review") # two reports on the old revision and one on the new revision. reports, count = db_spam_report.list_reports( review_id=self.review["id"]) # pylint: disable=unused-variable self.assertEqual(count, 3) # get all reports by a user. reports, count = db_spam_report.list_reports(user_id=self.user2.id) self.assertEqual(count, 1) # archive and include all archived reports. # there must be two reports including the archived one. db_spam_report.archive(self.user1.id, self.review["last_revision"]["id"]) reports, count = db_spam_report.list_reports(inc_archived=True) self.assertEqual(count, 3) # there must be one reports excluding the archived one. reports, count = db_spam_report.list_reports(inc_archived=False) self.assertEqual(count, 2)
def update_rating(review_id): # Update average rating if rating part of the review has changed review = db_review.get_by_id(review_id) rev_num = get_revision_number(review["id"], review["last_revision"]["id"]) if rev_num > 1: revisions = get(review["id"], limit=2, offset=0) if revisions[0]["rating"] != revisions[1]["rating"]: db_avg_rating.update(review["entity_id"], review["entity_type"]) else: db_avg_rating.update(review["entity_id"], review["entity_type"])
def test_get_revision_number(self): """Test to get the revision number of a revision of a specified review.""" rev_num = revision.get_revision_number(self.review["id"], self.review["last_revision"]["id"]) self.assertEqual(rev_num, 1) db_review.update( review_id=self.review["id"], drafted=self.review["is_draft"], text="Updated this review", ) self.review = db_review.get_by_id(self.review["id"]) rev_num = revision.get_revision_number(self.review["id"], self.review["last_revision"]["id"]) self.assertEqual(rev_num, 2)
def test_get_revision_number(self): """Test to get the revision number of a revision of a specified review.""" rev_num = revision.get_revision_number( self.review["id"], self.review["last_revision"]["id"]) self.assertEqual(rev_num, 1) db_review.update( review_id=self.review["id"], drafted=self.review["is_draft"], text="Updated this review", ) self.review = db_review.get_by_id(self.review["id"]) rev_num = revision.get_revision_number( self.review["id"], self.review["last_revision"]["id"]) self.assertEqual(rev_num, 2)
def test_list_reports(self): db_spam_report.create(self.revision_id, self.user2.id, "This is a report") db_review.update( review_id=self.review["id"], drafted=self.review["is_draft"], text="Updated Review", ) self.review = db_review.get_by_id(self.review["id"]) db_spam_report.create(self.review["last_revision"]["id"], self.user1.id, "This is again a report on the updated review") # two reports on the old revision and one on the new revision. reports, count = db_spam_report.list_reports(review_id=self.review["id"]) # pylint: disable=unused-variable self.assertEqual(count, 3) # get all reports by a user. reports, count = db_spam_report.list_reports(user_id=self.user2.id) self.assertEqual(count, 1) # archive and include all archived reports. # there must be two reports including the archived one. db_spam_report.archive(self.user1.id, self.review["last_revision"]["id"]) reports, count = db_spam_report.list_reports(inc_archived=True) self.assertEqual(count, 3) # there must be one reports excluding the archived one. reports, count = db_spam_report.list_reports(inc_archived=False) self.assertEqual(count, 2)
def test_hide_unhide(self): # create a review by self.user and check it's not hidden review = self.create_dummy_review() self.assertEqual(review["is_hidden"], False) # make self.hacker as current user self.temporary_login(self.hacker) # check that hide button is not visible to non-admin user response = self.client.get("/review/{}".format(review["id"])) self.assert200(response) self.assertNotIn("Hide this review", str(response.data)) # make self.hacker as admin User.is_admin = MagicMock(return_value=True) # check that hide button is visible to admin response = self.client.get("/review/{}".format(review["id"])) self.assert200(response) self.assertIn("Hide this review", str(response.data)) # hide the review response = self.client.post( "review/{}/hide".format(review["id"]), data=dict(reason="Test hiding review."), follow_redirects=True, ) self.assertIn("Review has been hidden.", str(response.data)) review = db_review.get_by_id(review["id"]) self.assertEqual(review["is_hidden"], True) # hiding already hidden review flashes message response = self.client.post( "review/{}/hide".format(review["id"]), data=dict(reason="Test hiding already hidden review."), follow_redirects=True, ) self.assertIn("Review is already hidden.", str(response.data)) # check that unhide button is visible to admin response = self.client.get("/review/{}".format(review["id"])) self.assert200(response) self.assertIn("Unhide this review", str(response.data)) # unhide review response = self.client.post( "review/{}/unhide".format(review["id"]), data=dict(reason="Test unhiding a hidden review."), follow_redirects=True, ) self.assertIn("Review is not hidden anymore.", str(response.data)) review = db_review.get_by_id(review["id"]) self.assertEqual(review["is_hidden"], False) # unhide a non-hidden review response = self.client.post( "review/{}/unhide".format(review["id"]), data=dict(reason="Test unhiding an already visible review."), follow_redirects=True, ) self.assertIn("Review is not hidden.", str(response.data))
def test_update(self): review = db_review.create( user_id=self.user.id, entity_id="e7aad618-fa86-3983-9e77-405e21796eca", entity_type="release_group", text="Testing", rating=5, is_draft=True, license_id=self.license["id"], ) another_license = db_license.create( id="License-2", full_name="Another License", ) # Update review to only rating db_review.update( review_id=review["id"], drafted=review["is_draft"], rating=4, is_draft=False, license_id=another_license["id"], language="es", ) # Checking if contents are updated retrieved_review = db_review.list_reviews()[0][0] self.assertEqual(retrieved_review["text"], None) self.assertEqual(retrieved_review["rating"], 4) self.assertFalse(retrieved_review["is_draft"]) self.assertEqual(retrieved_review["license_id"], another_license["id"]) self.assertEqual(retrieved_review["language"], "es") # Update review to only text db_review.update( review_id=review["id"], drafted=review["is_draft"], text="Testing update", is_draft=False, license_id=another_license["id"], language="es", ) # Checking if contents are updated retrieved_review = db_review.list_reviews()[0][0] self.assertEqual(retrieved_review["text"], "Testing update") self.assertEqual(retrieved_review["rating"], None) # Updating should create a new revision. revisions = db_revision.get(retrieved_review["id"], limit=None) self.assertEqual(len(revisions), 3) self.assertEqual(revisions[0]["timestamp"], retrieved_review["last_revision"]["timestamp"]) self.assertEqual(revisions[0]["text"], retrieved_review["text"]) self.assertEqual(revisions[0]["rating"], retrieved_review["rating"]) # Checking things that shouldn't be allowed with self.assertRaises(db_exceptions.BadDataException): db_review.update( review_id=retrieved_review["id"], drafted=retrieved_review["is_draft"], text="Sucks!", license_id=self.license["id"], ) review = db_review.get_by_id(review["id"]) with self.assertRaises(db_exceptions.BadDataException): db_review.update( review_id=review["id"], drafted=review["is_draft"], text="Sucks!", is_draft=True, )