def test_missing_object(self): rf = RequestFactory() expected = HttpResponse("Missing object", status=404) request = rf.post("get_article", data="", content_type="application/json") response = get_article.handler(request) self.assertEqual(str(response), str(expected)) request = rf.post("get_article", data=json.dumps({}), content_type="application/json") response = get_article.handler(request) self.assertEqual(str(response), str(expected))
def test_null_or_empty_website_name(self): rf = RequestFactory() expected = HttpResponse("Invalid arguments", status=400) request = rf.post("get_article", data=json.dumps( {"object": { "url": "url", "website_name": None }}), content_type="application/json") response = get_article.handler(request) self.assertEqual(str(response), str(expected)) request = rf.post("get_article", data=json.dumps( {"object": { "url": "url", "website_name": "" }}), content_type="application/json") response = get_article.handler(request) self.assertEqual(str(response), str(expected))
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 test_get_article_website_already_present(self): rf = RequestFactory() expected = JsonResponse({ "article": { "url": "url", "name": "article_name", "legit_reports": 0, "fake_reports": 0 }, "website": { "name": "website_name" } }) expected_article = { "url": "url", "name": "article_name", "legit_reports": 0, "fake_reports": 0 } website = Website(id=utils.hash_digest("website_name"), name="website_name", legit_articles=3, fake_articles=1) website.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(expected_article, json.loads(response.content)["article"]) self.assertEquals(website.as_dict(), json.loads(response.content)["website"])
def test_get_article(self): rf = RequestFactory() expected = JsonResponse({ "article": { "url": "url", "name": "article_name", "legit_reports": 0, "fake_reports": 0 }, "website": { "name": "website_name" } }) 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))
def test_invalid_token(self): utils.check_google_token = Mock(return_value=(False, "Error message")) expected = HttpResponse("Error message", status=403) response = get_article.handler(HttpRequest()) self.assertEqual(str(response), str(expected))
def test_invalid_user(self): expected = HttpResponse("Missing user", status=404) utils.check_google_token = Mock(return_value=(True, "uid")) response = get_article.handler(HttpRequest()) self.assertEqual(str(response), str(expected))