package api import ( "encoding/json" "testing" "github.com/google/uuid" "github.com/stretchr/testify/assert" "google.golang.org/protobuf/encoding/protojson" auditV1 "dev.azure.com/schwarzit/schwarzit.stackit-public/audit-go.git/gen/go/audit/v1" pkgAuditCommon "dev.azure.com/schwarzit/schwarzit.stackit-public/audit-go.git/pkg/audit/common" ) func Test_GetCalledServiceNameFromRequest(t *testing.T) { t.Run("request is nil", func(t *testing.T) { serviceName := GetCalledServiceNameFromRequest(nil, "resource-manager") assert.Equal(t, "resource-manager", serviceName) }) t.Run("localhost", func(t *testing.T) { request := pkgAuditCommon.ApiRequest{Host: "localhost:8080"} serviceName := GetCalledServiceNameFromRequest(&request, "resource-manager") assert.Equal(t, "resource-manager", serviceName) }) t.Run("cf", func(t *testing.T) { request := pkgAuditCommon.ApiRequest{Host: "stackit-resource-manager-go-dev.apps.01.cf.eu01.stackit.cloud"} serviceName := GetCalledServiceNameFromRequest(&request, "resource-manager") assert.Equal(t, "stackit-resource-manager-go-dev", serviceName) }) t.Run("cf invalid host", func(t *testing.T) { request := pkgAuditCommon.ApiRequest{Host: ""} serviceName := GetCalledServiceNameFromRequest(&request, "resource-manager") assert.Equal(t, "resource-manager", serviceName) }) t.Run("ip", func(t *testing.T) { request := pkgAuditCommon.ApiRequest{Host: "127.0.0.1"} serviceName := GetCalledServiceNameFromRequest(&request, "resource-manager") assert.Equal(t, "resource-manager", serviceName) }, ) t.Run("ip short", func(t *testing.T) { request := pkgAuditCommon.ApiRequest{Host: "::1"} serviceName := GetCalledServiceNameFromRequest(&request, "resource-manager") assert.Equal(t, "resource-manager", serviceName) }, ) } func Test_GetObjectIdAndTypeFromUrlPath(t *testing.T) { t.Run("object id and type not in url", func(t *testing.T) { objectId, objectType, err := GetObjectIdAndTypeFromUrlPath("/v2/projects/audit") assert.NoError(t, err) assert.Equal(t, "", objectId) assert.Nil(t, objectType) }) t.Run("object id and type in url", func(t *testing.T) { objectId, objectType, err := GetObjectIdAndTypeFromUrlPath("/v2/projects/f17d4064-9b65-4334-b6a7-8fed96340124") assert.NoError(t, err) assert.Equal(t, "f17d4064-9b65-4334-b6a7-8fed96340124", objectId) assert.Equal(t, pkgAuditCommon.ObjectTypeProject, *objectType) }) t.Run("multiple object ids and types in url", func(t *testing.T) { objectId, objectType, err := GetObjectIdAndTypeFromUrlPath("/v2/organization/8ee58bec-d496-4bb9-af8d-72fda4d78b6b/projects/f17d4064-9b65-4334-b6a7-8fed96340124") assert.NoError(t, err) assert.Equal(t, "f17d4064-9b65-4334-b6a7-8fed96340124", objectId) assert.Equal(t, pkgAuditCommon.ObjectTypeProject, *objectType) }) } func Test_ResponseBodyToBytes(t *testing.T) { t.Run( "nil response body", func(t *testing.T) { bytes, err := ResponseBodyToBytes(nil) assert.Nil(t, bytes) assert.Nil(t, err) }, ) t.Run( "bytes", func(t *testing.T) { responseBody := []byte("data") bytes, err := ResponseBodyToBytes(responseBody) assert.Nil(t, err) assert.Equal(t, responseBody, bytes) }, ) t.Run( "Protobuf message", func(t *testing.T) { protobufMessage := auditV1.ObjectIdentifier{Identifier: uuid.NewString(), Type: string(pkgAuditCommon.ObjectTypeProject)} bytes, err := ResponseBodyToBytes(&protobufMessage) assert.Nil(t, err) expected, err := protojson.Marshal(&protobufMessage) assert.Nil(t, err) assert.Equal(t, expected, bytes) }, ) t.Run( "struct", func(t *testing.T) { type CustomObject struct { Value string } responseBody := CustomObject{Value: "data"} bytes, err := ResponseBodyToBytes(responseBody) assert.Nil(t, err) expected, err := json.Marshal(responseBody) assert.Nil(t, err) assert.Equal(t, expected, bytes) }, ) t.Run( "map", func(t *testing.T) { responseBody := map[string]interface{}{"value": "data"} bytes, err := ResponseBodyToBytes(responseBody) assert.Nil(t, err) expected, err := json.Marshal(responseBody) assert.Nil(t, err) assert.Equal(t, expected, bytes) }, ) } func Test_ToArrayMap(t *testing.T) { t.Run("empty map", func(t *testing.T) { result := ToArrayMap(map[string]string{}) assert.Equal(t, map[string][]string{}, result) }) t.Run("empty map", func(t *testing.T) { result := ToArrayMap(map[string]string{"key1": "value1", "key2": "value2"}) assert.Equal(t, map[string][]string{ "key1": {"value1"}, "key2": {"value2"}, }, result) }) }