github.com/nats-io/nsc/v2@v2.8.7-0.20240307184528-efd7023c6896/cmd/select_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  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func TestSelectAccount(t *testing.T) {
    25  	require := require.New(t)
    26  	ts := NewTestStore(t, "test")
    27  	defer ts.Done(t)
    28  
    29  	_, _, err := ExecuteCmd(selectAccountCmd(), "A")
    30  	require.NotNil(err)
    31  
    32  	ts.AddAccount(t, "A")
    33  	ts.AddAccount(t, "B")
    34  
    35  	_, _, err = ExecuteCmd(selectAccountCmd(), "A")
    36  	require.Nil(err)
    37  	conf := GetConfig()
    38  	require.Equal(conf.Account, "A")
    39  
    40  	_, _, err = ExecuteCmd(selectAccountCmd(), "B")
    41  	require.Nil(err)
    42  	conf = GetConfig()
    43  	require.Equal(conf.Account, "B")
    44  
    45  	_, _, err = ExecuteCmd(selectAccountCmd(), "NO")
    46  	require.NotNil(err)
    47  	require.Contains(err.Error(), "\"NO\" not in accounts for operator")
    48  
    49  	conf = GetConfig()
    50  	require.Equal(conf.Account, "B")
    51  }
    52  
    53  func TestSelectOperator(t *testing.T) {
    54  	require := require.New(t)
    55  	ts := NewTestStore(t, "test")
    56  	defer ts.Done(t)
    57  
    58  	_, _, err := ExecuteCmd(selectOperatorCmd(), "test")
    59  	require.Nil(err)
    60  
    61  	ts.AddOperator(t, "test2")
    62  	ts.AddOperator(t, "test3")
    63  
    64  	_, _, err = ExecuteCmd(selectOperatorCmd(), "test2")
    65  	require.Nil(err)
    66  	conf := GetConfig()
    67  	require.Equal(conf.Operator, "test2")
    68  
    69  	_, _, err = ExecuteCmd(selectOperatorCmd(), "test3")
    70  	require.Nil(err)
    71  	conf = GetConfig()
    72  	require.Equal(conf.Operator, "test3")
    73  
    74  	_, _, err = ExecuteCmd(selectOperatorCmd(), "NO")
    75  	require.NotNil(err)
    76  	require.Contains(err.Error(), "operator \"NO\" not in")
    77  
    78  	conf = GetConfig()
    79  	require.Equal(conf.Operator, "test3")
    80  }