77 "context"
88 "database/sql"
99 "errors"
10+ "strings"
1011 "testing"
1112
1213 "github.com/google/uuid"
@@ -27,6 +28,16 @@ import (
2728 "github.com/mindersec/minder/pkg/entities/properties"
2829)
2930
31+ // toIdentifyingProps converts a map[string]any to map[string]*structpb.Value for tests
32+ func toIdentifyingProps (m map [string ]any ) map [string ]* structpb.Value {
33+ result := make (map [string ]* structpb.Value , len (m ))
34+ for k , v := range m {
35+ val , _ := structpb .NewValue (v )
36+ result [k ] = val
37+ }
38+ return result
39+ }
40+
3041func TestServer_RegisterEntity (t * testing.T ) {
3142 t .Parallel ()
3243
@@ -50,13 +61,10 @@ func TestServer_RegisterEntity(t *testing.T) {
5061 request : & pb.RegisterEntityRequest {
5162 Context : & pb.ContextV2 {},
5263 EntityType : pb .Entity_ENTITY_REPOSITORIES ,
53- IdentifyingProperties : func () * structpb.Struct {
54- s , _ := structpb .NewStruct (map [string ]any {
55- "github/repo_owner" : "test-owner" ,
56- "github/repo_name" : "test-repo" ,
57- })
58- return s
59- }(),
64+ IdentifyingProperties : toIdentifyingProps (map [string ]any {
65+ "github/repo_owner" : "test-owner" ,
66+ "github/repo_name" : "test-repo" ,
67+ }),
6068 },
6169 setupContext : func (ctx context.Context ) context.Context {
6270 return engcontext .WithEntityContext (ctx , & engcontext.EntityContext {
@@ -106,12 +114,9 @@ func TestServer_RegisterEntity(t *testing.T) {
106114 {
107115 name : "fails when entity_type is unspecified" ,
108116 request : & pb.RegisterEntityRequest {
109- Context : & pb.ContextV2 {},
110- EntityType : pb .Entity_ENTITY_UNSPECIFIED ,
111- IdentifyingProperties : func () * structpb.Struct {
112- s , _ := structpb .NewStruct (map [string ]any {"key" : "value" })
113- return s
114- }(),
117+ Context : & pb.ContextV2 {},
118+ EntityType : pb .Entity_ENTITY_UNSPECIFIED ,
119+ IdentifyingProperties : toIdentifyingProps (map [string ]any {"key" : "value" }),
115120 },
116121 setupContext : func (ctx context.Context ) context.Context {
117122 return engcontext .WithEntityContext (ctx , & engcontext.EntityContext {
@@ -145,12 +150,9 @@ func TestServer_RegisterEntity(t *testing.T) {
145150 {
146151 name : "fails when provider not found" ,
147152 request : & pb.RegisterEntityRequest {
148- Context : & pb.ContextV2 {},
149- EntityType : pb .Entity_ENTITY_REPOSITORIES ,
150- IdentifyingProperties : func () * structpb.Struct {
151- s , _ := structpb .NewStruct (map [string ]any {"key" : "value" })
152- return s
153- }(),
153+ Context : & pb.ContextV2 {},
154+ EntityType : pb .Entity_ENTITY_REPOSITORIES ,
155+ IdentifyingProperties : toIdentifyingProps (map [string ]any {"key" : "value" }),
154156 },
155157 setupContext : func (ctx context.Context ) context.Context {
156158 return engcontext .WithEntityContext (ctx , & engcontext.EntityContext {
@@ -172,13 +174,10 @@ func TestServer_RegisterEntity(t *testing.T) {
172174 request : & pb.RegisterEntityRequest {
173175 Context : & pb.ContextV2 {},
174176 EntityType : pb .Entity_ENTITY_REPOSITORIES ,
175- IdentifyingProperties : func () * structpb.Struct {
176- s , _ := structpb .NewStruct (map [string ]any {
177- "github/repo_owner" : "test-owner" ,
178- "github/repo_name" : "archived-repo" ,
179- })
180- return s
181- }(),
177+ IdentifyingProperties : toIdentifyingProps (map [string ]any {
178+ "github/repo_owner" : "test-owner" ,
179+ "github/repo_name" : "archived-repo" ,
180+ }),
182181 },
183182 setupContext : func (ctx context.Context ) context.Context {
184183 return engcontext .WithEntityContext (ctx , & engcontext.EntityContext {
@@ -209,13 +208,10 @@ func TestServer_RegisterEntity(t *testing.T) {
209208 request : & pb.RegisterEntityRequest {
210209 Context : & pb.ContextV2 {},
211210 EntityType : pb .Entity_ENTITY_REPOSITORIES ,
212- IdentifyingProperties : func () * structpb.Struct {
213- s , _ := structpb .NewStruct (map [string ]any {
214- "github/repo_owner" : "test-owner" ,
215- "github/repo_name" : "private-repo" ,
216- })
217- return s
218- }(),
211+ IdentifyingProperties : toIdentifyingProps (map [string ]any {
212+ "github/repo_owner" : "test-owner" ,
213+ "github/repo_name" : "private-repo" ,
214+ }),
219215 },
220216 setupContext : func (ctx context.Context ) context.Context {
221217 return engcontext .WithEntityContext (ctx , & engcontext.EntityContext {
@@ -243,12 +239,9 @@ func TestServer_RegisterEntity(t *testing.T) {
243239 {
244240 name : "handles internal errors appropriately" ,
245241 request : & pb.RegisterEntityRequest {
246- Context : & pb.ContextV2 {},
247- EntityType : pb .Entity_ENTITY_REPOSITORIES ,
248- IdentifyingProperties : func () * structpb.Struct {
249- s , _ := structpb .NewStruct (map [string ]any {"key" : "value" })
250- return s
251- }(),
242+ Context : & pb.ContextV2 {},
243+ EntityType : pb .Entity_ENTITY_REPOSITORIES ,
244+ IdentifyingProperties : toIdentifyingProps (map [string ]any {"key" : "value" }),
252245 },
253246 setupContext : func (ctx context.Context ) context.Context {
254247 return engcontext .WithEntityContext (ctx , & engcontext.EntityContext {
@@ -332,14 +325,11 @@ func TestParseIdentifyingProperties(t *testing.T) {
332325 {
333326 name : "parses valid properties" ,
334327 request : & pb.RegisterEntityRequest {
335- IdentifyingProperties : func () * structpb.Struct {
336- s , _ := structpb .NewStruct (map [string ]any {
337- "github/repo_owner" : "stacklok" ,
338- "github/repo_name" : "minder" ,
339- "upstream_id" : "12345" ,
340- })
341- return s
342- }(),
328+ IdentifyingProperties : toIdentifyingProps (map [string ]any {
329+ "github/repo_owner" : "stacklok" ,
330+ "github/repo_name" : "minder" ,
331+ "upstream_id" : "12345" ,
332+ }),
343333 },
344334 wantErr : false ,
345335 validate : func (t * testing.T , props * properties.Properties ) {
@@ -365,16 +355,12 @@ func TestParseIdentifyingProperties(t *testing.T) {
365355 {
366356 name : "rejects properties that are too large" ,
367357 request : & pb.RegisterEntityRequest {
368- IdentifyingProperties : func () * structpb.Struct {
358+ IdentifyingProperties : func () map [ string ] * structpb.Value {
369359 // Create a value large enough to exceed 32KB limit
370- largeValue := string (make ([]byte , 33 * 1024 ))
371- for i := range largeValue {
372- largeValue = string (append ([]byte (largeValue [:i ]), 'x' ))
373- }
374- s , _ := structpb .NewStruct (map [string ]any {
360+ largeValue := strings .Repeat ("x" , 33 * 1024 )
361+ return toIdentifyingProps (map [string ]any {
375362 "large_key" : largeValue ,
376363 })
377- return s
378364 }(),
379365 },
380366 wantErr : true ,
@@ -383,15 +369,11 @@ func TestParseIdentifyingProperties(t *testing.T) {
383369 {
384370 name : "rejects property key that is too long" ,
385371 request : & pb.RegisterEntityRequest {
386- IdentifyingProperties : func () * structpb.Struct {
387- longKey := string (make ([]byte , 201 ))
388- for i := range longKey {
389- longKey = string (append ([]byte (longKey [:i ]), 'a' ))
390- }
391- s , _ := structpb .NewStruct (map [string ]any {
372+ IdentifyingProperties : func () map [string ]* structpb.Value {
373+ longKey := strings .Repeat ("a" , 201 )
374+ return toIdentifyingProps (map [string ]any {
392375 longKey : "value" ,
393376 })
394- return s
395377 }(),
396378 },
397379 wantErr : true ,
@@ -400,17 +382,10 @@ func TestParseIdentifyingProperties(t *testing.T) {
400382 {
401383 name : "handles empty properties map" ,
402384 request : & pb.RegisterEntityRequest {
403- IdentifyingProperties : func () * structpb.Struct {
404- s , _ := structpb .NewStruct (map [string ]any {})
405- return s
406- }(),
407- },
408- wantErr : false ,
409- validate : func (t * testing.T , props * properties.Properties ) {
410- t .Helper ()
411- // Empty properties is valid (provider will validate)
412- assert .NotNil (t , props )
385+ IdentifyingProperties : toIdentifyingProps (map [string ]any {}),
413386 },
387+ wantErr : true , // Empty map is now an error (changed behavior)
388+ errContains : "identifying_properties is required" ,
414389 },
415390 }
416391
0 commit comments