forked from cnabio/cnab-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bundles may set the applyTo field on credentials. When it is set, only require the credential when it applies to the specified action. Implements cnabio/cnab-spec#407 Signed-off-by: Carolyn Van Slyck <[email protected]>
- Loading branch information
Showing
9 changed files
with
143 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package bundle | ||
|
||
// Scoped represents an item whose scope is limited to a set of actions. | ||
type Scoped interface { | ||
// GetApplyTo returns the list of applicable actions. | ||
GetApplyTo() []string | ||
} | ||
|
||
// AppliesTo returns a boolean value specifying whether or not | ||
// the scoped item applies to the provided action. | ||
func AppliesTo(s Scoped, action string) bool { | ||
applyTo := s.GetApplyTo() | ||
if len(applyTo) == 0 { | ||
return true | ||
} | ||
for _, act := range applyTo { | ||
if action == act { | ||
return true | ||
} | ||
} | ||
return false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package bundle | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestAppliesTo(t *testing.T) { | ||
makeTestTypes := func(applyTo []string) []Scoped { | ||
return []Scoped{ | ||
&Credential{ApplyTo: applyTo}, | ||
Output{ApplyTo: applyTo}, | ||
&Parameter{ApplyTo: applyTo}, | ||
} | ||
} | ||
|
||
t.Run("empty", func(t *testing.T) { | ||
testTypes := makeTestTypes(nil) | ||
|
||
for _, tt := range testTypes { | ||
assert.True(t, AppliesTo(tt, "install"), "%T.AppliesTo returned an incorrect result", tt) | ||
} | ||
}) | ||
|
||
t.Run("hit", func(t *testing.T) { | ||
testTypes := makeTestTypes([]string{"install", "upgrade", "custom"}) | ||
|
||
for _, tt := range testTypes { | ||
assert.True(t, AppliesTo(tt, "custom"), "%T.AppliesTo returned an incorrect result", tt) | ||
} | ||
}) | ||
|
||
t.Run("miss", func(t *testing.T) { | ||
testTypes := makeTestTypes([]string{"install", "upgrade", "uninstall"}) | ||
|
||
for _, tt := range testTypes { | ||
assert.False(t, AppliesTo(tt, "custom"), "%T.AppliesTo returned an incorrect result", tt) | ||
} | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -95,4 +95,4 @@ | |
], | ||
"title": "CNAB Claims json schema", | ||
"type": "object" | ||
} | ||
} |