Skip to content

Commit

Permalink
Add e2e test for server-url validation (rancher#10937)
Browse files Browse the repository at this point in the history
* e2e to test server-url validation

* refactor po and test

* e2e test for server-url validation in setup page

---------

Co-authored-by: Mo Mesgin <[email protected]>
  • Loading branch information
momesgin and Mo Mesgin authored May 8, 2024
1 parent a198835 commit 381a15b
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 4 deletions.
17 changes: 17 additions & 0 deletions cypress/e2e/blueprints/global_settings/settings-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,20 @@ export const settings = {
new: 'true'
}
};
export const serverUrlLocalhostCases = [
'http://LOCALhosT:8005',
'http://localhost:8005',
'https://localhost:8005',
'localhost',
'http://127.0.0.1',
'https://127.0.0.1',
'127.0.0.1'
];
export const urlWithTrailingForwardSlash = 'https://test.com/';
export const httpUrl = 'http://test.com';
export const nonUrlCases = [
'test',
'https',
'test.com',
'a.test.com'
];
9 changes: 9 additions & 0 deletions cypress/e2e/po/edit/settings.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import LabeledInputPo from '@/cypress/e2e/po/components/labeled-input.po';
import AsyncButtonPo from '@/cypress/e2e/po/components/async-button.po';
import RadioGroupInputPo from '@/cypress/e2e/po/components/radio-group-input.po';
import LabeledSelectPo from '@/cypress/e2e/po/components/labeled-select.po';
import BannersPo from '@/cypress/e2e/po/components/banners.po';

