package view import ( "context" "testing" "time" "edge-infra.dev/pkg/edge/api/fake" "edge-infra.dev/pkg/edge/api/graph/model" "edge-infra.dev/pkg/edge/edgeadmin/commands/operatorintervention/view/mocks" "edge-infra.dev/pkg/edge/edgecli" "edge-infra.dev/pkg/edge/edgecli/flagutil" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // Generate a mock config with dummy login details, fake token with far away expiry // and the fake API server URL. func testConfig(endpointURL string) *edgecli.Config { return &edgecli.Config{ CurrentBannerContext: "fakeBanner", BannerContexts: map[string]*edgecli.BannerContext{ "fakeBanner": { BannerEdgeID: "test-banner-edge-id-0", BannerName: "test-banner-0", TenantOrg: "test-org-0", Username: "test-user-0", TokenTime: time.Now().Add(time.Hour * 24).Format(time.RFC3339), Token: "fakeToken", Endpoint: endpointURL, }, }, } } func TestSummary(t *testing.T) { t.Parallel() tests := map[string]struct { roles []string }{ "Valid Without Role": {}, "Valid With Role": { roles: []string{"role1"}, }, "Valid With Multiple Roles": { roles: []string{"role1", "role3"}, }, "Invalid Role": { roles: []string{"role-that-does-not-exist"}, }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() server := fake.GetMockAPIServer(mocks.Summary) serverURL := server.URL + "/api/v2" defer server.Close() cmd := NewSummary(testConfig(serverURL)) cmd.Command() for _, role := range tc.roles { require.NoError(t, flagutil.SetFlag(cmd.Rags, flagutil.RoleFlag, role)) } err := cmd.Command().Exec(context.Background(), nil) assert.NoError(t, err) }) } } func TestAssembleRoleConfigurations(t *testing.T) { t.Parallel() defaultRules := []model.Rule{ { Privilege: &model.Privilege{Name: "priv1"}, Commands: []*model.Command{ {Name: "command1"}, {Name: "command2"}, }, }, { Privilege: &model.Privilege{Name: "priv2"}, Commands: []*model.Command{ {Name: "command3"}, }, }, { Privilege: &model.Privilege{Name: "priv3"}, Commands: []*model.Command{ {Name: "command4"}, }, }, { Privilege: &model.Privilege{Name: "priv4"}, Commands: []*model.Command{ {Name: "command5"}, {Name: "command6"}, }, }, { Privilege: &model.Privilege{Name: "priv-that-does-not-have-rules"}, }, } tests := map[string]struct { roleMappings []model.OiRoleMapping rules []model.Rule expected []RoleConfiguration }{ "One Role Mapping": { roleMappings: []model.OiRoleMapping{ { Role: "role1", Privileges: []*model.Privilege{ {Name: "priv1"}, {Name: "priv2"}, }, }, }, rules: defaultRules, expected: []RoleConfiguration{ { Role: "role1", Privileges: []model.Rule{ { Privilege: &model.Privilege{Name: "priv1"}, Commands: []*model.Command{ {Name: "command1"}, {Name: "command2"}, }, }, { Privilege: &model.Privilege{Name: "priv2"}, Commands: []*model.Command{ {Name: "command3"}, }, }, }, }, }, }, "Multiple Role Mappings": { roleMappings: []model.OiRoleMapping{ { Role: "role1", Privileges: []*model.Privilege{ {Name: "priv1"}, }, }, { Role: "role2", Privileges: []*model.Privilege{ {Name: "priv3"}, }, }, }, rules: defaultRules, expected: []RoleConfiguration{ { Role: "role1", Privileges: []model.Rule{ { Privilege: &model.Privilege{Name: "priv1"}, Commands: []*model.Command{ {Name: "command1"}, {Name: "command2"}, }, }, }, }, { Role: "role2", Privileges: []model.Rule{ { Privilege: &model.Privilege{Name: "priv3"}, Commands: []*model.Command{ {Name: "command4"}, }, }, }, }, }, }, "Role Mapping With No Rules": { roleMappings: []model.OiRoleMapping{ { Role: "role-oops", Privileges: []*model.Privilege{ {Name: "priv-that-does-not-have-rules"}, }, }, }, rules: defaultRules, expected: []RoleConfiguration{ { Role: "role-oops", Privileges: []model.Rule{ { Privilege: &model.Privilege{Name: "priv-that-does-not-have-rules"}, }, }, }, }, }, } for name, tc := range tests { tc := tc t.Run(name, func(t *testing.T) { t.Parallel() actual := assembleRoleConfigurations(tc.roleMappings, tc.rules) assert.Equal(t, tc.expected, actual) }) } }