Commit 4bf4934b authored by Trond Aasan's avatar Trond Aasan
Browse files

CIM-58 Rename update_person to upsert_person

The API endpoint is called upsert
parent a0949cae
......@@ -60,20 +60,20 @@ from cim_client import CimClient, CimEndpoints
from cim_client.models import PersonBase, PersonList
c = CimClient(
CimEndpoints(url='https://example.com',
update_person_url='/_webservices/?ws=contacts/upsert/1.0',
upsert_person_url='/_webservices/?ws=contacts/upsert/1.0',
delete_person_url='/_webservices/?ws=contacts/delete/1.0'),
tokens={'api_key': {'X-Gravitee-API-Key': 'c-d-a-b'},})
upd_schema = c.get_update_person_schema()
upd_schema = c.get_upsert_person_schema()
del_schema = c.get_delete_person_schema()
person = PersonBase.from_dict({'username': 'foo', 'firstname': 'John', 'lastname': 'Doe'})
response1 = c.update_person(person)
response1 = c.upsert_person(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.update_person(personlist)
response2 = c.upsert_person(personlist)
response3 = c.delete_person(person.username)
```
......@@ -69,13 +69,13 @@ class CimEndpoints:
def __init__(
self,
url: str,
update_person_url: Optional[str] = None,
upsert_person_url: Optional[str] = None,
delete_person_url: Optional[str] = None,
import_organisations_url: Optional[str] = None,
):
""" Get endpoints relative to the CIM API URL. """
self.baseurl = url
self.update_person_url = update_person_url or "?ws=contacts/upsert/1.0"
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.import_organisations_url = (
import_organisations_url or "?ws=contacts/organisations/1.0"
......@@ -84,14 +84,14 @@ class CimEndpoints:
def __repr__(self) -> str:
return "{cls.__name__}({url!r})".format(cls=type(self), url=self.baseurl)
def update_person(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.update_person_url)
def upsert_person(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.upsert_person_url)
def delete_person(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.delete_person_url)
def get_update_person_schema(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.update_person_url)
def get_upsert_person_schema(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.upsert_person_url)
def get_delete_person_schema(self) -> str:
return urllib.parse.urljoin(self.baseurl, self.delete_person_url)
......@@ -192,7 +192,7 @@ class CimClient(object):
return data
return cls.from_dict(data)
def get_update_person_schema(self) -> JsonType:
def get_upsert_person_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_update_person_schema()
url = self.endpoints.get_upsert_person_schema()
response = self.get(
url,
auth=self.auth,
......@@ -226,7 +226,7 @@ class CimClient(object):
)
return response
def post_person(
def upsert_person(
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.update_person()
url = self.endpoints.upsert_person()
if isinstance(persondata, PersonList):
persondata = persondata.persons
......@@ -291,17 +291,6 @@ class CimClient(object):
response.raise_for_status()
return None
def update_person(
self,
persons: Union[List[PersonBase], PersonBase, PersonList],
) -> Union[Tuple[str, bytes], None]:
"""Convenience method for updating
:param persons: The person or people we want to update in CIM
:return: String describing status or None if not found
"""
return self.post_person(persons)
def get_import_organisations_schema(self) -> JsonType:
"""GETs the current schema from CIM
......
......@@ -3,7 +3,7 @@
"client": {
"endpoints": {
"url": "https://api-gateway.uio.no/sws/v2/",
"update_person_url": "_webservices/?ws=contacts/upsert/1.0",
"upsert_person_url": "_webservices/?ws=contacts/upsert/1.0",
"delete_person_url": "_webservices/?ws=contacts/delete/1.0",
"import_organisations_url": "_webservices/?ws=contacts/organisations/1.0"
},
......
{
"endpoints": {
"url": "https://example.com",
"update_person_url": "update_person_url",
"upsert_person_url": "update_person_url",
"delete_person_url": "delete_person_url",
"import_organisations_url": "import_organisations_url"
},
......
......@@ -70,23 +70,23 @@ def test_init_from_dict(config_example):
e = client.endpoints
assert isinstance(e, CimEndpoints)
assert e.baseurl == config_example["endpoints"]["url"]
assert e.update_person_url == "update_person_url"
assert e.upsert_person_url == "update_person_url"
assert e.delete_person_url == "delete_person_url"
assert e.import_organisations_url == "import_organisations_url"
assert e.baseurl == "https://example.com"
def test_get_update_person_schema(client, requests_mock):
"""Ensure getting update schema works"""
def test_get_upsert_person_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_update_person_schema()
response = client.get_upsert_person_schema()
assert response == {"foo": "bar"}
def test_get_delete_person_schema(client, requests_mock):
"""Ensure getting update schema works"""
"""Ensure getting delete schema works"""
requests_mock.get(
"https://localhost/_webservices/?ws=contacts/delete/1.0", json={"foo": "bar"}
)
......@@ -127,42 +127,42 @@ def test_delete_person_failure_404(client, john_doe, requests_mock):
client.delete_person(Person.from_dict(john_doe).username)
def test_update_one_person(client, john_doe, requests_mock):
"""Ensure updating one works"""
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.update_person(Person.from_dict(john_doe))
response = client.upsert_person(Person.from_dict(john_doe))
assert response == ("Import success", b"")
def test_update_many_person(client, john_doe, jane_doe, requests_mock):
"""Ensure updating multiple works"""
def test_upsert_persons_many(client, john_doe, jane_doe, requests_mock):
"""Ensure upserting multiple works"""
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.update_person(personlist)
response = client.upsert_person(personlist)
assert response == ("Import success", b"")
def test_update_failure_400(client, john_doe, requests_mock):
def test_upsert_persons_failure_400(client, john_doe, requests_mock):
"""Ensure 400 raises an error"""
requests_mock.post(
"https://localhost/_webservices/?ws=contacts/upsert/1.0", status_code=400
)
with pytest.raises(HTTPError):
client.update_person(Person.from_dict(john_doe))
client.upsert_person(Person.from_dict(john_doe))
def test_update_failure_500(client, john_doe, requests_mock):
def test_upsert_persons_failure_500(client, john_doe, requests_mock):
"""Ensure 500 raises an error"""
requests_mock.post(
"https://localhost/_webservices/?ws=contacts/upsert/1.0", status_code=500
)
with pytest.raises(HTTPError):
client.update_person(Person.from_dict(john_doe))
client.upsert_person(Person.from_dict(john_doe))
def test_get_import_organisations_schema(client, requests_mock):
"""Ensure getting update schema works"""
"""Ensure getting import organisations schema works"""
requests_mock.get(
"https://localhost/_webservices/?ws=contacts/organisations/1.0",
json={"foo": "bar"},
......
......@@ -6,8 +6,8 @@ def test_init(baseurl):
assert endpoints.baseurl == baseurl
def test_update_person(baseurl, endpoints):
url = endpoints.update_person()
def test_upsert_person(baseurl, endpoints):
url = endpoints.upsert_person()
assert url == baseurl + "?ws=contacts/upsert/1.0"
......@@ -16,8 +16,8 @@ def test_delete_person(baseurl, endpoints):
assert url == baseurl + "?ws=contacts/delete/1.0"
def test_get_update_person_schema(baseurl, endpoints):
url = endpoints.get_update_person_schema()
def test_get_upsert_person_schema(baseurl, endpoints):
url = endpoints.get_upsert_person_schema()
assert url == baseurl + "?ws=contacts/upsert/1.0"
......@@ -26,9 +26,9 @@ def test_get_delete_person_schema(baseurl, endpoints):
assert url == baseurl + "?ws=contacts/delete/1.0"
def test_custom_update_person(custom_endpoints, baseurl):
def test_custom_upsert_person(custom_endpoints, baseurl):
assert (
custom_endpoints.update_person() == baseurl + "custom/?ws=contacts/upsert/1.0"
custom_endpoints.upsert_person() == baseurl + "custom/?ws=contacts/upsert/1.0"
)
......@@ -38,9 +38,9 @@ def test_custom_delete_person(custom_endpoints, baseurl):
)
def test_custom_get_update_person_schema(custom_endpoints, baseurl):
def test_custom_get_upsert_person_schema(custom_endpoints, baseurl):
assert (
custom_endpoints.get_update_person_schema()
custom_endpoints.get_upsert_person_schema()
== baseurl + "custom/?ws=contacts/upsert/1.0"
)
......
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