---
setup:
  - skip:
      features: headers

  - do:
      cluster.health:
        wait_for_status: yellow
  - do:
      security.put_user:
        username: "joe"
        body:  >
            {
              "password": "s3krit-password",
              "roles" : [ "admin_role" ]
            }

---
teardown:
  - do:
      security.delete_user:
        username: "joe"
        ignore: 404
---
"Test bulk put and delete roles api":
  - do:
      security.bulk_put_role:
        body:  >
          {
              "roles": {
                  "admin_role": {
                      "cluster": [
                          "all"
                      ],
                      "metadata": {
                          "key1": "val1",
                          "key2": "val2"
                      },
                      "indices": [
                          {
                              "names": "*",
                              "privileges": [
                                  "all"
                              ]
                          }
                      ]
                  },
                  "role_with_description": {
                      "description": "Allows all security-related operations such as CRUD operations on users and roles and cache clearing.",
                      "cluster": [
                          "manage_security"
                      ]
                  }
              }
          }
  - match: { created: ["admin_role", "role_with_description"] }

  - do:
      headers:
        Authorization: "Basic am9lOnMza3JpdC1wYXNzd29yZA=="
      security.get_role:
        name: "admin_role"
  - match: { admin_role.cluster.0:  "all" }
  - match: { admin_role.metadata.key1:  "val1" }
  - match: { admin_role.metadata.key2:  "val2" }
  - match: { admin_role.indices.0.names.0: "*" }
  - match: { admin_role.indices.0.privileges.0:  "all" }

  - do:
      headers:
        Authorization: "Basic am9lOnMza3JpdC1wYXNzd29yZA=="
      security.get_role:
        name: "role_with_description"
  - match: { role_with_description.cluster.0:  "manage_security" }
  - match: { role_with_description.description:  "Allows all security-related operations such as CRUD operations on users and roles and cache clearing." }

  # query match_all roles
  - do:
      headers:
        Authorization: "Basic am9lOnMza3JpdC1wYXNzd29yZA=="
      security.query_role:
        body: >
          {
            "query": {
              "bool": {
                "must_not": {
                  "term": {
                    "metadata._reserved": true
                  }
                }
              }
            },
          "sort": ["name"]
          }
  - match: { total: 2 }
  - match: { count: 2 }
  - match: { roles.0.name: "admin_role" }
  - match: { roles.1.name: "role_with_description" }

  - do:
      security.bulk_delete_role:
        body: >
          {
             "names": ["admin_role", "role_with_description"]
          }
  - match: { deleted.0: "admin_role" }
  - match: { deleted.1: "role_with_description" }
