Skip to content

Commit 212e751

Browse files
committed
create protected domain via the sdk
1 parent 11c0ca1 commit 212e751

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

sdk/rapid/exceptions.py

+8
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,11 @@ class SubjectNotFoundException(Exception):
8484

8585
class SubjectDeletionFailedException(Exception):
8686
pass
87+
88+
89+
class InvalidDomainNameException(Exception):
90+
pass
91+
92+
93+
class DomainConflictException(Exception):
94+
pass

sdk/rapid/rapid.py

+28
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
InvalidPermissionsException,
2828
SubjectAlreadyExistsException,
2929
SubjectNotFoundException,
30+
InvalidDomainNameException,
31+
DomainConflictException,
3032
)
3133

3234

@@ -413,3 +415,29 @@ def update_subject_permissions(self, subject_id: str, permissions: list[str]):
413415
raise InvalidPermissionsException(
414416
"One or more of the provided permissions is invalid or duplicated"
415417
)
418+
419+
def create_protected_domain(self, name: str):
420+
"""
421+
Creates a new protected domain.
422+
423+
Args:
424+
name (str): The name of the protected domain to create.
425+
426+
Raises:
427+
rapid.exceptions.InvalidDomainNameException: If the domain name is invalid.
428+
rapid.exceptions.DomainConflictException: If the domain already exists.
429+
430+
"""
431+
url = f"{self.auth.url}/protected_domains/{name}"
432+
response = requests.post(
433+
url, headers=self.generate_headers(), timeout=TIMEOUT_PERIOD
434+
)
435+
data = json.loads(response.content.decode("utf-8"))
436+
if response.status_code == 201:
437+
return
438+
elif response.status_code == 400:
439+
raise InvalidDomainNameException(data["details"])
440+
elif response.status_code == 409:
441+
raise DomainConflictException(data["details"])
442+
443+
raise Exception("Failed to create protected domain")

sdk/tests/test_rapid.py

+54-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
InvalidPermissionsException,
1919
SubjectNotFoundException,
2020
SubjectAlreadyExistsException,
21+
InvalidDomainNameException,
22+
DomainConflictException,
2123
)
2224
from .conftest import RAPID_URL, RAPID_TOKEN
2325

@@ -392,7 +394,9 @@ def test_delete_client_failure(self, requests_mock: Mocker, rapid: Rapid):
392394
rapid.delete_client("xxx-yyy-zzz")
393395

394396
@pytest.mark.usefixtures("requests_mock", "rapid")
395-
def update_subject_permissions_success(self, requests_mock: Mocker, rapid: Rapid):
397+
def test_update_subject_permissions_success(
398+
self, requests_mock: Mocker, rapid: Rapid
399+
):
396400
mocked_response = {"data": "dummy"}
397401
requests_mock.put(
398402
f"{RAPID_URL}/subject/permissions", json=mocked_response, status_code=200
@@ -401,10 +405,58 @@ def update_subject_permissions_success(self, requests_mock: Mocker, rapid: Rapid
401405
assert res == mocked_response
402406

403407
@pytest.mark.usefixtures("requests_mock", "rapid")
404-
def update_subject_permissions_failure(self, requests_mock: Mocker, rapid: Rapid):
408+
def test_update_subject_permissions_failure(
409+
self, requests_mock: Mocker, rapid: Rapid
410+
):
405411
mocked_response = {"data": "dummy"}
406412
requests_mock.put(
407413
f"{RAPID_URL}/subject/permissions", json=mocked_response, status_code=400
408414
)
409415
with pytest.raises(InvalidPermissionsException):
410416
rapid.update_subject_permissions("xxx-yyy-zzz", ["READ_ALL"])
417+
418+
@pytest.mark.usefixtures("requests_mock", "rapid")
419+
def test_create_protected_domain_invalid_name_failure(
420+
self, requests_mock: Mocker, rapid: Rapid
421+
):
422+
mocked_response = {
423+
"details": "The value set for domain [dummy] can only contain alphanumeric and underscore `_` characters and must start with an alphabetic character"
424+
}
425+
requests_mock.post(
426+
f"{RAPID_URL}/protected_domains/dummy",
427+
json=mocked_response,
428+
status_code=400,
429+
)
430+
with pytest.raises(InvalidDomainNameException) as exc_info:
431+
rapid.create_protected_domain("dummy")
432+
433+
assert (
434+
str(exc_info.value)
435+
== "The value set for domain [dummy] can only contain alphanumeric and underscore `_` characters and must start with an alphabetic character"
436+
)
437+
438+
@pytest.mark.usefixtures("requests_mock", "rapid")
439+
def test_create_protected_domain_conflict_failure(
440+
self, requests_mock: Mocker, rapid: Rapid
441+
):
442+
mocked_response = {"details": "The protected domain, [dummy] already exists"}
443+
requests_mock.post(
444+
f"{RAPID_URL}/protected_domains/dummy",
445+
json=mocked_response,
446+
status_code=409,
447+
)
448+
with pytest.raises(DomainConflictException) as exc_info:
449+
rapid.create_protected_domain("dummy")
450+
451+
assert str(exc_info.value) == "The protected domain, [dummy] already exists"
452+
453+
@pytest.mark.usefixtures("requests_mock", "rapid")
454+
def test_create_protected_domain_success(self, requests_mock: Mocker, rapid: Rapid):
455+
mocked_response = {"data": "dummy"}
456+
requests_mock.post(
457+
f"{RAPID_URL}/protected_domains/dummy",
458+
json=mocked_response,
459+
status_code=201,
460+
)
461+
res = rapid.create_protected_domain("dummy")
462+
assert res is None

0 commit comments

Comments
 (0)