0

您好我正在尝试编写一个使用 Microsoft Graph Api 的 taurus 测试。我的目标是在 POST 请求中使用来自两个 jsr223 响应的组 ID 和用户 ID 将用户添加到 Azure 组。问题是我不断得到

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

因为变量AzureGroupId没有正确设置。这是获取组的测试:

      # 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

这是获取用户的测试:

      # 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

最后我结合这两个测试的结果在这个测试中发出 POST 请求:

      # 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

POST 请求失败,因为url格式错误。有人可以解释一下如何从响应中设置变量,以便我可以在后续请求中将其作为 url 的一部分使用吗?

谢谢!

4

1 回答 1

0

变量的范围没有正确设置。这部分

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

需要设置。

于 2020-09-23T13:20:27.407 回答