"""File for steps which are used more often across multiple feature files."""importloggingfrombehaveimportgiven,runner,step,then,whenfromdjango.contrib.auth.modelsimportUserfromdjango.testimportClientfrompki.models.domainimportDomainModel
[docs]defstep_tpc_web_running(context:runner.Context)->None:# noqa: ARG001"""Verifies that the TPC_Web application is running. This step checks that the TPC_Web application is running and accessible at the expected URL. Args: context: the behave context """response=Client().get('/users/login/')ifresponse.status_code!=HTTP_OK:msg=f'{response.status_code} != {HTTP_OK}!'raiseAssertionError(msg)
@given('a domain with a name {domain_name} exist')
[docs]defstep_domain_exists(context:runner.Context,domain_name:str)->None:# noqa: ARG001""". Args: context: the behave context domain_name: a domain name """domain,created=DomainModel.objects.get_or_create(unique_name=domain_name)assertcreated,f" Domain creation failed"assertdomain.unique_name==domain_name,f" Domain name mismatch: expected '{domain_name}', got '{domain.name}'"context.domain=domain
@step('Commentary')
[docs]defcommentary_step(context:runner.Context)->None:"""Provides annotation "@Commentary" inside feature files for additional text explanation. Args: context: the behave context """scenario=context.formatter.current_scenariostep=scenario.current_stepstep.commentary_override=True
@given('the admin user is logged into TPC_Web')
[docs]defstep_admin_logged_in(context:runner.Context)->None:"""Logs the admin user into the TPC_Web interface. This step sets up the initial state for all scenarios, ensuring the admin is authenticated and on the TPC_Web dashboard. Args: context: the behave context """try:User.objects.create_superuser(username='admin',password='testing321')# noqa: S106client=Client()login_success=client.login(username='admin',password='testing321')# noqa: S106ifnotlogin_success:msg='Login unsuccessful'raiseAssertionError(msg)# noqa: TRY301context.authenticated_client=clientresponse=client.get('/pki/certificates/')ifresponse.status_code!=HTTP_OK:msg='Could not get a HTTP_OK from visiting the certificates page.'raiseAssertionError(msg)# noqa: TRY301exceptExceptionaserror:msg=f'Error: {error}'raiseAssertionError(msg)fromerror
@then('the system should display a confirmation message stating "{confirm_message}"')
[docs]defstep_confirmation_message(context:runner.Context,confirm_message:str)->None:# noqa: ARG001"""Verifies that the system displays a success message after an action. Args: context: the behave context """html=context.response.content#print("html", html)assertconfirm_message.encode()inhtml,f"Missing confirmation message, {confirm_message}"
@then('the system should display an error message stating {error_message}')
[docs]defstep_error_message(context:runner.Context,error_message:str)->None:# noqa: ARG001"""Verifies that the system displays a specific error message. Args: context: the behave context error_message (str): The expected error message text. """msg='Step not implemented: Error message check.'raiseAssertionError(msg)
@given('an API client is authenticated')
[docs]defstep_api_client_authenticated(context:runner.Context)->None:# noqa: ARG001"""Authenticates the API client to enable authorized interactions with the REST API. Args: context: the behave context """msg='Step not implemented: API client authentication.'raiseAssertionError(msg)
@then('the API response should have a status code of {status_code}')
[docs]defstep_verify_status_code(context:runner.Context,status_code:str)->None:# noqa: ARG001"""Verifies the API response status code. Args: context: the behave context status_code (str): The expected status code. """msg='Step not implemented: Verify API response status code.'raiseAssertionError(msg)
@then('the response payload should include an error message stating "{error_message}"')
[docs]defstep_verify_error_message(context:runner.Context,error_message:str)->None:# noqa: ARG001"""Verifies the response payload includes the specified error message. Args: context: the behave context error_message (str): The expected error message text. """html=context.response.contentasserterror_message.encode()inhtml, \
f"Missing error message, {error_message}"
@when('the admin clicks on "{button_name}"')
[docs]defstep_when_admin_click_button(context:runner.Context,button_name:str)->None:# noqa: ARG001"""Simulates click on given button. Args: context: the behave context error_message (str): The expected error message text. """ifbutton_name=="Add new Issuing CA":context.response=context.authenticated_client.get("/pki/issuing-cas/add/method-select/")# Check that page loaded successfullyassertcontext.response.status_code==200,f"Failed to load Add new Issuing CA page"elifbutton_name=="Import From PKCS#12 File":context.response=context.authenticated_client.get("/pki/issuing-cas/add/file-import/pkcs12")# Check that page loaded successfullyassertcontext.response.status_code==200,f"Failed to load issuing Add new Issuing CA using pkcs#12 import"elifbutton_name=="Delete selected Issuing CAs":context.response=context.authenticated_client.post(f"/pki/issuing-cas/delete/{context.issuing_ca.id}/",follow=True)elifbutton_name=="Import From Separate Key and Certificate Files":context.response=context.authenticated_client.get("/pki/issuing-cas/add/file-import/separate-files")# Check that page loaded successfullyassertcontext.response.status_code==200,f"Failed to load issuing Add new Issuing CA using import from separate files"elifbutton_name=="Add new Domain":context.response=context.authenticated_client.post('/pki/domains/add/',context.domain_add_form_data,follow=True)assertcontext.response.status_code==200,f"Failed to add new domain."elifbutton_name=="Add new Truststore":withopen(context.truststore_add_form_data['trust_store_file'],'rb')asf:context.truststore_add_form_data['trust_store_file']=fcontext.response=context.authenticated_client.post('/pki/truststores/add/',context.truststore_add_form_data,follow=True)assertcontext.response.status_code==200,f"Failed to add new truststore."elifbutton_name=="Create Device":context.response=context.authenticated_client.post('/devices/create/onboarding/',context.device_add_form_data,follow=True)assertcontext.response.status_code==200,f"Failed to add new device."else:msg='Step not implemented: Verify API response status code.'raiseAssertionError(msg)
@when('the admin navigates to the "{page_name}" page')
[docs]defstep_navigate_add_device(context:runner.Context,page_name:str)->None:# noqa: ARG001"""Navigates to the given page. Args: context (runner.Context): Behave context. page_name (str): Page name. """ifpage_name=="Add Device":context.response=context.authenticated_client.get("/devices/create/")elifpage_name=="create onboarding device":context.response=context.authenticated_client.get("/devices/create/onboarding/")elifpage_name=="device list":context.response=context.authenticated_client.get("/devices/")elifpage_name=="truststore list":context.response=context.authenticated_client.get("/pki/truststores/")elifpage_name=="Add new Domain":context.response=context.authenticated_client.get("/pki/domains/add/")elifpage_name=="domain list":context.response=context.authenticated_client.get("/pki/domains/")elifpage_name=="Add new Truststore":context.response=context.authenticated_client.get("/pki/truststores/add/")else:msg='Page name is not valid.'raiseAssertionError(msg)assertcontext.response.status_code==200,f"Failed to load {page_name} page"