gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/certificate_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package command
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"gitee.com/zhaochuninhefei/fabric-ca-gm/cmd/fabric-ca-client/command/mocks"
    14  	"gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/util"
    15  	"gitee.com/zhaochuninhefei/fabric-ca-gm/lib"
    16  	"github.com/pkg/errors"
    17  	"github.com/spf13/cobra"
    18  	"github.com/spf13/viper"
    19  	"github.com/stretchr/testify/assert"
    20  )
    21  
    22  // Unit Tests
    23  func TestNewCertificateCommand(t *testing.T) {
    24  	cmd := new(mocks.Command)
    25  	cmd.On("GetViper").Return(viper.New())
    26  	certCmd := newCertificateCommand(cmd)
    27  	assert.NotNil(t, certCmd)
    28  }
    29  
    30  func TestAddCertificateCommand(t *testing.T) {
    31  	cmd := new(mocks.Command)
    32  	cmd.On("GetViper").Return(viper.New())
    33  	certCmd := newCertificateCommand(cmd)
    34  	assert.NotNil(t, certCmd)
    35  	addCmd := addCertificateCommand(certCmd)
    36  	assert.NotNil(t, addCmd)
    37  }
    38  
    39  func TestCreateCertificateCommand(t *testing.T) {
    40  	cmd := new(mocks.Command)
    41  	cmd.On("GetViper").Return(viper.New())
    42  	certCmd := createCertificateCommand(cmd)
    43  	assert.NotNil(t, certCmd)
    44  }
    45  
    46  func TestBadPreRunCertificate(t *testing.T) {
    47  	mockBadClientCmd := new(mocks.Command)
    48  	mockBadClientCmd.On("SetDefaultLogLevel", "warning")
    49  	mockBadClientCmd.On("ConfigInit").Return(errors.New("Failed to initialize config"))
    50  	cmd := newCertificateCommand(mockBadClientCmd)
    51  	err := cmd.preRunCertificate(&cobra.Command{}, []string{})
    52  	util.ErrorContains(t, err, "Failed to initialize config", "Should have failed")
    53  }
    54  
    55  func TestGoodPreRunCertificate(t *testing.T) {
    56  	mockGoodClientCmd := new(mocks.Command)
    57  	mockGoodClientCmd.On("SetDefaultLogLevel", "warning")
    58  	mockGoodClientCmd.On("ConfigInit").Return(nil)
    59  	mockGoodClientCmd.On("GetClientCfg").Return(&lib.ClientConfig{})
    60  	cmd := newCertificateCommand(mockGoodClientCmd)
    61  	err := cmd.preRunCertificate(&cobra.Command{}, []string{})
    62  	assert.NoError(t, err, "Should not have failed")
    63  }
    64  
    65  func TestFailLoadIdentity(t *testing.T) {
    66  	mockBadClientCmd := new(mocks.Command)
    67  	mockBadClientCmd.On("SetDefaultLogLevel", "warning")
    68  	mockBadClientCmd.On("LoadMyIdentity").Return(nil, errors.New("Failed to load identity"))
    69  	cmd := newCertificateCommand(mockBadClientCmd)
    70  	err := cmd.runListCertificate(&cobra.Command{}, []string{})
    71  	util.ErrorContains(t, err, "Failed to load identity", "Should have failed")
    72  }
    73  
    74  func TestBadRunListCertificate(t *testing.T) {
    75  	cmd := new(mocks.Command)
    76  	cmd.On("SetDefaultLogLevel", "warning")
    77  	cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)
    78  	certCmd := newCertificateCommand(cmd)
    79  	certCmd.timeArgs = timeArgs{
    80  		Expiration: "30d:15d",
    81  	}
    82  	err := certCmd.runListCertificate(&cobra.Command{}, []string{})
    83  	util.ErrorContains(t, err, "Invalid expiration format, expecting", "Should have failed")
    84  }
    85  
    86  func TestBadExpirationTime(t *testing.T) {
    87  	cmd := new(mocks.Command)
    88  	cmd.On("SetDefaultLogLevel", "warning")
    89  	cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)
    90  	certCmd := newCertificateCommand(cmd)
    91  	certCmd.timeArgs = timeArgs{
    92  		Expiration: "30d:15d",
    93  	}
    94  	err := certCmd.getCertListReq()
    95  	util.ErrorContains(t, err, "Invalid expiration format, expecting", "Should have failed")
    96  
    97  	certCmd.timeArgs = timeArgs{
    98  		Expiration: "01/30/2015::15d",
    99  	}
   100  	err = certCmd.getCertListReq()
   101  	util.ErrorContains(t, err, "Invalid expiration format, use '-' instead of '/'", "Should have failed")
   102  }
   103  
   104  func TestGoodExpirationTime(t *testing.T) {
   105  	cmd := new(mocks.Command)
   106  	cmd.On("SetDefaultLogLevel", "warning")
   107  	cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)
   108  	certCmd := newCertificateCommand(cmd)
   109  	certCmd.timeArgs = timeArgs{
   110  		Expiration: "30d::15d",
   111  	}
   112  	err := certCmd.getCertListReq()
   113  	assert.NoError(t, err, "Failed to parse properly formated expiration time range")
   114  }
   115  
   116  func TestBadRevocationTime(t *testing.T) {
   117  	cmd := new(mocks.Command)
   118  	cmd.On("SetDefaultLogLevel", "warning")
   119  	cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)
   120  	certCmd := newCertificateCommand(cmd)
   121  	certCmd.timeArgs = timeArgs{
   122  		Revocation: "30d:15d",
   123  	}
   124  	err := certCmd.getCertListReq()
   125  	util.ErrorContains(t, err, "Invalid revocation format, expecting", "Should have failed")
   126  
   127  	certCmd.timeArgs = timeArgs{
   128  		Revocation: "1/30/2015::15d",
   129  	}
   130  	err = certCmd.getCertListReq()
   131  	util.ErrorContains(t, err, "Invalid revocation format, use '-' instead of '/'", "Should have failed")
   132  }
   133  
   134  func TestGoodRevocationTime(t *testing.T) {
   135  	cmd := new(mocks.Command)
   136  	cmd.On("SetDefaultLogLevel", "warning")
   137  	cmd.On("LoadMyIdentity").Return(&lib.Identity{}, nil)
   138  	certCmd := newCertificateCommand(cmd)
   139  	certCmd.timeArgs = timeArgs{
   140  		Revocation: "30d::15d",
   141  	}
   142  	err := certCmd.getCertListReq()
   143  	assert.NoError(t, err, "Failed to parse properly formated revocation time range")
   144  }
   145  
   146  func TestTimeRangeWithNow(t *testing.T) {
   147  	timeNow := time.Now().UTC().Format(time.RFC3339)
   148  	timeStr := getTime("now")
   149  	assert.Equal(t, timeNow, timeStr)
   150  }