-
Notifications
You must be signed in to change notification settings - Fork 178
Add Cypress tests for admin user access control validation bypass #1234
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -27,7 +27,7 @@ describe("Set publisher access control and visibility by roles", () => { | |
| cy.loginToPublisher(publisher, password); | ||
| }) | ||
|
|
||
| it.only("Set role based API Store visibility and access control for the api", () => { | ||
| it("Set role based API Store visibility and access control for the api", () => { | ||
| const role = 'internal/everyone'; | ||
| Utils.addAPI({ name: apiName, version: apiVersion }).then((apiId) => { | ||
| cy.visit(`/publisher/apis/${apiId}/overview`); | ||
|
|
@@ -57,4 +57,93 @@ describe("Set publisher access control and visibility by roles", () => { | |
| Utils.deleteAPI(apiId); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Admin user access control validation", () => { | ||
| const { carbonUsername, carbonPassword } = Utils.getUserInfo(); | ||
| const adminApiName = Utils.generateName(); | ||
| const adminApiVersion = '1.0.0'; | ||
|
|
||
| before(function () { | ||
| // Login as admin user who has apim:admin permission | ||
| cy.loginToPublisher(carbonUsername, carbonPassword); | ||
| }); | ||
|
|
||
| it("Admin user should bypass user role validation when setting access control", () => { | ||
| const systemRole = 'Internal/system'; // This is a system role, not a user role | ||
|
|
||
| Utils.addAPI({ name: adminApiName, version: adminApiVersion }).then((apiId) => { | ||
| cy.visit(`/publisher/apis/${apiId}/overview`); | ||
| cy.get('#itest-api-details-portal-config-acc').click(); | ||
| cy.get('#left-menu-itemDesignConfigurations').click(); | ||
|
|
||
| // Select the restricted by role option for access control | ||
| cy.get('#accessControl-selector').click(); | ||
| cy.get('#access-control-restricted-by-roles').click(); | ||
|
|
||
| // Add a system role that would normally trigger user role validation error for non-admin users | ||
| cy.get('[data-testid="access-control-select-role"]').type(`${systemRole}{enter}`); | ||
|
|
||
| // Verify no validation error appears for admin users | ||
| cy.get('[data-testid="access-control-select-role"]').should('not.contain', 'At least one role must be associated with the API creator'); | ||
| cy.get('[data-testid="access-control-select-role"]').should('not.have.class', 'Mui-error'); | ||
|
|
||
| // Verify save button is enabled (not disabled due to validation errors) | ||
| cy.get('#design-config-save-btn').should('not.be.disabled'); | ||
|
|
||
| // Save the configuration successfully | ||
| cy.get('#design-config-save-btn').scrollIntoView().click(); | ||
|
|
||
| // Verify the configuration was saved without errors | ||
| cy.get('div[data-testid="access-control-select-role"] span').contains(systemRole).should('exist'); | ||
|
|
||
| // Test is done. Now delete the api | ||
| Utils.deleteAPI(apiId); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe("Non-admin user access control validation", () => { | ||
| const { publisher, password } = Utils.getUserInfo(); | ||
| const nonAdminApiName = Utils.generateName(); | ||
| const nonAdminApiVersion = '1.0.0'; | ||
|
|
||
| before(function () { | ||
| // Login as non-admin user (regular publisher) | ||
| cy.loginToPublisher(publisher, password); | ||
| }); | ||
|
|
||
| it("Non-admin user should still see user role validation when configuring system-only roles", () => { | ||
| const systemRole = 'internal/subscriber'; // This is a system role, not a user role | ||
|
|
||
| Utils.addAPI({ name: nonAdminApiName, version: nonAdminApiVersion }).then((apiId) => { | ||
| cy.visit(`/publisher/apis/${apiId}/overview`); | ||
| cy.get('#itest-api-details-portal-config-acc').click(); | ||
| cy.get('#left-menu-itemDesignConfigurations').click(); | ||
|
|
||
| // Select the restricted by role option for access control | ||
| cy.get('#accessControl-selector').click(); | ||
| cy.get('#access-control-restricted-by-roles').click(); | ||
|
|
||
| // Add a system role that should trigger user role validation error for non-admin users | ||
| cy.get('[data-testid="access-control-select-role"]').type(`${systemRole}{enter}`); | ||
|
|
||
| // Wait for validation to complete | ||
| cy.wait(1000); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Replace arbitrary wait with explicit assertion. Using 🔎 Replace the arbitrary wait with a condition-based wait: // Add a system role that should trigger user role validation error for non-admin users
cy.get('[data-testid="access-control-select-role"]').type(`${systemRole}{enter}`);
-// Wait for validation to complete
-cy.wait(1000);
+// Wait for validation by checking for either error state or stable UI
+cy.get('[data-testid="access-control-select-role"]').should('be.visible');
// Verify validation error appears for non-admin usersOr wait for a specific validation element to appear: // Wait for error message to appear
cy.get('[data-testid="access-control-select-role"]')
.parents('form')
.find('.MuiFormHelperText-root.Mui-error', { timeout: 5000 })
.should('exist');🤖 Prompt for AI Agents |
||
|
|
||
| // Verify validation error appears for non-admin users | ||
| // Note: The exact error message and selectors may need adjustment based on actual implementation | ||
| cy.get('[data-testid="access-control-select-role"]').then(($element) => { | ||
| // Check if error state is present (either through error class or error message) | ||
| const hasErrorClass = $element.hasClass('Mui-error') || $element.find('.Mui-error').length > 0; | ||
| const hasErrorMessage = $element.text().includes('At least one role must be associated with the API creator'); | ||
|
|
||
| // For non-admin users, either error styling or validation message should be present | ||
| expect(hasErrorClass || hasErrorMessage).to.be.true; | ||
| }); | ||
|
Comment on lines
+135
to
+142
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Refactor to use native Cypress assertions for retry-ability. The current approach using 🔎 Refactor to use Cypress should() assertions:-// Verify validation error appears for non-admin users
-// Note: The exact error message and selectors may need adjustment based on actual implementation
-cy.get('[data-testid="access-control-select-role"]').then(($element) => {
- // Check if error state is present (either through error class or error message)
- const hasErrorClass = $element.hasClass('Mui-error') || $element.find('.Mui-error').length > 0;
- const hasErrorMessage = $element.text().includes('At least one role must be associated with the API creator');
-
- // For non-admin users, either error styling or validation message should be present
- expect(hasErrorClass || hasErrorMessage).to.be.true;
-});
+// Verify validation error appears for non-admin users
+// Check for error class on the input or its parent
+cy.get('[data-testid="access-control-select-role"]')
+ .parents('.MuiFormControl-root')
+ .should('satisfy', ($el) => {
+ return $el.find('.Mui-error').length > 0 ||
+ $el.text().includes('At least one role must be associated with the API creator');
+ });Or more explicitly: // Check for error helper text
cy.get('[data-testid="access-control-select-role"]')
.parents('.MuiFormControl-root')
.find('.MuiFormHelperText-root')
.should('satisfy', ($helperText) => {
return $helperText.hasClass('Mui-error') ||
$helperText.text().includes('At least one role must be associated');
});
🤖 Prompt for AI Agents |
||
|
|
||
| // Test is done. Now delete the api | ||
| Utils.deleteAPI(apiId); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: wso2/apim-apps
Length of output: 288
🏁 Script executed:
Repository: wso2/apim-apps
Length of output: 85
🏁 Script executed:
Repository: wso2/apim-apps
Length of output: 85
🏁 Script executed:
Repository: wso2/apim-apps
Length of output: 175
🏁 Script executed:
Repository: wso2/apim-apps
Length of output: 11374
🏁 Script executed:
Repository: wso2/apim-apps
Length of output: 22937
Fix error assertion selectors to target Material-UI FormHelperText component.
The assertions checking for error messages and error classes won't work because Material-UI renders validation errors in the FormHelperText component, which is a sibling to the input element, not a child of it.
Replace:
With selectors that target the FormHelperText element:
Or check the parent FormControl element for the error state.
🤖 Prompt for AI Agents