github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/env_test.go (about)

     1  /*
     2   * Copyright 2018-2023 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"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/nats-io/nsc/v2/cmd/store"
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  )
    29  
    30  func TestEnv_DefaultOutput(t *testing.T) {
    31  	ts := NewTestStore(t, "test")
    32  	defer ts.Done(t)
    33  
    34  	ts.AddAccount(t, "A")
    35  	_, stderr, err := ExecuteCmd(createEnvCmd())
    36  	require.NoError(t, err)
    37  	stderr = StripTableDecorations(stderr)
    38  	require.NoError(t, err)
    39  	require.Contains(t, stderr, fmt.Sprintf("$NKEYS_PATH (deprecated) Yes %s", AbbrevHomePaths(store.GetKeysDir())))
    40  	require.Contains(t, stderr, fmt.Sprintf("Current Store Dir %s", AbbrevHomePaths(filepath.Dir(ts.Store.Dir))))
    41  	require.Contains(t, stderr, "Current Operator test")
    42  }
    43  
    44  func TestEnv_SetAccountOutput(t *testing.T) {
    45  	ts := NewTestStore(t, "test")
    46  	defer ts.Done(t)
    47  
    48  	ts.AddAccount(t, "A")
    49  	ts.AddAccount(t, "B")
    50  
    51  	_, stderr, err := ExecuteCmd(createEnvCmd(), "--operator", "test", "--account", "B")
    52  	require.NoError(t, err)
    53  	stderr = StripTableDecorations(stderr)
    54  	require.Contains(t, stderr, fmt.Sprintf("$NKEYS_PATH (deprecated) Yes %s", AbbrevHomePaths(store.GetKeysDir())))
    55  	require.Contains(t, stderr, fmt.Sprintf("Current Store Dir %s", AbbrevHomePaths(filepath.Dir(ts.Store.Dir))))
    56  	require.Contains(t, stderr, "Current Operator test")
    57  	require.Contains(t, stderr, "Current Account B")
    58  }
    59  
    60  func TestEnv_FailsBadOperator(t *testing.T) {
    61  	ts := NewTestStore(t, "O")
    62  	defer ts.Done(t)
    63  
    64  	_, _, err := ExecuteCmd(createEnvCmd(), "-o", "X")
    65  	require.Error(t, err)
    66  	require.Contains(t, err.Error(), "operator \"X\" not in")
    67  }
    68  
    69  func TestEnv_FailsBadAccount(t *testing.T) {
    70  	ts := NewTestStore(t, "O")
    71  	defer ts.Done(t)
    72  
    73  	_, _, err := ExecuteCmd(createEnvCmd(), "-a", "A")
    74  	require.Error(t, err)
    75  	require.Contains(t, err.Error(), "\"A\" not in accounts for operator \"O\"")
    76  }
    77  
    78  func TestAllDir(t *testing.T) {
    79  	p := MakeTempDir(t)
    80  	defer os.RemoveAll(p)
    81  
    82  	_, _, err := ExecuteCmd(rootCmd, "env", "--all-dirs", p)
    83  	require.NoError(t, err)
    84  
    85  	_, _, err = ExecuteCmd(rootCmd, "add", "operator", "O", "--all-dirs", p)
    86  	require.NoError(t, err)
    87  
    88  	assert.FileExists(t, path.Join(p, "nsc.json"))
    89  	assert.DirExists(t, path.Join(p, "creds"))
    90  	assert.DirExists(t, path.Join(p, "keys"))
    91  	assert.DirExists(t, path.Join(p, "O"))
    92  }