2021-08-26 11:04:32 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2021-06-22 22:26:26 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-07-20 11:17:09 -07:00
|
|
|
package handlers
|
|
|
|
|
|
|
|
|
|
import (
|
2023-06-06 17:09:08 -04:00
|
|
|
"context"
|
2020-07-20 11:17:09 -07:00
|
|
|
"testing"
|
|
|
|
|
|
2021-08-26 11:04:32 -04:00
|
|
|
pb "github.com/hashicorp/boundary/sdk/pbs/controller/protooptions"
|
2020-07-20 11:17:09 -07:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestMaskManager(t *testing.T) {
|
2023-06-06 17:09:08 -04:00
|
|
|
mm, err := NewMaskManager(context.Background(), MaskDestination{&pb.TestProperlyNamedFields{}}, MaskSource{&pb.TestBase{}})
|
2020-08-14 14:08:12 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, []string(nil), mm.Translate([]string{"doesnt_exist"}))
|
|
|
|
|
assert.Equal(t, []string{"OtherFirstField"}, mm.Translate([]string{"first_field"}))
|
|
|
|
|
assert.Equal(t, []string{"OtherFirstField"}, mm.Translate([]string{"first_field", "doesnt_exist"}))
|
|
|
|
|
assert.Equal(t, []string(nil), mm.Translate([]string{"FiRsT_fIeLd"}))
|
|
|
|
|
assert.Equal(t, []string{"other_second_field"}, mm.Translate([]string{"strangly_formatted_field"}))
|
|
|
|
|
assert.Equal(t, []string{"other_second_field", "other_field_3"}, mm.Translate([]string{"strangly_formatted_field", "field3"}))
|
2021-11-04 10:43:08 -07:00
|
|
|
|
|
|
|
|
// the passedThroughPrefix allows fields to be passed through unmodified
|
|
|
|
|
assert.Equal(t, []string{"OtherFirstField", "attributes.doesnt_exist"}, mm.Translate([]string{"first_field", "attributes.doesnt_exist"}, "attributes."))
|
|
|
|
|
// the passedThroughPrefix is ignored if the field already matches a mapped value
|
|
|
|
|
assert.Equal(t, []string{"other_second_field", "other_field_3"}, mm.Translate([]string{"strangly_formatted_field", "field3"}, "strangely_"))
|
2020-08-14 14:08:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMaskManager_Split(t *testing.T) {
|
2023-06-06 17:09:08 -04:00
|
|
|
mm, err := NewMaskManager(context.Background(), MaskDestination{&pb.TestProperlyNamedFields{}}, MaskSource{&pb.TestBaseSplit1{}, &pb.TestBaseSplit2{}})
|
2020-07-20 11:17:09 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, []string(nil), mm.Translate([]string{"doesnt_exist"}))
|
|
|
|
|
assert.Equal(t, []string{"OtherFirstField"}, mm.Translate([]string{"first_field"}))
|
|
|
|
|
assert.Equal(t, []string{"OtherFirstField"}, mm.Translate([]string{"first_field", "doesnt_exist"}))
|
|
|
|
|
assert.Equal(t, []string(nil), mm.Translate([]string{"FiRsT_fIeLd"}))
|
|
|
|
|
assert.Equal(t, []string{"other_second_field"}, mm.Translate([]string{"strangly_formatted_field"}))
|
|
|
|
|
assert.Equal(t, []string{"other_second_field", "other_field_3"}, mm.Translate([]string{"strangly_formatted_field", "field3"}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMaskManager_errors(t *testing.T) {
|
2023-06-06 17:09:08 -04:00
|
|
|
ctx := context.Background()
|
|
|
|
|
_, err := NewMaskManager(ctx, MaskDestination{&pb.TestBase{}}, MaskSource{&pb.TestManyToOneMappings{}})
|
2020-07-20 11:17:09 -07:00
|
|
|
assert.Error(t, err)
|
2023-06-06 17:09:08 -04:00
|
|
|
_, err = NewMaskManager(ctx, MaskDestination{&pb.TestBase{}}, MaskSource{&pb.TestNameDoesntMap{}})
|
2020-07-20 11:17:09 -07:00
|
|
|
assert.Error(t, err)
|
2023-06-06 17:09:08 -04:00
|
|
|
_, err = NewMaskManager(ctx, MaskDestination{&pb.TestBase{}}, MaskSource{&pb.TestNotEnoughFields{}})
|
2020-07-20 11:17:09 -07:00
|
|
|
assert.Error(t, err)
|
|
|
|
|
}
|