test_classes: Extract home view helpers for reuse.

This commit is contained in:
Tim Abbott 2020-10-01 15:14:25 -07:00
parent 6d041a3b34
commit 8c8f3ee13b
2 changed files with 20 additions and 17 deletions

View File

@ -8,6 +8,7 @@ from contextlib import contextmanager
from typing import Any, Dict, Iterable, Iterator, List, Optional, Sequence, Set, Tuple, Union
from unittest import TestResult, mock
import lxml.html
import orjson
from django.apps import apps
from django.conf import settings
@ -444,6 +445,24 @@ Output:
result = self.client_post("/json/bots", bot_info)
self.assert_json_error(result, assert_json_error_msg)
def _get_page_params(self, result: HttpResponse) -> Dict[str, Any]:
"""Helper for parsing page_params after fetching the webapp's home view."""
doc = lxml.html.document_fromstring(result.content)
[div] = doc.xpath("//div[@id='page-params']")
page_params_json = div.get("data-params")
page_params = orjson.loads(page_params_json)
return page_params
def check_rendered_logged_in_app(self, result: HttpResponse) -> None:
"""Verifies that a visit of / was a 200 that rendered page_params
and not for a logged-out web-public visitor."""
self.assertEqual(result.status_code, 200)
page_params = self._get_page_params(result)
# It is important to check `is_web_public_guest` to verify
# that we treated this request as a normal logged-in session,
# not as a web-public visitor.
self.assertEqual(page_params['is_web_public_guest'], False)
def login_with_return(self, email: str, password: Optional[str]=None,
**kwargs: Any) -> HttpResponse:
if password is None:

View File

@ -1,10 +1,9 @@
import calendar
import urllib
from datetime import timedelta
from typing import Any, Dict
from typing import Any
from unittest.mock import patch
import lxml.html
import orjson
from django.conf import settings
from django.http import HttpResponse
@ -218,14 +217,6 @@ class HomeTest(ZulipTestCase):
"zulip_version",
]
def check_rendered_logged_in_app(self, result: HttpResponse) -> None:
self.assertEqual(result.status_code, 200)
page_params = self._get_page_params(result)
# It is important to check `is_web_public_guest` to verify
# that we treated this request as a normal logged-in session,
# not as a web-public visitor.
self.assertEqual(page_params['is_web_public_guest'], False)
def test_home(self) -> None:
# Keep this list sorted!!!
html_bits = [
@ -374,13 +365,6 @@ class HomeTest(ZulipTestCase):
result = self.client_get('/', dict(**kwargs))
return result
def _get_page_params(self, result: HttpResponse) -> Dict[str, Any]:
doc = lxml.html.document_fromstring(result.content)
[div] = doc.xpath("//div[@id='page-params']")
page_params_json = div.get("data-params")
page_params = orjson.loads(page_params_json)
return page_params
def _sanity_check(self, result: HttpResponse) -> None:
'''
Use this for tests that are geared toward specific edge cases, but