Skip to content

Commit

Permalink
testlib_refactor: extract password API requests into a separate class
Browse files Browse the repository at this point in the history
Jira ticket: CMK-20440

Change-Id: I14408b7bf66f97253ad293733c1047fadc14a3a9
  • Loading branch information
asyash26 committed Dec 17, 2024
1 parent 053b6ae commit 88a1f32
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions tests/testlib/openapi_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ def __init__(
self.background_jobs = BackgroundJobsAPI(self)
self.dcd = DcdAPI(self)
self.ldap_connection = LDAPConnectionAPI(self)
self.passwords = PasswordsAPI(self)

def set_authentication_header(self, user: str, password: str) -> None:
self.headers["Authorization"] = f"Bearer {user} {password}"
Expand Down Expand Up @@ -299,30 +300,6 @@ def _handle_wait_redirect(

time.sleep(0.5)

def create_password(
self,
ident: str,
title: str,
comment: str,
password: str,
owner: str = "admin",
) -> None:
"""Create a password via REST API."""
response = self.post(
"/domain-types/password/collections/all",
json={
"ident": ident,
"title": title,
"comment": comment,
"documentation_url": "localhost",
"password": password,
"owner": owner,
"shared": ["all"],
},
)
if response.status_code != 200:
raise UnexpectedResponse.from_response(response)


class BaseAPI:
def __init__(self, session: CMKOpenApiSession) -> None:
Expand Down Expand Up @@ -1136,3 +1113,29 @@ def delete(self, ldap_id: str) -> None:
resp = self.session.delete(f"/objects/ldap_connection/{ldap_id}", headers={"If-Match": "*"})
if resp.status_code != 204:
raise UnexpectedResponse.from_response(resp)


class PasswordsAPI(BaseAPI):
def create(
self,
ident: str,
title: str,
comment: str,
password: str,
owner: str = "admin",
) -> None:
"""Create a password via REST API."""
response = self.session.post(
"/domain-types/password/collections/all",
json={
"ident": ident,
"title": title,
"comment": comment,
"documentation_url": "localhost",
"password": password,
"owner": owner,
"shared": ["all"],
},
)
if response.status_code != 200:
raise UnexpectedResponse.from_response(response)

0 comments on commit 88a1f32

Please sign in to comment.