Forum Discussion

jashadip's avatar
jashadip
New Contributor
8 years ago

Not able to set basic authentication for all test requests across all test suites in a REST project.

I am trying to set basic authentication in all requests across multiple test suites in a REST project. To achieve that I am using a groovy script(kept in a different test suite), to set the username, password and endpoint for all requests. 

 

The problem seems to be that the username and password is reflected correctly in the Custom Properties section but when I run the actual REST service, there is an error since the authentication details are not taken up.

 

Mentioned below is groovy Script I am using. Any insight into this issue is appreciated.

 

SOAPUI version : Open Source v5.2.1

 

Groovy Script

 

import com.eviware.soapui.SoapUI
import com.eviware.soapui.settings.HttpSettings

/* Explantion for the Groovy to set properties for all testsuites/testcases and testrequests inside a SOAP project
* ===================================================================================
* def Project : this identifies all the test suites present inside the project
* def soapSuites : this identifies all the test cases inside the project
* def soapRequestsList : this identifies all the requests inside a test case
* method setPropertyValue : used to set all the properties as desired
*
* Auth preemptively is set true at the SOAP level
* ===================================================================================
*/

def project = context.testCase.testSuite.project;
for (testSuite in project.testSuiteList) {
//log.info testSuite.name;
def soapSuites = testSuite.getTestCaseList()
for(soapTestCase in soapSuites){
//log.info soapTestCase.name
def soapRequestsList = soapTestCase.getTestStepList()
//log.info soapRequestsList.name
for(soapRequest in soapRequestsList ){
soapRequest.setPropertyValue("Username","<username>")
soapRequest.setPropertyValue("Password","<password>")
soapRequest.setPropertyValue("Endpoint","<Using the endpoint of the environment under test>")
}
}
}

//To enable Pre-emptive authentication, set it true. Set it to false otherwise.
//This is a SOAP level setting and is applicable for all projects in SOAPUI
SoapUI.getSettings().setBoolean(HttpSettings.AUTHENTICATE_PREEMPTIVELY, true)
SoapUI.saveSettings()

  • fazlook's avatar
    fazlook
    Frequent Contributor

    Why don't you use a global variables in your project -> preferences ? instead ! 

     

    Use them as per the attached. 

    • Bill_In_Irvine's avatar
      Bill_In_Irvine
      Contributor

      fazlook,

       

      It' s not the authorization I am concerned with but I see your point. A global property for the keystore in the SSL preferences would probably work for my problem (which is finding how to set relative path to a key store). However I'd have to have a groovy script delete it when I run a different test suite for a different REST API. 

       

      Looks like the solution is to have a Setup Script set the keystore password and then the relative path via groovyUtils.projectPath prepended to the particular keystone filename.

       

      Then a test suite teardown to delete the password and keystore from the SoapUI settings xml file.

       

      Thanks

      Bill

  • The solution I have finally got working is to put the script in the Test Suite Setup (see attached - sorry if the screen shot is too small). I need to work it a bit more to do a teardown script to delete the keystore and password because I use my Ready API to run other tests. Also this stuff will run in testrunner as an automatic script, so the test suite must do everything from running regression tests.

     

    Note I decided I don't need property expansions for my password or keystore. And I also found that I cannot get log.info to print out anything from the Setup Script in the Test Suite level.

    • fazlook's avatar
      fazlook
      Frequent Contributor

      Glad you made it.

       

      BTW, I do use ReadyAPI in TC. Is that where you use it ?

       

      Thank you

       

      • Bill_In_Irvine's avatar
        Bill_In_Irvine
        Contributor

        Hi fazlook,

         

        I have to get used to this community blog interface. Someone forwarded me a different thread and I thought it was my own thread. My problem is not about using property expansion of the username, but it was similar, to allow flexibility of key stores so that this stuff can be run in a test runner script automatically when called in a bash file.

         

        I am using Ready API and my script is not in the Test Case level but it is the setup script in the test suite level.

         

        Now I have to go to my OP thread I put in yesterday and copy my same post. 

         

        Sorry for my confusion and sorry for confusion that makes it seem I'm the OP of this authentication thread.