package main

import "testing"

func TestMatchesSessionIDValidationRules(t *testing.T) {
	rules := []SessionIDValidationRule{
		{Path: `^/api/v1/orgs/\d+/data-proxy(?:/|$)`, Required: true},
		{Method: "GET", Path: `^/api/v1/orgs/\d+/ngm/clusters/\d+/continuous_profiling/(?:group_profiles|group_profile/detail|action_token|download)$`},
		{Method: "GET", Path: `^/api/v1/orgs/\d+/ngm/clusters/\d+/plan_replayer/list$`},
		{Method: "GET", Path: `^/api/v1/orgs/\d+/ngm/clusters/\d+/oom_record/(?:list|files)$`},
	}
	tests := []struct {
		name   string
		method string
		path   string
		want   bool
	}{
		{
			name:   "data proxy list",
			method: "GET",
			path:   "/api/v1/orgs/123/data-proxy/clusters",
			want:   true,
		},
		{
			name:   "data proxy query uses any method",
			method: "POST",
			path:   "/api/v1/orgs/123/data-proxy/clusters/456/query",
			want:   true,
		},
		{
			name:   "continuous profiling group profiles",
			method: "GET",
			path:   "/api/v1/orgs/123/ngm/clusters/456/continuous_profiling/group_profiles",
			want:   true,
		},
		{
			name:   "continuous profiling group profile detail",
			method: "GET",
			path:   "/api/v1/orgs/123/ngm/clusters/456/continuous_profiling/group_profile/detail",
			want:   true,
		},
		{
			name:   "continuous profiling action token",
			method: "GET",
			path:   "/api/v1/orgs/123/ngm/clusters/456/continuous_profiling/action_token",
			want:   true,
		},
		{
			name:   "continuous profiling download",
			method: "GET",
			path:   "/api/v1/orgs/123/ngm/clusters/456/continuous_profiling/download",
			want:   true,
		},
		{
			name:   "plan replayer list",
			method: "GET",
			path:   "/api/v1/orgs/123/ngm/clusters/456/plan_replayer/list",
			want:   true,
		},
		{
			name:   "oom record list",
			method: "GET",
			path:   "/api/v1/orgs/123/ngm/clusters/456/oom_record/list",
			want:   true,
		},
		{
			name:   "oom record files",
			method: "GET",
			path:   "/api/v1/orgs/123/ngm/clusters/456/oom_record/files",
			want:   true,
		},
		{
			name:   "unlisted ngm endpoint",
			method: "GET",
			path:   "/api/v1/orgs/123/ngm/clusters/456/continuous_profiling/other",
			want:   false,
		},
		{
			name:   "ngm method does not match",
			method: "POST",
			path:   "/api/v1/orgs/123/ngm/clusters/456/oom_record/list",
			want:   false,
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			got, err := matchesSessionIDValidationRules(test.method, test.path, rules)
			if err != nil {
				t.Fatal(err)
			}
			if got != test.want {
				t.Fatalf("matchesSessionIDValidationRules(%q, %q) = %t, want %t", test.method, test.path, got, test.want)
			}
		})
	}
}

func TestSessionIDValidationRuleForRequestReturnsRequiredFlag(t *testing.T) {
	rule, matched, err := sessionIDValidationRuleForRequest(
		"POST",
		"/api/v1/orgs/123/data-proxy/clusters/456/query",
		[]SessionIDValidationRule{{Path: `^/api/v1/orgs/\d+/data-proxy(?:/|$)`, Required: true}},
	)
	if err != nil {
		t.Fatal(err)
	}
	if !matched || !rule.Required {
		t.Fatalf("matched=%t required=%t, want true true", matched, rule.Required)
	}
}
