---
setup:
- skip:
    features:
    - "stash_in_key"
    - "headers"
    - "allowed_warnings_regex"
- do:
    cluster.health:
      wait_for_status: "yellow"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    security.put_user:
      username: "joe"
      body: "{\n  \"password\" : \"s3krit-password\",\n  \"roles\" : [ \"superuser\"\
        \ ],\n  \"full_name\" : \"Bazooka Joe\",\n  \"email\" : \"joe@bazooka.gum\"\
        \n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    security.put_user:
      username: "doe"
      body: "{\n  \"password\" : \"s3krit-password\",\n  \"roles\" : [ \"superuser\"\
        \ ],\n  \"full_name\" : \"Bazooka Doe\",\n  \"email\" : \"doe@bazooka.gum\"\
        \n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
teardown:
- skip:
    features:
    - "headers"
    - "allowed_warnings_regex"
- do:
    security.delete_user:
      username: "joe"
      ignore: 404
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    security.delete_user:
      username: "doe"
      ignore: 404
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
---
Test get user profiles:
- do:
    security.activate_user_profile:
      body: "{\n  \"grant_type\": \"password\",\n  \"username\": \"joe\",\n  \"password\"\
        \ : \"s3krit-password\"\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "uid"
- match:
    user.username: "joe"
- match:
    user.roles:
    - "superuser"
- match:
    user.full_name: "Bazooka Joe"
- match:
    user.realm_name: "default_native"
- is_false: "user.realm_domain"
- is_true: "_doc"
- set:
    uid: "profile_uid_1"
- do:
    security.activate_user_profile:
      body: "{\n  \"grant_type\": \"password\",\n  \"username\": \"doe\",\n  \"password\"\
        \ : \"s3krit-password\"\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "uid"
- match:
    user.username: "doe"
- match:
    user.roles:
    - "superuser"
- match:
    user.full_name: "Bazooka Doe"
- match:
    user.realm_name: "default_native"
- is_false: "user.realm_domain"
- is_true: "_doc"
- set:
    uid: "profile_uid_2"
- do:
    security.get_user_profile:
      uid: "u_eGdcwXYIE3LEOrqz6p-3DHQ4HrAtxuk_ttRNFh2m7rM_0,u_eZ75KhGvkY4_t0HfQpNPO1aO0tk6wd908bjUGieTKm8_0,does_not_exist"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    profiles: 2
- set:
    profiles.0: "profile"
- match:
    $profile.uid: "$profile_uid_1"
- match:
    $profile.user.username: "joe"
- match:
    $profile.data: {}
- set:
    profiles.1: "profile"
- match:
    $profile.uid: "$profile_uid_2"
- match:
    $profile.user.username: "doe"
- match:
    $profile.data: {}
- is_true: "errors"
- set:
    errors: "errors"
- match:
    $errors.count: 1
- match:
    $errors.details.does_not_exist.type: "resource_not_found_exception"
- do:
    security.get_user:
      username:
      - "joe"
      - "doe"
      with_profile_uid: true
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    joe.profile_uid: "$profile_uid_1"
- match:
    doe.profile_uid: "$profile_uid_2"
---
Test user profile apis:
- do:
    security.activate_user_profile:
      body: "{\n  \"grant_type\": \"password\",\n  \"username\": \"joe\",\n  \"password\"\
        \ : \"s3krit-password\"\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "uid"
- match:
    user.username: "joe"
- match:
    user.roles:
    - "superuser"
- match:
    user.full_name: "Bazooka Joe"
- match:
    user.realm_name: "default_native"
- is_false: "user.realm_domain"
- is_true: "_doc"
- set:
    uid: "profile_uid"
- do:
    security.get_user_profile:
      uid: "$profile_uid"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    profiles: 1
- is_false: "errors"
- set:
    profiles.0: "profile"
- match:
    $profile.uid: "$profile_uid"
- match:
    $profile.user.username: "joe"
- match:
    $profile.data: {}
- do:
    security.update_user_profile_data:
      uid: "$profile_uid"
      body: "{\n  \"labels\": {\n    \"kibana\": {\n      \"spaces\": \"demo\"\n \
        \   }\n  },\n  \"data\": {\n    \"app1\": {\n      \"theme\": \"default\"\n\
        \    },\n    \"app2\": {\n      \"theme\": \"dark\"\n    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- is_true: "acknowledged"
- do:
    security.get_user_profile:
      uid: "$profile_uid"
      data: "app1"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    profiles: 1
- is_false: "errors"
- set:
    profiles.0: "profile"
- match:
    $profile.uid: "$profile_uid"
- match:
    $profile.data:
      app1:
        theme: "default"
- do:
    security.activate_user_profile:
      body: "{\n  \"grant_type\": \"password\",\n  \"username\": \"joe\",\n  \"password\"\
        \ : \"s3krit-password\"\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- match:
    uid: "$profile_uid"
- do:
    security.get_user_profile:
      uid: "$profile_uid"
      data: "*"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    profiles: 1
- is_false: "errors"
- set:
    profiles.0: "profile"
- match:
    $profile.uid: "$profile_uid"
- match:
    $profile.labels:
      kibana:
        spaces: "demo"
- match:
    $profile.data:
      app1:
        theme: "default"
      app2:
        theme: "dark"
- do:
    security.get_user_profile:
      uid: "no_such_profile_uid"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    profiles: 0
- is_true: "errors"
- set:
    errors: "errors"
- match:
    $errors.count: 1
- match:
    $errors.details.no_such_profile_uid.type: "resource_not_found_exception"
---
Test enable disable user profile:
- do:
    security.activate_user_profile:
      body: "{\n  \"grant_type\": \"password\",\n  \"username\": \"joe\",\n  \"password\"\
        \ : \"s3krit-password\"\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- set:
    uid: "profile_uid"
- do:
    security.get_user_profile:
      uid: "$profile_uid"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- set:
    profiles.0: "profile"
- is_true: "$profile.enabled"
- do:
    security.disable_user_profile:
      uid: "$profile_uid"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    security.get_user_profile:
      uid: "$profile_uid"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- set:
    profiles.0: "profile"
- is_false: "$profile.enabled"
- do:
    security.enable_user_profile:
      uid: "$profile_uid"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- do:
    security.get_user_profile:
      uid: "$profile_uid"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- set:
    profiles.0: "profile"
- is_true: "$profile.enabled"
---
Test suggest user profile:
- do:
    security.activate_user_profile:
      body: "{\n  \"grant_type\": \"password\",\n  \"username\": \"joe\",\n  \"password\"\
        \ : \"s3krit-password\"\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- set:
    uid: "profile_uid"
- do:
    security.suggest_user_profiles:
      body: "{\n  \"name\": \"joe\",\n  \"size\": 5,\n  \"hint\": {\n    \"uids\"\
        : [\"u_79HkWkwmnBH5gqFKwoxggWPjEBOur1zLPXQPEl1VBW0_0\"],\n    \"labels\":\
        \ {\n      \"direction\": [\"north\", \"east\"]\n    }\n  }\n}\n"
    headers:
      Content-Type: "application/vnd.elasticsearch+json;compatible-with=8"
      Accept: "application/vnd.elasticsearch+json;compatible-with=8"
    allowed_warnings_regex:
    - ".*rollup functionality will be removed in Elasticsearch.*"
    - "Index \\[\\.profiling-.*\\] name begins with a dot.* and will not be allowed\
      \ in a future Elasticsearch version."
    - "Parameter \\[default_metric\\] is deprecated and will be removed in a future\
      \ version"
- length:
    profiles: 1
- match:
    profiles.0.uid: "$profile_uid"
