Forum Discussion
Hi pervindrap,
Indeed, more detailing your use case could be helpful.
Perhaps this posts that could guide you in the right direction concerning using SOAPui in build pipeline of VS:
Concerning the expiration of your token:
I have below test case which generates a token from an openidprovider. This token remains valid for only 10 minutes.
The get-set properties_openId & expiration groovy step =
import groovy.time.TimeCategory TimeZone.setDefault(TimeZone.getTimeZone('CET')) def expires_in = context.expand( '${Token#Response#$[\'expires_in\']}' ) int expires_in_seconds = expires_in as Integer //log.info expires_in_seconds currentDate = new Date() //log.info "currentdate" + currentDate use( TimeCategory ) { expirydate = currentDate + expires_in_seconds.seconds } //log.info "expirydate" + expirydate String expirydate_string = expirydate as String testRunner.testCase.testSuite.project.setPropertyValue ("openid_expirydate", expirydate_string) def id= context.expand( '${DataSource_OpenId#id}' ) def firstname = context.expand( '${DataSource_OpenId#first name}' ) def lastname = context.expand( '${DataSource_OpenId#last name}' ) def rol_claim = context.expand( '${DataSource_OpenId#rol_claim}' ) def description = context.expand( '${DataSource_OpenId#description }' ) def token_response = context.expand( '${Token#Response#$[\'id_token\']}' ) //log.info ("Forproperty "+description +" with id \""+id+"\" we generated following JWT token (valid until "+ expirydate + "): " +token_response) testRunner.testCase.testSuite.project.setPropertyValue ("authorization_"+description , token_response) def authorization = context.expand( '${#Project#authorization_'+description +'}' ) //log.info ("auhtorization in property = " + authorization) return expirydate
To check on the expiration date, so to make sure I always have a valid (not-expired) token I have to do some extra stuff. Otherwise there's a risk the automated tests (build agent level) would still be running after 10 minutes and thus getting an expired token into the authorization header (see previous post on the automation of authorization: https://community.smartbear.com/t5/SoapUI-Pro/oauth2-authorization-dynamic-test-automation-through-event/m-p/160713).
There are different ways to do this. Below script is an eventhandler TestRunListener.beforeRun.
I'd like to do this at test run level , mimicking a setup script at test case level. You could also do it at testStep level, but I feel this might be a bit overkill. Ideally, this should be somehow included in the RequestFilter.filterRequest, (so to focus only on those requests that actually use the open id ) but that script cannot invoke testRunner, which is needed to re-launch the openid-generation test case.
https://support.smartbear.com/readyapi/docs/testing/handling-events.html
So I will check at every start of a test case whether the token does not expire within 30 seconds. First of all: I am aware there is kind of a hole: if the expiration is reached during a test case that consumes openid tokens (so after 30 seconds that the test started) then some of the steps might fail due to expired tokens.
But for that checking each and every step whether the time is reached seems too much. In my case the whole set of test cases would take around 15 minutes. So there would be one point where there needs to be a re-generation of tokens. Not all of my testcases use openid (and definitely not all of my test steps) + only a few test cases contain that many steps/loops that it takes more than 30 seconds to complete. So I think I'm safe with a 30 seconds margin.
PS: I kept my "log.info" comments in there, just for ease of use so you can check what the code is doing in the script log tab.
import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import groovy.time.TimeCategory def project = testRunner.getTestCase().getTestSuite().getProject() def testsuite = project.getTestSuiteByName("_Authentication") def testcase = testsuite.getTestCaseByName("openid") def tcname = testRunner.getTestCase().getName() def tsname = testRunner.getTestCase().getTestSuite().getName() //log.info ("tcname = " +tcname) if (tsname != "_Authentication") //I do not need to run the TestRunListener.beforeRun when we actually run the _Authentication test Suite, otherwise we might create an infinite loop! { TimeZone.setDefault(TimeZone.getTimeZone('CET')) def expirydate_string = context.expand('${#Project#openid_expirydate}') //log.info expirydate_string Date now = new Date() //log.info ("current date plus 30 seconden : "+ now_plus_30_seconds) //expiry date is in string format. We need to convert it to a Date DateFormat date_format = new SimpleDateFormat("EEE MMM d HH:mm:ss z yyyy", Locale.US) Date expirydate_convertedtodate = date_format.parse(expirydate_string) //log.info ("expirydate converted: " + expirydate_convertedtodate) use (TimeCategory) { expiry_min_30_seconds = expirydate_convertedtodate - 30.seconds } if (expiry_min_30_seconds > now) { log.info ("Event handler TestRunListener.beforeRun for test case: \"" + tcname + "\". Expiry date openid not yet reached :"+expiry_min_30_seconds+" > " +now) } else { log.info ("!! Openid expired (or will expire in 30 seconds)!! " +expiry_min_30_seconds+" > " +now+ ". We run the Authorization openid testcase again to get new valid tokens.") testcase.run(new com.eviware.soapui.support.types.StringToObjectMap(),false) } } else log.info ("No event handler testRunListener.beforeRun run for the openid requests test case! Otherwise we may end up in an infinite loop.")
Related Content
- 3 years ago
- 7 years ago
- 3 years ago
- 4 years ago
Recent Discussions
- 14 hours ago
- 17 days ago