Commit 1a6ccb14 authored by Trond Aasan's avatar Trond Aasan
Browse files

CIM-58 Pluralise endpoint names to show that they take lists

parent 8b2909fb
......@@ -60,20 +60,20 @@ from cim_client import CimClient, CimEndpoints
from cim_client.models import PersonBase, PersonList
c = CimClient(
CimEndpoints(url='https://example.com',
upsert_person_url='/_webservices/?ws=contacts/upsert/1.0',
delete_person_url='/_webservices/?ws=contacts/delete/1.0'),
upsert_persons_url='/_webservices/?ws=contacts/upsert/1.0',
delete_persons_url='/_webservices/?ws=contacts/delete/1.0'),
tokens={'api_key': {'X-Gravitee-API-Key': 'c-d-a-b'},})
upd_schema = c.get_upsert_person_schema()
del_schema = c.get_delete_person_schema()
upd_schema = c.get_upsert_persons_schema()
del_schema = c.get_delete_persons_schema()
person = PersonBase.from_dict({'username': 'foo', 'firstname': 'John', 'lastname': 'Doe'})
response1 = c.upsert_person(person)
response1 = c.upsert_persons(person)
person2 = PersonBase.from_dict({'username': 'bar', 'firstname': 'Petter', 'lastname': 'Smart'})
persons = [person, person2]
personlist = PersonList(persons=persons)
# Note that delete_person supports both PersonList and [Person, Person, ...]
response2 = c.upsert_person(personlist)
response3 = c.delete_person(person.username)
response2 = c.upsert_persons(personlist)
response3 = c.delete_persons(person.username)
```
......@@ -69,14 +69,14 @@ class CimEndpoints:
def __init__(
self,
url: str,
upsert_person_url: Optional[str] = None,
delete_person_url: Optional[str] = None,
upsert_persons_url: Optional[str] = None,
delete_persons_url: Optional[str] = None,
import_organisations_url: Optional[str] = None,
):
""" Get endpoints relative to the CIM API URL. """
self.baseurl = url
self.upsert_person_url = upsert_person_url or "?ws=contacts/upsert/1.0"
self.delete_person_url = delete_person_url or "?ws=contacts/delete/1.0"
self.upsert_persons_url = upsert_persons_url or "?ws=contacts/upsert/1.0"
self.delete_persons_url = delete_persons_url or "?ws=contacts/delete/1.0"
self.import_organisations_url = (
import_organisations_url or "?ws=contacts/organisations/1.0"
)
......@@ -84,17 +84,17 @@ class CimEndpoints:
def __repr__(self) -> str:
return "{cls.__name__}({url!r})".format(cls=type(self), url=self.baseurl)
def upsert_person(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.upsert_person_url)
def upsert_persons(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.upsert_persons_url)
def delete_person(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.delete_person_url)
def delete_persons(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.delete_persons_url)
def get_upsert_person_schema(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.upsert_person_url)
def get_upsert_persons_schema(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.upsert_persons_url)
def get_delete_person_schema(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.delete_person_url)
def get_delete_persons_schema(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.delete_persons_url)
def import_organisations(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.import_organisations_url)
......@@ -192,7 +192,7 @@ class CimClient(object):
return data
return cls.from_dict(data)
def get_upsert_person_schema(self) -> JsonType:
def get_upsert_persons_schema(self) -> JsonType:
"""GETs the current schema from CIM
The schema can change depending on the installation.
......@@ -201,7 +201,7 @@ class CimClient(object):
checks whether the schema has changed lately so you don't update
using an outdated schema.
"""
url = self.endpoints.get_upsert_person_schema()
url = self.endpoints.get_upsert_persons_schema()
response = self.get(
url,
auth=self.auth,
......@@ -209,7 +209,7 @@ class CimClient(object):
)
return response
def get_delete_person_schema(self) -> JsonType:
def get_delete_persons_schema(self) -> JsonType:
"""GETs the current schema from CIM
The schema can change depending on the installation.
......@@ -218,7 +218,7 @@ class CimClient(object):
checks whether the schema has changed lately so you don't update
using an outdated schema.
"""
url = self.endpoints.get_delete_person_schema()
url = self.endpoints.get_delete_persons_schema()
response = self.get(
url,
auth=self.auth,
......@@ -226,7 +226,7 @@ class CimClient(object):
)
return response
def upsert_person(
def upsert_persons(
self,
persondata: Union[List[PersonBase], PersonBase, PersonList],
) -> Optional[Tuple[str, bytes]]:
......@@ -239,7 +239,7 @@ class CimClient(object):
Note also that the response will not contain info about failing to find
someone. You will have to compare with what you posted.
"""
url = self.endpoints.upsert_person()
url = self.endpoints.upsert_persons()
if isinstance(persondata, PersonList):
persondata = persondata.persons
......@@ -257,7 +257,7 @@ class CimClient(object):
response.raise_for_status()
return None
def delete_person(
def delete_persons(
self,
person_ids: List[str],
) -> Union[Tuple[str, bytes], None]:
......@@ -271,7 +271,7 @@ class CimClient(object):
:param person_ids: The person or people we want to delete from CIM
:return: String describing status or None if not found
"""
url = self.endpoints.delete_person()
url = self.endpoints.delete_persons()
headers = {"Content-Type": "application/json"}
response = self.post(url, json=person_ids, auth=self.auth, headers=headers)
......
......@@ -3,8 +3,8 @@
"client": {
"endpoints": {
"url": "https://example.com",
"upsert_person_url": "update_person_url",
"delete_person_url": "delete_person_url",
"upsert_persons_url": "upsert_persons_url",
"delete_persons_url": "delete_persons_url",
"import_organisations_url": "import_organisations_url"
},
"headers": {
......
......@@ -70,53 +70,53 @@ def test_init_from_dict(config_example):
e = client.endpoints
assert isinstance(e, CimEndpoints)
assert e.baseurl == config_example["endpoints"]["url"]
assert e.upsert_person_url == "update_person_url"
assert e.delete_person_url == "delete_person_url"
assert e.upsert_persons_url == "upsert_persons_url"
assert e.delete_persons_url == "delete_persons_url"
assert e.import_organisations_url == "import_organisations_url"
assert e.baseurl == "https://example.com"
def test_get_upsert_person_schema(client, requests_mock):
def test_get_upsert_persons_schema(client, requests_mock):
"""Ensure getting upsert schema works"""
requests_mock.get(
"https://localhost/_webservices/?ws=contacts/upsert/1.0", json={"foo": "bar"}
)
response = client.get_upsert_person_schema()
response = client.get_upsert_persons_schema()
assert response == {"foo": "bar"}
def test_get_delete_person_schema(client, requests_mock):
def test_get_delete_persons_schema(client, requests_mock):
"""Ensure getting delete schema works"""
requests_mock.get(
"https://localhost/_webservices/?ws=contacts/delete/1.0", json={"foo": "bar"}
)
response = client.get_delete_person_schema()
response = client.get_delete_persons_schema()
assert response == {"foo": "bar"}
def test_delete_person_success_200(client, john_doe, requests_mock):
def test_delete_persons_success_200(client, john_doe, requests_mock):
"""Ensure 404 returns None"""
requests_mock.post(
"https://localhost/_webservices/?ws=contacts/delete/1.0",
status_code=200,
json=["Delete person: johndoe(9999)"],
)
response = client.delete_person(Person.from_dict(john_doe).username)
response = client.delete_persons(Person.from_dict(john_doe).username)
assert response == ("Delete success", b'["Delete person: johndoe(9999)"]')
def test_delete_person_success_404(client, john_doe, requests_mock):
def test_delete_persons_success_404(client, john_doe, requests_mock):
"""Ensure 404 returns None when response text is `No person found: No deletion`"""
requests_mock.post(
"https://localhost/_webservices/?ws=contacts/delete/1.0",
status_code=404,
text="No person found: No deletion",
)
response = client.delete_person(Person.from_dict(john_doe).username)
response = client.delete_persons(Person.from_dict(john_doe).username)
assert response is None
def test_delete_person_failure_404(client, john_doe, requests_mock):
def test_delete_persons_failure_404(client, john_doe, requests_mock):
"""Ensure 404 fails when response text is not `No person found: No deletion`"""
requests_mock.post(
"https://localhost/_webservices/?ws=contacts/delete/1.0",
......@@ -124,13 +124,13 @@ def test_delete_person_failure_404(client, john_doe, requests_mock):
text='["Something, something, something dark side"]',
)
with pytest.raises(HTTPError):
client.delete_person(Person.from_dict(john_doe).username)
client.delete_persons(Person.from_dict(john_doe).username)
def test_upsert_persons_one(client, john_doe, requests_mock):
"""Ensure upserting one works"""
requests_mock.post("https://localhost/_webservices/?ws=contacts/upsert/1.0")
response = client.upsert_person(Person.from_dict(john_doe))
response = client.upsert_persons(Person.from_dict(john_doe))
assert response == ("Import success", b"")
......@@ -139,7 +139,7 @@ def test_upsert_persons_many(client, john_doe, jane_doe, requests_mock):
personlist = [Person.from_dict(i) for i in (john_doe, jane_doe)]
requests_mock.post("https://localhost/_webservices/?ws=contacts/upsert/1.0")
response = client.upsert_person(personlist)
response = client.upsert_persons(personlist)
assert response == ("Import success", b"")
......@@ -149,7 +149,7 @@ def test_upsert_persons_failure_400(client, john_doe, requests_mock):
"https://localhost/_webservices/?ws=contacts/upsert/1.0", status_code=400
)
with pytest.raises(HTTPError):
client.upsert_person(Person.from_dict(john_doe))
client.upsert_persons(Person.from_dict(john_doe))
def test_upsert_persons_failure_500(client, john_doe, requests_mock):
......@@ -158,7 +158,7 @@ def test_upsert_persons_failure_500(client, john_doe, requests_mock):
"https://localhost/_webservices/?ws=contacts/upsert/1.0", status_code=500
)
with pytest.raises(HTTPError):
client.upsert_person(Person.from_dict(john_doe))
client.upsert_persons(Person.from_dict(john_doe))
def test_get_import_organisations_schema(client, requests_mock):
......
......@@ -6,47 +6,47 @@ def test_init(baseurl):
assert endpoints.baseurl == baseurl
def test_upsert_person(baseurl, endpoints):
url = endpoints.upsert_person()
def test_upsert_persons(baseurl, endpoints):
url = endpoints.upsert_persons()
assert url == baseurl + "?ws=contacts/upsert/1.0"
def test_delete_person(baseurl, endpoints):
url = endpoints.delete_person()
def test_delete_persons(baseurl, endpoints):
url = endpoints.delete_persons()
assert url == baseurl + "?ws=contacts/delete/1.0"
def test_get_upsert_person_schema(baseurl, endpoints):
url = endpoints.get_upsert_person_schema()
def test_get_upsert_persons_schema(baseurl, endpoints):
url = endpoints.get_upsert_persons_schema()
assert url == baseurl + "?ws=contacts/upsert/1.0"
def test_get_delete_person_schema(baseurl, endpoints):
url = endpoints.get_delete_person_schema()
def test_get_delete_persons_schema(baseurl, endpoints):
url = endpoints.get_delete_persons_schema()
assert url == baseurl + "?ws=contacts/delete/1.0"
def test_custom_upsert_person(custom_endpoints, baseurl):
def test_custom_upsert_persons(custom_endpoints, baseurl):
assert (
custom_endpoints.upsert_person() == baseurl + "custom/?ws=contacts/upsert/1.0"
custom_endpoints.upsert_persons() == baseurl + "custom/?ws=contacts/upsert/1.0"
)
def test_custom_delete_person(custom_endpoints, baseurl):
def test_custom_delete_persons(custom_endpoints, baseurl):
assert (
custom_endpoints.delete_person() == baseurl + "custom/?ws=contacts/delete/1.0"
custom_endpoints.delete_persons() == baseurl + "custom/?ws=contacts/delete/1.0"
)
def test_custom_get_upsert_person_schema(custom_endpoints, baseurl):
def test_custom_get_upsert_persons_schema(custom_endpoints, baseurl):
assert (
custom_endpoints.get_upsert_person_schema()
custom_endpoints.get_upsert_persons_schema()
== baseurl + "custom/?ws=contacts/upsert/1.0"
)
def test_custom_get_delete_person_schema(custom_endpoints, baseurl):
def test_custom_get_delete_persons_schema(custom_endpoints, baseurl):
assert (
custom_endpoints.get_delete_person_schema()
custom_endpoints.get_delete_persons_schema()
== baseurl + "custom/?ws=contacts/delete/1.0"
)
Supports Markdown
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