From 275579a0a21ce0ccf3aca4aa13bb0eda56da25d4 Mon Sep 17 00:00:00 2001 From: Juan Antonio Osorio Date: Thu, 19 Sep 2024 17:21:03 +0300 Subject: [PATCH] Add unit tests for `TestPropertiesToProtoMessage` in Gitlab provider (#4544) Signed-off-by: Juan Antonio Osorio --- internal/providers/gitlab/properties_test.go | 144 +++++++++++++++++++ 1 file changed, 144 insertions(+) diff --git a/internal/providers/gitlab/properties_test.go b/internal/providers/gitlab/properties_test.go index 48f375710f..8a81bea74d 100644 --- a/internal/providers/gitlab/properties_test.go +++ b/internal/providers/gitlab/properties_test.go @@ -24,6 +24,7 @@ import ( "github.com/stretchr/testify/assert" "github.com/xanzy/go-gitlab" + "google.golang.org/protobuf/reflect/protoreflect" "github.com/stacklok/minder/internal/entities/properties" "github.com/stacklok/minder/internal/providers/credentials" @@ -256,6 +257,149 @@ func Test_gitlabClient_FetchAllProperties(t *testing.T) { } } +func TestPropertiesToProtoMessage(t *testing.T) { + t.Parallel() + + tests := []struct { + name string + entityType minderv1.Entity + props map[string]any + expected protoreflect.ProtoMessage + wantErr bool + }{ + { + name: "empty properties", + props: map[string]any{}, + entityType: minderv1.Entity_ENTITY_REPOSITORIES, + expected: nil, + wantErr: true, + }, + { + name: "single non-relevant property", + props: map[string]any{ + "key1": "value1", + }, + entityType: minderv1.Entity_ENTITY_REPOSITORIES, + expected: nil, + wantErr: true, + }, + { + name: "upstream ID but no other properties", + props: map[string]any{ + properties.PropertyUpstreamID: "1", + }, + entityType: minderv1.Entity_ENTITY_REPOSITORIES, + expected: nil, + wantErr: true, + }, + { + name: "upstream ID and name", + props: map[string]any{ + properties.PropertyUpstreamID: "1", + RepoPropertyProjectName: "group", + }, + entityType: minderv1.Entity_ENTITY_REPOSITORIES, + expected: nil, + wantErr: true, + }, + { + name: "upstream ID and namespace", + props: map[string]any{ + properties.PropertyUpstreamID: "1", + RepoPropertyNamespace: "group", + }, + entityType: minderv1.Entity_ENTITY_REPOSITORIES, + expected: nil, + wantErr: true, + }, + { + name: "missing is_private", + props: map[string]any{ + properties.PropertyUpstreamID: "1", + RepoPropertyProjectName: "group", + RepoPropertyNamespace: "project", + }, + entityType: minderv1.Entity_ENTITY_REPOSITORIES, + expected: nil, + wantErr: true, + }, + { + name: "missing is_fork", + props: map[string]any{ + properties.PropertyUpstreamID: "1", + RepoPropertyProjectName: "group", + RepoPropertyNamespace: "project", + properties.RepoPropertyIsPrivate: true, + }, + entityType: minderv1.Entity_ENTITY_REPOSITORIES, + expected: nil, + wantErr: true, + }, + { + name: "valid repository properties", + props: map[string]any{ + properties.PropertyUpstreamID: "1", + properties.PropertyName: "group/project", + RepoPropertyProjectName: "project", + RepoPropertyNamespace: "group", + properties.RepoPropertyIsPrivate: true, + properties.RepoPropertyIsFork: true, + properties.RepoPropertyIsArchived: false, + }, + entityType: minderv1.Entity_ENTITY_REPOSITORIES, + expected: &minderv1.Repository{ + RepoId: 1, + Name: "project", + Owner: "group", + IsPrivate: true, + IsFork: true, + Properties: MustNewProperties(map[string]any{ + properties.PropertyUpstreamID: "1", + properties.PropertyName: "group/project", + RepoPropertyProjectName: "project", + RepoPropertyNamespace: "group", + properties.RepoPropertyIsPrivate: true, + properties.RepoPropertyIsFork: true, + properties.RepoPropertyIsArchived: false, + }).ToProtoStruct(), + }, + }, + { + name: "invalid entity", + props: map[string]any{ + properties.PropertyUpstreamID: "1", + properties.PropertyName: "group/project", + RepoPropertyProjectName: "project", + RepoPropertyNamespace: "group", + properties.RepoPropertyIsPrivate: true, + properties.RepoPropertyIsFork: true, + properties.RepoPropertyIsArchived: false, + }, + entityType: minderv1.Entity_ENTITY_UNSPECIFIED, + expected: nil, + wantErr: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + + props := MustNewProperties(tt.props) + + g := &gitlabClient{} + result, err := g.PropertiesToProtoMessage(tt.entityType, props) + if tt.wantErr { + assert.Error(t, err) + return + } + + assert.NoError(t, err) + assert.Equal(t, tt.expected, result) + }) + } +} + // MustNewProperties creates Properties from a map or panics func MustNewProperties(props map[string]any) *properties.Properties { p, err := properties.NewProperties(props)