Merge branch 'develop' into codex/fix-inbox-finish-setup-guidance

This commit is contained in:
Muhsin Keloth
2026-04-21 13:51:36 +04:00
committed by GitHub
1089 changed files with 50709 additions and 11977 deletions
@@ -68,6 +68,12 @@ RSpec.describe 'Api::V1::Accounts::AutomationRulesController', type: :request do
'action_name': :assign_team,
'action_params': [1]
},
{
'action_name': :remove_assigned_agent
},
{
'action_name': :remove_assigned_team
},
{
'action_name': :add_label,
'action_params': %w[support priority_customer]
@@ -729,6 +729,23 @@ RSpec.describe 'Conversations API', type: :request do
expect(conversation.reload.assignee_last_seen_at).not_to be_nil
end
it 'marks unread notifications as read when updating last seen' do
allow(Rails.configuration.dispatcher).to receive(:dispatch)
notification = create(:notification, account: account, user: agent, primary_actor: conversation, read_at: nil)
post "/api/v1/accounts/#{account.id}/conversations/#{conversation.display_id}/update_last_seen",
headers: agent.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(notification.reload.read_at).to be_present
expect(Rails.configuration.dispatcher).to have_received(:dispatch).with(
'notification.updated',
kind_of(Time),
hash_including(notification: have_attributes(id: notification.id))
)
end
it 'throttles updates within an hour when there are no unread messages' do
conversation.update!(agent_last_seen_at: 30.minutes.ago)
# Ensure all messages are older than agent_last_seen_at (no unread messages)
@@ -78,6 +78,9 @@ RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do
'action_name': :add_label,
'action_params': %w[support priority_customer]
},
{
'action_name': :remove_assigned_agent
},
{
'action_name': :remove_assigned_team
},
@@ -484,6 +487,22 @@ RSpec.describe 'Api::V1::Accounts::MacrosController', type: :request do
expect(conversation.reload.team_id).to be_nil
end
it 'Unassign the agent' do
macro.update!(actions: [
{ 'action_name' => 'remove_assigned_agent' }
])
conversation.update!(assignee: user_1)
expect(conversation.reload.assignee).to be_present
perform_enqueued_jobs do
post "/api/v1/accounts/#{account.id}/macros/#{macro.id}/execute",
params: { conversation_ids: [conversation.display_id] },
headers: administrator.create_new_auth_token
end
expect(conversation.reload.assignee).to be_nil
end
end
end
end
@@ -26,8 +26,8 @@ RSpec.describe 'Accounts API', type: :request do
expect(AccountBuilder).to have_received(:new).with(params.except(:password).merge(user_password: params[:password]))
expect(account_builder).to have_received(:perform)
expect(response.headers.keys).to include('access-token', 'token-type', 'client', 'expiry', 'uid')
expect(response.body).to include('en')
expect(response.headers.keys).not_to include('access-token', 'token-type', 'client', 'expiry', 'uid')
expect(response.parsed_body['email']).to eq(email)
end
end
@@ -46,8 +46,8 @@ RSpec.describe 'Accounts API', type: :request do
as: :json
expect(ChatwootCaptcha).to have_received(:new).with('123')
expect(response.headers.keys).to include('access-token', 'token-type', 'client', 'expiry', 'uid')
expect(response.body).to include('en')
expect(response.headers.keys).not_to include('access-token', 'token-type', 'client', 'expiry', 'uid')
expect(response.parsed_body['email']).to eq(email)
end
end
@@ -68,6 +68,23 @@ RSpec.describe 'Accounts API', type: :request do
end
end
context 'when an authenticated user creates a second account' do
let(:existing_user) { create(:user, password: 'Password1!') }
it 'returns the full response with account_id' do
with_modified_env ENABLE_ACCOUNT_SIGNUP: 'true' do
post api_v1_accounts_url,
params: { account_name: 'Second Account', email: existing_user.email,
user_full_name: existing_user.name, password: 'Password1!' },
headers: existing_user.create_new_auth_token,
as: :json
expect(response).to have_http_status(:success)
expect(response.parsed_body.dig('data', 'account_id')).to be_present
end
end
end
context 'when ENABLE_ACCOUNT_SIGNUP env variable is set to false' do
it 'responds 404 on requests' do
params = { account_name: 'test', email: email, user_full_name: user_full_name }
@@ -105,7 +122,17 @@ RSpec.describe 'Accounts API', type: :request do
end
context 'when ENABLE_ACCOUNT_SIGNUP env variable is set to api_only' do
it 'does not respond 404 on requests' do
before do
GlobalConfig.clear_cache
InstallationConfig.where(name: 'ENABLE_ACCOUNT_SIGNUP').delete_all
end
after do
InstallationConfig.where(name: 'ENABLE_ACCOUNT_SIGNUP').delete_all
GlobalConfig.clear_cache
end
it 'returns auth headers and full response for api_only signup' do
params = { account_name: 'test', email: email, user_full_name: user_full_name, password: 'Password1!' }
with_modified_env ENABLE_ACCOUNT_SIGNUP: 'api_only' do
post api_v1_accounts_url,
@@ -113,6 +140,21 @@ RSpec.describe 'Accounts API', type: :request do
as: :json
expect(response).to have_http_status(:success)
expect(response.headers.keys).to include('access-token', 'token-type', 'client', 'expiry', 'uid')
end
end
end
context 'when CW_API_ONLY_SERVER is true' do
it 'returns auth headers and full response' do
params = { account_name: 'test', email: email, user_full_name: user_full_name, password: 'Password1!' }
with_modified_env ENABLE_ACCOUNT_SIGNUP: 'true', CW_API_ONLY_SERVER: 'true' do
post api_v1_accounts_url,
params: params,
as: :json
expect(response).to have_http_status(:success)
expect(response.headers.keys).to include('access-token', 'token-type', 'client', 'expiry', 'uid')
end
end
end
@@ -39,6 +39,11 @@ RSpec.describe 'Api::V1::Accounts::UploadController', type: :request do
let(:valid_external_url) { 'http://example.com/image.jpg' }
before do
allow(Resolv).to receive(:getaddresses).and_call_original
allow(Resolv).to receive(:getaddresses).with('example.com').and_return(['93.184.216.34'])
allow(Resolv).to receive(:getaddresses).with('error.example.com').and_return(['93.184.216.34'])
allow(Resolv).to receive(:getaddresses).with('nonexistent.example.com').and_return(['93.184.216.34'])
stub_request(:get, valid_external_url)
.to_return(status: 200, body: File.new(Rails.root.join('spec/assets/avatar.png')), headers: { 'Content-Type' => 'image/png' })
end
@@ -82,7 +87,7 @@ RSpec.describe 'Api::V1::Accounts::UploadController', type: :request do
params: { external_url: 'http://nonexistent.example.com' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
expect(response.parsed_body['error']).to eq('Failed to fetch file from URL')
end
it 'handles HTTP errors' do
@@ -96,6 +101,112 @@ RSpec.describe 'Api::V1::Accounts::UploadController', type: :request do
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to start_with('Failed to fetch file from URL')
end
it 'rejects oversized responses with a file-size message' do
stub_request(:get, valid_external_url)
.to_return(status: 200,
body: 'x' * (41 * 1024 * 1024),
headers: { 'Content-Type' => 'image/png' })
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: valid_external_url }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('File exceeds the maximum allowed size')
end
it 'rejects unsupported content types with a file-type message' do
stub_request(:get, valid_external_url)
.to_return(status: 200,
body: '<html></html>',
headers: { 'Content-Type' => 'text/html' })
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: valid_external_url }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('File type not supported (only images and videos are allowed)')
end
context 'with SSRF attack vectors' do
it 'blocks requests to private IP ranges (10.x.x.x)' do
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: 'http://10.0.0.1/secret' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
end
it 'blocks requests to private IP ranges (172.16.x.x)' do
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: 'http://172.16.0.1/secret' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
end
it 'blocks requests to private IP ranges (192.168.x.x)' do
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: 'http://192.168.1.1/secret' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
end
it 'blocks requests to loopback addresses' do
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: 'http://127.0.0.1/secret' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
end
it 'blocks requests to AWS metadata service (169.254.169.254)' do
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: 'http://169.254.169.254/latest/meta-data/iam/security-credentials/' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
end
it 'blocks requests to localhost' do
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: 'http://localhost/secret' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
end
it 'blocks requests to .local domains' do
allow(Resolv).to receive(:getaddresses).with('server.local').and_return(['192.168.1.100'])
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: 'http://server.local/secret' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
end
it 'blocks DNS rebinding attacks (hostname resolving to private IP)' do
allow(Resolv).to receive(:getaddresses).with('evil.attacker.com').and_return(['10.0.0.1'])
post upload_url,
headers: user.create_new_auth_token,
params: { external_url: 'http://evil.attacker.com/secret' }
expect(response).to have_http_status(:unprocessable_entity)
expect(response.parsed_body['error']).to eq('Invalid URL provided')
end
end
end
it 'returns an error when no file or URL is provided' do
@@ -56,6 +56,65 @@ RSpec.describe '/api/v1/widget/messages', type: :request do
expect(json_response['content']).to eq(message_params[:content])
end
it 'creates conversation with custom_attributes when first message is sent' do
conversation.destroy!
message_params = { content: 'hello world', timestamp: Time.current }
custom_attributes = { plan: 'enterprise', source: 'website' }
post api_v1_widget_messages_url,
params: { website_token: web_widget.website_token, message: message_params, custom_attributes: custom_attributes },
headers: { 'X-Auth-Token' => token },
as: :json
expect(response).to have_http_status(:success)
new_conversation = contact.conversations.last
expect(new_conversation.custom_attributes).to include('plan' => 'enterprise', 'source' => 'website')
end
it 'creates conversation with labels when first message is sent' do
conversation.destroy!
label = create(:label, title: 'vip', account: account)
message_params = { content: 'hello world', timestamp: Time.current }
post api_v1_widget_messages_url,
params: { website_token: web_widget.website_token, message: message_params, labels: [label.title] },
headers: { 'X-Auth-Token' => token },
as: :json
expect(response).to have_http_status(:success)
new_conversation = contact.conversations.last
expect(new_conversation.label_list).to include('vip')
end
it 'ignores invalid labels when creating conversation with first message' do
conversation.destroy!
create(:label, title: 'valid-label', account: account)
message_params = { content: 'hello world', timestamp: Time.current }
post api_v1_widget_messages_url,
params: { website_token: web_widget.website_token, message: message_params, labels: %w[valid-label nonexistent] },
headers: { 'X-Auth-Token' => token },
as: :json
expect(response).to have_http_status(:success)
new_conversation = contact.conversations.last
expect(new_conversation.label_list).to include('valid-label')
expect(new_conversation.label_list).not_to include('nonexistent')
end
it 'does not apply labels or custom_attributes when conversation already exists' do
create(:label, title: 'vip', account: account)
message_params = { content: 'hello world', timestamp: Time.current }
custom_attributes = { plan: 'enterprise' }
post api_v1_widget_messages_url,
params: { website_token: web_widget.website_token, message: message_params,
custom_attributes: custom_attributes, labels: ['vip'] },
headers: { 'X-Auth-Token' => token },
as: :json
expect(response).to have_http_status(:success)
conversation.reload
expect(conversation.custom_attributes).not_to include('plan' => 'enterprise')
expect(conversation.label_list).not_to include('vip')
end
it 'does not create the message' do
conversation.destroy! # Test all params
message_params = { content: "#{'h' * 150 * 1000}a", timestamp: Time.current }
@@ -45,7 +45,10 @@ RSpec.describe 'Summary Reports API', type: :request do
headers: admin.create_new_auth_token,
as: :json
expect(V2::Reports::AgentSummaryBuilder).to have_received(:new).with(account: account, params: params)
expect(V2::Reports::AgentSummaryBuilder).to have_received(:new).with(
account: account,
params: params.merge(type: :agent)
)
expect(agent_summary_builder).to have_received(:build)
expect(response).to have_http_status(:success)
@@ -96,7 +99,10 @@ RSpec.describe 'Summary Reports API', type: :request do
headers: admin.create_new_auth_token,
as: :json
expect(V2::Reports::InboxSummaryBuilder).to have_received(:new).with(account: account, params: params)
expect(V2::Reports::InboxSummaryBuilder).to have_received(:new).with(
account: account,
params: params.merge(type: :inbox)
)
expect(inbox_summary_builder).to have_received(:build)
expect(response).to have_http_status(:success)
@@ -147,7 +153,10 @@ RSpec.describe 'Summary Reports API', type: :request do
headers: admin.create_new_auth_token,
as: :json
expect(V2::Reports::TeamSummaryBuilder).to have_received(:new).with(account: account, params: params)
expect(V2::Reports::TeamSummaryBuilder).to have_received(:new).with(
account: account,
params: params.merge(type: :team)
)
expect(team_summary_builder).to have_received(:build)
expect(response).to have_http_status(:success)