def test_render_form_option_stickiness(self, search_field, population,
                                           sign_in, result_detail):
        """
        This uses combinatoric parametrize calls to run through every combination of
        options and ensure that, after rendering, everything is rendered as we expect
        based on the search parameters.

        This generates tests for every combination of the parametrized fields listed above,
        so that we can have a great deal of confidence that the page is rendering as expected
        based on its input.
        """
        query_value = ("lovelace" if search_field
                       not in ("phone", "box_number") else "12345")

        request = SearchDirectoryFormInput(
            method=search_field,
            population=PopulationType(population),
            length=ResultDetail(result_detail),
            query=query_value,
        )

        if sign_in:
            with self.html_validator.validate_response(
                    self.flask_client.get("/saml/login",
                                          follow_redirects=True)) as html:
                self.html_validator.assert_not_has_sign_in_link()
                assert html.find("label",
                                 attrs={"for": "population-option-students"})

        response = self.flask_client.post("/", data=request.dict())
        self.assert_form_fields_match_expected(response,
                                               request,
                                               sign_in,
                                               recurse=True)
예제 #2
0
 def get_person_listing(
     request: Request,
     logger: Logger,
     session: LocalProxy,
     service: DirectorySearchService,
 ):
     context = RenderingContext.construct(
         uwnetid=session.get("uwnetid"),
     )
     template = "views/person.html"
     try:
         request_input = SearchDirectoryFormInput.parse_obj(request.form)
         context.request_input = request_input
         context.search_result = service.get_listing(
             b64decode(request_input.person_href.encode("UTF-8")).decode("UTF-8")
         )
     except Exception as e:
         template = "views/index.html"
         logger.exception(str(e))
         SearchBlueprint.handle_search_exception(e, context)
     finally:
         return (
             render_template(template, **context.dict(exclude_none=True)),
             context.status_code,
         )
예제 #3
0
    def search_listing(
        request: Request,
        service: DirectorySearchService,
        logger: Logger,
        session: LocalProxy,
        settings: ApplicationConfig,
    ):
        context = RenderingContext.construct(
            uwnetid=session.get("uwnetid"),
            show_experimental=settings.show_experimental,
        )
        try:
            form_input = SearchDirectoryFormInput.parse_obj(request.form)
            context.request_input = form_input

            request_input = SearchDirectoryInput.from_form_input(form_input)
            context.search_result = service.search_directory(request_input)
        except Exception as e:
            logger.exception(str(e))
            SearchBlueprint.handle_search_exception(e, context)
        finally:
            return (
                render_template(
                    "views/search_results.html", **context.dict(exclude_none=True)
                ),
                context.status_code,
            )
    def _get_request_input_from_more_button(
        button: BeautifulSoup, ) -> SearchDirectoryFormInput:
        """This iterates through the hidden input elements that make up our
        "more form", which is a form masquerading as a button for the time being.
        The element values are serialized into the same request input that
        clicking on the button would generate, so that we can validate the
        correct options were set, and that the server renders those
        overrides correctly.
        """
        def get_field_value(field):
            return button.find("input",
                               attrs=dict(name=field)).attrs.get("value")

        return SearchDirectoryFormInput(
            population=get_field_value("population"),
            query=get_field_value("query"),
            method=get_field_value("method"),
            length=get_field_value("length"),
            render_query=get_field_value("render_query"),
            render_method=get_field_value("render_method"),
            render_length=get_field_value("render_length"),
        )
 def test_sanitized_name(self, name, expected):
     assert SearchDirectoryFormInput(method="name", query=name).query == expected
 def test_population_default(self, render_population, expected):
     form_input = SearchDirectoryFormInput(render_population=render_population)
     assert PopulationType(form_input.render_population) == PopulationType(expected)
def test_form_input_validation(query, method, is_valid):
    try:
        SearchDirectoryFormInput(query=query, method=method)
    except ValidationError:
        assert not is_valid
def test_form_input_strips_illegal_chars(query_value, expected_value):
    form_input = SearchDirectoryFormInput(query=query_value)
    assert form_input.query == expected_value