def test_submit_same_report_twice(self): rf = RequestFactory() user = User(id="uid", name="name", email="*****@*****.**", weight=10) user.save() website_id = utils.hash_digest("website_name") website = Website(id=website_id, name="website_name") website.save() article_id = utils.hash_digest("article_url") article = Article(id=article_id, url="article_url", website=website) article.save() expected = HttpResponse("Created", status=201) request = rf.post( "submit_report", data=json.dumps({"object": { "url": "article_url", "report": "L" }}), content_type="application/json") response = submit_report.handler(request) self.assertEqual(str(response), str(expected)) self.assertEqual( Report.objects.get(user=user, article=article).value, Report.Values.L.name) self.assertEquals( Article.objects.get(id=article_id).get_status(), Article.Status.L) request = rf.post( "submit_report", data=json.dumps({"object": { "url": "article_url", "report": "F" }}), content_type="application/json") response = submit_report.handler(request) self.assertEqual(str(response), str(expected)) self.assertEqual( Report.objects.get(user=user, article=article).value, Report.Values.F.name) self.assertEquals( Article.objects.get(id=article_id).get_status(), Article.Status.F) request = rf.post( "submit_report", data=json.dumps({"object": { "url": "article_url", "report": "L" }}), content_type="application/json") response = submit_report.handler(request) self.assertEqual(str(response), str(expected)) self.assertEqual( Report.objects.get(user=user, article=article).value, Report.Values.L.name) self.assertEquals( Article.objects.get(id=article_id).get_status(), Article.Status.L)
def test_submit_report_change_status_legit_to_fake(self): rf = RequestFactory() user = User(id="uid", name="name", email="*****@*****.**", weight=50) user.save() user2 = User(id="uid2", name="name2", email="*****@*****.**", weight=10) user2.save() user3 = User(id="uid3", name="name3", email="*****@*****.**", weight=30) user3.save() website_id = utils.hash_digest("website_name") website = Website(id=website_id, name="website_name", legit_articles=1) website.save() article_id = utils.hash_digest("article_url") article = Article(id=article_id, url="article_url", website=website, legit_reports=30.0, fake_reports=10.0) article.save() report1 = Report(user=user2, article=article, value=Report.Values.F.name) report1.save() report2 = Report(user=user3, article=article, value=Report.Values.L.name) report2.save() expected = HttpResponse("Created", status=201) request = rf.post( "submit_report", data=json.dumps({"object": { "url": "article_url", "report": "F" }}), content_type="application/json") response = submit_report.handler(request) self.assertEqual(str(response), str(expected)) self.assertEquals( Article.objects.get(id=article_id).get_status(), Article.Status.F) self.assertEquals( Website.objects.get(id=website_id).legit_percentage(), 0.00)
def test_get_article_with_report(self): rf = RequestFactory() expected = JsonResponse({ "article": { "url": "url", "name": "article_name", "legit_reports": 0, "fake_reports": 0 }, "website": { "name": "website_name" }, "report": { "user_id": "uid", "article": "url", "value": "L" } }) website = Website(id=utils.hash_digest("website_name"), name="website_name") website.save() article = Article(id=utils.hash_digest("url"), url="url", name="article_name", website=website) article.save() user = User.objects.get(id="uid") report = Report(user=user, article=article, value=Report.Values.L.name) report.save() request = rf.post("get_article", data=json.dumps({ "object": { "url": "url", "website_name": "website_name" } }), content_type="application/json") response = get_article.handler(request) self.assertEqual(str(response), str(expected)) self.assertEquals(article.as_dict(), json.loads(response.content)["article"]) self.assertEquals(website.as_dict(), json.loads(response.content)["website"]) self.assertEquals(report.as_dict(), json.loads(response.content)["report"])
def handler(request): """ Returns the article with the input url :param request: the HTTP request :return: HttpResponse 403 if the verification token is invalid HttpResponse 404 if the object is invalid HttpResponse 400 if the arguments of the object are invalid JsonResponse 200 with body (article, website) as JSON if the article has been created """ is_token_valid, message = utils.check_google_token(request) if not is_token_valid: log.error(LOGGING_TAG + message) return HttpResponse(message, status=403) user_id = message user = utils.get_user_by_id(user_id) if not user: log.error(LOGGING_TAG + "Missing user") return HttpResponse("Missing user", status=404) is_object_present, object_json = utils.get_object(request) if not is_object_present: log.error(LOGGING_TAG + object_json["error"]) return HttpResponse(object_json["error"], status=404) url, website_name = object_json["url"], object_json["website_name"] if not url or not website_name: log.error(LOGGING_TAG + "Invalid arguments") return HttpResponse("Invalid arguments", status=400) article_id = utils.hash_digest(url) qs = Article.objects.filter(id=article_id) if len(qs) == 0: article, website = add_article(article_id, url, website_name) else: article = qs[0] # here I use get because I'm sure that the website exists website = Website.objects.get(id=article.website.id) qs = Report.objects.filter(user=user, article=article) if len(qs) != 0: report = qs[0] data = { "article": article.as_dict(), "website": website.as_dict(), "report": report.as_dict() } else: data = {"article": article.as_dict(), "website": website.as_dict()} return JsonResponse(data, status=200)
def test_get_article_already_present(self): rf = RequestFactory() expected = JsonResponse({ "article": { "url": "url", "name": "article_name", "legit_reports": 0, "fake_reports": 0 }, "website": { "name": "website_name" } }) website = Website(id=utils.hash_digest("website_name"), name="website_name") website.save() article = Article(id=utils.hash_digest("url"), url="url", name="article_name", website=website) article.save() request = rf.post("get_article", data=json.dumps({ "object": { "url": "url", "website_name": "website_name" } }), content_type="application/json") response = get_article.handler(request) self.assertEqual(str(response), str(expected)) self.assertEquals(article.as_dict(), json.loads(response.content)["article"]) self.assertEquals(website.as_dict(), json.loads(response.content)["website"])
def test_submit_report(self): rf = RequestFactory() user = User(id="uid", name="name", email="*****@*****.**") user.save() website_id = utils.hash_digest("website_name") website = Website(id=website_id, name="website_name") website.save() article_id = utils.hash_digest("article_url") article = Article(id=article_id, url="article_url", website=website) article.save() expected = HttpResponse("Created", status=201) request = rf.post( "submit_report", data=json.dumps({"object": { "url": "article_url", "report": "L" }}), content_type="application/json") response = submit_report.handler(request) self.assertEqual(str(response), str(expected))
def add_article(article_id, url, website_name): """ Adds an article to the database :param article_id: the article id :param url: the article url :param website_name: the article's website name :return: the tuple (Article, Website) """ url = url article_name = utils.parse_article_name_from_url(url) website_id = utils.hash_digest(website_name) qs = Website.objects.filter(id=website_id) website = add_website(website_id, website_name) if len(qs) == 0 else qs[0] article = Article(id=article_id, url=url, name=article_name, website=website) article.save() log.info(LOGGING_TAG + "Article " + article_name + " created") return article, website
def handler(request): """ Adds a report to the system :param request: the HTTP request :return: HttpResponse 403 if the verification token is invalid HttpResponse 404 if the object is invalid HttpResponse 400 if the arguments of the object are invalid JsonResponse 201 if the article has been created """ is_token_valid, message = utils.check_google_token(request) if not is_token_valid: log.error(LOGGING_TAG + message) return HttpResponse(message, status=403) user_id = message user = utils.get_user_by_id(user_id) if not user: log.error(LOGGING_TAG + "Missing user") return HttpResponse("Missing user", status=404) is_object_present, object_json = utils.get_object(request) if not is_object_present: log.error(LOGGING_TAG + object_json["error"]) return HttpResponse(object_json["error"], status=404) article_url, report = object_json["url"], object_json["report"] if report == "L": report_value = Report.Values.L elif report == "F": report_value = Report.Values.F else: log.error(LOGGING_TAG + "Invalid report value") return HttpResponse("Invalid report value", status=400) qs = Article.objects.filter(id=utils.hash_digest(article_url)) if len(qs) == 0: log.error(LOGGING_TAG + "Article not found") return HttpResponse("Article not found", status=404) article = qs[0] previous_status = article.get_status() qs = Report.objects.filter(user=user, article=article) if len(qs) == 0: report = Report(user=user, article=article, value=report_value.name) user.n_reports += 1 user.save() # in the case the report was not present before, we only have to add the weight of the user to the # corresponding value if report_value == Report.Values.L: article.legit_reports += user.weight else: article.fake_reports += user.weight else: report = qs[0] old_report_value = report.value report.value = report_value.name # in case the report was already present, we take its old value and update the numbers of legit # and fake reports for the article accordingly if old_report_value != report.value: if report.value == Report.Values.L.name: article.fake_reports -= user.weight article.legit_reports += user.weight else: article.legit_reports -= user.weight article.fake_reports += user.weight report.save() log.info(LOGGING_TAG + "Report with user " + user.email + " and article " + article.url + " created") article.save() updated_status = article.get_status() if previous_status != updated_status: change_article_status(report, article, previous_status, updated_status) return HttpResponse("Created", status=201)