def test_has_multiple_keywords(self): request = construct_dummy_request({ 'string': 'foo', 'bool': True }) response = has_keywords(('string', str), ('bool', bool))(request) self.assertTrue(response) self.assertIn('string', request.validated) self.assertIn('bool', request.validated)
def test_has_keywords(): request = construct_dummy_request() fn = core.has_keywords(('foo', int)) response = fn(request) assert type(response) == bool assert response is False request = construct_dummy_request({'foo': 'bar'}) fn = core.has_keywords(('foo', int)) response = fn(request) assert type(response) == bool assert response is False request = construct_dummy_request({'foo': 2}) fn = core.has_keywords(('foo', int)) response = fn(request) assert type(response) == bool assert response is True
def test_has_number_keywords(self): request = construct_dummy_request({ 'int': 4, 'float': 4.0 }) response = has_keywords(('int', int), ('float', float))(request) self.assertTrue(response) self.assertIn('int', request.validated) self.assertIn('float', request.validated)
def valid_new_issue(request): """ Verifies given data for a new issue :param request: :return: """ fn_validator = has_keywords(('title', str), ('info', str), ('long_info', str)) if not fn_validator(request): return False title = escape_if_string(request.validated, 'title') info = escape_if_string(request.validated, 'info') long_info = escape_if_string(request.validated, 'long_info') db_dup1 = DBDiscussionSession.query(Issue).filter_by(title=title).all() db_dup2 = DBDiscussionSession.query(Issue).filter_by(info=info).all() db_dup3 = DBDiscussionSession.query(Issue).filter_by( long_info=long_info).all() if db_dup1 or db_dup2 or db_dup3: _tn = Translator(get_language_from_cookie(request)) add_error(request, 'Issue data is a duplicate', _tn.get(_.duplicate)) return False return True
def valid_user_as_author_of_statement(request): """ :param request: :return: """ if not has_keywords(('statement_id', int))(request): return False statement_id = request.validated['statement_id'] if valid_user(request): db_user = request.validated['user'] db_textversion = DBDiscussionSession.query(TextVersion) \ .filter_by(statement_uid=statement_id) \ .order_by(TextVersion.uid.asc()).first() if db_textversion and db_textversion.author_uid == db_user.uid: request.validated['statement'] = DBDiscussionSession.query( Statement).get(statement_id) return True else: _tn = Translator(get_language_from_cookie(request)) add_error(request, _tn.get(_.userIsNotAuthorOfStatement)) return False
def test_has_keywords_without_keyword(self): request = construct_dummy_request({'foo': 42}) response = has_keywords(('bar', int))(request) self.assertFalse(response) self.assertNotIn('bar', request.validated)
def test_has_keywords_with_wrong_type(self): request = construct_dummy_request({'int': 4}) response = has_keywords(('int', float))(request) self.assertFalse(response) self.assertNotIn('int', request.validated)
def test_has_list_keywords(self): request = construct_dummy_request({'list': ['<:)']}) response = has_keywords(('list', list))(request) self.assertTrue(response) self.assertIn('list', request.validated)
def test_has_one_keyword(self): request = construct_dummy_request({'string': 'foo'}) response = has_keywords(('string', str))(request) self.assertTrue(response) self.assertIn('string', request.validated)
""" ref_uid = request.matchdict["ref_uid"] db_ref = get_reference_by_id(ref_uid) if db_ref: return get_all_references_by_reference_text(db_ref.reference) return error("Reference could not be found", "API/GET Reference Usages", "Error when trying to find matching reference for id") # ============================================================================= # USER MANAGEMENT # ============================================================================= @login.post(require_csrf=False) @validate(has_keywords(('nickname', str), ('password', str)), validate_credentials) def user_login(request): """ Check provided credentials and return a token, if it is a valid user. The function body is only executed, if the validator added a request.validated field. :param request: :return: token and nickname """ return { 'nickname': request.validated['nickname'], 'token': request.validated['token'] }
return { 'language': str(ui_locales), 'title': 'Admin - ' + table_name, 'project': project_name, 'extras': extras_dict, 'table': table_dict, 'discussion': { 'broke_limit': False } } @update_row.post() @validate(valid_user_as_admin, valid_table_name, has_keywords(('keys', list), ('uids', list), ('values', list))) def main_update(request): """ View configuration for updating any row :param request: current webservers request :return: dict() """ logger('Admin', 'def ' + str(request.params)) table = request.validated['table'] uids = request.validated['uids'] keys = request.validated['keys'] values = request.validated['values'] return lib.update_row(table, uids, keys, values)
return_dict = graph return_dict.update({'type': 'complete'}) if not error: return_dict.update({'path': get_path_of_user(request.application_url, path, db_issue)}) return_dict.update({'error': ''}) else: # gets called if the data itself is malicious ui_locales = get_language_from_cookie(request) _t = Translator(ui_locales) error = _t.get(_.internalKeyError) return_dict = {'error': error} return return_dict @partial_graph.post() @validate(valid_issue_by_id, has_keywords(('uid', int), ('is_argument', bool), ('path', str))) def get_d3_partial_dump(request): logger('Graph', 'main: ' + str(request.json_body)) path = request.validated['path'] uid = request.validated['uid'] is_argument = request.validated['is_argument'] db_issue = request.validated['issue'] return_dict = { 'type': 'partial' } if is_argument: graph, error = get_partial_graph_for_argument(uid, db_issue) else: graph, error = get_partial_graph_for_statement(uid, db_issue, path)
@view_config(route_name='delete_statistics', renderer='json') @validate(valid_user) def delete_statistics(request): """ Request to delete votes/clicks of the user. :param request: request of the web server :return: json-dict() """ logger('delete_statistics', 'main') db_user = request.validated['user'] return clear_vote_and_seen_values_of_user(db_user) @view_config(request_method='POST', route_name='user_login', renderer='json') @validate(has_keywords(('user', str), ('password', str), ('keep_login', bool)), has_maybe_keywords(('redirect_url', str, ''))) def user_login(request): """ Will login the user by his nickname and password :param request: request of the web server :return: dict() with error """ logger('views', 'main') lang = get_language_from_cookie(request) nickname = request.validated['user'] password = request.validated['password'] keep_login = request.validated['keep_login'] redirect_url = request.validated['redirect_url']