github.com/jenkins-x/jx/v2@v2.1.155/pkg/vault/rule_test.go (about)

     1  // +build unit
     2  
     3  package vault_test
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/hashicorp/hcl"
     9  	"github.com/jenkins-x/jx/v2/pkg/vault"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestEncodeVaultPathRule(t *testing.T) {
    14  	tests := map[string]struct {
    15  		rule *vault.PathRule
    16  		err  bool
    17  	}{
    18  		"marshal policy": {
    19  			rule: &vault.PathRule{
    20  				Path: []vault.PathPolicy{{
    21  					Prefix:       "secrets/*",
    22  					Capabilities: []string{vault.CreateCapability, vault.ReadCapability},
    23  				}},
    24  			},
    25  			err: false,
    26  		},
    27  		"marshal empty policy": {
    28  			rule: &vault.PathRule{
    29  				Path: []vault.PathPolicy{{
    30  					Prefix:       "",
    31  					Capabilities: []string{},
    32  				}},
    33  			},
    34  			err: false,
    35  		},
    36  		"marshal nil policy with error": {
    37  			rule: nil,
    38  			err:  true,
    39  		},
    40  	}
    41  
    42  	for name, tc := range tests {
    43  		t.Run(name, func(t *testing.T) {
    44  			output, err := tc.rule.String()
    45  			if tc.err {
    46  				assert.Error(t, err, "should encode policy with an error")
    47  			} else {
    48  				assert.NoError(t, err, "should encode policy without error")
    49  				var rule vault.PathRule
    50  				err = hcl.Decode(&rule, output)
    51  				assert.NoError(t, err, "should decode policy without error")
    52  				assert.Equal(t, *tc.rule, rule)
    53  			}
    54  		})
    55  	}
    56  }