From e323cf3031de3bf0af48f955cc9bc69eb2ab395a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?J=C3=A9rome=20Perrin?= <jerome@nexedi.com>
Date: Mon, 16 Jan 2023 15:06:12 +0900
Subject: [PATCH] Revert "erp5_web_shadir: more useful checks"

This reverts commit c45c229521756913777a2fc44edffb53d2e192df.

to get test passing again
---
 .../extension.erp5.ShaDir.py                  | 35 ++++++-----
 .../extension.erp5.ShaDir.xml                 | 27 ++++++++
 .../erp5_web_shadir/WebSite_getJSONSchema.py  | 39 ++++++++++++
 .../erp5_web_shadir/WebSite_getJSONSchema.xml | 62 +++++++++++++++++++
 4 files changed, 149 insertions(+), 14 deletions(-)
 create mode 100644 bt5/erp5_web_shadir/SkinTemplateItem/portal_skins/erp5_web_shadir/WebSite_getJSONSchema.py
 create mode 100644 bt5/erp5_web_shadir/SkinTemplateItem/portal_skins/erp5_web_shadir/WebSite_getJSONSchema.xml

diff --git a/bt5/erp5_web_shadir/ExtensionTemplateItem/portal_components/extension.erp5.ShaDir.py b/bt5/erp5_web_shadir/ExtensionTemplateItem/portal_components/extension.erp5.ShaDir.py
index ff44e2db921..9369d21cae0 100644
--- a/bt5/erp5_web_shadir/ExtensionTemplateItem/portal_components/extension.erp5.ShaDir.py
+++ b/bt5/erp5_web_shadir/ExtensionTemplateItem/portal_components/extension.erp5.ShaDir.py
@@ -25,11 +25,10 @@
 #
 ##############################################################################
 
+
 import hashlib
 import json
-from base64 import b64decode
-from binascii import a2b_hex
-from zExceptions import BadRequest
+import validictory
 from Products.ERP5Type.UnrestrictedMethod import super_user
 
 
