---
setup:
  - skip:
      features: headers

  - do:
      cluster.health:
        wait_for_status: yellow

  - do:
      security.put_user:
        username: "joe"
        body:  >
          {
            "password": "s3krit-password",
            "roles" : [ "superuser" ]
          }

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

---
"Test overwriting a user":
  - do:
      security.get_user:
        username: "joe"
  - match: { joe.username: "joe" }
  - match: { joe.roles.0:  "superuser" }

  - do:
      headers:
        Authorization: "Basic am9lOnMza3JpdC1wYXNzd29yZA=="
      cluster.health: {}
  - match: { timed_out: false }

  - do:
      security.put_user:
        username: "joe"
        body:  >
            {
              "password" : "s3krit-password2",
              "roles" : [ "superuser", "foo" ],
              "full_name" : "Bazooka Joe",
              "email" : "joe@bazooka.gum",
              "metadata" : {
                "key1" : "val1",
                "key2" : "val2"
              }
            }
  - match: { created: false }

  - do:
      security.get_user:
        username: "joe"
  - match: { joe.username:  "joe" }
  - match: { joe.roles.0:  "superuser" }
  - match: { joe.roles.1:  "foo" }
  - match: { joe.full_name:  "Bazooka Joe" }
  - match: { joe.email:  "joe@bazooka.gum" }
  - match: { joe.metadata.key1:  "val1" }
  - match: { joe.metadata.key2:  "val2" }

  - do:
      headers:
        Authorization: "Basic am9lOnMza3JpdC1wYXNzd29yZDI="
      cluster.health: {}
  - match: { timed_out: false }
