def update_login(nickname, password=None, remember_me=False): where = [db.or_(User.nickname == nickname, User.email == nickname)] if password is not None: where.append(User.password == password) user = User.query.filter(*where).one() login_user(UserInfo(user.id), remember=remember_me) return user
def update_login(nickname, password=None, remember_me=False): where = [db.or_(User.nickname == nickname, User.email == nickname)] if password is not None: where.append(User.password == password) user = User.query.filter(*where).one() login_user(user.get_id(), remember_me=remember_me) return user
def test_workflow_creation(self): from invenio.webdeposit_load_deposition_types import \ deposition_metadata from invenio.bibworkflow_model import Workflow from invenio.webdeposit_workflow import DepositionWorkflow from invenio.webdeposit_utils import get_latest_or_new_workflow, \ get_workflow, delete_workflow from invenio.sqlalchemyutils import db from invenio.webuser_flask import login_user login_user(1) number_of_dep_types = len(deposition_metadata) # Test for every deposition type for deposition_type in deposition_metadata.keys(): # New workflow is created workflow = get_latest_or_new_workflow(deposition_type, user_id=1) assert workflow is not None # The just created workflow is retrieved as latest workflow2 = get_latest_or_new_workflow(deposition_type, user_id=1) assert workflow2 is not None assert str(workflow2.uuid) == str(workflow.uuid) # and also retrieved with its uuid workflow = get_workflow(deposition_type, workflow.uuid) assert workflow is not None # Test get_workflow function with random arguments workflow = get_workflow('deposition_type_that_doesnt_exist', 'some_uuid') assert workflow is None deposition_type = deposition_metadata.keys()[-1] workflow = get_workflow(deposition_type, 'some_uuid_that_doesnt_exist') assert workflow is None # Create workflow without using webdeposit_utils wf = deposition_metadata[deposition_type]["workflow"] workflow = DepositionWorkflow(deposition_type=deposition_type, workflow=wf, user_id=1) # Test that the retrieved workflow is the same and not None workflow2 = get_workflow(deposition_type, workflow.get_uuid()) assert workflow2 is not None assert workflow2.get_uuid() == workflow.get_uuid() # Check the number of created workflows workflows = db.session.query(Workflow).all() assert len(workflows) == number_of_dep_types + 1 uuid = workflow.get_uuid() delete_workflow(1, uuid) workflow = get_workflow(deposition_type, uuid) assert workflow is None
def test_workflow_creation(self): from invenio.webdeposit_load_deposition_types import \ deposition_metadata from invenio.bibworkflow_model import Workflow from invenio.webdeposit_workflow import DepositionWorkflow from invenio.webdeposit_utils import get_latest_or_new_workflow, \ get_workflow, delete_workflow, InvenioWebDepositNoDepositionType from invenio.webuser_flask import login_user login_user(1) number_of_dep_types = len(deposition_metadata) # Test for every deposition type for deposition_type in deposition_metadata.keys(): # New workflow is created workflow = get_latest_or_new_workflow(deposition_type, user_id=1) self.assertTrue(workflow is not None) # The just created workflow is retrieved as latest workflow2 = get_latest_or_new_workflow(deposition_type, user_id=1) self.assertTrue(workflow2 is not None) self.assertEqual(str(workflow2.uuid), str(workflow.uuid)) # and also retrieved with its uuid workflow = get_workflow(workflow.uuid, deposition_type) self.assertTrue(workflow is not None) # Test get_workflow function with random arguments deposition_type = deposition_metadata.keys()[-1] workflow = get_workflow('some_uuid_that_doesnt_exist', deposition_type) self.assertTrue(workflow is None) # Create workflow without using webdeposit_utils workflow = DepositionWorkflow(deposition_type=deposition_type, user_id=1) self.assertRaises(InvenioWebDepositNoDepositionType, get_workflow, workflow.get_uuid(), 'deposition_type_that_doesnt_exist') # Test that the retrieved workflow is the same and not None workflow2 = get_workflow(workflow.get_uuid(), deposition_type) self.assertTrue(workflow2 is not None) self.assertEqual(workflow2.get_uuid(), workflow.get_uuid()) # Check the number of created workflows count_workflows = Workflow.get( Workflow.module_name == "webdeposit").count() self.assertEqual(count_workflows, number_of_dep_types + 1) uuid = workflow.get_uuid() delete_workflow(1, uuid) workflow = get_workflow(uuid, deposition_type) self.assertTrue(workflow is None)
def auth_key(*args, **kwargs): if 'apikey' in request.values: from invenio.web_api_key_model import WebAPIKey from invenio.webuser_flask import login_user user_id = WebAPIKey.acc_get_uid_from_request() if user_id == -1: abort(401) login_user(user_id) else: abort(401) return fn(*args, **kwargs)
def auth_key(*args, **kwargs): if 'apikey' in request.values: from invenio.web_api_key_model import WebAPIKey from invenio.webuser_flask import login_user user_id = WebAPIKey.acc_get_uid_from_request() if user_id == -1: restful.abort(401) login_user(user_id) else: restful.abort(401) return fn(*args, **kwargs)
def register(): req = request.get_legacy_request() # FIXME if CFG_ACCESS_CONTROL_LEVEL_SITE > 0: return webuser.page_not_authorized(req, "../youraccount/register?ln=%s" % g.ln, navmenuid='youraccount') form = RegisterForm(request.values, csrf_enabled=False) #uid = current_user.get_id() title = _("Register") messages = [] state = "" if form.validate_on_submit(): ruid = webuser.registerUser(req, form.email.data.encode('utf8'), form.password.data.encode('utf8'), form.nickname.data.encode('utf8'), ln=g.ln) if ruid == 0: title = _("Account created") messages.append(_("Your account has been successfully created.")) state = "success" if CFG_ACCESS_CONTROL_NOTIFY_USER_ABOUT_NEW_ACCOUNT == 1: messages.append(_("In order to confirm its validity, an email message containing an account activation key has been sent to the given email address.")) messages.append(_("Please follow instructions presented there in order to complete the account registration process.")) if CFG_ACCESS_CONTROL_LEVEL_ACCOUNTS >= 1: messages.append(_("A second email will be sent when the account has been activated and can be used.")) elif CFG_ACCESS_CONTROL_NOTIFY_USER_ABOUT_NEW_ACCOUNT != 1: user = User.query.filter(User.email == form.email.data.lower()).one() login_user(user.get_id()) messages.append(_("You can now access your account.")) else: title = _("Registration failure") state = "danger" if ruid == 5: messages.append(_("Users cannot register themselves, only admin can register them.")) elif ruid == 6 or ruid == 1: # Note, code 1 is used both for invalid email, and email sending # problems, however the email address is validated by the form, # so we only have to report a problem sending the email here messages.append(_("The site is having troubles in sending you an email for confirming your email address.")) messages.append(_("The error has been logged and will be taken in consideration as soon as possible.")) else: # Errors [-2, (1), 2, 3, 4] taken care of by form validation messages.append(_("Internal error %s") % ruid) elif request.method == 'POST': title = _("Registration failure") state = "warning" return render_template('webaccount_register.html', form=form, title=title, messages=messages, state=state)
def test_low_level_login(data, self): users = data.UserData assert current_user.is_guest login_user(users.admin.id) assert current_user.get_id() == users.admin.id logout_user() assert current_user.get_id() != users.admin.id assert current_user.is_guest login_user(users.romeo.id) assert not current_user.is_guest assert current_user.get_id() == users.romeo.id login_user(users.admin.id) assert current_user.get_id() == users.admin.id logout_user()
def register(): req = request.get_legacy_request() # FIXME if CFG_ACCESS_CONTROL_LEVEL_SITE > 0: return webuser.page_not_authorized(req, "../youraccount/register?ln=%s" % g.ln, navmenuid='youraccount') form = RegisterForm(request.values, csrf_enabled=False) #uid = current_user.get_id() title = _("Register") messages = [] state = "" if form.validate_on_submit(): ruid = webuser.registerUser(req, form.email.data.encode('utf8'), form.password.data.encode('utf8'), form.nickname.data.encode('utf8'), ln=g.ln) if ruid == 0: title = _("Account created") messages.append(_("Your account has been successfully created.")) state = "success" if CFG_ACCESS_CONTROL_NOTIFY_USER_ABOUT_NEW_ACCOUNT == 1: messages.append( _("In order to confirm its validity, an email message containing an account activation key has been sent to the given email address." )) messages.append( _("Please follow instructions presented there in order to complete the account registration process." )) if CFG_ACCESS_CONTROL_LEVEL_ACCOUNTS >= 1: messages.append( _("A second email will be sent when the account has been activated and can be used." )) elif CFG_ACCESS_CONTROL_NOTIFY_USER_ABOUT_NEW_ACCOUNT != 1: user = User.query.filter( User.email == form.email.data.lower()).one() login_user(user.get_id()) messages.append(_("You can now access your account.")) else: title = _("Registration failure") state = "danger" if ruid == 5: messages.append( _("Users cannot register themselves, only admin can register them." )) elif ruid == 6 or ruid == 1: # Note, code 1 is used both for invalid email, and email sending # problems, however the email address is validated by the form, # so we only have to report a problem sending the email here messages.append( _("The site is having troubles in sending you an email for confirming your email address." )) messages.append( _("The error has been logged and will be taken in consideration as soon as possible." )) else: # Errors [-2, (1), 2, 3, 4] taken care of by form validation messages.append(_("Internal error %s") % ruid) elif request.method == 'POST': title = _("Registration failure") state = "warning" return render_template('webaccount_register.html', form=form, title=title, messages=messages, state=state)
def test_form_functions(self): from invenio.webdeposit_load_deposition_types import \ deposition_metadata from invenio.webdeposit_load_forms import forms from invenio.webdeposit_workflow import DepositionWorkflow from invenio.webdeposit_utils import get_form, \ get_form_status, set_form_status, CFG_DRAFT_STATUS from invenio.bibworkflow_model import Workflow for metadata in deposition_metadata.values(): for wf_function in metadata['workflow']: if 'render_form' == wf_function.func_name: break from invenio.webuser_flask import login_user login_user(1) deposition_workflow = DepositionWorkflow(deposition_type='Article', user_id=1) uuid = deposition_workflow.get_uuid() # Run the workflow to insert a form in the db deposition_workflow.run() # There is only one form in the db workflows = Workflow.get(module_name='webdeposit') assert len(workflows.all()) == 1 assert len(workflows[0].extra_data['drafts']) == 1 # Test that guest user doesn't have access to the form form = get_form(0, uuid=uuid) assert form is None # Test that the current form has the right type form = get_form(1, uuid=deposition_workflow.get_uuid()) assert isinstance(form, forms['ArticleForm']) assert str(uuid) == str(deposition_workflow.get_uuid()) # Test that form is returned with get_form function form = get_form(1, deposition_workflow.get_uuid()) assert form is not None form = get_form(1, deposition_workflow.get_uuid(), step=0) assert form is not None # Second step doesn't have a form form = get_form(1, deposition_workflow.get_uuid(), step=1) assert form is None form_status = get_form_status(1, deposition_workflow.get_uuid()) assert form_status == CFG_DRAFT_STATUS['unfinished'] form_status = get_form_status(1, deposition_workflow.get_uuid(), step=2) assert form_status is None set_form_status(1, uuid, CFG_DRAFT_STATUS['finished']) form_status = get_form_status(1, deposition_workflow.get_uuid()) assert form_status == CFG_DRAFT_STATUS['finished']
def test_record_creation(self): import os from wtforms import TextAreaField from datetime import datetime from invenio.search_engine import record_exists from invenio.cache import cache from invenio.config import CFG_PREFIX from invenio.webuser_flask import login_user from invenio.bibworkflow_model import Workflow from invenio.bibworkflow_config import CFG_WORKFLOW_STATUS from invenio.bibsched_model import SchTASK from invenio.webdeposit_utils import get_form, create_workflow, \ set_form_status, CFG_DRAFT_STATUS from invenio.webdeposit_load_deposition_types import \ deposition_metadata from invenio.webdeposit_workflow_utils import \ create_record_from_marc from invenio.bibfield import get_record login_user(1) for deposition_type in deposition_metadata.keys(): deposition = create_workflow(deposition_type, 1) assert deposition is not None # Check if deposition creates a record create_rec = create_record_from_marc() function_exists = False for workflow_function in deposition.workflow: if create_rec.func_code == workflow_function.func_code: function_exists = True if not function_exists: # if a record is not created, #continue with the next deposition continue uuid = deposition.get_uuid() cache.delete_many("1:current_deposition_type", "1:current_uuid") cache.add("1:current_deposition_type", deposition_type) cache.add("1:current_uuid", uuid) # Run the workflow deposition.run() # Create form's json based on the field name form = get_form(1, uuid=uuid) webdeposit_json = {} # Fill the json with dummy data for field in form: if isinstance(field, TextAreaField): # If the field is associated with a marc field if field.has_recjson_key() or field.has_cook_function(): webdeposit_json[field.name] = "test " + field.name draft = dict( form_type=form.__class__.__name__, form_values=webdeposit_json, step=0, # dummy step status=CFG_DRAFT_STATUS['finished'], timestamp=str(datetime.now())) # Add a draft for the first step Workflow.set_extra_data(user_id=1, uuid=uuid, key='drafts', value={0: draft}) workflow_status = CFG_WORKFLOW_STATUS.RUNNING while workflow_status != CFG_WORKFLOW_STATUS.COMPLETED: # Continue workflow deposition.run() set_form_status(1, uuid, CFG_DRAFT_STATUS['finished']) workflow_status = deposition.get_status() # Workflow is finished. Test if record is created recid = deposition.get_data('recid') assert recid is not None # Test that record id exists assert record_exists(recid) == 1 # Test that the task exists task_id = deposition.get_data('task_id') assert task_id is not None bibtask = SchTASK.query.filter(SchTASK.id == task_id).first() assert bibtask is not None # Run bibupload, bibindex, webcoll manually cmd = "%s/bin/bibupload %s" % (CFG_PREFIX, task_id) assert not os.system(cmd) rec = get_record(recid) marc = rec.legacy_export_as_marc() for field in form: if isinstance(field, TextAreaField): # If the field is associated with a marc field if field.has_recjson_key() or field.has_cook_function(): assert "test " + field.name in marc
def test_form_functions(self): from invenio.webdeposit_load_deposition_types import \ deposition_metadata from invenio.webdeposit_load_forms import forms from invenio.webdeposit_model import WebDepositDraft from invenio.webdeposit_workflow import DepositionWorkflow from invenio.webdeposit_utils import get_current_form, get_form, \ get_form_status, CFG_DRAFT_STATUS from invenio.sqlalchemyutils import db from invenio.webdeposit_workflow_utils import render_form, \ wait_for_submission from invenio.cache import cache for metadata in deposition_metadata.values(): for wf_function in metadata['workflow']: if 'render_form' == wf_function.func_name: break from invenio.webuser_flask import login_user login_user(1) wf = [render_form(forms.values()[0]), wait_for_submission()] deposition_workflow = DepositionWorkflow( deposition_type='TestWorkflow', workflow=wf, user_id=1) uuid = deposition_workflow.get_uuid() cache.delete_many("1:current_deposition_type", "1:current_uuid") cache.add("1:current_deposition_type", 'TestWorkflow') cache.add("1:current_uuid", uuid) # Run the workflow to insert a form to the db deposition_workflow.run() # There is only one form in the db drafts = db.session.query(WebDepositDraft) assert len(drafts.all()) == 1 # Test that guest user doesn't have access to the form uuid, form = get_current_form(0, deposition_type='TestWorkflow', uuid=uuid) assert form is None # Test that the current form has the right type uuid, form = get_current_form(1, deposition_type='TestWorkflow', uuid=deposition_workflow.get_uuid()) assert isinstance(form, forms.values()[0]) assert str(uuid) == str(deposition_workflow.get_uuid()) # Test that form is returned with get_form function form = get_form(1, deposition_workflow.get_uuid()) assert form is not None form = get_form(1, deposition_workflow.get_uuid(), step=0) assert form is not None # Second step doesn't have a form form = get_form(1, deposition_workflow.get_uuid(), step=1) assert form is None form_status = get_form_status(1, deposition_workflow.get_uuid()) assert form_status == CFG_DRAFT_STATUS['unfinished'] form_status = get_form_status(1, deposition_workflow.get_uuid(), step=2) assert form_status is None db.session.query(WebDepositDraft).\ update({'status': CFG_DRAFT_STATUS['finished']}) form_status = get_form_status(1, deposition_workflow.get_uuid()) assert form_status == CFG_DRAFT_STATUS['finished']