Added a test that checks the mailarch search failure return, and tweaks the no-result return handling.

- Legacy-Id: 15745
This commit is contained in:
Henrik Levkowetz 2018-11-10 19:06:40 +00:00
parent 236ee7639d
commit 0d9c97c5ca
3 changed files with 27 additions and 7 deletions

View file

@ -525,6 +525,22 @@ class ReviewTests(TestCase):
self.assertEqual(messages[1]["subject"], "Review of {}".format(review_req.doc.name))
self.assertEqual(messages[1]["splitfrom"], ["John Doe II", "johndoe2@example.com"])
self.assertEqual(messages[1]["utcdate"][0], "")
# Test failure to return mailarch results
no_result_path = os.path.join(self.review_dir, "mailarch_no_result.html")
with open(no_result_path, "w") as f:
f.write('Content-Type: text/html\n\n<html><body><div class="xtr"><div class="xtd no-results">No results found</div></div>')
ietf.review.mailarch.construct_query_urls = lambda review_req, query=None: { "query_data_url": "file://" + os.path.abspath(no_result_path) }
url = urlreverse('ietf.doc.views_review.search_mail_archive', kwargs={ "name": doc.name, "request_id": review_req.pk })
r = self.client.get(url)
self.assertEqual(r.status_code, 200)
result = json.loads(r.content)
self.assertNotIn('messages', result)
self.assertIn('No results found', result['error'])
finally:
ietf.review.mailarch.construct_query_urls = real_fn

View file

@ -703,11 +703,10 @@ def search_mail_archive(request, name, request_id):
try:
res["messages"] = mailarch.retrieve_messages(res["query_data_url"])[:MAX_RESULTS]
except KeyError as e:
res["error"] = "No results found"
except Exception as e:
if unicode(e) == "NONE":
res["error"] = "No results found"
else:
res["error"] = "Retrieval from mail archive failed: %s" % unicode(e)
res["error"] = "Retrieval from mail archive failed: %s" % unicode(e)
# raise # useful when debugging
return JsonResponse(res)

View file

@ -4,7 +4,9 @@
import datetime, tarfile, mailbox, tempfile, hashlib, base64, email.utils
import urllib
import urllib2, contextlib
import re
import debug # pyflakes:ignore
from pyquery import PyQuery
from django.conf import settings
@ -94,8 +96,11 @@ def retrieve_messages(query_data_url):
content_type = fileobj.info()["Content-type"]
if not content_type.startswith("application/x-tar"):
if content_type.startswith("text/html"):
if not re.search("no-results", fileobj.read(20000)) is None:
raise Exception("NONE")
r = fileobj.read(20000)
q = PyQuery(r)
div = q('div[class~="no-results"]')
if div:
raise KeyError("No results: %s -> %s" % (query_data_url, div.text(), ))
raise Exception("Export failed - this usually means no matches were found")
with tarfile.open(fileobj=fileobj, mode='r|*') as tar: