diff --git a/central/alert/datastore/internal/store/postgres/store_test.go b/central/alert/datastore/internal/store/postgres/store_test.go index 17af491be80c..8e05eed0fe6c 100644 --- a/central/alert/datastore/internal/store/postgres/store_test.go +++ b/central/alert/datastore/internal/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *AlertsStoreSuite) TestSACExists() { } func (s *AlertsStoreSuite) TestSACGet() { - objA := &storage.Alert{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/cluster/store/cluster/postgres/store_test.go b/central/cluster/store/cluster/postgres/store_test.go index cb0be204673c..7603e7633f58 100644 --- a/central/cluster/store/cluster/postgres/store_test.go +++ b/central/cluster/store/cluster/postgres/store_test.go @@ -304,26 +304,19 @@ func (s *ClustersStoreSuite) TestSACExists() { } func (s *ClustersStoreSuite) TestSACGet() { - objA := &storage.Cluster{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/compliance/datastore/internal/store/postgres/metadata/store_test.go b/central/compliance/datastore/internal/store/postgres/metadata/store_test.go index 650f626af8c6..3abf66475d71 100644 --- a/central/compliance/datastore/internal/store/postgres/metadata/store_test.go +++ b/central/compliance/datastore/internal/store/postgres/metadata/store_test.go @@ -304,26 +304,19 @@ func (s *ComplianceRunMetadataStoreSuite) TestSACExists() { } func (s *ComplianceRunMetadataStoreSuite) TestSACGet() { - objA := &storage.ComplianceRunMetadata{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetRunId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetRunId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/compliance/datastore/internal/store/postgres/results/store_test.go b/central/compliance/datastore/internal/store/postgres/results/store_test.go index 75a629c9b3e8..58c8559d71a5 100644 --- a/central/compliance/datastore/internal/store/postgres/results/store_test.go +++ b/central/compliance/datastore/internal/store/postgres/results/store_test.go @@ -304,26 +304,19 @@ func (s *ComplianceRunResultsStoreSuite) TestSACExists() { } func (s *ComplianceRunResultsStoreSuite) TestSACGet() { - objA := &storage.ComplianceRunResults{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetRunMetadata().GetRunId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetRunMetadata().GetRunId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/deployment/store/postgres/store_test.go b/central/deployment/store/postgres/store_test.go index 45d3a899626b..26b0e60417b0 100644 --- a/central/deployment/store/postgres/store_test.go +++ b/central/deployment/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *DeploymentsStoreSuite) TestSACExists() { } func (s *DeploymentsStoreSuite) TestSACGet() { - objA := &storage.Deployment{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/namespace/store/postgres/store_test.go b/central/namespace/store/postgres/store_test.go index 1cb117b59632..bc891e60f29f 100644 --- a/central/namespace/store/postgres/store_test.go +++ b/central/namespace/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *NamespacesStoreSuite) TestSACExists() { } func (s *NamespacesStoreSuite) TestSACGet() { - objA := &storage.NamespaceMetadata{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/networkbaseline/store/postgres/store_test.go b/central/networkbaseline/store/postgres/store_test.go index b11857e77508..ae4eda9d281b 100644 --- a/central/networkbaseline/store/postgres/store_test.go +++ b/central/networkbaseline/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *NetworkBaselinesStoreSuite) TestSACExists() { } func (s *NetworkBaselinesStoreSuite) TestSACGet() { - objA := &storage.NetworkBaseline{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetDeploymentId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetDeploymentId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/networkpolicies/datastore/internal/store/postgres/store_test.go b/central/networkpolicies/datastore/internal/store/postgres/store_test.go index b141d4d412c7..a400021d985e 100644 --- a/central/networkpolicies/datastore/internal/store/postgres/store_test.go +++ b/central/networkpolicies/datastore/internal/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *NetworkpoliciesStoreSuite) TestSACExists() { } func (s *NetworkpoliciesStoreSuite) TestSACGet() { - objA := &storage.NetworkPolicy{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/pod/store/postgres/store_test.go b/central/pod/store/postgres/store_test.go index f5c541a606ef..7d43f74ca4a7 100644 --- a/central/pod/store/postgres/store_test.go +++ b/central/pod/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *PodsStoreSuite) TestSACExists() { } func (s *PodsStoreSuite) TestSACGet() { - objA := &storage.Pod{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/processbaseline/store/postgres/store_test.go b/central/processbaseline/store/postgres/store_test.go index bb02c3752515..b8efca660625 100644 --- a/central/processbaseline/store/postgres/store_test.go +++ b/central/processbaseline/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *ProcessBaselinesStoreSuite) TestSACExists() { } func (s *ProcessBaselinesStoreSuite) TestSACGet() { - objA := &storage.ProcessBaseline{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/processbaselineresults/datastore/internal/store/postgres/store_test.go b/central/processbaselineresults/datastore/internal/store/postgres/store_test.go index f5771084faae..95cb64860df5 100644 --- a/central/processbaselineresults/datastore/internal/store/postgres/store_test.go +++ b/central/processbaselineresults/datastore/internal/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *ProcessBaselineResultsStoreSuite) TestSACExists() { } func (s *ProcessBaselineResultsStoreSuite) TestSACGet() { - objA := &storage.ProcessBaselineResults{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetDeploymentId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetDeploymentId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/processindicator/store/postgres/store_test.go b/central/processindicator/store/postgres/store_test.go index 622d17b30df6..3a05d7f8d3e0 100644 --- a/central/processindicator/store/postgres/store_test.go +++ b/central/processindicator/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *ProcessIndicatorsStoreSuite) TestSACExists() { } func (s *ProcessIndicatorsStoreSuite) TestSACGet() { - objA := &storage.ProcessIndicator{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/rbac/k8srole/internal/store/postgres/store_test.go b/central/rbac/k8srole/internal/store/postgres/store_test.go index 1d24cf96b27d..427fff05b440 100644 --- a/central/rbac/k8srole/internal/store/postgres/store_test.go +++ b/central/rbac/k8srole/internal/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *K8sRolesStoreSuite) TestSACExists() { } func (s *K8sRolesStoreSuite) TestSACGet() { - objA := &storage.K8SRole{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/rbac/k8srolebinding/internal/store/postgres/store_test.go b/central/rbac/k8srolebinding/internal/store/postgres/store_test.go index fd721e9c8355..4c5be806e320 100644 --- a/central/rbac/k8srolebinding/internal/store/postgres/store_test.go +++ b/central/rbac/k8srolebinding/internal/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *RoleBindingsStoreSuite) TestSACExists() { } func (s *RoleBindingsStoreSuite) TestSACGet() { - objA := &storage.K8SRoleBinding{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/risk/datastore/internal/store/postgres/store_test.go b/central/risk/datastore/internal/store/postgres/store_test.go index 54f306a58245..08c148790cb9 100644 --- a/central/risk/datastore/internal/store/postgres/store_test.go +++ b/central/risk/datastore/internal/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *RisksStoreSuite) TestSACExists() { } func (s *RisksStoreSuite) TestSACGet() { - objA := &storage.Risk{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/secret/internal/store/postgres/store_test.go b/central/secret/internal/store/postgres/store_test.go index 3e47730d4119..ca379c12d7d5 100644 --- a/central/secret/internal/store/postgres/store_test.go +++ b/central/secret/internal/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *SecretsStoreSuite) TestSACExists() { } func (s *SecretsStoreSuite) TestSACGet() { - objA := &storage.Secret{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/central/serviceaccount/internal/store/postgres/store_test.go b/central/serviceaccount/internal/store/postgres/store_test.go index 68618e64116f..2c189f2f00e2 100644 --- a/central/serviceaccount/internal/store/postgres/store_test.go +++ b/central/serviceaccount/internal/store/postgres/store_test.go @@ -305,26 +305,19 @@ func (s *ServiceAccountsStoreSuite) TestSACExists() { } func (s *ServiceAccountsStoreSuite) TestSACGet() { - objA := &storage.ServiceAccount{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], objA.GetId()) + actual, exists, err := s.store.Get(testCase.context, objA.GetId()) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual) diff --git a/tools/generate-helpers/pg-table-bindings/store_test.go.tpl b/tools/generate-helpers/pg-table-bindings/store_test.go.tpl index 61be573e1490..13a1c8bfdf09 100644 --- a/tools/generate-helpers/pg-table-bindings/store_test.go.tpl +++ b/tools/generate-helpers/pg-table-bindings/store_test.go.tpl @@ -352,26 +352,19 @@ func (s *{{$namePrefix}}StoreSuite) TestSACExists() { } func (s *{{$namePrefix}}StoreSuite) TestSACGet() { - objA := &{{.Type}}{} - s.NoError(testutils.FullInit(objA, testutils.UniqueInitializer(), testutils.JSONFieldsFilter)) - - withAllAccessCtx := sac.WithAllAccess(context.Background()) + objA, _, testCases := s.getTestData(storage.Access_READ_ACCESS) s.Require().NoError(s.store.Upsert(withAllAccessCtx, objA)) - ctxs := getSACContexts(objA, storage.Access_READ_ACCESS) - for name, expected := range map[string]bool{ - withAllAccess: true, - withNoAccess: false, - withNoAccessToCluster: false, - withAccessToDifferentNs: false, - withAccess: true, - withAccessToCluster: true, - } { + for name, testCase := range testCases { s.T().Run(fmt.Sprintf("with %s", name), func(t *testing.T) { - actual, exists, err := s.store.Get(ctxs[name], {{ range $field := .Schema.PrimaryKeys }}{{$field.Getter "objA"}}, {{end}}) + actual, exists, err := s.store.Get(testCase.context, {{ range $field := .Schema.PrimaryKeys }}{{$field.Getter "objA"}}, {{end}}) assert.NoError(t, err) - assert.Equal(t, expected, exists) - if expected == true { + + // Assumption from the test case structure: objA is always in the visible list + // in the first position. + expectedFound := len(testCase.expectedObjects) > 0 + assert.Equal(t, expectedFound, exists) + if expectedFound { assert.Equal(t, objA, actual) } else { assert.Nil(t, actual)