Commit 044156c9 authored by Roberto Rosario's avatar Roberto Rosario

Fix workflow document signing action

Also add message when trying to use action for an initial state
when the created document has no version associated. GitLab
issue #726. Thanks to forum user @holzhannes for the
report.
Signed-off-by: Roberto Rosario's avatarRoberto Rosario <roberto.rosario@mayan-edms.com>
parent b2c62ff3
Pipeline #106307483 skipped with stage
......@@ -38,6 +38,10 @@
the object is appended to the label inside parenthesis. Closes
GitLab issue #722. Thanks to Rob de Canha-Knight (@rssfed23) for the
report and research.
- Fix workflow document signing action. Also add message when trying to use
action for an initial state when the created document has no version
associated. GitLab issue #726. Thanks to forum user @holzhannes for the
report.
3.3.6 (2019-12-19)
==================
......
from __future__ import unicode_literals
import json
from mayan.apps.django_gpg.tests.literals import TEST_KEY_PRIVATE_PASSPHRASE
from mayan.apps.django_gpg.tests.mixins import KeyTestMixin
from mayan.apps.document_states.literals import WORKFLOW_ACTION_ON_ENTRY
from mayan.apps.document_states.tests.mixins import WorkflowTestMixin
from mayan.apps.documents.tests.base import GenericDocumentViewTestCase
......@@ -14,13 +17,16 @@ from ..workflow_actions import (
class DocumentSignatureWorkflowActionTestCase(
GenericDocumentViewTestCase, KeyTestMixin, WorkflowTestMixin,
):
auto_upload_document = False
def test_document_signature_detached_action(self):
self.upload_document()
self._create_test_key_private()
signature_count = DetachedSignature.objects.count()
action = DocumentSignatureDetachedAction(
form_data={
'key': self.test_key_private,
'key': self.test_key_private.pk,
'passphrase': TEST_KEY_PRIVATE_PASSPHRASE
}
)
......@@ -28,14 +34,73 @@ class DocumentSignatureWorkflowActionTestCase(
self.assertNotEqual(signature_count, DetachedSignature.objects.count())
def test_document_signature_embedded_action(self):
self.upload_document()
self._create_test_key_private()
signature_count = EmbeddedSignature.objects.count()
action = DocumentSignatureEmbeddedAction(
form_data={
'key': self.test_key_private,
'key': self.test_key_private.pk,
'passphrase': TEST_KEY_PRIVATE_PASSPHRASE
}
)
action.execute(context={'document': self.test_document})
self.assertNotEqual(signature_count, EmbeddedSignature.objects.count())
def test_document_signature_detached_action_via_workflow(self):
self._create_test_workflow()
self.test_workflow.document_types.add(self.test_document_type)
self._create_test_workflow_states()
self._create_test_workflow_transitions()
self._create_test_key_private()
self.test_workflow_state_2.actions.create(
label='test action', when=WORKFLOW_ACTION_ON_ENTRY,
enabled=True,
action_path='mayan.apps.document_signatures.workflow_actions.DocumentSignatureDetachedAction',
action_data=json.dumps(
{
'key': self.test_key_private.pk,
'passphrase': TEST_KEY_PRIVATE_PASSPHRASE
}
),
)
self.upload_document()
self.test_workflow_instance = self.test_document.workflows.first()
signature_count = DetachedSignature.objects.count()
self.test_workflow_instance.do_transition(
transition=self.test_workflow_transition
)
self.assertNotEqual(signature_count, DetachedSignature.objects.count())
def test_document_signature_embedded_action_via_workflow(self):
self._create_test_workflow()
self.test_workflow.document_types.add(self.test_document_type)
self._create_test_workflow_states()
self._create_test_workflow_transitions()
self._create_test_key_private()
self.test_workflow_state_2.actions.create(
label='test action', when=WORKFLOW_ACTION_ON_ENTRY,
enabled=True,
action_path='mayan.apps.document_signatures.workflow_actions.DocumentSignatureEmbeddedAction',
action_data=json.dumps(
{
'key': self.test_key_private.pk,
'passphrase': TEST_KEY_PRIVATE_PASSPHRASE
}
),
)
self.upload_document()
self.test_workflow_instance = self.test_document.workflows.first()
signature_count = EmbeddedSignature.objects.count()
self.test_workflow_instance.do_transition(
transition=self.test_workflow_transition
)
self.assertNotEqual(signature_count, EmbeddedSignature.objects.count())
......@@ -8,6 +8,7 @@ from mayan.apps.acls.models import AccessControlList
from mayan.apps.django_gpg.models import Key
from mayan.apps.django_gpg.permissions import permission_key_sign
from mayan.apps.document_states.classes import WorkflowAction
from mayan.apps.document_states.exceptions import WorkflowStateActionError
from .models import DetachedSignature, EmbeddedSignature
......@@ -42,6 +43,23 @@ class DocumentSignatureDetachedAction(WorkflowAction):
}
}
def get_arguments(self, context):
latest_version = context['document'].latest_version
if not latest_version:
raise WorkflowStateActionError(
_(
'Document has no version to sign. You might be trying to '
'use this action in an initial state before the created '
'document is yet to be processed.'
)
)
return {
'document_version': latest_version,
'key': Key.objects.get(pk=self.form_data['key']),
'passphrase': self.form_data.get('passphrase')
}
def get_form_schema(self, request):
user = request.user
logger.debug('user: %s', user)
......@@ -58,9 +76,7 @@ class DocumentSignatureDetachedAction(WorkflowAction):
def execute(self, context):
DetachedSignature.objects.sign_document_version(
document_version=context['document'].latest_version,
key=self.form_data['key'],
passphrase=self.form_data.get('passphrase'),
**self.get_arguments(context=context)
)
......@@ -69,7 +85,5 @@ class DocumentSignatureEmbeddedAction(DocumentSignatureDetachedAction):
def execute(self, context):
EmbeddedSignature.objects.sign_document_version(
document_version=context['document'].latest_version,
key=self.form_data['key'],
passphrase=self.form_data.get('passphrase'),
**self.get_arguments(context=context)
)
......@@ -37,7 +37,7 @@ def handler_launch_workflow(sender, instance, created, **kwargs):
)
if created:
Workflow.objects.launch_for(instance)
Workflow.objects.launch_for(document=instance)
def handler_trigger_transition(sender, **kwargs):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment