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.