Skip to content

Commit

Permalink
fix: updated sensitive value to include hints and null values
Browse files Browse the repository at this point in the history
  • Loading branch information
jbristowe committed Apr 5, 2021
1 parent 4342427 commit 0bf4bab
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 7 deletions.
13 changes: 7 additions & 6 deletions octopusdeploy/sensitive_value.go
Expand Up @@ -2,19 +2,20 @@ package octopusdeploy

// NewSensitiveValue creates and initializes a sensitive value.
func NewSensitiveValue(newValue string) *SensitiveValue {
if len(newValue) == 0 {
if len(newValue) > 0 {
return &SensitiveValue{
HasValue: false,
HasValue: true,
NewValue: &newValue,
}
}

return &SensitiveValue{
HasValue: true,
NewValue: newValue,
HasValue: false,
}
}

type SensitiveValue struct {
HasValue bool `json:"HasValue"`
NewValue string `json:"NewValue,omitempty"`
HasValue bool
Hint *string
NewValue *string
}
134 changes: 134 additions & 0 deletions octopusdeploy/sensitive_value_test.go
@@ -0,0 +1,134 @@
package octopusdeploy

import (
"encoding/json"
"testing"

"github.com/kinbiko/jsonassert"
"github.com/stretchr/testify/require"
)

func TestSensitiveValueBehaviour(t *testing.T) {
sv := SensitiveValue{}

require.NotNil(t, sv)
require.False(t, sv.HasValue)
require.Nil(t, sv.Hint)
require.Nil(t, sv.NewValue)

svp := &SensitiveValue{}

require.NotNil(t, svp)
require.False(t, svp.HasValue)
require.Nil(t, sv.Hint)
require.Nil(t, sv.NewValue)

sensitiveValueAsJSON, err := json.Marshal(svp)
require.NoError(t, err)
require.NotNil(t, sensitiveValueAsJSON)
jsonassert.New(t).Assertf(string(sensitiveValueAsJSON), emptySensitiveValueAsJSON)

svp = &SensitiveValue{
HasValue: true,
}

require.NotNil(t, svp)
require.True(t, svp.HasValue)
require.Nil(t, sv.Hint)
require.Nil(t, sv.NewValue)

sensitiveValueAsJSON, err = json.Marshal(svp)
require.NoError(t, err)
require.NotNil(t, sensitiveValueAsJSON)
jsonassert.New(t).Assertf(string(sensitiveValueAsJSON), emptyHasValueSensitiveValueAsJSON)

svp = &SensitiveValue{
HasValue: true,
Hint: String("this is the hint"),
NewValue: String("test"),
}

require.NotNil(t, svp)
require.True(t, svp.HasValue)
require.Equal(t, "this is the hint", *svp.Hint)
require.Equal(t, "test", *svp.NewValue)

sensitiveValueAsJSON, err = json.Marshal(svp)
require.NoError(t, err)
require.NotNil(t, sensitiveValueAsJSON)
jsonassert.New(t).Assertf(string(sensitiveValueAsJSON), testWithHintSensitiveValueAsJSON)
}

func TestNewSensitiveValueBehaviour(t *testing.T) {
svp := NewSensitiveValue("")

require.NotNil(t, svp)
require.False(t, svp.HasValue)
require.Nil(t, svp.Hint)
require.Nil(t, svp.NewValue)

svp = NewSensitiveValue("test")

require.NotNil(t, svp)
require.True(t, svp.HasValue)
require.Nil(t, svp.Hint)
require.Equal(t, "test", *svp.NewValue)
}

func TestNewSensitiveValueMarshalJSON(t *testing.T) {
sensitiveValue := NewSensitiveValue("")

sensitiveValueAsJSON, err := json.Marshal(sensitiveValue)
require.NoError(t, err)
require.NotNil(t, sensitiveValueAsJSON)
jsonassert.New(t).Assertf(string(sensitiveValueAsJSON), emptySensitiveValueAsJSON)

sensitiveValue = NewSensitiveValue("test")

sensitiveValueAsJSON, err = json.Marshal(sensitiveValue)
require.NoError(t, err)
require.NotNil(t, sensitiveValueAsJSON)
jsonassert.New(t).Assertf(string(sensitiveValueAsJSON), testNoHintSensitiveValueAsJSON)
}

func TestNewSensitiveValueUnmarshalJSON(t *testing.T) {
var emptySensitiveValue SensitiveValue
err := json.Unmarshal([]byte(emptySensitiveValueAsJSON), &emptySensitiveValue)

require.NoError(t, err)
require.NotNil(t, emptySensitiveValue)
require.False(t, emptySensitiveValue.HasValue)
require.Nil(t, emptySensitiveValue.NewValue)

var testSensitiveValue SensitiveValue
err = json.Unmarshal([]byte(testNoHintSensitiveValueAsJSON), &testSensitiveValue)

require.NoError(t, err)
require.NotNil(t, testSensitiveValue)
require.True(t, testSensitiveValue.HasValue)
require.Equal(t, "test", *testSensitiveValue.NewValue)
}

const emptySensitiveValueAsJSON string = `{
"HasValue": false,
"Hint": null,
"NewValue": null
}`

const emptyHasValueSensitiveValueAsJSON string = `{
"HasValue": true,
"Hint": null,
"NewValue": null
}`

const testNoHintSensitiveValueAsJSON string = `{
"HasValue": true,
"Hint": null,
"NewValue": "test"
}`

const testWithHintSensitiveValueAsJSON string = `{
"HasValue": true,
"Hint": "this is the hint",
"NewValue": "test"
}`
2 changes: 1 addition & 1 deletion octopusdeploy/util.go
Expand Up @@ -75,7 +75,7 @@ func ValidateRequiredSensitiveValue(propertyName string, sensitiveValue *Sensiti
return fmt.Errorf("%s is a required property; its underlying value is not set", propertyName)
}

if len(sensitiveValue.NewValue) == 0 {
if len(*sensitiveValue.NewValue) == 0 {
return fmt.Errorf("%s is a required property; its underlying value is not set", propertyName)
}

Expand Down

0 comments on commit 0bf4bab

Please sign in to comment.