* feat(proj-alias): support project scoped alias via suffixes * fix: generate resouce docs using make gen * fix: address review comments * chore: make gen changes * fix(proja-alias): validate scope id before calling remove alias suffix * fix: filter unknown scopes in validation
111 lines
2.3 KiB
Go
111 lines
2.3 KiB
Go
// Copyright IBM Corp. 2020, 2026
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
package provider
|
|
|
|
import "testing"
|
|
|
|
func TestValidateAliasSuffixScope(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
tests := []struct {
|
|
name string
|
|
scopeId string
|
|
wantError bool
|
|
}{
|
|
{
|
|
name: "global scope rejected",
|
|
scopeId: globalScopeId,
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "org scope allowed",
|
|
scopeId: "o_1234567890",
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "project scope allowed",
|
|
scopeId: "p_1234567890",
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "unknown scope type rejected",
|
|
scopeId: "s_1234567890",
|
|
wantError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
tt := tt
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
err := validateAliasSuffixScope(tt.scopeId)
|
|
if tt.wantError && err == nil {
|
|
t.Fatal("expected error but got nil")
|
|
}
|
|
if !tt.wantError && err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestAliasSuffixFromScopeResponseMap(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("nil map", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := aliasSuffixFromScopeResponseMap(nil)
|
|
if err == nil {
|
|
t.Fatal("expected error for nil map")
|
|
}
|
|
})
|
|
|
|
t.Run("missing key", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := aliasSuffixFromScopeResponseMap(map[string]interface{}{})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got != "" {
|
|
t.Fatalf("expected empty alias suffix, got %q", got)
|
|
}
|
|
})
|
|
|
|
t.Run("nil value", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := aliasSuffixFromScopeResponseMap(map[string]interface{}{aliasSuffixKey: nil})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got != "" {
|
|
t.Fatalf("expected empty alias suffix, got %q", got)
|
|
}
|
|
})
|
|
|
|
t.Run("wrong type", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
_, err := aliasSuffixFromScopeResponseMap(map[string]interface{}{aliasSuffixKey: 123})
|
|
if err == nil {
|
|
t.Fatal("expected error for non-string alias suffix")
|
|
}
|
|
})
|
|
|
|
t.Run("string value", func(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
got, err := aliasSuffixFromScopeResponseMap(map[string]interface{}{aliasSuffixKey: "example.boundary"})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if got != "example.boundary" {
|
|
t.Fatalf("unexpected alias suffix: %q", got)
|
|
}
|
|
})
|
|
}
|