Skip to content

Commit e1130f4

Browse files
authored
feat(plugin): allowing referer-restriction to dynamically adapt to the BE rules (#2001)
* feat(plugin): add basic * feat(plugin): fix UI referer-restriction * feat(plugin): fix variable name * feat(plugin): add test case for referer plugin
1 parent f9f4b36 commit e1130f4

File tree

3 files changed

+43
-23
lines changed

3 files changed

+43
-23
lines changed

web/cypress/integration/route/create-route-with-referer-restriction-form.spec.js

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ context('Create and delete route with referer-restriction form', () => {
3333
deleteAlert: '.ant-modal-body',
3434
whitlist: '#whitelist_0',
3535
alert: '.ant-form-item-explain-error [role=alert]',
36+
newAdd: '.ant-btn-dashed',
37+
whitlist_1: '#whitelist_1',
38+
passSwitcher: '#bypass_missing',
3639
};
3740

3841
const data = {
@@ -41,6 +44,9 @@ context('Create and delete route with referer-restriction form', () => {
4144
weight: 1,
4245
deleteRouteSuccess: 'Delete Route Successfully',
4346
submitSuccess: 'Submit Successfully',
47+
wrongIp: 'qq@',
48+
correctIp: 'apisix-dashboard_1.com',
49+
activeClass: 'ant-switch-checked',
4450
};
4551

4652
beforeEach(() => {
@@ -75,7 +81,8 @@ context('Create and delete route with referer-restriction form', () => {
7581
.should('be.visible')
7682
.within(() => {
7783
cy.get(selector.disabledSwitcher).click();
78-
cy.get(selector.checkedSwitcher).should('exist');
84+
cy.get(selector.disabledSwitcher).should('have.class', data.activeClass);
85+
cy.get(selector.passSwitcher).should('not.have.class', data.activeClass);
7986
});
8087

8188
// config referer-restriction form without whitelist
@@ -90,8 +97,18 @@ context('Create and delete route with referer-restriction form', () => {
9097
cy.get(selector.notificationCloseIcon).click();
9198

9299
// config referer-restriction form with whitelist
93-
cy.get(selector.whitlist).type('127.0.0.1');
100+
cy.get(selector.whitlist).type(data.wrongIp);
101+
cy.get(selector.whitlist).closest('div').next().children('span').should('not.exist');
102+
cy.get(selector.alert).should('exist');
103+
cy.get(selector.whitlist).clear().type(data.correctIp);
94104
cy.get(selector.alert).should('not.exist');
105+
106+
cy.get(selector.newAdd).click();
107+
cy.get(selector.whitlist).closest('div').next().children('span').should('exist');
108+
cy.get(selector.whitlist_1).closest('div').next().children('span').should('exist');
109+
cy.get(selector.whitlist_1).type(data.correctIp);
110+
cy.get(selector.alert).should('not.exist');
111+
95112
cy.get(selector.disabledSwitcher).click();
96113
cy.get(selector.drawer).within(() => {
97114
cy.contains('Submit').click({

web/src/components/Plugin/UI/plugin.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export const PluginForm: React.FC<Props> = ({ name, schema, renderForm, form })
7474
case 'limit-conn':
7575
return <LimitConn form={form} schema={schema} />;
7676
case 'referer-restriction':
77-
return <RefererRestriction form={form} />;
77+
return <RefererRestriction form={form} schema={schema} />
7878
default:
7979
return null;
8080
}

web/src/components/Plugin/UI/referer-restriction.tsx

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { MinusCircleOutlined, PlusOutlined } from '@ant-design/icons';
2222

2323
type Props = {
2424
form: FormInstance;
25+
schema: Record<string, any> | undefined;
2526
};
2627

2728
const FORM_ITEM_LAYOUT = {
@@ -46,10 +47,17 @@ const removeBtnStyle = {
4647
alignItems: 'center',
4748
};
4849

49-
const RefererRestriction: React.FC<Props> = ({ form }) => {
50-
const { formatMessage } = useIntl();
50+
const RefererRestriction: React.FC<Props> = ({ form, schema }) => {
51+
const { formatMessage } = useIntl()
52+
const properties = schema?.properties
53+
const allowListMinLength = properties.whitelist.minItems
54+
const whiteInit = Array(allowListMinLength).join('.').split('.')
5155
return (
52-
<Form form={form} {...FORM_ITEM_LAYOUT} initialValues={{ whitelist: [''] }}>
56+
<Form
57+
form={form}
58+
{...FORM_ITEM_LAYOUT}
59+
initialValues={{ whitelist: whiteInit }}
60+
>
5361
<Form.List name="whitelist">
5462
{(fields, { add, remove }) => {
5563
return (
@@ -73,33 +81,28 @@ const RefererRestriction: React.FC<Props> = ({ form }) => {
7381
validateTrigger={['onChange', 'onBlur', 'onClick']}
7482
noStyle
7583
required
76-
rules={[
77-
{
78-
message: formatMessage({
79-
id: 'page.route.form.itemRulesPatternMessage.domain',
80-
}),
81-
pattern: new RegExp(/^\*?[0-9a-zA-Z-._]+$/, 'g'),
82-
},
83-
{
84-
required: true,
85-
message: `${formatMessage({
86-
id: 'component.global.pleaseEnter',
87-
})} whitelist`,
88-
},
89-
]}
84+
rules={[{
85+
message: formatMessage({
86+
id: 'page.route.form.itemRulesPatternMessage.domain',
87+
}),
88+
pattern: new RegExp(`${properties.whitelist.items.pattern}`, 'g')
89+
}, {
90+
required: true,
91+
message: `${formatMessage({ id: 'component.global.pleaseEnter' })} whitelist`
92+
}]}
9093
>
9194
<Input />
9295
</Form.Item>
9396
</Col>
9497
<Col style={{ ...removeBtnStyle, marginLeft: -10 }}>
95-
{fields.length > 1 ? (
98+
{fields.length > allowListMinLength &&
9699
<MinusCircleOutlined
97100
className="dynamic-delete-button"
98101
onClick={() => {
99102
remove(field.name);
100103
}}
101104
/>
102-
) : null}
105+
}
103106
</Col>
104107
</Row>
105108
))}
@@ -129,7 +132,7 @@ const RefererRestriction: React.FC<Props> = ({ form }) => {
129132
})}
130133
valuePropName="checked"
131134
>
132-
<Switch />
135+
<Switch defaultChecked={properties.bypass_missing.default} />
133136
</Form.Item>
134137
</Form>
135138
);

0 commit comments

Comments
 (0)