...

Source file src/edge-infra.dev/pkg/edge/edgeadmin/commands/operatorintervention/view/summary_test.go

Documentation: edge-infra.dev/pkg/edge/edgeadmin/commands/operatorintervention/view

     1  package view
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"edge-infra.dev/pkg/edge/api/fake"
     9  	"edge-infra.dev/pkg/edge/api/graph/model"
    10  	"edge-infra.dev/pkg/edge/edgeadmin/commands/operatorintervention/view/mocks"
    11  	"edge-infra.dev/pkg/edge/edgecli"
    12  	"edge-infra.dev/pkg/edge/edgecli/flagutil"
    13  
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  // Generate a mock config with dummy login details, fake token with far away expiry
    19  // and the fake API server URL.
    20  func testConfig(endpointURL string) *edgecli.Config {
    21  	return &edgecli.Config{
    22  		CurrentBannerContext: "fakeBanner",
    23  		BannerContexts: map[string]*edgecli.BannerContext{
    24  			"fakeBanner": {
    25  				BannerEdgeID: "test-banner-edge-id-0",
    26  				BannerName:   "test-banner-0",
    27  				TenantOrg:    "test-org-0",
    28  				Username:     "test-user-0",
    29  				TokenTime:    time.Now().Add(time.Hour * 24).Format(time.RFC3339),
    30  				Token:        "fakeToken",
    31  				Endpoint:     endpointURL,
    32  			},
    33  		},
    34  	}
    35  }
    36  
    37  func TestSummary(t *testing.T) {
    38  	t.Parallel()
    39  
    40  	tests := map[string]struct {
    41  		roles []string
    42  	}{
    43  		"Valid Without Role": {},
    44  		"Valid With Role": {
    45  			roles: []string{"role1"},
    46  		},
    47  		"Valid With Multiple Roles": {
    48  			roles: []string{"role1", "role3"},
    49  		},
    50  		"Invalid Role": {
    51  			roles: []string{"role-that-does-not-exist"},
    52  		},
    53  	}
    54  
    55  	for name, tc := range tests {
    56  		tc := tc
    57  		t.Run(name, func(t *testing.T) {
    58  			t.Parallel()
    59  
    60  			server := fake.GetMockAPIServer(mocks.Summary)
    61  			serverURL := server.URL + "/api/v2"
    62  			defer server.Close()
    63  
    64  			cmd := NewSummary(testConfig(serverURL))
    65  			cmd.Command()
    66  
    67  			for _, role := range tc.roles {
    68  				require.NoError(t, flagutil.SetFlag(cmd.Rags, flagutil.RoleFlag, role))
    69  			}
    70  
    71  			err := cmd.Command().Exec(context.Background(), nil)
    72  			assert.NoError(t, err)
    73  		})
    74  	}
    75  }
    76  
    77  func TestAssembleRoleConfigurations(t *testing.T) {
    78  	t.Parallel()
    79  
    80  	defaultRules := []model.Rule{
    81  		{
    82  			Privilege: &model.Privilege{Name: "priv1"},
    83  			Commands: []*model.Command{
    84  				{Name: "command1"},
    85  				{Name: "command2"},
    86  			},
    87  		},
    88  		{
    89  			Privilege: &model.Privilege{Name: "priv2"},
    90  			Commands: []*model.Command{
    91  				{Name: "command3"},
    92  			},
    93  		},
    94  		{
    95  			Privilege: &model.Privilege{Name: "priv3"},
    96  			Commands: []*model.Command{
    97  				{Name: "command4"},
    98  			},
    99  		},
   100  		{
   101  			Privilege: &model.Privilege{Name: "priv4"},
   102  			Commands: []*model.Command{
   103  				{Name: "command5"},
   104  				{Name: "command6"},
   105  			},
   106  		},
   107  		{
   108  			Privilege: &model.Privilege{Name: "priv-that-does-not-have-rules"},
   109  		},
   110  	}
   111  
   112  	tests := map[string]struct {
   113  		roleMappings []model.OiRoleMapping
   114  		rules        []model.Rule
   115  		expected     []RoleConfiguration
   116  	}{
   117  		"One Role Mapping": {
   118  			roleMappings: []model.OiRoleMapping{
   119  				{
   120  					Role: "role1",
   121  					Privileges: []*model.Privilege{
   122  						{Name: "priv1"},
   123  						{Name: "priv2"},
   124  					},
   125  				},
   126  			},
   127  			rules: defaultRules,
   128  			expected: []RoleConfiguration{
   129  				{
   130  					Role: "role1",
   131  					Privileges: []model.Rule{
   132  						{
   133  							Privilege: &model.Privilege{Name: "priv1"},
   134  							Commands: []*model.Command{
   135  								{Name: "command1"},
   136  								{Name: "command2"},
   137  							},
   138  						},
   139  						{
   140  							Privilege: &model.Privilege{Name: "priv2"},
   141  							Commands: []*model.Command{
   142  								{Name: "command3"},
   143  							},
   144  						},
   145  					},
   146  				},
   147  			},
   148  		},
   149  		"Multiple Role Mappings": {
   150  			roleMappings: []model.OiRoleMapping{
   151  				{
   152  					Role: "role1",
   153  					Privileges: []*model.Privilege{
   154  						{Name: "priv1"},
   155  					},
   156  				},
   157  				{
   158  					Role: "role2",
   159  					Privileges: []*model.Privilege{
   160  						{Name: "priv3"},
   161  					},
   162  				},
   163  			},
   164  			rules: defaultRules,
   165  			expected: []RoleConfiguration{
   166  				{
   167  					Role: "role1",
   168  					Privileges: []model.Rule{
   169  						{
   170  							Privilege: &model.Privilege{Name: "priv1"},
   171  							Commands: []*model.Command{
   172  								{Name: "command1"},
   173  								{Name: "command2"},
   174  							},
   175  						},
   176  					},
   177  				},
   178  				{
   179  					Role: "role2",
   180  					Privileges: []model.Rule{
   181  						{
   182  							Privilege: &model.Privilege{Name: "priv3"},
   183  							Commands: []*model.Command{
   184  								{Name: "command4"},
   185  							},
   186  						},
   187  					},
   188  				},
   189  			},
   190  		},
   191  		"Role Mapping With No Rules": {
   192  			roleMappings: []model.OiRoleMapping{
   193  				{
   194  					Role: "role-oops",
   195  					Privileges: []*model.Privilege{
   196  						{Name: "priv-that-does-not-have-rules"},
   197  					},
   198  				},
   199  			},
   200  			rules: defaultRules,
   201  			expected: []RoleConfiguration{
   202  				{
   203  					Role: "role-oops",
   204  					Privileges: []model.Rule{
   205  						{
   206  							Privilege: &model.Privilege{Name: "priv-that-does-not-have-rules"},
   207  						},
   208  					},
   209  				},
   210  			},
   211  		},
   212  	}
   213  
   214  	for name, tc := range tests {
   215  		tc := tc
   216  		t.Run(name, func(t *testing.T) {
   217  			t.Parallel()
   218  
   219  			actual := assembleRoleConfigurations(tc.roleMappings, tc.rules)
   220  			assert.Equal(t, tc.expected, actual)
   221  		})
   222  	}
   223  }
   224  

View as plain text