github.com/osdi23p228/fabric@v0.0.0-20221218062954-77808885f5db/common/policies/implicitmetaparser_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package policies
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  
    13  	"github.com/golang/protobuf/proto"
    14  	cb "github.com/hyperledger/fabric-protos-go/common"
    15  	"github.com/stretchr/testify/assert"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestImplicitMetaParserWrongTokenCount(t *testing.T) {
    20  	errorMatch := "expected two space separated tokens, but got"
    21  
    22  	t.Run("NoArgs", func(t *testing.T) {
    23  		res, err := ImplicitMetaFromString("")
    24  		assert.Nil(t, res)
    25  		require.Error(t, err)
    26  		assert.Regexp(t, errorMatch, err.Error())
    27  	})
    28  
    29  	t.Run("OneArg", func(t *testing.T) {
    30  		res, err := ImplicitMetaFromString("ANY")
    31  		assert.Nil(t, res)
    32  		require.Error(t, err)
    33  		assert.Regexp(t, errorMatch, err.Error())
    34  	})
    35  
    36  	t.Run("ThreeArgs", func(t *testing.T) {
    37  		res, err := ImplicitMetaFromString("ANY of these")
    38  		assert.Nil(t, res)
    39  		require.Error(t, err)
    40  		assert.Regexp(t, errorMatch, err.Error())
    41  	})
    42  }
    43  
    44  func TestImplicitMetaParserBadRule(t *testing.T) {
    45  	res, err := ImplicitMetaFromString("BAD Rule")
    46  	assert.Nil(t, res)
    47  	require.Error(t, err)
    48  	assert.Regexp(t, "unknown rule type 'BAD'", err.Error())
    49  }
    50  
    51  func TestImplicitMetaParserGreenPath(t *testing.T) {
    52  	for _, rule := range []cb.ImplicitMetaPolicy_Rule{cb.ImplicitMetaPolicy_ANY, cb.ImplicitMetaPolicy_ALL, cb.ImplicitMetaPolicy_MAJORITY} {
    53  		t.Run(rule.String(), func(t *testing.T) {
    54  			subPolicy := "foo"
    55  			res, err := ImplicitMetaFromString(fmt.Sprintf("%v %s", rule, subPolicy))
    56  			require.NoError(t, err)
    57  			assert.True(t, proto.Equal(res, &cb.ImplicitMetaPolicy{
    58  				SubPolicy: subPolicy,
    59  				Rule:      rule,
    60  			}))
    61  		})
    62  	}
    63  }