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

     1  /*
     2   * Copyright 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  	"testing"
    20  
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  func Test_DeleteMapping(t *testing.T) {
    25  	ts := NewTestStore(t, "O")
    26  	defer ts.Done(t)
    27  
    28  	ts.AddAccount(t, "A")
    29  	_, _, err := ExecuteCmd(createAddMappingCmd(), "--account", "A", "--from", "from1", "--to", "to1", "--weight", "50")
    30  	require.NoError(t, err)
    31  	_, _, err = ExecuteCmd(createAddMappingCmd(), "--account", "A", "--from", "from1", "--to", "to2", "--weight", "50")
    32  	require.NoError(t, err)
    33  	_, _, err = ExecuteCmd(createAddMappingCmd(), "--account", "A", "--from", "from2", "--to", "to1", "--weight", "50")
    34  	require.NoError(t, err)
    35  	_, _, err = ExecuteCmd(createAddMappingCmd(), "--account", "A", "--from", "from2", "--to", "to2", "--weight", "50")
    36  	require.NoError(t, err)
    37  
    38  	ac, err := ts.Store.ReadAccountClaim("A")
    39  	require.NoError(t, err)
    40  	require.NotNil(t, ac)
    41  	require.Len(t, ac.Mappings, 2)
    42  	require.Len(t, ac.Mappings["from1"], 2)
    43  	require.Len(t, ac.Mappings["from2"], 2)
    44  
    45  	// remove all mappings for from1
    46  	_, _, err = ExecuteCmd(createDeleteMappingCmd(), "--account", "A", "--from", "from1")
    47  	require.NoError(t, err)
    48  	ac, err = ts.Store.ReadAccountClaim("A")
    49  	require.NoError(t, err)
    50  	require.NotNil(t, ac)
    51  	require.Len(t, ac.Mappings, 1)
    52  	require.Len(t, ac.Mappings["from2"], 2)
    53  
    54  	// remove particular mapping to1 from from2
    55  	_, _, err = ExecuteCmd(createDeleteMappingCmd(), "--account", "A", "--from", "from2", "--to", "to1")
    56  	require.NoError(t, err)
    57  	ac, err = ts.Store.ReadAccountClaim("A")
    58  	require.NoError(t, err)
    59  	require.NotNil(t, ac)
    60  	require.Len(t, ac.Mappings, 1)
    61  	require.Len(t, ac.Mappings["from2"], 1)
    62  
    63  	// remove non existing mapping to3 from from2
    64  	_, _, err = ExecuteCmd(createDeleteMappingCmd(), "--account", "A", "--from", "from2", "--to", "to3")
    65  	require.NoError(t, err)
    66  	ac, err = ts.Store.ReadAccountClaim("A")
    67  	require.NoError(t, err)
    68  	require.NotNil(t, ac)
    69  	require.Len(t, ac.Mappings, 1)
    70  	require.Len(t, ac.Mappings["from2"], 1)
    71  
    72  	// remove particular mapping to2 from from2
    73  	_, _, err = ExecuteCmd(createDeleteMappingCmd(), "--account", "A", "--from", "from2", "--to", "to2")
    74  	require.NoError(t, err)
    75  	ac, err = ts.Store.ReadAccountClaim("A")
    76  	require.NoError(t, err)
    77  	require.NotNil(t, ac)
    78  	require.Len(t, ac.Mappings, 0)
    79  }