---
setup:
  - skip:
      features: headers

  - do:
      cluster.health:
          wait_for_status: yellow

  - do:
      security.put_role:
        name: "mixed_role"
        body:  >
            {
              "indices": [
                { "names": ["only_read"], "privileges": ["read"] },
                { "names": ["only_index"], "privileges": ["index"] },
                { "names": ["only_delete"], "privileges": ["delete"] },
                { "names": ["everything"], "privileges": ["all"] }
              ]
            }

  - do:
      security.put_user:
        username: "test_user"
        body:  >
          {
            "password" : "x-pack-test-password",
            "roles" : [ "mixed_role" ],
            "full_name" : "user with mixed privileges to multiple indices"
          }

  - do:
      indices.create:
        index: only_read
        body:
          settings:
            index:
              number_of_shards:   1
              number_of_replicas: 1
          mappings:
            properties:
              name:
                type: "keyword"

  - do:
      indices.create:
        index: only_index
        body:
          settings:
            index:
              number_of_shards:   1
              number_of_replicas: 1
          mappings:
            properties:
              name:
                type: "keyword"
  - do:
      indices.create:
        index: only_delete
        body:
          settings:
            index:
              number_of_shards:   1
              number_of_replicas: 1
          mappings:
            properties:
              name:
                type: "keyword"

  - do:
      indices.create:
        index: everything
        body:
          settings:
            index:
              number_of_shards:   1
              number_of_replicas: 1
          mappings:
            properties:
              name:
                type: "keyword"
---
teardown:
  - do:
      security.delete_user:
        username: "test_user"
        ignore: 404

  - do:
      security.delete_role:
        name: "mixed_role"
        ignore: 404

---
"Test indexing a document when allowed":

  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      create:
        id: "1"
        index: only_index
        body: >
          {
            "name" : "doc1"
          }

  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      create:
        id: "2"
        index: everything
        body: >
          {
            "name" : "doc2"
          }

  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      bulk:
        refresh: true
        body:
            - '{"index": {"_index": "only_index", "_id": "3"}}'
            - '{"name": "doc3"}'
            - '{"index": {"_index": "everything", "_id": "4"}}'
            - '{"name": "doc4"}'

  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      bulk:
        refresh: true
        body:
            - '{"index": {"_index": "only_index", "_id": "5"}}'
            - '{"name": "doc5"}'

  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      bulk:
        refresh: true
        body:
            - '{"index": {"_index": "everything", "_id": "6"}}'
            - '{"name": "doc6"}'

  - do: # superuser
      search:
        rest_total_hits_as_int: true
        index: only_index

  - match: { hits.total: 3 }

  - do: # superuser
      search:
        rest_total_hits_as_int: true
        index: everything

  - match: { hits.total: 3 }

---
"Test indexing a document when not allowed":
  - do:
      catch: forbidden
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      create:
        refresh: true
        id: "7"
        index: only_read
        body: >
          {
            "name" : "doc7"
          }

  - do:
      catch: forbidden
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      create:
        refresh: true
        id: "8"
        index: only_delete
        body: >
          {
            "name" : "doc8"
          }

  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      bulk:
        refresh: true
        body:
            - '{"index": {"_index": "only_read", "_id": "9"}}'
            - '{"name": "doc9"}'
            - '{"index": {"_index": "only_delete", "_id": "10"}}'
            - '{"name": "doc10"}'
  - match: { errors: true }
  - match: { items.0.index.status: 403 }
  - match: { items.0.index.error.type: "security_exception" }
  - match: { items.1.index.status: 403 }
  - match: { items.1.index.error.type: "security_exception" }

  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      bulk:
        refresh: true
        body:
            - '{"index": {"_index": "only_read", "_id": "11"}}'
            - '{"name": "doc11"}'
  - match: { errors: true }
  - match: { items.0.index.status: 403 }
  - match: { items.0.index.error.type: "security_exception" }

  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      bulk:
        refresh: true
        body:
            - '{"index": {"_index": "only_delete", "_id": "12"}}'
            - '{"name": "doc12"}'
  - match: { errors: true }
  - match: { items.0.index.status: 403 }
  - match: { items.0.index.error.type: "security_exception" }

  - do: # superuser
      search:
        rest_total_hits_as_int: true
        index: only_read
  - match: { hits.total: 0 }

  - do: # superuser
      search:
        rest_total_hits_as_int: true
        index: only_delete
  - match: { hits.total: 0 }

---
"Test bulk indexing documents when only some are allowed":
  - do:
      headers: { Authorization: "Basic dGVzdF91c2VyOngtcGFjay10ZXN0LXBhc3N3b3Jk" } # test_user
      bulk:
        body:
            - '{"index": {"_index": "only_read", "_id": "13"}}'
            - '{"name": "doc13"}'
            - '{"index": {"_index": "only_index", "_id": "14"}}'
            - '{"name": "doc14"}'
  - match: { errors: true }
  - match: { items.0.index.status: 403 }
  - match: { items.0.index.error.type: "security_exception" }
  - match: { items.1.index.status: 201 }

  - do: # superuser
      indices.refresh:
        index: only_index

  - do: # superuser
      search:
        rest_total_hits_as_int: true
        index: only_index
        body: { "query": { "term": { "_id": "14" } } }
  - match: { hits.total: 1 }
