Authorisation token expires too soon
I'm running data-driven tests that have 5 steps:
- 1. Get new Authorisation token
- 2. DataSource (get a few thousand random rows from DB2)
- 3. REST Request
- 4. Assert script
- 5. DataSource Loop
The problem is that the Authorisation token expires after say 30 seconds, so if I increase the number of rows in my select statement, after 30 seconds every REST request fails with 401 - unauthorized.
I set in Auth Repository the 'Access Token Expiration Time' of my OAuth 2.0 (Azure) profile to Custom/25 Seconds but nothing changed.
I could not find anything on static variables so I implemented the following:
//test case property "Duration" to simulate static variable
//get value of property "Duration"
def dur = testRunner.testCase.getPropertyValue("Duration").toInteger()
//if test is more than 'Duration' get another token and increment 'Duration'
if ( testRunner.getTimeTaken() > dur)
{
testRunner.gotoStepByName('GetAcessToken')
testRunner.testCase.setPropertyValue( "Duration", (dur+30000).toString() )
dur = testRunner.testCase.getPropertyValue("Duration").toInteger()
}
This solution has 2 issues:
- 1. For every test I need an extra Initialisation step to reset the Duration property to (testRunner.testCase.setPropertyValue( 'Duration', '30000' ) for the next run
- 2. After running the Get Access token step, ReadyAPI continues on with a new Data Source step so the test will never end!
The only way I see how to resolve this is instead of asking ReadyAPI to run step GetAcessToken:
testRunner.gotoStepByName('GetAcessToken')
is to replace this line of code with all the code of step GetAcessToken.
Surely there must be a more elegant solution
The acquisition of a new access token is very slow so I can't afford to have it inside the loop. As you can see from the code snippet below I request for a new token and then wait in a loop till I have one:
def oldToken = oAuthProfile.getAccessToken();
def oAuthClientFacade = new OltuOAuth2AzureClientFacade (TokenType.ACCESS);
oAuthClientFacade.requestAccessToken(oAuthProfile, true,true);while(oldToken == oAuthProfile.getAccessToken())
{
sleep(100);
}I'm pretty happy with what I've got now:
I get the access token in the TestRunListener.beforeRun event which also sets a test case property "Duration" to the time I want to wait till I need another token.
Inside the test loop, in my groovy asserts script, I check if the test has been running for a time exceeding the Duration property, and if yes, I get a new access token and increment the Duration property