Skip to content

Commit

Permalink
Fix get object mock and add proper delete function testing
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszkowalczyk98 committed Nov 22, 2023
1 parent f406e3d commit acf2dfb
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 8 deletions.
26 changes: 26 additions & 0 deletions client/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,30 @@ func TestDelete(t *testing.T) {
if err != nil {
t.Error(err)
}

tests := []struct {
name string
key string
want string
wantErr bool
}{
{
name: "test non existing key",
key: test.Key9,
want: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := treasury.ReadValue(tt.key)
if (err != nil) != tt.wantErr {
t.Errorf("Client.ReadValue() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("Client.ReadValue() = %v, want %v", got, tt.want)
}
})
}
}
27 changes: 24 additions & 3 deletions client/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

const (
templateTestSourceFile = "../test/resources/source.secret.tpl"
templateTestSourceFile = "../test/resources/source.existing_secret.tpl"
templateTestSourceFile2 = "../test/resources/source.not_existing_secret.tpl"
templateTestDestinationFile = "../test/output/destination.secret"
templateTestDestinationParentDir = "../test/output"
)
Expand All @@ -29,8 +30,28 @@ func TestTemplate(t *testing.T) {
"Name": "some_testing_template",
}

if err := treasury.Template(templateTestSourceFile, templateTestDestinationFile, 0, map[string]string{}, envMap); err != nil {
t.Error("Could not generate secret file from template. Error: ", err.Error())
tests := []struct {
file string
wantErr bool
}{
{
file: templateTestSourceFile,
wantErr: false,
},
{
file: templateTestSourceFile2,
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.file, func(t *testing.T) {
err := treasury.Template(tt.file, templateTestDestinationFile, 0, map[string]string{}, envMap)
if (err != nil) != tt.wantErr {
t.Errorf("Failed to use treasury template, error = %v, wantErr %v", err, tt.wantErr)
return
}
})
}

_, err = os.Stat(templateTestDestinationParentDir)
Expand Down
10 changes: 6 additions & 4 deletions test/backend/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,12 @@ func (m *MockBackendClient) PutObject(input *types.PutObjectInput) error {
}

func (m *MockBackendClient) GetObject(input *types.GetObjectInput) (*types.GetObjectOutput, error) {
if _, ok := KeyValueMap[input.Key]; !ok {
return &types.GetObjectOutput{
Value: "",
}, fmt.Errorf("Missing key:%s in KeyValue map", input.Key)
}

return &types.GetObjectOutput{
Value: KeyValueMap[input.Key],
}, nil
Expand All @@ -86,9 +92,5 @@ func (m *MockBackendClient) DeleteObject(input *types.DeleteObjectInput) error {

delete(KeyValueMap, input.Key)

if _, ok := KeyValueMap[input.Key]; ok {
return errors.New(fmt.Sprintf("Failed to delete key:%s from KeyValue map", input.Key))
}

return nil
}
2 changes: 2 additions & 0 deletions test/resources/source.not_existing_secret.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
APPLICATION_NON_EXISTING_KEY={{ read "test/none/user_api_pass" }}
NAME={{ .Name }}
1 change: 0 additions & 1 deletion test/resources/source.secret.tpl
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
APPLICATION_SECRET_KEY={{ read "test/webapp/user_api_pass" }}
APPLICATION_NON_EXISTING_KEY={{ read "test/none/user_api_pass" }}
NAME={{ .Name }}

0 comments on commit acf2dfb

Please sign in to comment.