github.com/kbehouse/nsc@v0.0.6/cmd/deleteexport_test.go (about) 1 /* 2 * Copyright 2018 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/nats-io/jwt/v2" 22 "github.com/stretchr/testify/require" 23 ) 24 25 func Test_DeleteExport(t *testing.T) { 26 ts := NewTestStore(t, "add_server") 27 defer ts.Done(t) 28 29 ts.AddExport(t, "A", jwt.Stream, "foo", true) 30 ts.AddExport(t, "A", jwt.Stream, "baz", true) 31 ts.AddExport(t, "B", jwt.Service, "bar", true) 32 33 tests := CmdTests{ 34 {createDeleteExportCmd(), []string{"delete", "export", "--account", "A"}, nil, []string{"subject is required"}, true}, 35 {createDeleteExportCmd(), []string{"delete", "export", "--account", "A", "--subject", "a"}, nil, []string{"no export matching \"a\" found"}, true}, 36 {createDeleteExportCmd(), []string{"delete", "export", "--account", "A", "--subject", "foo"}, nil, []string{"deleted stream export \"foo\""}, false}, 37 {createDeleteExportCmd(), []string{"delete", "export", "--account", "B"}, nil, []string{"deleted service export \"bar\""}, false}, 38 } 39 40 tests.Run(t, "root", "delete") 41 } 42 43 func Test_DeleteExportAccountRequired(t *testing.T) { 44 ts := NewTestStore(t, "add_server") 45 defer ts.Done(t) 46 47 ts.AddExport(t, "A", jwt.Stream, "foo", true) 48 ts.AddExport(t, "B", jwt.Service, "bar", true) 49 GetConfig().SetAccount("") 50 _, _, err := ExecuteCmd(createDeleteExportCmd(), "--subject", "foo") 51 require.Error(t, err) 52 require.Contains(t, err.Error(), "account is required") 53 } 54 55 func Test_DeleteExportInteractiveManagedStore(t *testing.T) { 56 as, m := RunTestAccountServer(t) 57 defer as.Close() 58 59 ts := NewTestStoreWithOperatorJWT(t, string(m["operator"])) 60 defer ts.Done(t) 61 62 ts.AddExport(t, "A", jwt.Stream, "foo", true) 63 ts.AddExport(t, "A", jwt.Stream, "baz", true) 64 ts.AddAccount(t, "B") 65 66 cmd := createDeleteExportCmd() 67 HoistRootFlags(cmd) 68 69 input := []interface{}{0, 0, 0, ts.GetAccountKeyPath(t, "A")} 70 _, _, err := ExecuteInteractiveCmd(cmd, input) 71 require.NoError(t, err) 72 73 ac, err := ts.Store.ReadAccountClaim("A") 74 require.NoError(t, err) 75 require.Len(t, ac.Exports, 1) 76 }