github.com/kbehouse/nsc@v0.0.6/cmd/listkeys_test.go (about)

     1  /*
     2   * Copyright 2018-2019 The NATS Authors
     3   * Licensed under the Apache License, Version 2.0 (the "License");
     4   * you may not use this file except in compliance with the License.
     5   * You may obtain a copy of the License at
     6   *
     7   * http://www.apache.org/licenses/LICENSE-2.0
     8   *
     9   * Unless required by applicable law or agreed to in writing, software
    10   * distributed under the License is distributed on an "AS IS" BASIS,
    11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12   * See the License for the specific language governing permissions and
    13   * limitations under the License.
    14   */
    15  
    16  package cmd
    17  
    18  import (
    19  	"fmt"
    20  	"os"
    21  	"path/filepath"
    22  	"testing"
    23  
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func Test_ListKeysDefault(t *testing.T) {
    28  	ts := NewTestStore(t, "O")
    29  	defer ts.Done(t)
    30  
    31  	ts.AddAccount(t, "A")
    32  	ts.AddUser(t, "A", "U")
    33  
    34  	_, stderr, err := ExecuteCmd(createListKeysCmd())
    35  	require.NoError(t, err)
    36  	stderr = StripTableDecorations(stderr)
    37  
    38  	require.Contains(t, stderr, fmt.Sprintf("%s %s *", "O", ts.GetOperatorPublicKey(t)))
    39  	require.Contains(t, stderr, fmt.Sprintf("%s %s *", "A", ts.GetAccountPublicKey(t, "A")))
    40  	require.Contains(t, stderr, fmt.Sprintf("%s %s *", "U", ts.GetUserPublicKey(t, "A", "U")))
    41  }
    42  
    43  func Test_listKeysOperatorOnly(t *testing.T) {
    44  	ts := NewTestStore(t, "O")
    45  	defer ts.Done(t)
    46  
    47  	ts.AddAccount(t, "A")
    48  	ts.AddUser(t, "A", "U")
    49  
    50  	_, stderr, err := ExecuteCmd(createListKeysCmd(), "--operator")
    51  	require.NoError(t, err)
    52  	stderr = StripTableDecorations(stderr)
    53  
    54  	require.Contains(t, stderr, fmt.Sprintf("%s %s *", "O", ts.GetOperatorPublicKey(t)))
    55  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "A", ts.GetAccountPublicKey(t, "A")))
    56  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "U", ts.GetUserPublicKey(t, "A", "U")))
    57  }
    58  
    59  func Test_listKeysAccountOnly(t *testing.T) {
    60  	ts := NewTestStore(t, "O")
    61  	defer ts.Done(t)
    62  
    63  	ts.AddAccount(t, "A")
    64  	ts.AddUser(t, "A", "U")
    65  
    66  	_, stderr, err := ExecuteCmd(createListKeysCmd(), "--accounts")
    67  	require.NoError(t, err)
    68  	stderr = StripTableDecorations(stderr)
    69  
    70  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "O", ts.GetOperatorPublicKey(t)))
    71  	require.Contains(t, stderr, fmt.Sprintf("%s %s *", "A", ts.GetAccountPublicKey(t, "A")))
    72  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "U", ts.GetUserPublicKey(t, "A", "U")))
    73  }
    74  
    75  func Test_ListKeysUserOnly(t *testing.T) {
    76  	ts := NewTestStore(t, "O")
    77  	defer ts.Done(t)
    78  
    79  	ts.AddAccount(t, "A")
    80  	ts.AddUser(t, "A", "U")
    81  
    82  	_, stderr, err := ExecuteCmd(createListKeysCmd(), "--users")
    83  	require.NoError(t, err)
    84  	stderr = StripTableDecorations(stderr)
    85  
    86  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "O", ts.GetOperatorPublicKey(t)))
    87  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "A", ts.GetAccountPublicKey(t, "A")))
    88  	require.Contains(t, stderr, fmt.Sprintf("%s %s *", "U", ts.GetUserPublicKey(t, "A", "U")))
    89  }
    90  
    91  func Test_ListKeysOther(t *testing.T) {
    92  	ts := NewTestStore(t, "O")
    93  	defer ts.Done(t)
    94  
    95  	ts.AddAccount(t, "A")
    96  	ts.AddUser(t, "A", "U")
    97  
    98  	_, pk, kp := CreateOperatorKey(t)
    99  	_, err := ts.KeyStore.Store(kp)
   100  	require.NoError(t, err)
   101  
   102  	_, stderr, err := ExecuteCmd(createListKeysCmd(), "--all", "--not-referenced")
   103  	require.NoError(t, err)
   104  	stderr = StripTableDecorations(stderr)
   105  
   106  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "O", ts.GetOperatorPublicKey(t)))
   107  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "A", ts.GetAccountPublicKey(t, "A")))
   108  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "U", ts.GetUserPublicKey(t, "A", "U")))
   109  	require.Contains(t, stderr, fmt.Sprintf("%s %s *", "?", pk))
   110  }
   111  
   112  func Test_ListKeysFilter(t *testing.T) {
   113  	ts := NewTestStore(t, "O")
   114  	defer ts.Done(t)
   115  
   116  	ts.AddAccount(t, "A")
   117  	ts.AddUser(t, "A", "U")
   118  
   119  	opk := ts.GetOperatorPublicKey(t)
   120  
   121  	_, stderr, err := ExecuteCmd(createListKeysCmd(), "--all", "--filter", opk[:10])
   122  	require.NoError(t, err)
   123  	stderr = StripTableDecorations(stderr)
   124  
   125  	require.Contains(t, stderr, fmt.Sprintf("%s %s *", "O", opk))
   126  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "A", ts.GetAccountPublicKey(t, "A")))
   127  	require.NotContains(t, stderr, fmt.Sprintf("%s %s *", "U", ts.GetUserPublicKey(t, "A", "U")))
   128  }
   129  
   130  func Test_ListKeysNoKeyStore(t *testing.T) {
   131  	ts := NewEmptyStore(t)
   132  	defer ts.Done(t)
   133  
   134  	require.NoError(t, os.Remove(filepath.Join(ts.Dir, "keys")))
   135  	_, _, err := ExecuteCmd(createListKeysCmd())
   136  	require.Error(t, err)
   137  	require.Contains(t, err.Error(), "does not exist")
   138  }