From 97ab82892da9acc2defb0f32967b32a2757d3005 Mon Sep 17 00:00:00 2001 From: Sojan Date: Tue, 24 Dec 2019 15:31:10 +0530 Subject: [PATCH] Chore: labels to Conversations [#245] --- .../api/v1/conversations/labels_controller.rb | 4 +- .../conversations/labels/create.json.jbuilder | 8 +++ .../conversations/labels_controller_spec.rb | 67 +++++++++++++++++++ .../api/v1/labels_controller_spec.rb | 34 ++++++++++ 4 files changed, 111 insertions(+), 2 deletions(-) create mode 100644 app/views/api/v1/conversations/labels/create.json.jbuilder create mode 100644 spec/controllers/api/v1/conversations/labels_controller_spec.rb create mode 100644 spec/controllers/api/v1/labels_controller_spec.rb diff --git a/app/controllers/api/v1/conversations/labels_controller.rb b/app/controllers/api/v1/conversations/labels_controller.rb index 87273ac59..e9074ac03 100644 --- a/app/controllers/api/v1/conversations/labels_controller.rb +++ b/app/controllers/api/v1/conversations/labels_controller.rb @@ -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 diff --git a/app/views/api/v1/conversations/labels/create.json.jbuilder b/app/views/api/v1/conversations/labels/create.json.jbuilder new file mode 100644 index 000000000..13f99090d --- /dev/null +++ b/app/views/api/v1/conversations/labels/create.json.jbuilder @@ -0,0 +1,8 @@ +json.data do + json.meta do + end + + json.payload do + json.labels @labels + end +end diff --git a/spec/controllers/api/v1/conversations/labels_controller_spec.rb b/spec/controllers/api/v1/conversations/labels_controller_spec.rb new file mode 100644 index 000000000..1381d976d --- /dev/null +++ b/spec/controllers/api/v1/conversations/labels_controller_spec.rb @@ -0,0 +1,67 @@ +require 'rails_helper' + +RSpec.describe 'Conversation Label API', type: :request do + let(:account) { create(:account) } + + describe 'GET /api/v1/conversations//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//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 diff --git a/spec/controllers/api/v1/labels_controller_spec.rb b/spec/controllers/api/v1/labels_controller_spec.rb new file mode 100644 index 000000000..ab26b048f --- /dev/null +++ b/spec/controllers/api/v1/labels_controller_spec.rb @@ -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