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