github.com/Datadog/cnab-go@v0.3.3-beta1.0.20191007143216-bba4b7e723d0/bundle/credentials_test.go (about) 1 package bundle 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 ) 9 10 func TestCompleteCredDefinition(t *testing.T) { 11 payload := `{ 12 "credentials": { 13 "something": { 14 "description" : "wicked this way comes", 15 "path" : "/cnab/app/a/credential", 16 "required" : true 17 } 18 } 19 }` 20 21 definitions, err := Unmarshal([]byte(payload)) 22 require.NoError(t, err, "given credentials payload was valid json") 23 24 something, ok := definitions.Credentials["something"] 25 assert.True(t, ok, "should have found the `something` entry") 26 27 assert.Equal(t, "/cnab/app/a/credential", something.Path, "did not contain the expected path") 28 assert.Equal(t, "wicked this way comes", something.Description, "did not contain the expected description") 29 30 } 31 32 func TestHandleMultipleCreds(t *testing.T) { 33 payload := `{ 34 "credentials": { 35 "something": { }, 36 "else": { } 37 } 38 }` 39 40 definitions, err := Unmarshal([]byte(payload)) 41 require.NoError(t, err, "given credentials payload was valid json") 42 43 assert.Equal(t, 2, len(definitions.Credentials), "credentials should have contained two entries") 44 45 _, ok := definitions.Credentials["something"] 46 assert.True(t, ok, "should have found the `something` entry") 47 48 _, ok = definitions.Credentials["else"] 49 assert.True(t, ok, "should have found the `else` entry") 50 51 } 52 53 func TestNotRequiredIsFalse(t *testing.T) { 54 payload := `{ 55 "credentials": { 56 "something": { 57 "path" : "/cnab/app/a/credential", 58 "required" : false 59 } 60 } 61 }` 62 63 definitions, err := Unmarshal([]byte(payload)) 64 require.NoError(t, err, "given credentials payload was valid json") 65 66 something, ok := definitions.Credentials["something"] 67 assert.True(t, ok, "should have found the credential") 68 assert.False(t, something.Required, "required was set to `false` in the json") 69 } 70 71 func TestUnspecifiedRequiredIsFalse(t *testing.T) { 72 payload := `{ 73 "credentials": { 74 "something": { 75 "path" : "/cnab/app/a/credential" 76 } 77 } 78 }` 79 80 definitions, err := Unmarshal([]byte(payload)) 81 require.NoError(t, err, "given credentials payload was valid json") 82 83 something, ok := definitions.Credentials["something"] 84 assert.True(t, ok, "should have found the credential") 85 assert.False(t, something.Required, "required was unspecified in the json") 86 } 87 88 func TestRequiredIsTrue(t *testing.T) { 89 payload := `{ 90 "credentials": { 91 "something": { 92 "path" : "/cnab/app/a/credential", 93 "required" : true 94 } 95 } 96 }` 97 98 definitions, err := Unmarshal([]byte(payload)) 99 require.NoError(t, err, "given credentials payload was valid json") 100 101 something, ok := definitions.Credentials["something"] 102 assert.True(t, ok, "should have found the credential") 103 assert.True(t, something.Required, "required was set to `true` in the json") 104 }