def test_json_get_set_functions(self):
        import json
        from flask import current_app, url_for
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio.webdeposit_utils import create_workflow
        from wtforms import TextAreaField
        from invenio_deposit import forms
        from invenio.modules.apikeys import build_web_request

        self.uuid = create_workflow(self.deposition, user_id=1).get_uuid()

        # Get form from deposition
        for fun in deposition_metadata[self.deposition]['workflow']:
            if fun.func_name == 'render':
                form_type = fun.__form_type__

        form = forms[form_type]()

        # Insert form data
        form_data = {}
        for field in form:
            if isinstance(field, TextAreaField):
                form_data[field.name] = 'testing webdeposit API'

        data = {'form_data': json.dumps(form_data),
                'uuid': self.uuid}
        url = url_for('webdeposit_api.json_set',
                      deposition_metadata=self.deposition)
        url_set = build_web_request(url, {},
                                    uid=1, api_key=self.apikey,
                                    timestamp=False)
        with current_app.test_client() as c:
            response = c.post(url_set, data=data)
            assert response._status_code == 200

            url = url_for(
                'webdeposit_api.json_get', deposition_metadata=self.deposition)
            url_get = build_web_request(url, {'uuid':
                                        self.uuid},
                                        uid=1, api_key=self.apikey,
                                        timestamp=False)
            response = c.get(url_get)
            assert response._status_code == 200

            for field in form:
                if isinstance(field, TextAreaField):
                    assert response.json[field.name] == 'testing webdeposit API'
    def test_deposit_files(self):
        from flask import current_app, url_for
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio.modules.workflows.models import Workflow
        from invenio.webdeposit_utils import create_workflow, deposit_files, \
            get_latest_or_new_workflow

        user_id = self.login_user()

        # Test for every deposition type
        for deposition_type in deposition_metadata.keys():
            workflow = create_workflow(deposition_type, user_id)
            uuid = workflow.get_uuid()

            pdffile = make_pdf_fixture("test.pdf")

            with current_app.test_request_context(
                url_for(
                    'webdeposit.upload_file', deposition_type=deposition_type,
                    uuid=uuid
                ),
                method='POST',
                data={
                    'file': pdffile,
                    'name': 'test.pdf', }):

                deposit_files(user_id, deposition_type, uuid, preingest=True)

            workflow = get_latest_or_new_workflow(deposition_type, user_id)
            workflow.run()

            draft = Workflow.get(
                Workflow.id_user == user_id, Workflow.uuid == uuid
            ).one().extra_data['drafts'][0]

            assert len(draft['form_values']['files']) == 1
            filemeta = draft['form_values']['files'][0]
            assert filemeta['name'] == 'test.pdf'
            assert filemeta['content_type'] == 'application/pdf'
    def test_record_creation(self):
        import os
        from wtforms import TextAreaField
        from datetime import datetime

        from invenio.legacy.search_engine import record_exists
        from invenio.cache import cache
        from invenio.config import CFG_PREFIX
        from invenio.modules.workflows.models import Workflow
        from invenio.modules.workflows.config import CFG_WORKFLOW_STATUS
        from invenio.modules.scheduler.models import SchTASK

        from invenio.webdeposit_utils import get_form, create_workflow, \
            set_form_status, CFG_DRAFT_STATUS
        from invenio_deposit.loader import \
            deposition_metadata
        from invenio.webdeposit_workflow_utils import \
            create_record_from_marc
        from invenio.modules.record.api import get_record

        user_id = self.login_user()
        for deposition_type in deposition_metadata.keys():

            deposition = create_workflow(deposition_type, user_id)
            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(user_id, 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=user_id, 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(user_id, 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