github.com/yimialmonte/fabric@v2.1.1+incompatible/common/channelconfig/acls_test.go (about)

     1  /*
     2  Copyright State Street Corp. 2018 All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package channelconfig
     8  
     9  import (
    10  	"testing"
    11  
    12  	pb "github.com/hyperledger/fabric-protos-go/peer"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  const (
    17  	sampleAPI1Name      = "Foo"
    18  	sampleAPI1PolicyRef = "foo"
    19  
    20  	sampleAPI2Name      = "Bar"
    21  	sampleAPI2PolicyRef = "/Channel/foo"
    22  )
    23  
    24  var sampleAPIsProvider = map[string]*pb.APIResource{
    25  	sampleAPI1Name: {PolicyRef: sampleAPI1PolicyRef},
    26  	sampleAPI2Name: {PolicyRef: sampleAPI2PolicyRef},
    27  }
    28  
    29  func TestGreenAPIsPath(t *testing.T) {
    30  	ag := newAPIsProvider(sampleAPIsProvider)
    31  	assert.NotNil(t, ag)
    32  
    33  	t.Run("PresentAPIs", func(t *testing.T) {
    34  		assert.Equal(t, "/Channel/Application/"+sampleAPI1PolicyRef, ag.PolicyRefForAPI(sampleAPI1Name))
    35  		assert.Equal(t, sampleAPI2PolicyRef, ag.PolicyRefForAPI(sampleAPI2Name))
    36  	})
    37  
    38  	t.Run("MissingAPIs", func(t *testing.T) {
    39  		assert.Empty(t, ag.PolicyRefForAPI("missing"))
    40  	})
    41  }
    42  
    43  func TestNilACLs(t *testing.T) {
    44  	ccg := newAPIsProvider(nil)
    45  
    46  	assert.NotNil(t, ccg)
    47  	assert.NotNil(t, ccg.aclPolicyRefs)
    48  	assert.Empty(t, ccg.aclPolicyRefs)
    49  }
    50  
    51  func TestEmptyACLs(t *testing.T) {
    52  	ccg := newAPIsProvider(map[string]*pb.APIResource{})
    53  
    54  	assert.NotNil(t, ccg)
    55  	assert.NotNil(t, ccg.aclPolicyRefs)
    56  	assert.Empty(t, ccg.aclPolicyRefs)
    57  }
    58  
    59  func TestEmptyPolicyRef(t *testing.T) {
    60  	var ars = map[string]*pb.APIResource{
    61  		"unsetAPI": {PolicyRef: ""},
    62  	}
    63  
    64  	ccg := newAPIsProvider(ars)
    65  
    66  	assert.NotNil(t, ccg)
    67  	assert.NotNil(t, ccg.aclPolicyRefs)
    68  	assert.Empty(t, ccg.aclPolicyRefs)
    69  
    70  	ars = map[string]*pb.APIResource{
    71  		"unsetAPI": {PolicyRef: ""},
    72  		"setAPI":   {PolicyRef: sampleAPI2PolicyRef},
    73  	}
    74  
    75  	ccg = newAPIsProvider(ars)
    76  
    77  	assert.NotNil(t, ccg)
    78  	assert.NotNil(t, ccg.aclPolicyRefs)
    79  	assert.NotEmpty(t, ccg.aclPolicyRefs)
    80  	assert.NotContains(t, ccg.aclPolicyRefs, sampleAPI1Name)
    81  
    82  }