From 8b3ed1a8e6f1b4fc6637ee27a7b178ea89667f88 Mon Sep 17 00:00:00 2001
From: Julien Muchembled <jm@nexedi.com>
Date: Tue, 7 Feb 2023 21:38:33 +0100
Subject: [PATCH] erp5_web_shadir: update tests

See commit c45c229521756913777a2fc44edffb53d2e192df
("erp5_web_shadir: more useful checks").
---
 .../portal_components/test.erp5.ShaDirMixin.py        | 11 ++++-------
 .../portal_components/test.erp5.testShaDir.py         |  9 ++++-----
 .../portal_components/test.erp5.testShaDirExternal.py |  9 ++-------
 3 files changed, 10 insertions(+), 19 deletions(-)

diff --git a/bt5/erp5_web_shadir/TestTemplateItem/portal_components/test.erp5.ShaDirMixin.py b/bt5/erp5_web_shadir/TestTemplateItem/portal_components/test.erp5.ShaDirMixin.py
index 3b8ad994a3c..f734be307f1 100644
--- a/bt5/erp5_web_shadir/TestTemplateItem/portal_components/test.erp5.ShaDirMixin.py
+++ b/bt5/erp5_web_shadir/TestTemplateItem/portal_components/test.erp5.ShaDirMixin.py
@@ -27,11 +27,11 @@
 #
 ##############################################################################
 
-import base64
 import hashlib
 import json
 import platform
 import random
+from base64 import b64encode
 from DateTime import DateTime
 
 
@@ -47,8 +47,6 @@ class ShaDirMixin(object):
     self.portal = self.getPortal()
 
     self.key = 'mykey' + str(random.random())
-    self.file_name = 'file.txt'
-    self.urlmd5 = hashlib.md5(self.key).hexdigest()
     self.file_content = 'This is the content.'
     self.file_sha512sum = hashlib.sha512(self.file_content).hexdigest()
     self.distribution = 'pypi'
@@ -58,21 +56,20 @@ class ShaDirMixin(object):
     libc_version = '%s %s' % (platform.libc_ver()[0], platform.libc_ver()[1])
     self.architecture = '%s %s' % (platform.machine(), libc_version)
 
-    self.data_list = [json.dumps({'file': self.file_name,
-                      'urlmd5': self.urlmd5,
+    self.data_list = [json.dumps({
                       'sha512': self.file_sha512sum,
                       'creation_date': str(self.creation_date),
                       'expiration_date': str(self.expiration_date),
                       'distribution': self.distribution,
                       'architecture': self.architecture}),
-                      "User SIGNATURE goes here."]
+                      b64encode("User SIGNATURE goes here.")]
 
     self.data = json.dumps(self.data_list)
     self.sha512sum = hashlib.sha512(self.data).hexdigest()
 
     self.header_dict = {
       'Content-Type': 'application/json',
-      'Authorization': 'Basic %s' % (base64.encodestring('ERP5TypeTestCase:').strip())
+      'Authorization': 'Basic ' + b64encode('ERP5TypeTestCase:'),
     }
 
     module = self.portal.web_site_module
diff --git a/bt5/erp5_web_shadir/TestTemplateItem/portal_components/test.erp5.testShaDir.py b/bt5/erp5_web_shadir/TestTemplateItem/portal_components/test.erp5.testShaDir.py
index 166cf05fc11..8be4ea40f62 100644
--- a/bt5/erp5_web_shadir/TestTemplateItem/portal_components/test.erp5.testShaDir.py
+++ b/bt5/erp5_web_shadir/TestTemplateItem/portal_components/test.erp5.testShaDir.py
@@ -32,6 +32,7 @@ import httplib
 import urlparse
 import json
 import random
+from base64 import b64encode
 from unittest import expectedFailure
 from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
 from erp5.component.test.ShaDirMixin import ShaDirMixin
@@ -214,17 +215,15 @@ class TestShaDir(ShaDirMixin, ERP5TypeTestCase):
     self.postInformation()
     self.tic()
 
-    urlmd5_2 = 'anotherurlmd5' + str(random.random())
-    sha512_2 = 'anothersha512_2' + str(random.random())
+    sha512_2 = hashlib.sha512(str(random.random())).hexdigest()
     key_2 = 'another_key' + str(random.random())
-    data_list_2 = [json.dumps({'file': self.file_name,
-                      'urlmd5': urlmd5_2,
+    data_list_2 = [json.dumps({
                       'sha512': sha512_2,
                       'creation_date': str(self.creation_date),
                       'expiration_date': str(self.expiration_date),
                       'distribution': self.distribution,
                       'architecture': self.architecture}),
-                      "User SIGNATURE goes here."]
+                      b64encode("User SIGNATURE goes here.")]
     data_2 = json.dumps(data_list_2)
     self.postInformation(key_2, data_2)
     self.tic()
diff --git a/bt5/networkcache_erp5/TestTemplateItem/portal_components/test.erp5.testShaDirExternal.py b/bt5/networkcache_erp5/TestTemplateItem/portal_components/test.erp5.testShaDirExternal.py
index d1cbd3e59b7..ac179bb2e0a 100644
--- a/bt5/networkcache_erp5/TestTemplateItem/portal_components/test.erp5.testShaDirExternal.py
+++ b/bt5/networkcache_erp5/TestTemplateItem/portal_components/test.erp5.testShaDirExternal.py
@@ -146,14 +146,10 @@ class TestShaDirExternal(ShaDirMixin, ShaSecurityMixin, ERP5TypeTestCase):
     self.assertEqual(302, result.status)
 
   def test_external_post_with_wrong_data(self):
-    """
-      The data which is sent to the server must follow a JSON schema.
-      If the data does not follow the schema it must return the error.
-    """
     # Removing a required property
     data = json.loads(self.data)
     data[0] = json.loads(data[0])
-    data[0].pop('file')
+    del data[0]['sha512']
     data[0] = json.dumps(data[0])
     data = json.dumps(data)
 
@@ -165,7 +161,6 @@ class TestShaDirExternal(ShaDirMixin, ShaSecurityMixin, ERP5TypeTestCase):
       data = result.read()
     finally:
       connection.close()
-    self.assertTrue("Required field 'file' is missing" in data, data)
-    self.assertEqual(500, result.status)
+    self.assertEqual(400, result.status)
     self.assertEqual('text/html; charset=utf-8',
                                          result.getheader("content-type"))
-- 
2.30.9