microsoft-graph-apijsr223taurus

Storing a variable from jsr223 response to use in subsequent tests


Hi I am trying to write a taurus test that uses the Microsoft Graph Api. My goal is to use the group id and user id from two jsr223 responses in a POST request to add a user to an Azure group. The problem is I keep getting

 Non HTTP response message: Illegal character in path at index 41: https://graph.microsoft.com/v1.0/groups/${AzureGroupId}/members/$ref

because the variable AzureGroupId is not getting set properly. Here is the test to get the groups:

      # Microsoft Graph Get AD groups
      - url: ${MicrosoftGraph}/groups?$orderby=displayName
        label: 1302_FetchMicrosoftAADGroups
        method: GET
        headers:
          Authorization: Bearer ${graph_api_access_token}
          Content-Type: "application/json"
          Accept: "application/json, text/plain, */*"
        assert-httpcode:
          - 200
          - 202
        jsr223:
          - langauge: groovy
            execute: after
            script-text: |
              import groovy.json.JsonSlurper
              def slurperresponse = new JsonSlurper().parseText(prev.getResponseDataAsString())
              for (entry in slurperresponse){
                if(entry.displayName == "ACME"){
                   vars.put("AzureGroupId",  entry.id)
                   break;
                }
              }
            script-file: jsr223/logger.groovy
            parameters: fetch_microsoft_ad_groups

And here is the test to get the users:

      # Microsoft Graph Get AD users
      - url: ${MicrosoftGraph}/users
        label: 1302_FetchMicrosoftAADUsers
        method: GET
        headers:
          Authorization: Bearer ${graph_api_access_token}
          Content-Type: "application/json"
          Accept: "application/json, text/plain, */*"
        assert-httpcode:
          - 200
          - 202
        jsr223:
          - langauge: groovy
            execute: after
            script-text: |
              import groovy.json.JsonSlurper
              def slurperresponse = new JsonSlurper().parseText(prev.getResponseDataAsString())
              for (entry in slurperresponse){
                if(entry.userPrincipalName == "jon.doe@gmail.com"){
                   vars.put("AzureUserId",  entry.id)
                   break;
                }
              }
            script-file: jsr223/logger.groovy
            parameters: fetch_microsoft_ad_users

Finally I am combining the results of these two test to make the POST request in this test:

      # Microsoft Graph Add a user
      - url: ${MicrosoftGraph}/groups/${AzureGroupId}/members/$ref
        label: 1324_AddUserToAzureADGroup
        method: POST
        headers:
          Authorization: Bearer ${graph_api_access_token}
          Content-Type: "application/json"
          Accept: "application/json, text/plain, */*"
        body:
          "@odata.id": "https://graph.microsoft.com/v1.0/directoryObjects/${AzureUserId}"
        assert-httpcode:
          - 204
        jsr223:
          - langauge: groovy
            execute: after
            script-file: jsr223/logger.groovy
            parameters: add_user_to_active_directory_group

The POST request fails because the url is malformed. Could someone please explain how to set the variable from the response so that I can use it in subsequent requests as a apart of the url?

Thanks!


Solution

  • The scope of the variable wasn't set properly. The section

    scenarios:
      TestScenario:
        variables:
          AzureGroupId: 0
          AzureUserId: 0
          AzureUserName: ""
    

    needed to be set.