-
Notifications
You must be signed in to change notification settings - Fork 13.1k
fix: schema definition for livechat/custom-fields.save #38376
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
Open
d-gubert
wants to merge
8
commits into
develop
Choose a base branch
from
chore/improve-livechat-customfield-endpoint
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd69d2c
fix: schema definition POSTLivechatSaveCustomFieldsSchema
d-gubert cda9430
refactor: Kevin said this is not needed
d-gubert 84c4627
types
KevLehman e63bead
test
KevLehman f526671
fix: schema definition
d-gubert f4f2cbd
add changeset
d-gubert 9fe7257
tests: add http API tests for the livechat/custom-fields.save endpoint
d-gubert e80eb4f
Merge branch 'develop' into chore/improve-livechat-customfield-endpoint
KevLehman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| --- | ||
| '@rocket.chat/model-typings': patch | ||
| '@rocket.chat/rest-typings': patch | ||
| '@rocket.chat/models': patch | ||
| '@rocket.chat/meteor': patch | ||
| --- | ||
|
|
||
| Fix a validation issue in the `livechat/custom-fields.save` endpoint |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
297 changes: 297 additions & 0 deletions
297
apps/meteor/tests/end-to-end/api/livechat/custom-fields-save.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,297 @@ | ||
| import type { ILivechatCustomField } from '@rocket.chat/core-typings'; | ||
| import { expect } from 'chai'; | ||
| import { after, before, describe, it } from 'mocha'; | ||
| import type { Response } from 'supertest'; | ||
|
|
||
| import { getCredentials, api, request, credentials } from '../../../data/api-data'; | ||
| import { createCustomField, deleteCustomField } from '../../../data/livechat/custom-fields'; | ||
| import { | ||
| getSettingValueById, | ||
| removePermissionFromAllRoles, | ||
| restorePermissionToRoles, | ||
| updateSetting, | ||
| } from '../../../data/permissions.helper'; | ||
|
|
||
| describe('LIVECHAT - custom fields', () => { | ||
| let settingLivechatEnabled: boolean; | ||
|
|
||
| before((done) => getCredentials(done)); | ||
|
|
||
| before(async () => { | ||
| settingLivechatEnabled = (await getSettingValueById('Livechat_enabled')) as boolean; | ||
| await updateSetting('Livechat_enabled', true); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| await updateSetting('Livechat_enabled', settingLivechatEnabled); | ||
| }); | ||
|
|
||
| describe('livechat/custom-fields.save', () => { | ||
| let customFieldId: string; | ||
|
|
||
| after(async () => { | ||
| if (customFieldId) { | ||
| await deleteCustomField(customFieldId); | ||
| } | ||
| }); | ||
|
|
||
| describe('Authentication/Authorization', () => { | ||
| it('should return an "unauthenticated error" when user is not logged in', async () => { | ||
| await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .send({ | ||
| customFieldId: null, | ||
| customFieldData: { | ||
| field: 'test_field', | ||
| label: 'Test Field', | ||
| scope: 'visitor', | ||
| visibility: 'public', | ||
| }, | ||
| }) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(401); | ||
| }); | ||
|
|
||
| it('should return an "unauthorized error" when the user does not have the necessary permission', async () => { | ||
| await removePermissionFromAllRoles('view-livechat-manager'); | ||
|
|
||
| await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .set(credentials) | ||
| .send({ | ||
| customFieldId: null, | ||
| customFieldData: { | ||
| field: 'test_field', | ||
| label: 'Test Field', | ||
| scope: 'visitor', | ||
| visibility: 'public', | ||
| }, | ||
| }) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(403); | ||
|
|
||
| await restorePermissionToRoles('view-livechat-manager'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Create custom field', () => { | ||
| afterEach(async () => { | ||
| if (customFieldId) { | ||
| await deleteCustomField(customFieldId); | ||
| customFieldId = ''; | ||
| } | ||
| }); | ||
|
|
||
| it('should create a new custom field with minimal required fields', async () => { | ||
| const fieldName = `field_${Date.now()}`; | ||
|
|
||
| await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .set(credentials) | ||
| .send({ | ||
| customFieldId: null, | ||
| customFieldData: { | ||
| field: fieldName, | ||
| label: 'Test Field', | ||
| scope: 'visitor', | ||
| visibility: 'public', | ||
| }, | ||
| }) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(200) | ||
| .expect((res: Response) => { | ||
| expect(res.body).to.have.property('success', true); | ||
| expect(res.body).to.have.property('customField'); | ||
| expect(res.body.customField).to.have.property('_id'); | ||
| expect(res.body.customField).to.have.property('label', 'Test Field'); | ||
| expect(res.body.customField).to.have.property('scope', 'visitor'); | ||
| expect(res.body.customField).to.have.property('visibility', 'public'); | ||
| customFieldId = res.body.customField._id; | ||
| }); | ||
| }); | ||
|
|
||
| it('should create a new custom field with scope "room"', async () => { | ||
| const fieldName = `room_field_${Date.now()}`; | ||
|
|
||
| await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .set(credentials) | ||
| .send({ | ||
| customFieldId: null, | ||
| customFieldData: { | ||
| field: fieldName, | ||
| label: 'Room Test Field', | ||
| scope: 'room', | ||
| visibility: 'public', | ||
| }, | ||
| }) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(200) | ||
| .expect((res: Response) => { | ||
| expect(res.body).to.have.property('success', true); | ||
| expect(res.body).to.have.property('customField'); | ||
| expect(res.body.customField).to.have.property('scope', 'room'); | ||
| customFieldId = res.body.customField._id; | ||
| }); | ||
| }); | ||
|
|
||
| it('should create a new custom field with all optional fields', async () => { | ||
| const fieldName = `full_field_${Date.now()}`; | ||
|
|
||
| await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .set(credentials) | ||
| .send({ | ||
| customFieldId: null, | ||
| customFieldData: { | ||
| field: fieldName, | ||
| label: 'Full Test Field', | ||
| scope: 'visitor', | ||
| visibility: 'public', | ||
| type: 'input', | ||
| regexp: '^[A-Za-z]+$', | ||
| required: true, | ||
| defaultValue: 'default', | ||
| options: 'option1,option2,option3', | ||
| public: true, | ||
| searchable: true, | ||
| }, | ||
| }) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(200) | ||
| .expect((res: Response) => { | ||
| expect(res.body).to.have.property('success', true); | ||
| expect(res.body).to.have.property('customField'); | ||
| expect(res.body.customField).to.have.property('type', 'input'); | ||
| expect(res.body.customField).to.have.property('regexp', '^[A-Za-z]+$'); | ||
| expect(res.body.customField).to.have.property('required', true); | ||
| expect(res.body.customField).to.have.property('defaultValue', 'default'); | ||
| expect(res.body.customField).to.have.property('options', 'option1,option2,option3'); | ||
| expect(res.body.customField).to.have.property('public', true); | ||
| expect(res.body.customField).to.have.property('searchable', true); | ||
| customFieldId = res.body.customField._id; | ||
| }); | ||
| }); | ||
|
|
||
| it('should fail when trying to create a custom field with a field name that already exists', async () => { | ||
| const fieldName = `duplicate_field_${Date.now()}`; | ||
|
|
||
| // Create the first custom field | ||
| const { body } = await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .set(credentials) | ||
| .send({ | ||
| customFieldId: null, | ||
| customFieldData: { | ||
| field: fieldName, | ||
| label: 'First Field', | ||
| scope: 'visitor', | ||
| visibility: 'public', | ||
| }, | ||
| }) | ||
| .expect(200); | ||
|
|
||
| customFieldId = body.customField._id; | ||
|
|
||
| // Try to create another with the same field name | ||
| await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .set(credentials) | ||
| .send({ | ||
| customFieldId: null, | ||
| customFieldData: { | ||
| field: fieldName, | ||
| label: 'Second Field', | ||
| scope: 'visitor', | ||
| visibility: 'public', | ||
| }, | ||
| }) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(400) | ||
| .expect((res: Response) => { | ||
| expect(res.body).to.have.property('success', false); | ||
| expect(res.body).to.have.property('error'); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('Update custom field', () => { | ||
| let existingField: ILivechatCustomField; | ||
|
|
||
| before(async () => { | ||
| const fieldName = `update_test_field_${Date.now()}`; | ||
| existingField = await createCustomField({ | ||
| searchable: true, | ||
| field: fieldName, | ||
| label: 'Original Label', | ||
| defaultValue: 'original_default', | ||
| scope: 'visitor', | ||
| visibility: 'public', | ||
| regexp: '', | ||
| }); | ||
| }); | ||
|
|
||
| after(async () => { | ||
| if (existingField?._id) { | ||
| await deleteCustomField(existingField._id); | ||
| } | ||
| }); | ||
|
|
||
| it('should fail when trying to update a non-existent custom field', async () => { | ||
| await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .set(credentials) | ||
| .send({ | ||
| customFieldId: 'non-existent-id', | ||
| customFieldData: { | ||
| field: 'test_field', | ||
| label: 'Updated Label', | ||
| scope: 'visitor', | ||
| visibility: 'public', | ||
| }, | ||
| }) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(400) | ||
| .expect((res: Response) => { | ||
| expect(res.body).to.have.property('success', false); | ||
| expect(res.body).to.have.property('error'); | ||
| }); | ||
| }); | ||
|
|
||
| it('should update an existing custom field with all optional fields', async () => { | ||
| await request | ||
| .post(api('livechat/custom-fields.save')) | ||
| .set(credentials) | ||
| .send({ | ||
| customFieldId: existingField._id, | ||
| customFieldData: { | ||
| field: existingField._id, | ||
| label: 'Fully Updated Field', | ||
| scope: existingField.scope, | ||
| visibility: 'public', | ||
| type: 'select', | ||
| regexp: '^[0-9]+$', | ||
| required: true, | ||
| defaultValue: 'new_default', | ||
| options: 'opt1,opt2,opt3', | ||
| public: true, | ||
| searchable: false, | ||
| }, | ||
| }) | ||
| .expect('Content-Type', 'application/json') | ||
| .expect(200) | ||
| .expect((res: Response) => { | ||
| expect(res.body).to.have.property('success', true); | ||
| expect(res.body.customField).to.have.property('label', 'Fully Updated Field'); | ||
| expect(res.body.customField).to.have.property('type', 'select'); | ||
| expect(res.body.customField).to.have.property('regexp', '^[0-9]+$'); | ||
| expect(res.body.customField).to.have.property('required', true); | ||
| expect(res.body.customField).to.have.property('defaultValue', 'new_default'); | ||
| expect(res.body.customField).to.have.property('options', 'opt1,opt2,opt3'); | ||
| expect(res.body.customField).to.have.property('public', true); | ||
| expect(res.body.customField).to.have.property('searchable', false); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.