export default class SettingsEditPo extends PagePo {
private static createPath(clusterId: string, setting: string) {
Expand Down Expand Up @@ -36,6 +37,14 @@ export default class SettingsEditPo extends PagePo {
selectSettings.clickOptionWithLabel(label);
}

serverUrlLocalhostWarningBanner(): BannersPo {
return new BannersPo('[data-testid="setting-serverurl-localhost-warning"]');
}

errorBannerContent(label: string): Cypress.Chainable {
return new BannersPo('[data-testid="setting-error-banner"]', this.self()).banner().contains(label);
}

useDefaultButton(): Cypress.Chainable {
return cy.getId('advanced_settings_use_default');
}
Expand Down
9 changes: 9 additions & 0 deletions cypress/e2e/po/pages/rancher-setup-configure.po.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import CheckboxInputPo from '@/cypress/e2e/po/components/checkbox-input.po';
import RadioGroupInputPo from '@/cypress/e2e/po/components/radio-group-input.po';
import AsyncButtonPo from '@/cypress/e2e/po/components/async-button.po';
import PasswordPo from '@/cypress/e2e/po/components/password.po';
import BannersPo from '@/cypress/e2e/po/components/banners.po';

export class RancherSetupConfigurePage extends PagePo {
static url = '/auth/setup'
Expand Down Expand Up @@ -31,6 +32,14 @@ export class RancherSetupConfigurePage extends PagePo {
return new LabeledInputPo(cy.getId('setup-server-url'));
}

serverUrlLocalhostWarningBanner(): BannersPo {
return new BannersPo('[data-testid="setup-serverurl-localhost-warning"]');
}

errorBannerContent(label: string): Cypress.Chainable {
return new BannersPo('[data-testid="setup-error-banner"]', this.self()).banner().contains(label);
}

termsAgreement(): CheckboxInputPo {
return new CheckboxInputPo(cy.getId('setup-agreement'));
}
Expand Down
34 changes: 33 additions & 1 deletion cypress/e2e/tests/pages/global-settings/settings-p2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import BurgerMenuPo from '@/cypress/e2e/po/side-bars/burger-side-menu.po';
import ClusterManagerCreateRke2CustomPagePo from '@/cypress/e2e/po/edit/provisioning.cattle.io.cluster/create/cluster-create-rke2-custom.po';
import AccountPagePo from '@/cypress/e2e/po/pages/account-api-keys.po';
import ClusterManagerListPagePo from '@/cypress/e2e/po/pages/cluster-manager/cluster-manager-list.po';
import { settings } from '@/cypress/e2e/blueprints/global_settings/settings-data';
import {
settings, serverUrlLocalhostCases, urlWithTrailingForwardSlash, httpUrl, nonUrlCases
} from '@/cypress/e2e/blueprints/global_settings/settings-data';

const settingsPage = new SettingsPagePo('local');
const homePage = new HomePagePo();
Expand Down Expand Up @@ -82,6 +84,36 @@ describe('Settings', { testIsolation: 'off' }, () => {
});
});

it('can validate server-url', { tags: ['@globalSettings', '@adminUser'] }, () => {
SettingsPagePo.navTo();

settingsPage.editSettingsByLabel('server-url');

const settingsEdit = settingsPage.editSettings('local', 'server-url');

settingsEdit.waitForPage();
settingsEdit.title().contains('Setting: server-url').should('be.visible');

// Check showing localhost warning banner
serverUrlLocalhostCases.forEach((url) => {
settingsEdit.settingsInput().set(url);
settingsEdit.serverUrlLocalhostWarningBanner().banner().should('exist').and('be.visible');
});
// Check showing error banner when the urls has trailing forward slash
settingsEdit.settingsInput().set(urlWithTrailingForwardSlash);
settingsEdit.errorBannerContent('Server URL should not have a trailing forward slash.').should('exist').and('be.visible');
// Check showing error banner when the url is not HTTPS
settingsEdit.settingsInput().set(httpUrl);
settingsEdit.errorBannerContent('Server URL must be https.').should('exist').and('be.visible');
// // Check showing error banner when the input value is not a url
nonUrlCases.forEach((inputValue) => {
settingsEdit.settingsInput().set(inputValue);
settingsEdit.errorBannerContent('Server URL must be an URL.').should('exist').and('be.visible');
// A non-url is also a non-https
settingsEdit.errorBannerContent('Server URL must be https.').should('exist').and('be.visible');
});
});

it('can update ui-index', { tags: ['@globalSettings', '@adminUser'] }, () => {
// Update setting
SettingsPagePo.navTo();
Expand Down
31 changes: 28 additions & 3 deletions cypress/e2e/tests/setup/rancher-setup.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { RancherSetupLoginPagePo } from '@/cypress/e2e/po/pages/rancher-setup-login.po';
import { RancherSetupConfigurePage } from '~/cypress/e2e/po/pages/rancher-setup-configure.po';
import HomePagePo from '~/cypress/e2e/po/pages/home.po';
import { PARTIAL_SETTING_THRESHOLD } from '~/cypress/support/utils/settings-utils';
import { RancherSetupConfigurePage } from '@/cypress/e2e/po/pages/rancher-setup-configure.po';
import HomePagePo from '@/cypress/e2e/po/pages/home.po';
import { PARTIAL_SETTING_THRESHOLD } from '@/cypress/support/utils/settings-utils';
import { serverUrlLocalhostCases, urlWithTrailingForwardSlash, httpUrl, nonUrlCases } from '@/cypress/e2e/blueprints/global_settings/settings-data';

// Cypress or the GrepTags avoid to run multiples times the same test for each tag used.
// This is a temporary solution till initialization is not handled as a test
Expand Down Expand Up @@ -58,6 +59,30 @@ describe('Rancher setup', { tags: ['@adminUserSetup', '@standardUserSetup', '@se

rancherSetupConfigurePage.waitForPage();
rancherSetupConfigurePage.canSubmit().should('eq', false);
// Check server url validation
rancherSetupConfigurePage.serverUrl().self().should('be.visible');
rancherSetupConfigurePage.serverUrl().self().invoke('val').then((initialServerUrl) => {
// Check showing localhost warning banner
serverUrlLocalhostCases.forEach((url) => {
rancherSetupConfigurePage.serverUrl().set(url);
rancherSetupConfigurePage.serverUrlLocalhostWarningBanner().banner().should('exist').and('be.visible');
});
// Check showing error banner when the urls has trailing forward slash
rancherSetupConfigurePage.serverUrl().set(urlWithTrailingForwardSlash);
rancherSetupConfigurePage.errorBannerContent('Server URL should not have a trailing forward slash.').should('exist').and('be.visible');
// Check showing error banner when the url is not HTTPS
rancherSetupConfigurePage.serverUrl().set(httpUrl);
rancherSetupConfigurePage.errorBannerContent('Server URL must be https.').should('exist').and('be.visible');
// // Check showing error banner when the input value is not a url
nonUrlCases.forEach((inputValue) => {
rancherSetupConfigurePage.serverUrl().set(inputValue);
rancherSetupConfigurePage.errorBannerContent('Server URL must be an URL.').should('exist').and('be.visible');
// A non-url is also a non-https
rancherSetupConfigurePage.errorBannerContent('Server URL must be https.').should('exist').and('be.visible');
});
rancherSetupConfigurePage.serverUrl().set(initialServerUrl);
});

rancherSetupConfigurePage.termsAgreement().set();
rancherSetupConfigurePage.canSubmit().should('eq', true);
rancherSetupConfigurePage.submit();
Expand Down
2 changes: 2 additions & 0 deletions shell/edit/management.cattle.io.setting.vue
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,15 @@ export default {
v-if="showLocalhostWarning"
color="warning"
:label="t('validation.setting.serverUrl.localhost')"
data-testid="setting-serverurl-localhost-warning"
/>
<Banner
v-for="(err, i) in fvGetPathErrors(['value'])"
:key="i"
color="error"
:label="err"
data-testid="setting-error-banner"
/>
<div class="mt-20">
Expand Down
2 changes: 2 additions & 0 deletions shell/pages/auth/setup.vue
Original file line number Diff line number Diff line change
Expand Up @@ -389,12 +389,14 @@ export default {
v-if="showLocalhostWarning"
color="warning"
:label="t('validation.setting.serverUrl.localhost')"
data-testid="setup-serverurl-localhost-warning"
/>
<Banner
v-for="(err, i) in fvGetPathErrors(['serverUrl'])"
:key="i"
color="error"
:label="err"
data-testid="setup-error-banner"
/>
<LabeledInput
v-model="serverUrl"
Expand Down

0 comments on commit 381a15b

Please sign in to comment.