How to manage different auth profiles per environments in a REST api project
Hello everybody, i have the task to create tests in a Scrum team, which should then run in different environments (Dev, Release, Master). The speciality is, that I have to test REST services in the ...
giovanni_favara , I am not sure how to answer this specific to Azure. However, Combination of AuthProfiles and Environment should work seamlessly. I have implemented this to get OAuth2.0 token for different environments.
Step1: Create authProfiles for different environments say,
qaAuthProfile
devAuthProfile
Step 2: Create environments and assign OAuth2.0 profiles as shown below
3. Implemented an eventhandler script to refresh access token:
import com.eviware.soapui.impl.rest.actions.oauth.OltuOAuth2ClientFacade
import com.eviware.soapui.support.editor.inspectors.auth.TokenType
import com.eviware.soapui.impl.rest.OAuth2Profile.*
//## Get Project ##//
def project = testRunner.testCase.testSuite.getProject()
//## Get TestStep ##//
def TestStep = context.getCurrentStep()
//## Get oAuth Profile Name ##//
def oAuthProfileName
def oAuthProfile
def TokenStatus
def oAuthClientFacade
def currentToken
def ENV
def i = 0
if (TestStep.config.type == 'restrequest'){
//## get current environment ##//
log.info "Running GetAccessToken event script..."
ENV = testRunner.testCase.testSuite.project.activeEnvironment.name
log.info "test environment is $ENV..."
//## Get oAuth profile name list ##//
def oAuthProfilelist = project.getOAuth2ProfileContainer().getOAuth2ProfileNameList()
oAuthProfilelist.each{
oAuthProfileName = oAuthProfilelist[i]
log.info "oAuth Profile name is: $oAuthProfileName"
if(oAuthProfileName.contains("$ENV")){
//log.info "oAuth Profile for $ENV is: $oAuthProfileName"
return
}
i++
}
//## Get Token Status ##//
oAuthProfile = project.getAuthRepository().getEntry("$oAuthProfileName")
TokenStatus = oAuthProfile.accessTokenStatus.toString()
log.info "Access Token Status for $oAuthProfileName is: $TokenStatus"
Thread.sleep(5000)
//## Get Access Token from the server if expired ##//
if ((TokenStatus != 'RETRIEVED_FROM_SERVER') || (currentToken == oAuthProfile.getAccessToken())) {
log.info "Retrieving token from the auth server..."
oAuthClientFacade = new OltuOAuth2ClientFacade(TokenType.ACCESS)
oAuthClientFacade.requestAccessToken(oAuthProfile, true)
log.info "Access Token Status is " + oAuthProfile.accessTokenStatus
Thread.sleep(6000)
}
}
Above script will retrieve access token from specific auth profile based on the selected environment.
thx for your post. What kind of event you use for your script? I tried with "SubmitListener.beforeSubmit" and with "RequestFilter.filterRequest" but I get an error: Mon Jan 13 15:35:43 CET 2020: ERROR: com.eviware.soapui.support.scripting.ScriptException: Error in RequestFilter.filterRequest Mon Jan 13 15:35:43 CET 2020: ERROR: An error occurred [Error in RequestFilter.filterRequest], see error log for details
...
Caused by: groovy.lang.MissingPropertyException: No such property: testRunner for class: Script3
...
Event scripts cannot be debugged. Do you have a tip on how to check event-scripts?
I have analyzed the error. The error probably occurs because the script assumes the normal oAuth2.0. But I use the version of oAuth 2.0 for Azure. Other libraries must be included. I am currently in the process of rebuilding your script.