Skip to content

Commit

Permalink
Naji/backport 13287 (#13520)
Browse files Browse the repository at this point in the history
* fix moderated sessions setter and getter

* fix godoc

* add tests

* address pr comments

* simplify test
  • Loading branch information
NajiObeid authored Jun 15, 2022
1 parent 3997af9 commit a8e8b91
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 6 deletions.
12 changes: 6 additions & 6 deletions api/types/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,24 +279,24 @@ func (c *LicenseV3) SetSupportsDatabaseAccess(value Bool) {
c.Spec.SupportsDatabaseAccess = value
}

// GetSupportsDesktopAccess returns database access support flag
// GetSupportsDesktopAccess returns desktop access support flag
func (c *LicenseV3) GetSupportsDesktopAccess() Bool {
return c.Spec.SupportsDesktopAccess
}

// SetSupportsDesktopAccess sets database access support flag
// SetSupportsDesktopAccess sets desktop access support flag
func (c *LicenseV3) SetSupportsDesktopAccess(value Bool) {
c.Spec.SupportsDesktopAccess = value
}

// GetSupportsModeratedSessions returns database access support flag
// GetSupportsModeratedSessions returns moderated sessions support flag
func (c *LicenseV3) GetSupportsModeratedSessions() Bool {
return c.Spec.SupportsDesktopAccess
return c.Spec.SupportsModeratedSessions
}

// SetSupportsModeratedSessions sets database access support flag
// SetSupportsModeratedSessions sets moderated sessions support flag
func (c *LicenseV3) SetSupportsModeratedSessions(value Bool) {
c.Spec.SupportsDesktopAccess = value
c.Spec.SupportsModeratedSessions = value
}

// String represents a human readable version of license enabled features
Expand Down
130 changes: 130 additions & 0 deletions api/types/license_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
/*
Copyright 2022 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package types

import (
"reflect"
"runtime"
"testing"

"github.com/stretchr/testify/require"
)

func TestLicenseSettersAndGetters(t *testing.T) {
// All getters inspected in this test.
allFields := []func(License) Bool{
License.GetReportsUsage,
License.GetCloud,
License.GetSupportsKubernetes,
License.GetSupportsApplicationAccess,
License.GetSupportsDatabaseAccess,
License.GetSupportsDesktopAccess,
License.GetSupportsModeratedSessions,
}

// unsetFields returns a list of license fields getters minus
// the one passed as an argument.
unsetFields := func(getter func(License) Bool) []func(License) Bool {
var unsetFields []func(License) Bool
for _, fieldGetter := range allFields {
if fnName(fieldGetter) != fnName(getter) {
unsetFields = append(unsetFields, fieldGetter)
}
}
return unsetFields
}

tt := []struct {
name string
setter func(License, Bool)
getter func(License) Bool
unsetFields [](func(License) Bool)
}{
{
name: "Set ReportsUsage",
setter: License.SetReportsUsage,
getter: License.GetReportsUsage,
unsetFields: unsetFields(License.GetReportsUsage),
},
{
name: "Set Cloud",
setter: License.SetCloud,
getter: License.GetCloud,
unsetFields: unsetFields(License.GetCloud),
},
{
name: "Set Kubernetes Support",
setter: License.SetSupportsKubernetes,
getter: License.GetSupportsKubernetes,
unsetFields: unsetFields(License.GetSupportsKubernetes),
},
{
name: "Set Application Access Support",
setter: License.SetSupportsApplicationAccess,
getter: License.GetSupportsApplicationAccess,
unsetFields: unsetFields(License.GetSupportsApplicationAccess),
},
{
name: "Set Database Access Support",
setter: License.SetSupportsDatabaseAccess,
getter: License.GetSupportsDatabaseAccess,
unsetFields: unsetFields(License.GetSupportsDatabaseAccess),
},
{
name: "Set Desktop Access Support",
setter: License.SetSupportsDesktopAccess,
getter: License.GetSupportsDesktopAccess,
unsetFields: unsetFields(License.GetSupportsDesktopAccess),
},
{
name: "Set Moderated Sessions Support",
setter: License.SetSupportsModeratedSessions,
getter: License.GetSupportsModeratedSessions,
unsetFields: unsetFields(License.GetSupportsModeratedSessions),
},
}

for _, tc := range tt {
t.Run(tc.name, func(t *testing.T) {
license := &LicenseV3{
Spec: LicenseSpecV3{
SupportsApplicationAccess: NewBoolP(false),
},
}
tc.setter(license, true)
require.True(t, bool(tc.getter(license)))
for _, unset := range tc.unsetFields {
require.False(t, bool(unset(license)))
}
})
}

// Manually test Application Access.
// If unset application access is set to true by default.
license := &LicenseV3{}
require.True(t, bool(license.GetSupportsApplicationAccess()))
require.False(t, bool(license.GetReportsUsage()))
require.False(t, bool(license.GetCloud()))
require.False(t, bool(license.GetSupportsKubernetes()))
require.False(t, bool(license.GetSupportsDatabaseAccess()))
require.False(t, bool(license.GetSupportsDesktopAccess()))
require.False(t, bool(license.GetSupportsModeratedSessions()))
}

func fnName(i interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(i).Pointer()).Name()
}

0 comments on commit a8e8b91

Please sign in to comment.