From 27c0690d145c87b1eb571cf50250155704a4c6a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Laur=C3=A9line=20Gu=C3=A9rin?= <zebuline@entrouvert.com>
Date: Tue, 19 Jan 2021 14:18:44 +0100
Subject: [PATCH] api: real formdata status (#50294)

---
 tests/test_api.py | 72 ++++++++++++++++++++++++++++++++++++++---------
 wcs/formdata.py   |  3 ++
 2 files changed, 62 insertions(+), 13 deletions(-)

diff --git a/tests/test_api.py b/tests/test_api.py
index 65914c9f..d516ba75 100644
--- a/tests/test_api.py
+++ b/tests/test_api.py
@@ -1275,9 +1275,10 @@ def test_formdata(pub, local_user):
             data_source={'type': 'foobar'}),
         fields.BlockField(id='5', label='test', varname='blockdata', type='block:foobar', max_items=3),
     ]
-    workflow = Workflow.get_default_workflow()
+    Workflow.wipe()
+    workflow = Workflow(name='foo')
+    workflow.possible_status = Workflow.get_default_workflow().possible_status[:]
     workflow.roles['_foobar'] = 'Foobar'
-    workflow.id = '2'
     workflow.store()
     formdef.workflow_id = workflow.id
     formdef.workflow_roles = {'_receiver': role.id,
@@ -1331,7 +1332,7 @@ def test_formdata(pub, local_user):
     assert resp.json['user']['name'] == local_user.name
     assert resp.json['fields']['foobar'] == 'foo@localhost'
     assert resp.json['fields']['date'] == '2014-01-20'
-    assert resp.json['fields']['file']['content'] == 'YmFzZTY0bWU=' # base64('base64me')
+    assert resp.json['fields']['file']['content'] == 'YmFzZTY0bWU='  # base64('base64me')
     assert resp.json['fields']['file']['filename'] == 'test.txt'
     assert resp.json['fields']['file']['content_type'] == 'text/plain'
     assert resp.json['fields']['item'] == 'foo'
@@ -1340,6 +1341,7 @@ def test_formdata(pub, local_user):
     assert resp.json['fields']['blockdata'] == 'hello'
     assert resp.json['fields']['blockdata_raw'] == [{'foo': 'plop', 'bar': 'foo', 'bar_raw': '1', 'bar_structured': 'XXX'}]
     assert resp.json['workflow']['status']['name'] == 'New'
+    assert resp.json['workflow']['real_status']['name'] == 'New'
     assert resp.json['submission']['channel'] == 'web'
     assert resp.json['geolocations']['base']['lon'] == 10
     assert resp.json['geolocations']['base']['lat'] == -12
@@ -1354,10 +1356,24 @@ def test_formdata(pub, local_user):
     resp2 = get_app(pub).get(sign_uri('/test/%s/' % formdata.id,
         user=local_user), status=403)
 
+    # check status visibility
+    workflow.add_status('Status1', 'st1')
+    workflow.possible_status[-1].visibility = ['unknown']
+    workflow.store()
+    formdata.jump_status('st1')
+    assert formdata.status == 'wf-st1'
+
+    resp = get_app(pub).get(
+            sign_uri('/api/forms/test/%s/' % formdata.id, user=local_user),
+            status=200)
+    assert resp.json['workflow']['status'] == {'id': 'new', 'name': 'New'}
+    assert resp.json['workflow']['real_status'] == {'id': 'st1', 'name': 'Status1'}
+
 
 def test_formdata_backoffice_fields(pub, local_user):
     test_formdata(pub, local_user)
-    workflow = Workflow.get(2)
+    Workflow.wipe()
+    workflow = Workflow(name='foo')
     workflow.backoffice_fields_formdef = WorkflowBackofficeFieldsFormDef(workflow)
     workflow.backoffice_fields_formdef.fields = [
         fields.StringField(id='bo1', label='1st backoffice field',
@@ -1440,19 +1456,49 @@ def test_formdata_duplicated_varnames(pub, local_user):
 
 
 def test_formdata_edit(pub, local_user):
-    test_formdata(pub, local_user)
-    formdef = FormDef.select()[0]
-    formdata = formdef.data_class().select()[0]
-    workflow = formdef.workflow
+    Role.wipe()
+    role = Role(name='test')
+    role.id = '123'
+    role.store()
+    another_role = Role(name='another')
+    another_role.id = '321'
+    another_role.store()
+    local_user.roles = [role.id]
+    local_user.store()
+    FormDef.wipe()
+    formdef = FormDef()
+    formdef.name = 'test'
+    formdef.fields = [
+        fields.StringField(id='0', label='foobar', varname='foobar'),
+    ]
+    Workflow.wipe()
+    workflow = Workflow(name='foo')
+    workflow.possible_status = Workflow.get_default_workflow().possible_status[:]
+    workflow.roles['_foobar'] = 'Foobar'
+    workflow.store()
+    formdef.workflow_id = workflow.id
+    formdef.workflow_roles = {'_receiver': role.id,
+                              '_foobar': another_role.id}
+    formdef.store()
+    formdef.data_class().wipe()
+    formdata = formdef.data_class()()
+    formdata.data = {
+        '0': 'foo@localhost',
+    }
+    formdata.user_id = local_user.id
+    formdata.just_created()
+    formdata.status = 'wf-new'
+    formdata.evolution[-1].status = 'wf-new'
+    formdata.store()
 
     # not user
-    resp = get_app(pub).post_json(
+    get_app(pub).post_json(
             sign_uri('/api/forms/test/%s/' % formdata.id),
             {'data': {'0': 'bar@localhost'}},
             status=403)
 
     # no editable action
-    resp = get_app(pub).post_json(
+    get_app(pub).post_json(
             sign_uri('/api/forms/test/%s/' % formdata.id, user=local_user),
             {'data': {'0': 'bar@localhost'}},
             status=403)
@@ -1464,7 +1510,7 @@ def test_formdata_edit(pub, local_user):
     wfedit.parent = workflow.possible_status[1]
     workflow.store()
 
-    resp = get_app(pub).post_json(
+    get_app(pub).post_json(
             sign_uri('/api/forms/test/%s/' % formdata.id, user=local_user),
             {'data': {'0': 'bar@localhost'}},
             status=200)
@@ -1473,7 +1519,7 @@ def test_formdata_edit(pub, local_user):
     # not editable by user role
     wfedit.by = ['XX']
     workflow.store()
-    resp = get_app(pub).post_json(
+    get_app(pub).post_json(
             sign_uri('/api/forms/test/%s/' % formdata.id, user=local_user),
             {'data': {'0': 'bar@localhost'}},
             status=403)
@@ -1483,7 +1529,7 @@ def test_formdata_edit(pub, local_user):
     wfedit.by = [local_user.roles[0]]
     workflow.store()
 
-    resp = get_app(pub).post_json(
+    get_app(pub).post_json(
             sign_uri('/api/forms/test/%s/' % formdata.id, user=local_user),
             {'data': {'0': 'bar2@localhost'}},
             status=200)
diff --git a/wcs/formdata.py b/wcs/formdata.py
index e4705ced..8fb4c105 100644
--- a/wcs/formdata.py
+++ b/wcs/formdata.py
@@ -1108,6 +1108,9 @@ class FormData(StorableObject):
         wf_status = self.get_visible_status(user)
         if wf_status:
             data['workflow']['status'] = {'id': wf_status.id, 'name': wf_status.name}
+        wf_real_status = self.get_status()
+        if wf_real_status:
+            data['workflow']['real_status'] = {'id': wf_real_status.id, 'name': wf_real_status.name}
         # Workflow data have unknown purpose, do not store them in anonymised export
         if self.workflow_data and not anonymise:
             data['workflow']['data'] = self.workflow_data
-- 
2.29.2

