Commit df8d9ffc authored by Titouan Soulard's avatar Titouan Soulard

erp5_action_information_api: update calls to API in tests

- Remove Manager proxy role
- Use `publish` method for calling API
- Refactor API calls
parent 21c05457
import json
from AccessControl import getSecurityManager
u = getSecurityManager().getUser()
context.log(u.getUserName())
base_url_absolute = portal.portal_callables.absolute_url().strip()
base_url_relative = portal.portal_callables.getPath().strip()
......
......@@ -52,14 +52,6 @@
<key> <string>_params</string> </key>
<value> <string>portal, action_filter, url</string> </value>
</item>
<item>
<key> <string>_proxy_roles</string> </key>
<value>
<tuple>
<string>Manager</string>
</tuple>
</value>
</item>
<item>
<key> <string>id</string> </key>
<value> <string>ActionInformationAPI_api_slap</string> </value>
......
......@@ -25,11 +25,8 @@
#
##############################################################################
import httplib
import io
import json
import ssl
import urlparse
from Products.ERP5Type.tests.ERP5TypeTestCase import ERP5TypeTestCase
from Products.ERP5Type.tests.utils import createZODBPythonScript
......@@ -97,7 +94,7 @@ class TestActionInformationAPI(ERP5TypeTestCase):
self.tic()
self.commit()
script = createZODBPythonScript(
createZODBPythonScript(
self.portal.portal_skins.custom,
action_script,
"data",
......@@ -112,40 +109,29 @@ if email is None:
context.setDefaultEmailUrlString(email)
return {"status": 200}""")
script.manage_proxy(roles=["Manager"])
self.tic()
self.commit()
def test_hyperdocument(self):
base_url = self.portal.portal_callables.absolute_url()
endpoint_url = self.web_service.absolute_url() + "/api"
endpoint_scheme, endpoint_netloc, _, _, _ = urlparse.urlsplit(endpoint_url)
expected_document = """{
"links": [
{
"url": "%s",
"$schemaResponse": "%s/Person_updateEmail/getOutputJSONSchema",
"method": "POST",
"$schemaRequest": "%s/Person_updateEmail/getInputJSONSchema"
}
]
}""" % (endpoint_url, base_url, base_url)
self.login()
if endpoint_scheme == "https":
connection = httplib.HTTPSConnection(endpoint_netloc, context=ssl._create_unverified_context(), timeout=10)
else:
connection = httplib.HTTPConnection(endpoint_netloc, timeout=10)
connection.request(
method="GET",
url=endpoint_url
def loggedInRequest(self, path, method, content):
return self.publish(
self.web_service.getPath() + path,
request_method=method,
stdin=io.BytesIO(
json.dumps(content).encode()
),
env={"CONTENT_TYPE": "application/json"},
user="ERP5TypeTestCase"
)
response = connection.getresponse()
response_body = response.read()
connection.close()
self.assertEqual(response_body, expected_document)
def test_hyperdocument(self):
response = self.loggedInRequest("/api", "GET", {})
response_json = json.loads(response.getBody())
self.assertEqual(len(response_json["links"]), 1)
self.assertTrue("/api" in response_json["links"][0]["url"])
self.assertTrue("Person_updateEmail/getInputJSONSchema" in response_json["links"][0]["$schemaRequest"])
self.assertTrue("Person_updateEmail/getOutputJSONSchema" in response_json["links"][0]["$schemaResponse"])
def test_update(self):
person = self.portal.person_module.newContent(
......@@ -157,20 +143,12 @@ return {"status": 200}""")
self.tic()
self.commit()
self.login()
response = self.publish(
self.web_service.getPath() + "/api",
request_method="POST",
stdin=io.BytesIO(
json.dumps({
response = self.loggedInRequest("/api", "POST", {
"$schemaDocument": self.portal.portal_callables.absolute_url() + "/Person_updateEmail/getInputJSONSchema",
"reference": "%s_person" % self.current_id,
"email": "alice@looking.glass"
}).encode()
),
env={"CONTENT_TYPE": "application/json"},
user="ERP5TypeTestCase"
)
})
self.assertEqual(response.getBody(), json.dumps({ "status": 200 }, indent=2))
self.assertEqual(response.getStatus(), 200)
self.assertEqual(person.getDefaultEmailUrlString(), "alice@looking.glass")
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