def test_invalid_article(self): rf = RequestFactory() user = User(id="uid", name="name", email="*****@*****.**") user.save() expected = HttpResponse("Article not found", status=404) request = rf.post("submit_report", data=json.dumps( {"object": { "url": None, "report": "L" }}), content_type="application/json") response = submit_report.handler(request) self.assertEqual(str(response), str(expected)) expected = HttpResponse("Article not found", status=404) request = rf.post("submit_report", data=json.dumps( {"object": { "url": "not_found", "report": "L" }}), content_type="application/json") response = submit_report.handler(request) self.assertEqual(str(response), str(expected))
def test_invalid_type(self): rf = RequestFactory() user = User(id="uid", name="name", email="*****@*****.**") user.save() expected = HttpResponse("Wrong leaderboard type", status=400) request = rf.post("get_leaderboard", data=json.dumps({"object": { "type": None }}), content_type="application/json") response = get_leaderboard.handler(request) self.assertEqual(str(response), str(expected)) request = rf.post("get_leaderboard", data=json.dumps({"object": { "type": "" }}), content_type="application/json") response = get_leaderboard.handler(request) self.assertEqual(str(response), str(expected)) request = rf.post("get_leaderboard", data=json.dumps({"object": { "type": "INVALID" }}), content_type="application/json") response = get_leaderboard.handler(request) self.assertEqual(str(response), str(expected))
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_friendship(self): rf = RequestFactory() user1 = User.objects.get(id="uid") user2 = User(id="uid2", name="name2", email="*****@*****.**") user2.save() expected = HttpResponse("Created", status=201) request = rf.post("add_friend", data=json.dumps( {"object": { "friend_email": "*****@*****.**" }}), content_type="application/json") response = add_friend.handler(request) self.assertEqual(str(response), str(expected)) friendship = Friendship.objects.get(user=user1, friend=user2) self.assertTrue(friendship.user == user1 and friendship.friend == user2)
def handler(request): """ Adds a user to the database :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 HttpResponse 201 if the user has been created HttpResponse 200 if the user was already present """ 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 user: log.info(LOGGING_TAG + "User already present") return HttpResponse("User already present", status=200) 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) name, email = object_json["name"], object_json[ "email"] if not name or not utils.check_email(email): log.error(LOGGING_TAG + "Invalid arguments") return HttpResponse("Invalid arguments", status=400) user = User(id=user_id, name=name, email=email) user.save() log.info(LOGGING_TAG + "User " + user.email + " created") return HttpResponse("User added", status=201)
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 test_submit_report_change_status_fake_to_undefined(self): rf = RequestFactory() user = User(id="uid", name="name", email="*****@*****.**", weight=10) user.save() user2 = User(id="uid2", name="name2", email="*****@*****.**", weight=10) user2.save() website_id = utils.hash_digest("website_name") website = Website(id=website_id, name="website_name", fake_articles=1) website.save() article_id = utils.hash_digest("article_url") article = Article(id=article_id, url="article_url", website=website, legit_reports=0.0, fake_reports=10.0) article.save() report1 = Report(user=user2, article=article, value=Report.Values.F.name) report1.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.assertEquals( Article.objects.get(id=article_id).get_status(), Article.Status.U) self.assertEquals( Website.objects.get(id=website_id).legit_percentage(), 1.00)
def test_leaderboard(self): rf = RequestFactory() expected = JsonResponse( { "user_position": 2, "leaderboard": [{ "name": "user2", "score": 12 }, { "name": "user1", "score": 8 }] }, status=200) user1 = User(id="uid", name="user1", email="*****@*****.**", n_reports=4, weight=2.0) user1.save() user2 = User(id="uid2", name="user2", email="*****@*****.**", n_reports=4, weight=2.3) user2.save() request = rf.post("get_leaderboard", data=json.dumps({"object": { "type": "GLOBAL" }}), content_type="application/json") response = get_leaderboard.handler(request) self.assertEqual(str(response), str(expected))
def setUpClass(cls): cls.cls_atomics = cls._enter_atomics() # mocks the google_token check User(id="uid", name="name", email="*****@*****.**").save() utils.check_google_token = Mock(return_value=(True, "uid"))