@@ -40,10 +39,17 @@ def WebSection_getDocumentValue(self, key, portal=None, language=None,\
 
      - POST /<key>
         + parameters required:
+           * file: the name of the file
+           * urlmd5: mdsum of orginal url
            * sha512: the hash (sha512) of the file content
 
+        + parameters not required:
+           * valid-until: the date which the file must be expired
+           * architecture: computer architecture
+
        Used to add information on shadir server.
 
+
      - GET /<key>
        Return list of information for a given key
        Raise HTTP error (404) if key does not exist
@@ -77,17 +83,16 @@ def WebSection_setObject(self, id, ob, **kw):
   """
   portal = self.getPortalObject()
   data = self.REQUEST.get('BODY')
-  try:
-    metadata, signature = json.loads(data)
-    metadata = json.loads(metadata)
-    # a few basic checks
-    b64decode(signature)
-    if len(a2b_hex(metadata['sha512'])) != 64:
-      raise Exception('sha512: invalid length')
-  except Exception as e:
-    raise BadRequest(str(e))
-
-  expiration_date = metadata.get('expiration_date')
+  schema = self.WebSite_getJSONSchema()
+  structure = json.loads(data)
+  # 0 elementh in structure is json in json
+  # 1 elementh is just signature
+  structure = [json.loads(structure[0]), structure[1]]
+
+  validictory.validate(structure, schema)
+
+  file_name = structure[0].get('file', None)
+  expiration_date = structure[0].get('expiration_date', None)
 
   data_set = portal.portal_catalog.getResultValue(portal_type='Data Set',
                                                   reference=id)
@@ -100,6 +105,7 @@ def WebSection_setObject(self, id, ob, **kw):
 
 
   reference = hashlib.sha512(data).hexdigest()
+  ob.setFilename(file_name)
   ob.setFollowUp(data_set.getRelativeUrl())
   ob.setContentType('application/json')
   ob.setReference(reference)
@@ -125,3 +131,4 @@ def WebSection_putFactory(self, name, typ, body):
                                                     filename=name,
                                                     discover_metadata=False)
   return document
+
diff --git a/bt5/erp5_web_shadir/ExtensionTemplateItem/portal_components/extension.erp5.ShaDir.xml b/bt5/erp5_web_shadir/ExtensionTemplateItem/portal_components/extension.erp5.ShaDir.xml
index 8fe279844e8..56df64e4fbd 100644
--- a/bt5/erp5_web_shadir/ExtensionTemplateItem/portal_components/extension.erp5.ShaDir.xml
+++ b/bt5/erp5_web_shadir/ExtensionTemplateItem/portal_components/extension.erp5.ShaDir.xml
@@ -74,6 +74,33 @@
                       <key> <string>action</string> </key>
                       <value> <string>validate</string> </value>
                   </item>
+                  <item>
+                      <key> <string>actor</string> </key>
+                      <value> <string>ERP5TypeTestCase</string> </value>
+                  </item>
+                  <item>
+                      <key> <string>comment</string> </key>
+                      <value> <string></string> </value>
+                  </item>
+                  <item>
+                      <key> <string>time</string> </key>
+                      <value>
+                        <object>
+                          <klass>
+                            <global name="DateTime" module="DateTime.DateTime"/>
+                          </klass>
+                          <tuple>
+                            <none/>
+                          </tuple>
+                          <state>
+                            <tuple>
+                              <float>1377844502.87</float>
+                              <string>GMT+9</string>
+                            </tuple>
+                          </state>
+                        </object>
+                      </value>
+                  </item>
                   <item>
                       <key> <string>validation_state</string> </key>
                       <value> <string>validated</string> </value>
diff --git a/bt5/erp5_web_shadir/SkinTemplateItem/portal_skins/erp5_web_shadir/WebSite_getJSONSchema.py b/bt5/erp5_web_shadir/SkinTemplateItem/portal_skins/erp5_web_shadir/WebSite_getJSONSchema.py
new file mode 100644
index 00000000000..6e206282655
--- /dev/null
+++ b/bt5/erp5_web_shadir/SkinTemplateItem/portal_skins/erp5_web_shadir/WebSite_getJSONSchema.py
@@ -0,0 +1,39 @@
+return {
+   'type': 'array',
+   'items': [
+      {'type': 'object',
+       'properties':{
+          'file':{
+             'type': 'string',
+             'required': True,
+          },
+          'urlmd5': {
+             'type': 'string',
+             'required': True,
+          },
+          'sha512': {
+             'type': 'string',
+             'required': True,
+          },
+          'creation_date': {
+             'type': 'string',
+             'required': False,
+          },
+          'expiration_date': {
+             'type': 'string',
+             'required': False,
+          },
+          'distribution': {
+             'type': 'string',
+             'required': False,
+          },
+          'architecture': {
+             'type': 'string',
+             'required': False,
+          },
+       }
+     },
+     {'type': 'string',
+      'blank': True},
+   ]
+}
diff --git a/bt5/erp5_web_shadir/SkinTemplateItem/portal_skins/erp5_web_shadir/WebSite_getJSONSchema.xml b/bt5/erp5_web_shadir/SkinTemplateItem/portal_skins/erp5_web_shadir/WebSite_getJSONSchema.xml
new file mode 100644
index 00000000000..cb7e99e54eb
--- /dev/null
+++ b/bt5/erp5_web_shadir/SkinTemplateItem/portal_skins/erp5_web_shadir/WebSite_getJSONSchema.xml
@@ -0,0 +1,62 @@
+<?xml version="1.0"?>
+<ZopeData>
+  <record id="1" aka="AAAAAAAAAAE=">
+    <pickle>
+      <global name="PythonScript" module="Products.PythonScripts.PythonScript"/>
+    </pickle>
+    <pickle>
+      <dictionary>
+        <item>
+            <key> <string>Script_magic</string> </key>
+            <value> <int>3</int> </value>
+        </item>
+        <item>
+            <key> <string>_bind_names</string> </key>
+            <value>
+              <object>
+                <klass>
+                  <global name="NameAssignments" module="Shared.DC.Scripts.Bindings"/>
+                </klass>
+                <tuple/>
+                <state>
+                  <dictionary>
+                    <item>
+                        <key> <string>_asgns</string> </key>
+                        <value>
+                          <dictionary>
+                            <item>
+                                <key> <string>name_container</string> </key>
+                                <value> <string>container</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_context</string> </key>
+                                <value> <string>context</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_m_self</string> </key>
+                                <value> <string>script</string> </value>
+                            </item>
+                            <item>
+                                <key> <string>name_subpath</string> </key>
+                                <value> <string>traverse_subpath</string> </value>
+                            </item>
+                          </dictionary>
+                        </value>
+                    </item>
+                  </dictionary>
+                </state>
+              </object>
+            </value>
+        </item>
+        <item>
+            <key> <string>_params</string> </key>
+            <value> <string></string> </value>
+        </item>
+        <item>
+            <key> <string>id</string> </key>
+            <value> <string>WebSite_getJSONSchema</string> </value>
+        </item>
+      </dictionary>
+    </pickle>
+  </record>
+</ZopeData>
-- 
2.30.9