mirror of
				https://github.com/lingble/chatwoot.git
				synced 2025-11-04 04:57:51 +00:00 
			
		
		
		
	Chore: Added tests for conversations_controller (#508)
* Chore: Added tests for conversations_controller * Chore: Remove get_messages function from conversations
This commit is contained in:
		@@ -1,11 +1,5 @@
 | 
			
		||||
class Api::V1::ConversationsController < Api::BaseController
 | 
			
		||||
  before_action :set_conversation, except: [:index, :get_messages]
 | 
			
		||||
 | 
			
		||||
  # TODO: move this to public controller
 | 
			
		||||
  skip_before_action :authenticate_user!, only: [:get_messages]
 | 
			
		||||
  skip_before_action :set_current_user, only: [:get_messages]
 | 
			
		||||
  skip_before_action :check_subscription, only: [:get_messages]
 | 
			
		||||
  skip_around_action :handle_with_exception, only: [:get_messages]
 | 
			
		||||
  before_action :set_conversation, except: [:index]
 | 
			
		||||
 | 
			
		||||
  def index
 | 
			
		||||
    result = conversation_finder.perform
 | 
			
		||||
@@ -27,11 +21,6 @@ class Api::V1::ConversationsController < Api::BaseController
 | 
			
		||||
    head :ok
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  def get_messages
 | 
			
		||||
    @conversation = Conversation.find(params[:id])
 | 
			
		||||
    @messages = messages_finder.perform
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  private
 | 
			
		||||
 | 
			
		||||
  def parsed_last_seen_at
 | 
			
		||||
 
 | 
			
		||||
@@ -1,15 +0,0 @@
 | 
			
		||||
json.payload do
 | 
			
		||||
  json.array! @messages do |message|
 | 
			
		||||
    json.id message.id
 | 
			
		||||
    json.content message.content
 | 
			
		||||
    json.inbox_id message.inbox_id
 | 
			
		||||
    json.conversation_id message.conversation.display_id
 | 
			
		||||
    json.message_type message.message_type_before_type_cast
 | 
			
		||||
    json.created_at message.created_at.to_i
 | 
			
		||||
    json.private message.private
 | 
			
		||||
    json.source_id message.source_id
 | 
			
		||||
    json.attachment message.attachment.push_event_data if message.attachment
 | 
			
		||||
    json.sender message.user.push_event_data if message.user
 | 
			
		||||
    json.contact message.contact
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
@@ -83,7 +83,6 @@ Rails.application.routes.draw do
 | 
			
		||||
        member do
 | 
			
		||||
          post :toggle_status
 | 
			
		||||
          post :update_last_seen
 | 
			
		||||
          get :get_messages
 | 
			
		||||
        end
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										113
									
								
								spec/controllers/api/v1/conversations_controller_spec.rb
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										113
									
								
								spec/controllers/api/v1/conversations_controller_spec.rb
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,113 @@
 | 
			
		||||
require 'rails_helper'
 | 
			
		||||
 | 
			
		||||
RSpec.describe 'Conversations API', type: :request do
 | 
			
		||||
  let(:account) { create(:account) }
 | 
			
		||||
 | 
			
		||||
  describe 'GET /api/v1/conversations' do
 | 
			
		||||
    context 'when it is an unauthenticated user' do
 | 
			
		||||
      it 'returns unauthorized' do
 | 
			
		||||
        get '/api/v1/conversations'
 | 
			
		||||
 | 
			
		||||
        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) }
 | 
			
		||||
 | 
			
		||||
      before do
 | 
			
		||||
        conversation = create(:conversation, account: account)
 | 
			
		||||
        create(:inbox_member, user: agent, inbox: conversation.inbox)
 | 
			
		||||
      end
 | 
			
		||||
 | 
			
		||||
      it 'returns all conversations' do
 | 
			
		||||
        get '/api/v1/conversations',
 | 
			
		||||
            headers: agent.create_new_auth_token,
 | 
			
		||||
            as: :json
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(:success)
 | 
			
		||||
        expect(JSON.parse(response.body, symbolize_names: true)[:data][:meta][:all_count]).to eq(1)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'GET /api/v1/conversations/:id' do
 | 
			
		||||
    let(:conversation) { create(:conversation, account: account) }
 | 
			
		||||
 | 
			
		||||
    context 'when it is an unauthenticated user' do
 | 
			
		||||
      it 'returns unauthorized' do
 | 
			
		||||
        get "/api/v1/conversations/#{conversation.display_id}"
 | 
			
		||||
 | 
			
		||||
        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 'shows the conversation' do
 | 
			
		||||
        get "/api/v1/conversations/#{conversation.display_id}",
 | 
			
		||||
            headers: agent.create_new_auth_token,
 | 
			
		||||
            as: :json
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(:success)
 | 
			
		||||
        expect(JSON.parse(response.body, symbolize_names: true)[:meta][:contact_id]).to eq(conversation.contact_id)
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'POST /api/v1/conversations/:id/toggle_status' do
 | 
			
		||||
    let(:conversation) { create(:conversation, account: account) }
 | 
			
		||||
 | 
			
		||||
    context 'when it is an unauthenticated user' do
 | 
			
		||||
      it 'returns unauthorized' do
 | 
			
		||||
        post "/api/v1/conversations/#{conversation.display_id}/toggle_status"
 | 
			
		||||
 | 
			
		||||
        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 'toggles the conversation status' do
 | 
			
		||||
        expect(conversation.status).to eq('open')
 | 
			
		||||
 | 
			
		||||
        post "/api/v1/conversations/#{conversation.display_id}/toggle_status",
 | 
			
		||||
             headers: agent.create_new_auth_token,
 | 
			
		||||
             as: :json
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(:success)
 | 
			
		||||
        expect(conversation.reload.status).to eq('resolved')
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
 | 
			
		||||
  describe 'POST /api/v1/conversations/:id/update_last_seen' do
 | 
			
		||||
    let(:conversation) { create(:conversation, account: account) }
 | 
			
		||||
 | 
			
		||||
    context 'when it is an unauthenticated user' do
 | 
			
		||||
      it 'returns unauthorized' do
 | 
			
		||||
        post "/api/v1/conversations/#{conversation.display_id}/update_last_seen"
 | 
			
		||||
 | 
			
		||||
        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 'updates last seen' do
 | 
			
		||||
        params = { agent_last_seen_at: '-1' }
 | 
			
		||||
 | 
			
		||||
        post "/api/v1/conversations/#{conversation.display_id}/update_last_seen",
 | 
			
		||||
             headers: agent.create_new_auth_token,
 | 
			
		||||
             params: params,
 | 
			
		||||
             as: :json
 | 
			
		||||
 | 
			
		||||
        expect(response).to have_http_status(:success)
 | 
			
		||||
        expect(conversation.reload.agent_last_seen_at).to eq(DateTime.strptime(params[:agent_last_seen_at].to_s, '%s'))
 | 
			
		||||
      end
 | 
			
		||||
    end
 | 
			
		||||
  end
 | 
			
		||||
end
 | 
			
		||||
		Reference in New Issue
	
	Block a user