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

     1  /*
     2   * Copyright 2018-2021 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  	"net/url"
    20  	"testing"
    21  
    22  	cli "github.com/nats-io/cliprompts/v2"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  func Test_InitLocal(t *testing.T) {
    27  	ts := NewTestStore(t, "X")
    28  	defer ts.Done(t)
    29  
    30  	_, _, err := ExecuteCmd(createInitCmd(), "--name", "O")
    31  	require.NoError(t, err)
    32  
    33  	ts.VerifyOperator(t, "O", false)
    34  	ts.VerifyAccount(t, "O", "O", true)
    35  	ts.VerifyUser(t, "O", "O", "O", true)
    36  	ts.VerifyAccount(t, "O", "SYS", true)
    37  	ts.VerifyUser(t, "O", "SYS", "sys", true)
    38  }
    39  
    40  func Test_InitExists(t *testing.T) {
    41  	ts := NewTestStore(t, "O")
    42  	defer ts.Done(t)
    43  
    44  	_, _, err := ExecuteCmd(createInitCmd(), "--name", "O")
    45  	require.Error(t, err)
    46  }
    47  
    48  func Test_InitDeploy(t *testing.T) {
    49  	ts := NewTestStore(t, "X")
    50  	defer ts.Done(t)
    51  
    52  	as, _ := RunTestAccountServer(t)
    53  	defer as.Close()
    54  
    55  	ourl, err := url.Parse(as.URL)
    56  	require.NoError(t, err)
    57  	ourl.Path = "/jwt/v1/operator"
    58  
    59  	_, _, err = ExecuteCmd(createInitCmd(), "--name", "O", "--url", ourl.String())
    60  	require.NoError(t, err)
    61  
    62  	ts.VerifyOperator(t, "T", true)
    63  	ts.VerifyAccount(t, "T", "O", true)
    64  	ts.VerifyUser(t, "T", "O", "O", true)
    65  }
    66  
    67  func Test_InitRandomName(t *testing.T) {
    68  	ts := NewTestStore(t, "X")
    69  	defer ts.Done(t)
    70  
    71  	as, _ := RunTestAccountServer(t)
    72  	defer as.Close()
    73  
    74  	ourl, err := url.Parse(as.URL)
    75  	require.NoError(t, err)
    76  	ourl.Path = "/jwt/v1/operator"
    77  
    78  	_, _, err = ExecuteCmd(createInitCmd(), "--url", ourl.String())
    79  	require.NoError(t, err)
    80  
    81  	name := GetLastRandomName()
    82  
    83  	ts.VerifyOperator(t, "T", true) // Operator name comes from the URL
    84  	ts.VerifyAccount(t, "T", name, true)
    85  	ts.VerifyUser(t, "T", name, name, true)
    86  }
    87  
    88  func Test_InitStarName(t *testing.T) {
    89  	ts := NewTestStore(t, "X")
    90  	defer ts.Done(t)
    91  
    92  	as, _ := RunTestAccountServer(t)
    93  	defer as.Close()
    94  
    95  	ourl, err := url.Parse(as.URL)
    96  	require.NoError(t, err)
    97  	ourl.Path = "/jwt/v1/operator"
    98  
    99  	_, _, err = ExecuteCmd(createInitCmd(), "--url", ourl.String(), "-n", "*")
   100  	require.NoError(t, err)
   101  
   102  	name := GetLastRandomName()
   103  
   104  	ts.VerifyOperator(t, "T", true) // Operator name comes from the URL
   105  	ts.VerifyAccount(t, "T", name, true)
   106  	ts.VerifyUser(t, "T", name, name, true)
   107  }
   108  
   109  func Test_InitWellKnown(t *testing.T) {
   110  	ts := NewTestStore(t, "X")
   111  	defer ts.Done(t)
   112  
   113  	// run a jwt account server
   114  	as, _ := RunTestAccountServer(t)
   115  	defer as.Close()
   116  
   117  	// add an entry to well known
   118  	ourl, err := url.Parse(as.URL)
   119  	require.NoError(t, err)
   120  	ourl.Path = "/jwt/v1/operator"
   121  
   122  	var twko KnownOperator
   123  	twko.AccountServerURL = ourl.String()
   124  	twko.Name = "T"
   125  
   126  	// make it be the first
   127  	var wkops KnownOperators
   128  	wkops = append(wkops, twko)
   129  
   130  	ops, _ := GetWellKnownOperators()
   131  	wkops = append(wkops, ops...)
   132  	wellKnownOperators = wkops
   133  
   134  	_, _, err = ExecuteCmd(createInitCmd(), "--remote-operator", "T", "--name", "A")
   135  	require.NoError(t, err)
   136  
   137  	ts.VerifyOperator(t, "T", true)
   138  	ts.VerifyAccount(t, "T", "A", true)
   139  	ts.VerifyUser(t, "T", "A", "A", true)
   140  }
   141  
   142  func Test_InitWellKnown2(t *testing.T) {
   143  	as, m := RunTestAccountServer(t)
   144  	defer as.Close()
   145  
   146  	ts := NewTestStoreWithOperatorJWT(t, string(m["operator"]))
   147  	defer ts.Done(t)
   148  
   149  	// add an entry to well known
   150  	ourl, err := url.Parse(as.URL)
   151  	require.NoError(t, err)
   152  	ourl.Path = "/jwt/v1/operator"
   153  
   154  	var twko KnownOperator
   155  	twko.AccountServerURL = ourl.String()
   156  	twko.Name = "T"
   157  
   158  	// make it be the first
   159  	var wkops KnownOperators
   160  	wkops = append(wkops, twko)
   161  
   162  	ops, _ := GetWellKnownOperators()
   163  	wkops = append(wkops, ops...)
   164  	wellKnownOperators = wkops
   165  
   166  	// get the managed operator on the first
   167  	_, _, err = ExecuteCmd(createInitCmd(), "--remote-operator", "T", "--name", "A")
   168  	require.NoError(t, err)
   169  
   170  	// now add another account
   171  	_, _, err = ExecuteCmd(createInitCmd(), "--remote-operator", "T", "--name", "B")
   172  	require.NoError(t, err)
   173  
   174  	ts.VerifyOperator(t, "T", true)
   175  	ts.VerifyAccount(t, "T", "B", true)
   176  	ts.VerifyUser(t, "T", "B", "B", true)
   177  }
   178  
   179  func Test_InitWellKnownV1Operator(t *testing.T) {
   180  	ts := NewTestStore(t, "X")
   181  	defer ts.Done(t)
   182  
   183  	_, _, okp := CreateOperatorKey(t)
   184  	// run a jwt account server
   185  	as, _ := RunTestAccountServerWithOperatorKP(t, okp, TasOpts{Vers: 1})
   186  	defer as.Close()
   187  
   188  	// add an entry to well known
   189  	ourl, err := url.Parse(as.URL)
   190  	require.NoError(t, err)
   191  	ourl.Path = "/jwt/v1/operator"
   192  
   193  	var twko KnownOperator
   194  	twko.AccountServerURL = ourl.String()
   195  	twko.Name = "T"
   196  
   197  	// make it be the first
   198  	var wkops KnownOperators
   199  	wkops = append(wkops, twko)
   200  
   201  	ops, _ := GetWellKnownOperators()
   202  	wkops = append(wkops, ops...)
   203  	wellKnownOperators = wkops
   204  
   205  	_, _, err = ExecuteCmd(createInitCmd(), "--remote-operator", "T", "--name", "A")
   206  	require.Error(t, err)
   207  	require.Contains(t, err.Error(), "the operator jwt (v1) is incompatible this version of nsc")
   208  }
   209  
   210  func Test_InitWellKnownInteractive(t *testing.T) {
   211  	as, m := RunTestAccountServer(t)
   212  	defer as.Close()
   213  
   214  	ts := NewTestStoreWithOperatorJWT(t, string(m["operator"]))
   215  	defer ts.Done(t)
   216  
   217  	// add an entry to well known
   218  	ourl, err := url.Parse(as.URL)
   219  	require.NoError(t, err)
   220  	ourl.Path = "/jwt/v1/operator"
   221  
   222  	var twko KnownOperator
   223  	twko.AccountServerURL = ourl.String()
   224  	twko.Name = "T"
   225  
   226  	// make it be the first
   227  	var wkops KnownOperators
   228  	wkops = append(wkops, twko)
   229  
   230  	ops, _ := GetWellKnownOperators()
   231  	wkops = append(wkops, ops...)
   232  	wellKnownOperators = wkops
   233  
   234  	_, _, err = ExecuteInteractiveCmd(createInitCmd(), []interface{}{ts.GetStoresRoot(), 0, "A"})
   235  	require.NoError(t, err)
   236  
   237  	ts.VerifyOperator(t, "T", true)
   238  	ts.VerifyAccount(t, "T", "A", true)
   239  	ts.VerifyUser(t, "T", "A", "A", true)
   240  }
   241  
   242  func Test_InitLocalInteractive(t *testing.T) {
   243  	ts := NewTestStore(t, "X")
   244  	defer ts.Done(t)
   245  
   246  	cli.LogFn = t.Log
   247  	_, _, err := ExecuteInteractiveCmd(createInitCmd(), []interface{}{ts.GetStoresRoot(), 1, "O"})
   248  	require.NoError(t, err)
   249  
   250  	ts.VerifyOperator(t, "O", false)
   251  	ts.VerifyAccount(t, "O", "O", true)
   252  	ts.VerifyUser(t, "O", "O", "O", true)
   253  	ts.VerifyAccount(t, "O", "SYS", true)
   254  	ts.VerifyUser(t, "O", "SYS", "sys", true)
   255  }
   256  
   257  func Test_InitCustomInteractive(t *testing.T) {
   258  	ts := NewTestStore(t, "X")
   259  	defer ts.Done(t)
   260  
   261  	// run a jwt account server
   262  	as, _ := RunTestAccountServer(t)
   263  	defer as.Close()
   264  
   265  	// add an entry to well known
   266  	ourl, err := url.Parse(as.URL)
   267  	require.NoError(t, err)
   268  	ourl.Path = "/jwt/v1/operator"
   269  
   270  	_, _, err = ExecuteInteractiveCmd(createInitCmd(), []interface{}{ts.GetStoresRoot(), 2, ourl.String(), "A"})
   271  	require.NoError(t, err)
   272  
   273  	ts.VerifyOperator(t, "T", true)
   274  	ts.VerifyAccount(t, "T", "A", true)
   275  	ts.VerifyUser(t, "T", "A", "A", true)
   276  }
   277  
   278  func Test_InitDuplicate(t *testing.T) {
   279  	as, m := RunTestAccountServer(t)
   280  	defer as.Close()
   281  
   282  	ts := NewTestStoreWithOperatorJWT(t, string(m["operator"]))
   283  	defer ts.Done(t)
   284  
   285  	ourl, err := url.Parse(as.URL)
   286  	require.NoError(t, err)
   287  	ourl.Path = "/jwt/v1/operator"
   288  	u := ourl.String()
   289  
   290  	// get the managed operator on the first
   291  	_, _, err = ExecuteCmd(createInitCmd(), "--url", u, "--name", "A")
   292  	require.NoError(t, err)
   293  
   294  	ts.VerifyOperator(t, "T", true)
   295  	ts.VerifyAccount(t, "T", "A", true)
   296  	ts.VerifyUser(t, "T", "A", "A", true)
   297  
   298  	// try to do it again with the same name
   299  	_, _, err = ExecuteCmd(createInitCmd(), "--url", u, "--name", "A")
   300  	require.Error(t, err)
   301  	require.Contains(t, err.Error(), "an account named \"A\" already exists")
   302  }