def test_provided_string_expected_int_should_fail(self): request = construct_dummy_request(json_body={'foo': 'bar'}) fn = core.has_maybe_keywords(('foo', int, 2)) response = fn(request) self.assertIsInstance(response, bool) self.assertEqual(len(request.validated), 0) self.assertFalse(response)
def test_empty_dummy_request(self): request = construct_dummy_request(json_body={}) fn = core.has_maybe_keywords(('foo', int, 2)) response = fn(request) self.assertIsInstance(response, bool) self.assertEqual(len(request.validated), 1) self.assertEqual(request.validated.get('foo'), 2) self.assertTrue(response)
def test_provided_int_expected_int_should_succeed(self): request = construct_dummy_request(json_body={'foo': 9000}) fn = core.has_maybe_keywords(('foo', int, 2)) response = fn(request) self.assertIsInstance(response, bool) self.assertEqual(len(request.validated), 1) self.assertEqual(request.validated.get('foo'), 9000) self.assertTrue(response)
def test_has_maybe_keywords(): request = construct_dummy_request({'foo': 9000}) fn = core.has_maybe_keywords(('foo', int, 2)) response = fn(request) assert type(response) == bool assert len(request.validated) == 1 assert request.validated.get('foo') == 9000 assert response is True request = construct_dummy_request() fn = core.has_maybe_keywords(('foo', int, 2)) response = fn(request) assert type(response) == bool assert len(request.validated) == 1 assert request.validated.get('foo') == 2 assert response is True request = construct_dummy_request({'foo': 'bar'}) fn = core.has_maybe_keywords(('foo', int, 2)) response = fn(request) assert type(response) == bool assert len(request.validated) == 0 assert response is False
from dbas.strings.translator import Translator from dbas.validators.core import validate, has_keywords_in_json_path, has_maybe_keywords from dbas.validators.database import valid_database_model from dbas.validators.discussion import valid_premisegroup, valid_text_values, valid_statement, valid_argument from dbas.validators.reviews import valid_review_reason, valid_not_executed_review, valid_uid_as_row_in_review_queue from dbas.validators.user import valid_user, valid_user_as_author, valid_user_as_author_of_statement, \ valid_user_as_author_of_argument from websocket.lib import send_request_for_recent_reviewer_socketio LOG = logging.getLogger(__name__) @view_config(route_name='flag_argument_or_statement', renderer='json') @validate(valid_user, valid_review_reason, has_keywords_in_json_path(('uid', int), ('is_argument', bool)), has_maybe_keywords(('extra_uid', int, None))) def flag_argument_or_statement(request): """ Flags an argument or statement for a specific reason :param request: current request of the server :return: json-dict() """ LOG.debug("Flag an argument or statement. %s", request.json_body) ui_locales = get_discussion_language(request.matchdict, request.params, request.session) uid = request.validated['uid'] reason = request.validated['reason'] extra_uid = request.validated['extra_uid'] is_argument = request.validated['is_argument'] db_user = request.validated['user']
db_new_statement: Statement = DBDiscussionSession.query( Statement).get(statement_uid) store_reference(reference_text, host, path, db_user, db_new_statement, db_issue) if origin: add_origin_for_list_of_statements(origin, statement_uids) # ----------------------------------------------------------------------------- @zinit.post(require_csrf=False) @positions.post(require_csrf=False) @validate(valid_token, valid_issue_by_slug, valid_new_position_in_body, valid_reason_in_body, valid_reason_and_position_not_equal, has_maybe_keywords(('reference', str, None)), valid_optional_origin) def add_position_with_premise(request): db_user: User = request.validated['user'] db_issue: Issue = request.validated['issue'] reference_text: str = request.validated['reference'] origin: DataOrigin = request.validated['origin'] history = history_handler.save_and_set_cookie(request, db_user, db_issue) host, path = split_url(request.environ.get("HTTP_REFERER")) new_position = set_position(db_user, db_issue, request.validated['position-text']) conclusion_id: int = new_position['statement_uids'][0] db_conclusion: Statement = DBDiscussionSession.query(Statement).get( conclusion_id)
valid_notification_recipient from dbas.validators.user import valid_user LOG = logging.getLogger(__name__) @view_config(request_method='POST', route_name='user_login', renderer='json') @validate( spec_keyword_in_json_body( (str, "user", lambda user, expected_type: isinstance( user, expected_type) and user != ""), (str, "password", lambda password, expected_type: isinstance( password, expected_type) and password != ""), (bool, 'keep_login', lambda keep_login, expected_type: isinstance( keep_login, expected_type))), 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 """ LOG.debug("Login user with Nickname and Password") lang = get_language_from_cookie(request) nickname = request.validated.get('user') password = request.validated.get('password') keep_login = request.validated.get('keep_login') redirect_url = request.validated.get('redirect_url') login_data = login_local_user(nickname, password, request.mailer, lang)
from dbas.strings.keywords import Keywords as _ from dbas.strings.translator import Translator from dbas.validators.common import valid_lang_cookie_fallback from dbas.validators.core import has_keywords_in_json_path, has_maybe_keywords, validate from dbas.validators.notifications import valid_notification_title, valid_notification_text, \ valid_notification_recipient from dbas.validators.user import valid_user LOG = logging.getLogger(__name__) @view_config(request_method='POST', route_name='user_login', renderer='json') @validate( has_keywords_in_json_path(('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 """ LOG.debug("Login user with Nickname and Password") 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'] login_data = login_local_user(nickname, password, request.mailer, lang)
""" if reference_text: for statement_uid in statement_uids: LOG.info("Assigning reference to statement_uid %s", statement_uid) db_new_statement: Statement = DBDiscussionSession.query(Statement).get(statement_uid) store_reference(reference_text, host, path, db_user, db_new_statement, db_issue) if origin: add_origin_for_list_of_statements(origin, statement_uids) # ----------------------------------------------------------------------------- @zinit.post(require_csrf=False) @positions.post(require_csrf=False) @validate(valid_token, valid_issue_by_slug, valid_new_position_in_body, valid_reason_in_body, valid_reason_and_position_not_equal, has_maybe_keywords(('reference', str, None)), valid_optional_origin) def add_position_with_premise(request): db_user: User = request.validated['user'] db_issue: Issue = request.validated['issue'] reference_text: str = request.validated['reference'] origin: DataOrigin = request.validated['origin'] history = history_handler.save_and_set_cookie(request, db_user, db_issue) host, path = split_url(request.environ.get("HTTP_REFERER")) new_position = set_position(db_user, db_issue, request.validated['position-text']) if new_position['errors']: return new_position['errors'] conclusion_id: int = new_position['statement_uids'][0] db_conclusion: Statement = DBDiscussionSession.query(Statement).get(conclusion_id)
@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'] login_data = login_user(nickname, password, request.mailer, lang)