Ejemplo n.º 1
0
def check_user(site: str, user: str, sig: str = "") -> UserCheck:
    validate_username(user)
    errors: Set[Result] = set()
    failure = None
    html_sig = ""
    if site not in datasources.get_sitematrix():
        flask.abort(404)

    sitedata = datasources.get_site_data(site)
    dbname = sitedata.dbname
    user = (user[0].upper() + user[1:]).replace("_", " ")

    if not sig:
        # signature not supplied, get data from database
        user_props = datasources.get_user_properties(user, dbname)
        logger.debug(user_props)

        if not user_props.nickname:
            # user does not exist or uses default sig
            if not datasources.check_user_exists(user, sitedata):
                # user does not exist
                errors.add(WebAppMessage.USER_DOES_NOT_EXIST)
                failure = True
            else:
                # user exists but uses default signature
                errors.add(WebAppMessage.DEFAULT_SIG)
                sig = get_default_sig(site, user, user)
                failure = False
        elif not user_props.fancysig:
            # user exists but uses non-fancy sig with nickname
            errors.add(WebAppMessage.SIG_NOT_FANCY)
            sig = get_default_sig(site, user, user_props.nickname)
            failure = False
        else:
            # user exists and has custom fancy sig, check it
            sig = user_props.nickname

    if failure is None:
        # OK so far, actually check the signature
        errors = cast(Set[Result], sigprobs.check_sig(user, sig, sitedata,
                                                      site))
        html_sig = get_rendered_sig(site,
                                    sigprobs.evaluate_subst(sig, sitedata))
        logger.debug(errors)

    if not errors:
        # check returned no errors
        errors.add(WebAppMessage.NO_ERRORS)
        failure = False

    data = UserCheck(
        site=site,
        username=user,
        errors=list(errors),
        signature=sig,
        failure=failure,
        html_sig=html_sig,
    )
    return data
Ejemplo n.º 2
0
def test_check_sig_shortcircuit(site, sitedata):
    errors = sigprobs.check_sig(
        "Example",
        "{{foo}}",
        sitedata,
        site["domain"],
        checks=Checks.LINT | Checks.FANCY,
    )
    assert errors == {mock.sentinel.check_fanciness}
    sigprobs.get_lint_errors.assert_not_called
Ejemplo n.º 3
0
def test_check_sig_flags(flags, sentinels, site, sitedata):
    with mock.patch("sigprobs.check_links", return_value=mock.sentinel.check_links):
        with mock.patch(
            "sigprobs.check_length", return_value=mock.sentinel.check_length
        ):
            with mock.patch(
                "sigprobs.check_fanciness", return_value=mock.sentinel.check_fanciness
            ):
                with mock.patch(
                    "sigprobs.get_lint_errors",
                    return_value={mock.sentinel.lint_errors, None},
                ):
                    with mock.patch(
                        "sigprobs.check_tildes", return_value=mock.sentinel.check_tildes
                    ):
                        errors = sigprobs.check_sig(
                            "Example", "{{foo}}", sitedata, site["domain"], checks=flags
                        )
    assert sentinels == errors
Ejemplo n.º 4
0
def test_check_sig(site, sitedata, *args):
    errors = sigprobs.check_sig("", "", sitedata, site["domain"])
    for function in args:
        function.assert_called_once
    assert set(function.return_value for function in args) == errors