mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-10-30 18:47:51 +00:00 
			
		
		
		
	Chore: labels to Conversations [#245]
This commit is contained in:
		| @@ -2,8 +2,8 @@ class Api::V1::Conversations::LabelsController < Api::BaseController | ||||
|   before_action :set_conversation, only: [:create, :index] | ||||
|  | ||||
|   def create | ||||
|     @conversation.update_labels(params[:labels].values) # .values is a hack | ||||
|     head :ok | ||||
|     @conversation.update_labels(params[:labels]) | ||||
|     @labels = @conversation.label_list | ||||
|   end | ||||
|  | ||||
|   def index # all labels of the current conversation | ||||
|   | ||||
| @@ -0,0 +1,8 @@ | ||||
| json.data do | ||||
|   json.meta do | ||||
|   end | ||||
|  | ||||
|   json.payload do | ||||
|     json.labels @labels | ||||
|   end | ||||
| end | ||||
| @@ -0,0 +1,67 @@ | ||||
| require 'rails_helper' | ||||
|  | ||||
| RSpec.describe 'Conversation Label API', type: :request do | ||||
|   let(:account) { create(:account) } | ||||
|  | ||||
|   describe 'GET /api/v1/conversations/<id>/labels' do | ||||
|     let(:conversation) { create(:conversation, account: account) } | ||||
|  | ||||
|     before do | ||||
|       conversation.update_labels('label1, label2') | ||||
|     end | ||||
|  | ||||
|     context 'when it is an unauthenticated user' do | ||||
|       it 'returns unauthorized' do | ||||
|         get api_v1_conversation_labels_url(conversation) | ||||
|         expect(response).to have_http_status(:unauthorized) | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     context 'when it is an authenticated user' do | ||||
|       let(:agent) { create(:user, account: account, role: :agent) } | ||||
|  | ||||
|       it 'returns all the labels for the conversation' do | ||||
|         get api_v1_conversation_labels_url(conversation.display_id), | ||||
|             headers: agent.create_new_auth_token, | ||||
|             as: :json | ||||
|  | ||||
|         expect(response).to have_http_status(:success) | ||||
|         expect(response.body).to include('label1') | ||||
|         expect(response.body).to include('label2') | ||||
|       end | ||||
|     end | ||||
|   end | ||||
|  | ||||
|   describe 'POST /api/v1/conversations/<id>/labels' do | ||||
|     let(:conversation) { create(:conversation, account: account) } | ||||
|  | ||||
|     before do | ||||
|       conversation.update_labels('label1, label2') | ||||
|     end | ||||
|  | ||||
|     context 'when it is an unauthenticated user' do | ||||
|       it 'returns unauthorized' do | ||||
|         post api_v1_conversation_labels_url(conversation.display_id), | ||||
|              params: { labels: 'label3,label4' }, | ||||
|              as: :json | ||||
|  | ||||
|         expect(response).to have_http_status(:unauthorized) | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     context 'when it is an authenticated user' do | ||||
|       let(:agent) { create(:user, account: account, role: :agent) } | ||||
|  | ||||
|       it 'creates labels for the conversation' do | ||||
|         post api_v1_conversation_labels_url(conversation.display_id), | ||||
|              params: { labels: 'label3,label4' }, | ||||
|              headers: agent.create_new_auth_token, | ||||
|              as: :json | ||||
|  | ||||
|         expect(response).to have_http_status(:success) | ||||
|         expect(response.body).to include('label3') | ||||
|         expect(response.body).to include('label4') | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| end | ||||
							
								
								
									
										34
									
								
								spec/controllers/api/v1/labels_controller_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								spec/controllers/api/v1/labels_controller_spec.rb
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,34 @@ | ||||
| require 'rails_helper' | ||||
|  | ||||
| RSpec.describe 'Label API', type: :request do | ||||
|   let(:account) { create(:account) } | ||||
|   let(:conversation) { create(:conversation, account: account) } | ||||
|  | ||||
|   before do | ||||
|     conversation.update_labels('label1, label2') | ||||
|   end | ||||
|  | ||||
|   describe 'GET /api/v1/labels' do | ||||
|     context 'when it is an unauthenticated user' do | ||||
|       it 'returns unauthorized' do | ||||
|         get '/api/v1/labels' | ||||
|  | ||||
|         expect(response).to have_http_status(:unauthorized) | ||||
|       end | ||||
|     end | ||||
|  | ||||
|     context 'when it is an authenticated user' do | ||||
|       let(:agent) { create(:user, account: account, role: :agent) } | ||||
|  | ||||
|       it 'returns all the labels in account' do | ||||
|         get '/api/v1/labels', | ||||
|             headers: agent.create_new_auth_token, | ||||
|             as: :json | ||||
|  | ||||
|         expect(response).to have_http_status(:success) | ||||
|         expect(response.body).to include('label1') | ||||
|         expect(response.body).to include('label2') | ||||
|       end | ||||
|     end | ||||
|   end | ||||
| end | ||||
		Reference in New Issue
	
	Block a user
	 Sojan
					Sojan