---
setup:
  - skip:
      features: headers
  - do:
      cluster.health:
        wait_for_status: yellow

---
teardown:
  - do:
      security.delete_role:
        name: "admin_role2"
        ignore: 404

  - do:
      security.delete_user:
        username: "joe"
        ignore: 404

  - do:
      delete:
        index: foo
        id: "1"
        ignore: 404

---
"Test put role api using as array of index names":

  - do:
      security.put_role:
        name: "admin_role2"
        body:  >
            {
              "cluster": ["all"],
              "indices": [
                {
                  "names": ["foo", "bar"],
                  "privileges": ["all"]
                }
              ]
            }
  - match: { role: { created: true } }

  - do:
      security.put_user:
        username: "joe"
        body:  >
            {
              "password": "s3krit-password",
              "roles" : [ "admin_role2" ]
            }
  - match: { created: true }

  - do:
      index:
        index: foo
        id: "1"
        body: { foo: bar }

  - do:
      headers:
        Authorization: "Basic am9lOnMza3JpdC1wYXNzd29yZA=="
      get:
        index: foo
        id: "1"
  - match: { _index: foo }
  - match: { _id: "1"}
  - match: { _source: { foo: bar }}

# test that the role works on the cluster level
  - do:
      headers:
        Authorization: "Basic am9lOnMza3JpdC1wYXNzd29yZA=="
      info: {}

  - do:
      security.get_role:
        name: "admin_role2"
  - match: { admin_role2.cluster.0:  "all" }
  - match: { admin_role2.indices.0.names.0: "foo" }
  - match: { admin_role2.indices.0.names.1: "bar" }
  - match: { admin_role2.indices.0.privileges.0:  "all" }
