github.com/nats-io/nsc@v0.0.0-20221206222106-35db9400b257/cmd/env_test.go (about)

     1  /*
     2   * Copyright 2018-2022 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  	"path/filepath"
    21  	"testing"
    22  
    23  	"github.com/nats-io/nsc/cmd/store"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestEnv_DefaultOutput(t *testing.T) {
    28  	ts := NewTestStore(t, "test")
    29  	defer ts.Done(t)
    30  
    31  	ts.AddAccount(t, "A")
    32  	_, stderr, err := ExecuteCmd(createEnvCmd())
    33  	require.NoError(t, err)
    34  	stderr = StripTableDecorations(stderr)
    35  	require.NoError(t, err)
    36  	require.Contains(t, stderr, fmt.Sprintf("$NKEYS_PATH Yes %s", AbbrevHomePaths(store.GetKeysDir())))
    37  	require.Contains(t, stderr, fmt.Sprintf("Current Store Dir %s", AbbrevHomePaths(filepath.Dir(ts.Store.Dir))))
    38  	require.Contains(t, stderr, "Current Operator test")
    39  }
    40  
    41  func TestEnv_SetAccountOutput(t *testing.T) {
    42  	ts := NewTestStore(t, "test")
    43  	defer ts.Done(t)
    44  
    45  	ts.AddAccount(t, "A")
    46  	ts.AddAccount(t, "B")
    47  
    48  	_, stderr, err := ExecuteCmd(createEnvCmd(), "--operator", "test", "--account", "B")
    49  	require.NoError(t, err)
    50  	stderr = StripTableDecorations(stderr)
    51  	require.Contains(t, stderr, fmt.Sprintf("$NKEYS_PATH Yes %s", AbbrevHomePaths(store.GetKeysDir())))
    52  	require.Contains(t, stderr, fmt.Sprintf("Current Store Dir %s", AbbrevHomePaths(filepath.Dir(ts.Store.Dir))))
    53  	require.Contains(t, stderr, "Current Operator test")
    54  	require.Contains(t, stderr, "Current Account B")
    55  }
    56  
    57  func TestEnv_FailsBadOperator(t *testing.T) {
    58  	ts := NewTestStore(t, "O")
    59  	defer ts.Done(t)
    60  
    61  	_, _, err := ExecuteCmd(createEnvCmd(), "-o", "X")
    62  	require.Error(t, err)
    63  	require.Contains(t, err.Error(), "operator \"X\" not in")
    64  }
    65  
    66  func TestEnv_FailsBadAccount(t *testing.T) {
    67  	ts := NewTestStore(t, "O")
    68  	defer ts.Done(t)
    69  
    70  	_, _, err := ExecuteCmd(createEnvCmd(), "-a", "A")
    71  	require.Error(t, err)
    72  	require.Contains(t, err.Error(), "\"A\" not in accounts for operator \"O\"")
    73  }