chore: Add a check for defined labels in SDK API (#1259)

This commit is contained in:
Pranav Raj S 2020-09-20 19:29:39 +05:30 committed by GitHub
parent cdd428f503
commit 0deb1af852
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 3 deletions

View file

@ -1,6 +1,6 @@
class Api::V1::Widget::LabelsController < Api::V1::Widget::BaseController
def create
if conversation.present?
if conversation.present? && label_defined_in_account?
conversation.label_list.add(permitted_params[:label])
conversation.save!
end
@ -19,6 +19,11 @@ class Api::V1::Widget::LabelsController < Api::V1::Widget::BaseController
private
def label_defined_in_account?
label = @account.labels.find_by(title: permitted_params[:label])
label.present?
end
def permitted_params
params.permit(:id, :label, :website_token)
end

View file

@ -12,8 +12,24 @@ RSpec.describe '/api/v1/widget/labels', type: :request do
describe 'POST /api/v1/widget/labels' do
let(:params) { { website_token: web_widget.website_token, label: 'customer-support' } }
context 'with correct website token' do
it 'returns the list of labels' do
context 'with correct website token and undefined label' do
it 'does not add the label' do
post '/api/v1/widget/labels',
params: params,
headers: { 'X-Auth-Token' => token },
as: :json
expect(response).to have_http_status(:success)
expect(conversation.reload.label_list.count).to eq 0
end
end
context 'with correct website token and a defined label' do
before do
account.labels.create(title: 'customer-support')
end
it 'add the label to the conversation' do
post '/api/v1/widget/labels',
params: params,
headers: { 'X-Auth-Token' => token },