github.com/authzed/spicedb@v1.32.1-0.20240520085336-ebda56537386/pkg/namespace/metadata_test.go (about)

     1  package namespace
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"google.golang.org/protobuf/types/known/anypb"
     8  
     9  	core "github.com/authzed/spicedb/pkg/proto/core/v1"
    10  
    11  	iv1 "github.com/authzed/spicedb/pkg/proto/impl/v1"
    12  )
    13  
    14  func TestMetadata(t *testing.T) {
    15  	require := require.New(t)
    16  
    17  	marshalled, err := anypb.New(&iv1.DocComment{
    18  		Comment: "Hi there",
    19  	})
    20  	require.Nil(err)
    21  
    22  	marshalledKind, err := anypb.New(&iv1.RelationMetadata{
    23  		Kind: iv1.RelationMetadata_PERMISSION,
    24  	})
    25  	require.Nil(err)
    26  
    27  	ns := &core.NamespaceDefinition{
    28  		Name: "somens",
    29  		Relation: []*core.Relation{
    30  			{
    31  				Name: "somerelation",
    32  				Metadata: &core.Metadata{
    33  					MetadataMessage: []*anypb.Any{
    34  						marshalledKind, marshalled,
    35  					},
    36  				},
    37  			},
    38  			{
    39  				Name: "anotherrelation",
    40  			},
    41  		},
    42  		Metadata: &core.Metadata{
    43  			MetadataMessage: []*anypb.Any{
    44  				marshalled,
    45  			},
    46  		},
    47  	}
    48  
    49  	verr := ns.Validate()
    50  	require.NoError(verr)
    51  
    52  	require.Equal([]string{"Hi there"}, GetComments(ns.Metadata))
    53  	require.Equal([]string{"Hi there"}, GetComments(ns.Relation[0].Metadata))
    54  	require.Equal(iv1.RelationMetadata_PERMISSION, GetRelationKind(ns.Relation[0]))
    55  
    56  	require.Equal([]string{}, GetComments(ns.Relation[1].Metadata))
    57  
    58  	FilterUserDefinedMetadataInPlace(ns)
    59  	require.Equal([]string{}, GetComments(ns.Metadata))
    60  	require.Equal([]string{}, GetComments(ns.Relation[0].Metadata))
    61  
    62  	require.Equal(iv1.RelationMetadata_PERMISSION, GetRelationKind(ns.Relation[0]))
    63  }