mirror of
https://github.com/RocketChat/Rocket.Chat.git
synced 2025-12-28 06:47:25 +00:00
fix!: title and value properties should be required on attachments.fields (#37233)
This commit is contained in:
parent
3c8c320595
commit
62708dcd12
5
.changeset/hungry-fans-wait.md
Normal file
5
.changeset/hungry-fans-wait.md
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'@rocket.chat/meteor': minor
|
||||||
|
---
|
||||||
|
|
||||||
|
Validates attachment fields to require `title` and `value` properties on APIs `chat.postMessage` and `chat.sendMessage`.
|
||||||
@ -79,8 +79,8 @@ const validateAttachmentsFields = (attachmentField: any) => {
|
|||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
|
|
||||||
if (typeof attachmentField.value !== 'undefined') {
|
if (!attachmentField.value || !attachmentField.title) {
|
||||||
attachmentField.value = String(attachmentField.value);
|
throw new Error('Invalid attachment field, title and value is required');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -464,6 +464,126 @@ describe('[Chat]', () => {
|
|||||||
.end(done);
|
.end(done);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should throw an error when the properties (attachments.fields.title) is missing', (done) => {
|
||||||
|
void request
|
||||||
|
.post(api('chat.postMessage'))
|
||||||
|
.set(credentials)
|
||||||
|
.send({
|
||||||
|
channel: testChannel.name,
|
||||||
|
text: 'Sample message',
|
||||||
|
emoji: ':smirk:',
|
||||||
|
alias: 'Gruggy',
|
||||||
|
avatar: 'http://res.guggy.com/logo_128.png',
|
||||||
|
attachments: [
|
||||||
|
{
|
||||||
|
color: '#ff0000',
|
||||||
|
text: 'Yay for gruggy!',
|
||||||
|
ts: '2016-12-09T16:53:06.761Z',
|
||||||
|
thumb_url: 'http://res.guggy.com/logo_128.png',
|
||||||
|
message_link: 'https://google.com',
|
||||||
|
collapsed: false,
|
||||||
|
author_name: 'Bradley Hilton',
|
||||||
|
author_link: 'https://rocket.chat/',
|
||||||
|
author_icon: 'https://avatars.githubusercontent.com/u/850391?v=3',
|
||||||
|
title: 'Attachment Example',
|
||||||
|
title_link: 'https://youtube.com',
|
||||||
|
title_link_download: true,
|
||||||
|
image_url: 'http://res.guggy.com/logo_128.png',
|
||||||
|
audio_url: 'http://www.w3schools.com/tags/horse.mp3',
|
||||||
|
video_url: 'http://www.w3schools.com/tags/movie.mp4',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
short: true,
|
||||||
|
value: 'This is attachment field value',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
.expect('Content-Type', 'application/json')
|
||||||
|
.expect(400)
|
||||||
|
.expect((res) => {
|
||||||
|
expect(res.body).to.have.property('success', false);
|
||||||
|
expect(res.body).to.have.property('error');
|
||||||
|
})
|
||||||
|
.end(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should throw an error when the properties (attachments.fields.value) is missing', (done) => {
|
||||||
|
void request
|
||||||
|
.post(api('chat.postMessage'))
|
||||||
|
.set(credentials)
|
||||||
|
.send({
|
||||||
|
channel: testChannel.name,
|
||||||
|
text: 'Sample message',
|
||||||
|
emoji: ':smirk:',
|
||||||
|
alias: 'Gruggy',
|
||||||
|
avatar: 'http://res.guggy.com/logo_128.png',
|
||||||
|
attachments: [
|
||||||
|
{
|
||||||
|
color: '#ff0000',
|
||||||
|
text: 'Yay for gruggy!',
|
||||||
|
ts: '2016-12-09T16:53:06.761Z',
|
||||||
|
thumb_url: 'http://res.guggy.com/logo_128.png',
|
||||||
|
message_link: 'https://google.com',
|
||||||
|
collapsed: false,
|
||||||
|
author_name: 'Bradley Hilton',
|
||||||
|
author_link: 'https://rocket.chat/',
|
||||||
|
author_icon: 'https://avatars.githubusercontent.com/u/850391?v=3',
|
||||||
|
title: 'Attachment Example',
|
||||||
|
title_link: 'https://youtube.com',
|
||||||
|
title_link_download: true,
|
||||||
|
image_url: 'http://res.guggy.com/logo_128.png',
|
||||||
|
audio_url: 'http://www.w3schools.com/tags/horse.mp3',
|
||||||
|
video_url: 'http://www.w3schools.com/tags/movie.mp4',
|
||||||
|
fields: [
|
||||||
|
{
|
||||||
|
short: true,
|
||||||
|
title: 'This is attachment field title',
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
.expect('Content-Type', 'application/json')
|
||||||
|
.expect(400)
|
||||||
|
.expect((res) => {
|
||||||
|
expect(res.body).to.have.property('success', false);
|
||||||
|
expect(res.body).to.have.property('error');
|
||||||
|
})
|
||||||
|
.end(done);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('attachment.fields should work fine when value and title are provided', (done) => {
|
||||||
|
void request
|
||||||
|
.post(api('chat.postMessage'))
|
||||||
|
.set(credentials)
|
||||||
|
.send({
|
||||||
|
channel: testChannel.name,
|
||||||
|
text: 'Sample message',
|
||||||
|
attachments: [
|
||||||
|
{
|
||||||
|
text: 'This is attachment field',
|
||||||
|
color: '#764FA5',
|
||||||
|
fields: [{ short: true, value: 'This is value', title: 'This is title' }],
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
.expect('Content-Type', 'application/json')
|
||||||
|
.expect(200)
|
||||||
|
.expect((res) => {
|
||||||
|
expect(res.body).to.have.property('success', true);
|
||||||
|
expect(res.body).to.not.have.property('error');
|
||||||
|
expect(res.body).to.have.nested.property('message.msg', 'Sample message');
|
||||||
|
expect(res.body).to.have.nested.property('message.attachments').to.be.an('array');
|
||||||
|
expect(res.body).to.have.nested.property('message.attachments[0].fields').to.be.an('array');
|
||||||
|
expect(res.body).to.have.nested.property('message.attachments[0].fields[0].short', true);
|
||||||
|
expect(res.body).to.have.nested.property('message.attachments[0].fields[0].value', 'This is value');
|
||||||
|
expect(res.body).to.have.nested.property('message.attachments[0].fields[0].title', 'This is title');
|
||||||
|
})
|
||||||
|
.end(done);
|
||||||
|
});
|
||||||
|
|
||||||
it('should return statusCode 200 when postMessage successfully', (done) => {
|
it('should return statusCode 200 when postMessage successfully', (done) => {
|
||||||
void request
|
void request
|
||||||
.post(api('chat.postMessage'))
|
.post(api('chat.postMessage'))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user