@given('a truststore {truststore_name} with {intended_usage} exist')
[docs]defstep_truststore_exists(context:runner.Context,truststore_name:str,intended_usage:str)->None:# noqa: ARG001""". Args: context: Behave context. truststore_name (str): The name of the truststore. intended_usage (str): The intended usage of the truststore. """truststore_file_path=os.path.abspath(f"{CURRENT_DIR}/../../../tests/data/trust-store/trust_store.pem")usage=0ifintended_usage=="TLS":usage=1elifintended_usage=="Generic":usage=2withopen(truststore_file_path,'rb')asf:# Prepare POST datatruststore_add_form_data={'unique_name':truststore_name,'intended_usage':usage,'trust_store_file':f,}context.response=context.authenticated_client.post('/pki/truststores/add/',truststore_add_form_data,follow=True)assertcontext.response.status_code==200,f"Failed to add new truststore."context.truststore=TruststoreModel.objects.get(unique_name=truststore_name)
@when('the admin fills in the truststore details with {name}, {intended_usage} and {file_type}')
[docs]defstep_fill_truststore_details(context:runner.Context,name:str,intended_usage:str,file_type:str)->None:# noqa: ARG001"""Fills in the truststore creation form. Args: context (runner.Context): Behave context. name (str): The name of the truststore. intended_usage (str): The intended usage of the truststore. file_type (str): The file type of the truststore. """truststore_file_path=os.path.abspath(f"{CURRENT_DIR}/../../../tests/data/trust-store/trust_store{file_type}")usage=0ifintended_usage=="TLS":usage=1elifintended_usage=="Generic":usage=2# Prepare POST datacontext.truststore_add_form_data={'unique_name':name,'intended_usage':usage,'trust_store_file':truststore_file_path,}
@then('the new truststore with {name} and {intended_usage} should appear in the truststore list')
[docs]defstep_truststore_list(context:runner.Context,name:str,intended_usage:str)->None:# noqa: ARG001"""Verifies that the new truststore appears in the truststore list. Args: context (runner.Context): Behave context. name (str): The name of the truststore. intended_usage (str): The intended usage of the truststore. """soup=BeautifulSoup(context.response.content,"html.parser")# Find all <td> elementstds=soup.find_all("td")# Get their text content (unescaped and stripped)values=[td.get_text(strip=True)fortdintds]assertnameinvalues,f"Truststore {name} doesn't exist"assertintended_usageinvalues,f"Intended usage {intended_usage} doesn't exist"
@when('the admin deletes the truststore with the name {name}')
[docs]defstep_delete_truststore(context:runner.Context,name:str)->None:# noqa: ARG001"""Deletes an truststore by name. Args: context (runner.Context): Behave context. name (str): The name of the truststore to be deleted. """context.response=context.authenticated_client.get('/pki/truststores/delete/'+str(context.truststore.id),follow=True,HTTP_X_REQUESTED_WITH="XMLHttpRequest")assertcontext.response.status_code==200,"Truststore delete form submission failed"assertb"Confirm Truststore Deletion"incontext.response.contentcontext.response=context.authenticated_client.post(f"/pki/truststores/delete/{context.truststore.id}/",data={},follow=True)assertcontext.response.status_code==200,"Truststore deletion response"assertnotTruststoreModel.objects.filter(id=context.truststore.id).exists(),f"Deletion of Truststore with name {name} failed"
@then('the truststore {name} should no longer appear in the truststore list')
[docs]defstep_verify_truststore_deletion(context:runner.Context,name:str)->None:# noqa: ARG001"""Verifies that the truststore no longer appears in the list. Args: context (runner.Context): Behave context. name (str): The name of the truststore. """assertnamenotincontext.response,f"Truststore with name {name} still exist in the list"
@when('the admin attempts to view the details of a non-existent truststore {non_existent_truststore_id}')
[docs]defstep_attempt_view_nonexistent(context:runner.Context,non_existent_truststore_id:str)->None:# noqa: ARG001"""Attempts to view details of a non-existent truststore. Args: context (runner.Context): Behave context. non_existent_truststore_id (str): The id a non-existent truststore. """#Navigate (GET request) to the truststore detailed pagecontext.response=context.authenticated_client.get(f"/pki/truststores/config/{non_existent_truststore_id}")