Skip to content

Commit 95372d6

Browse files
committed
fix: check for nil project when adapting proto specification
Signed-off-by: Kush Sharma <[email protected]>
1 parent f3e7721 commit 95372d6

File tree

2 files changed

+73
-7
lines changed

2 files changed

+73
-7
lines changed

api/handler/v1/adapter.go

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -225,16 +225,23 @@ func (adapt *Adapter) ToProjectProtoWithSecrets(spec models.ProjectSpec) *pb.Pro
225225
}
226226

227227
func (adapt *Adapter) FromProjectProtoWithSecrets(conf *pb.ProjectSpecification) models.ProjectSpec {
228+
if conf == nil {
229+
return models.ProjectSpec{}
230+
}
228231
pConf := map[string]string{}
229-
for key, val := range conf.GetConfig() {
230-
pConf[strings.ToUpper(key)] = val
232+
if conf.GetConfig() != nil {
233+
for key, val := range conf.GetConfig() {
234+
pConf[strings.ToUpper(key)] = val
235+
}
231236
}
232237
pSec := models.ProjectSecrets{}
233-
for _, s := range conf.GetSecrets() {
234-
pSec = append(pSec, models.ProjectSecretItem{
235-
Name: s.Name,
236-
Value: s.Value,
237-
})
238+
if conf.GetSecrets() != nil {
239+
for _, s := range conf.GetSecrets() {
240+
pSec = append(pSec, models.ProjectSecretItem{
241+
Name: s.Name,
242+
Value: s.Value,
243+
})
244+
}
238245
}
239246
return models.ProjectSpec{
240247
Name: conf.GetName(),

api/handler/v1/adapter_test.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ package v1_test
22

33
import (
44
"context"
5+
"reflect"
56
"testing"
67
"time"
78

9+
pb "github.com/odpf/optimus/api/proto/odpf/optimus"
10+
811
"github.com/odpf/optimus/mock"
912

1013
v1 "github.com/odpf/optimus/api/handler/v1"
@@ -86,3 +89,59 @@ func TestAdapter(t *testing.T) {
8689
assert.Nil(t, err)
8790
})
8891
}
92+
93+
func TestAdapter_FromProjectProtoWithSecrets(t *testing.T) {
94+
type args struct {
95+
conf *pb.ProjectSpecification
96+
}
97+
tests := []struct {
98+
name string
99+
args args
100+
want models.ProjectSpec
101+
}{
102+
{
103+
name: "null project should be handled correctly",
104+
args: args{
105+
conf: nil,
106+
},
107+
want: models.ProjectSpec{},
108+
},
109+
{
110+
name: "proto should be converted correctly",
111+
args: args{
112+
conf: &pb.ProjectSpecification{
113+
Name: "hello",
114+
Config: map[string]string{
115+
"key": "val",
116+
},
117+
Secrets: []*pb.ProjectSpecification_ProjectSecret{
118+
{
119+
Name: "key",
120+
Value: "sec",
121+
},
122+
},
123+
},
124+
},
125+
want: models.ProjectSpec{
126+
Name: "hello",
127+
Config: map[string]string{
128+
"KEY": "val",
129+
},
130+
Secret: []models.ProjectSecretItem{
131+
{
132+
Name: "key",
133+
Value: "sec",
134+
},
135+
},
136+
},
137+
},
138+
}
139+
for _, tt := range tests {
140+
t.Run(tt.name, func(t *testing.T) {
141+
adapt := &v1.Adapter{}
142+
if got := adapt.FromProjectProtoWithSecrets(tt.args.conf); !reflect.DeepEqual(got, tt.want) {
143+
t.Errorf("FromProjectProtoWithSecrets() = %v, want %v", got, tt.want)
144+
}
145+
})
146+
}
147+
}

0 commit comments

Comments
 (0)