Skip to content

Commit 836f5ed

Browse files
committed
Add sidebar coverage test for trimmed standards annotation
The SecurityGroupPolicySummarySidebar drill-down applies the same comma-split-without-trim fix as useSecurityGroupViolations, but the existing regression test only exercised the Overview card path, leaving the sidebar's fixed line uncovered on new code (SonarCloud flagged 50% coverage on new code, below the 70% gate). Add a test that renders SecurityGroupPolicySummarySidebar directly with a policy whose matching standard is not first in its comma-separated annotation, and asserts the policy appears in the filtered list. Verified this test fails without the sidebar's .trim() fix and passes with it. Signed-off-by: Sunny Gupta <sungupta@redhat.com>
1 parent 2aa0aec commit 836f5ed

1 file changed

Lines changed: 58 additions & 1 deletion

File tree

frontend/src/routes/Governance/overview/Overview.test.tsx

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import {
1313
mockPolicy,
1414
mockPolicyNoStatus,
1515
} from '../governance.sharedMocks'
16-
import GovernanceOverview from './Overview'
16+
import GovernanceOverview, { SecurityGroupViolations } from './Overview'
17+
import { SecurityGroupPolicySummarySidebar } from './SecurityGroupPolicySummarySidebar'
1718
import userEvent from '@testing-library/user-event'
1819
import { defaultContext, PluginDataContext } from '../../../lib/PluginDataContext'
1920
import { Policy, PolicyApiVersion, PolicyKind } from '../../../resources'
@@ -214,4 +215,60 @@ describe('Overview Page', () => {
214215
expect(screen.getAllByText('NIST SP 800-53').length).toBe(1)
215216
expect(screen.getAllByText('PCI-DSS 4.0').length).toBe(1)
216217
})
218+
219+
test('SecurityGroupPolicySummarySidebar should match policies by trimmed annotation value', async () => {
220+
// Regression test for the sidebar drill-down: a policy whose standard is not
221+
// the first item in its comma-separated annotation must still match the
222+
// clicked-on violation, which is keyed by the trimmed value.
223+
const policyWithStandardLastInList: Policy = {
224+
apiVersion: PolicyApiVersion,
225+
kind: PolicyKind,
226+
metadata: {
227+
name: 'policy-standards-last-sidebar',
228+
namespace: 'test',
229+
uid: 'standards-test-uid-3',
230+
annotations: {
231+
'policy.open-cluster-management.io/standards': 'CIS OpenShift Benchmark, PCI-DSS 4.0, NIST SP 800-53',
232+
},
233+
},
234+
spec: {
235+
disabled: false,
236+
'policy-templates': [],
237+
remediationAction: 'inform',
238+
},
239+
status: {
240+
compliant: 'Compliant',
241+
},
242+
}
243+
const violation: SecurityGroupViolations = {
244+
name: 'NIST SP 800-53',
245+
compliant: 1,
246+
noncompliant: 0,
247+
pending: 0,
248+
}
249+
250+
const pluginData = {
251+
...defaultContext,
252+
loadStarted: true,
253+
loadCompleted: true,
254+
}
255+
render(
256+
<PluginDataContext.Provider value={pluginData}>
257+
<RecoilRoot
258+
initializeState={(snapshot) => {
259+
snapshot.set(policiesState, [policyWithStandardLastInList])
260+
}}
261+
>
262+
<MemoryRouter>
263+
<SecurityGroupPolicySummarySidebar violation={violation} secGroupName="standards" compliance="compliant" />
264+
</MemoryRouter>
265+
</RecoilRoot>
266+
</PluginDataContext.Provider>
267+
)
268+
269+
// Before the fix, the sidebar's filter compared the untrimmed " NIST SP 800-53"
270+
// split token against violation.name ("NIST SP 800-53") and never matched,
271+
// so the policy would not appear in this list.
272+
expect(await screen.findByText('policy-standards-last-sidebar')).toBeInTheDocument()
273+
})
217274
})

0 commit comments

Comments
 (0)