Commit a0915b07 authored by Jo Sama's avatar Jo Sama 😱
Browse files

Add person-related functions

parent 65525604
......@@ -3,7 +3,7 @@ import os
import json
from topdesk_client import get_client
from topdesk_client.models import Operator
from topdesk_client.models import Operator, Person
# @pytest.fixture
# def request():
......@@ -55,3 +55,25 @@ def uncreated_operator():
@pytest.fixture
def operator_list_data():
return load_json_file('operator_list.json')
@pytest.fixture
def person_data():
return load_json_file('person.json')
@pytest.fixture
def person():
d = load_json_file('person.json')
return Person.from_dict(d)
@pytest.fixture
def uncreated_person():
return Person.from_dict(
load_json_file('uncreated_person.json'))
@pytest.fixture
def person_list_data():
return load_json_file('person_list.json')
{
"id": "a-b-c-d",
"surName": "Josefsen",
"firstName": "Franz",
"branch": {
"id": "b-c-d-e",
"name": "USIT",
"clientReferenceNumber": "x-y-z",
"timeZone": "GMT+1",
"extraA": {
"id": "id_extra_a",
"name": "name_extra_a"
},
"extraB": {
"id": "id_extra_b",
"name": "name_extra_b"}
},
"phoneNumber": "+47 800 00 000",
"mobileNumber": "900 00 000",
"email": "f.j@example.org",
"networkLoginName": "fj",
"employeeNumber": "123456"
}
[
{
"id": "a-b-c-d",
"surName": "Josefsen",
"firstName": "Franz",
"branch": {
"id": "b-c-d-e",
"name": "USIT",
"clientReferenceNumber": "x-y-z",
"timeZone": "GMT+1",
"extraA": {
"id": "id_extra_a",
"name": "name_extra_a"
},
"extraB": {
"id": "id_extra_b",
"name": "name_extra_b"
}
},
"phoneNumber": "+47 800 00 000",
"mobileNumber": "900 00 000",
"email": "f.j@example.org",
"networkLoginName": "fj",
"employeeNumber": "234567"
},
{
"id": "c-d-e-f",
"surName": "Franzen",
"firstName": "Josef",
"branch": {
"id": "d-e-f-g",
"name": "LOS",
"clientReferenceNumber": "x-y-z",
"timeZone": "GMT+1",
"extraA": {
"id": "id_extra_a",
"name": "name_extra_a"
},
"extraB": {
"id": "id_extra_b",
"name": "name_extra_b"
}
},
"phoneNumber": "+47 800 00 001",
"mobileNumber": "900 00 001",
"email": "j.f@example.org",
"networkLoginName": "jf",
"employeeNumber": "123456"
}
]
{
"surName": "Josefsen",
"firstName": "Franz",
"branch": {
"id": "b-c-d-e",
"name": "USIT",
"clientReferenceNumber": "x-y-z",
"timeZone": "GMT+1",
"extraA": {
"id": "id_extra_a",
"name": "name_extra_a"
},
"extraB": {
"id": "id_extra_b",
"name": "name_extra_b"}
},
"phoneNumber": "+47 800 00 000",
"mobileNumber": "900 00 000",
"email": "f.j@example.org",
"networkLoginName": "fj",
"employeeNumber": "123456"
}
import types
from topdesk_client.models import Operator
from topdesk_client.models import Operator, Person
def test_get_operator(client, base_url, requests_mock, operator_data):
......@@ -44,3 +44,46 @@ def test_update_operator(client,
updated_operator = Operator.from_json(object_sent)
assert(updated_operator == operator)
def test_get_person(client, base_url, requests_mock, person_data):
identity = 'a-b-c-d'
requests_mock.get(client.urls.get_person(identity),
json=person_data)
expected = client.get_person(identity)
assert isinstance(expected, Person)
def test_list_persons(client, base_url, requests_mock, person_list_data):
requests_mock.get(client.urls.get_persons(),
json=person_list_data)
expected = client.list_persons()
assert isinstance(expected, types.GeneratorType)
for x in expected:
assert isinstance(x, Person)
def test_create_person(client,
base_url,
requests_mock,
uncreated_person):
m = requests_mock.post(client.urls.get_persons())
client.create_person(uncreated_person)
object_sent = m.request_history[-1].text
sent_person = Person.from_json(object_sent)
assert(sent_person == uncreated_person)
def test_update_person(client,
base_url,
requests_mock,
person):
m = requests_mock.patch(client.urls.get_person(person.id))
client.update_person(person)
object_sent = m.request_history[-1].text
updated_person = Person.from_json(object_sent)
assert(updated_person == person)
......@@ -3,7 +3,7 @@ import requests
from urllib.parse import urljoin, urlparse
from .models import Operator
from .models import Operator, Person
logger = logging.getLogger(__name__)
......@@ -21,6 +21,12 @@ class Endpoints:
def get_operators(self):
return self._prepend_base_url('/operators/')
def get_person(self, identity):
return urljoin(self._prepend_base_url('/persons/id/'), identity)
def get_persons(self):
return self._prepend_base_url('/persons/')
class TopDeskClient:
def __init__(self, url, headers=None, rewrite_url=None):
......@@ -94,6 +100,26 @@ class TopDeskClient:
for x in self.get(self.urls.get_operators()):
yield Operator.from_dict(x)
def get_person(self, identity):
url = self.urls.get_person(identity)
return Person.from_dict(self.get(url))
def create_person(self, person):
url = self.urls.get_persons()
self.post(url,
return_response=True,
data=person.json())
def update_person(self, person):
url = self.urls.get_person(person.id)
self.patch(url,
return_response=True,
data=person.json())
def list_persons(self):
for x in self.get(self.urls.get_persons()):
yield Person.from_dict(x)
def get_client(config):
return TopDeskClient(**config)
......@@ -172,3 +172,25 @@ class Operator(BaseModel):
'phone': {'alias': 'telephone'},
'mobile': {'alias': 'mobileNumber'},
}
class Person(BaseModel):
id: Optional[str]
first_name: str
last_name: str
user_name: str
phone: str
mobile: str
email: pydantic.EmailStr
employee_number: str
branch: BranchReference
class Config:
fields = {
'first_name': 'firstName',
'last_name': 'surName',
'user_name': 'networkLoginName',
'employee_number': 'employeeNumber',
'phone': 'phoneNumber',
'mobile': 'mobileNumber'
}
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