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
def check_impersonation(sig: str, user: str, sitedata: SiteData) -> Optional[SigError]: wikitext = mwph.parse(sig) problem = False for link in wikitext.ifilter_wikilinks(): if not link.text: continue text = datasources.normal_name(link.text) if compare_links(user, sitedata, link) is True: if text == datasources.normal_name(user): # one link matches, that's good enough break elif datasources.check_user_exists(text, sitedata): problem = True if problem: return SigError.LINK_NAME else: return None
def test_db_check_user_exists(user, expected, sitedata): with mock.patch("datasources.do_db_query", return_value=((12345, ), ) if expected else ()): result = datasources.check_user_exists(user, sitedata) assert result is expected
def test_api_check_user_exists(user, expected, sitedata): result = datasources.check_user_exists(user, sitedata) assert result is expected
def test_check_user_exists(userid, expected, site, sitedata): db_query = mock.Mock(return_value=userid) with mock.patch("datasources.db.do_db_query", db_query): exists = datasources.check_user_exists("Example", sitedata) assert expected == exists db_query.assert_called_once_with(site["dbname"], mock.ANY, user="******")