name: Check RADIUS audit log
testcases:
- name: get_login_token
  steps:
  - type: get_login_token

- name: move_from_redis_to_db
  steps:
  - type: pfcron_flush_radius_audit_log

- name: get_time
  steps:
  - type: exec
    script: "date '+%Y-%m-%d %H:%M:%S' --date='2 minutes ago'"
    vars:
      two_minutes_ago:
        from: result.systemout

# only latest search entry since two minutes that matches
# auth_status equals Accept (to avoid Disconnect)
# mac equals {{.node01_ens7_mac_address}}"
# connection type of test suite connection profile
- name: get_id_of_radius_audit_log_entry
  steps:
  - type: http
    method: POST
    url: '{{.pfserver_webadmin_url}}/api/v1/radius_audit_logs/search'
    ignore_verify_ssl: true
    body: >-
      {
        "cursor": 0,
        "fields": [
          "id"
        ],
        "sort": [
          "created_at DESC"
        ],        
        "limit": 1,
        "query": {
          "op": "and",
          "values": [
            {
              "op": "or",
              "values": [
                {
                  "field": "mac",
                  "op": "equals",
                  "value": "{{.node01_ens7_mac_address}}"
                }
              ]
            },
            {
              "op": "or",
              "values": [
                {
                  "field": "auth_status",
                  "op": "equals",
                  "value": "Accept"
                }
              ]
            },
            {
              "op": "or",
              "values": [
                {
                  "field": "connection_type",
                  "op": "equals",
                  "value": "{{.wired_dot1x_eap_tls_scep.profiles.wired.filters.connection_type}}"
                }
              ]
            },
            {
              "op": "or",
              "values": [            
                {
                  "field": "created_at",
                  "op": "greater_than",
                  "value": "{{.get_time.two_minutes_ago}}"
                }
              ]
            }
          ]
        }
      }
    headers:
      "Authorization": "{{.get_login_token.result.token}}"
      "Content-Type": "application/json"
    assertions:
      - result.statuscode ShouldEqual 200
      - result.bodyjson.items.items0 ShouldContainKey id
    vars:
      id:
        from: result.bodyjson.items.items0.id
      
- name: check_radius_reply
  steps:
  - type: http
    method: GET
    url: '{{.pfserver_webadmin_url}}/api/v1/radius_audit_log/{{.get_id_of_radius_audit_log_entry.id}}'
    ignore_verify_ssl: true
    headers:
      "Authorization": "{{.get_login_token.result.token}}"
      "Content-Type": "application/json"
    assertions:
      - result.statuscode ShouldEqual 200
      - result.bodyjson.item.radius_reply ShouldContainSubstring 'Tunnel-Private-Group-Id = "{{.wired_dot1x_eap_tls_scep.roles.dot1x_eap_tls.vlan_id}}"'
      - result.bodyjson.item.profile ShouldEqual "{{.wired_dot1x_eap_tls_scep.profiles.wired.id}}"
