2022-12-23 15:41:10 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2023-06-07 09:26:58 +08:00
|
|
|
describe "Uploading files in chat messages", type: :system do
|
2022-12-23 15:41:10 +08:00
|
|
|
fab!(:current_user) { Fabricate(:user) }
|
|
|
|
fab!(:channel_1) { Fabricate(:chat_channel) }
|
|
|
|
fab!(:message_1) { Fabricate(:chat_message, chat_channel: channel_1) }
|
|
|
|
|
|
|
|
let(:chat) { PageObjects::Pages::Chat.new }
|
2023-05-31 00:37:30 +08:00
|
|
|
let(:channel_page) { PageObjects::Pages::ChatChannel.new }
|
2022-12-23 15:41:10 +08:00
|
|
|
|
|
|
|
before { chat_system_bootstrap }
|
|
|
|
|
|
|
|
context "when uploading to a new message" do
|
|
|
|
before do
|
2023-12-06 14:59:18 +08:00
|
|
|
Jobs.run_immediately!
|
2022-12-23 15:41:10 +08:00
|
|
|
channel_1.add(current_user)
|
|
|
|
sign_in(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows uploading a single file" do
|
|
|
|
chat.visit_channel(channel_1)
|
|
|
|
file_path = file_from_fixtures("logo.png", "images").path
|
2023-09-26 10:20:21 +08:00
|
|
|
|
2022-12-23 15:41:10 +08:00
|
|
|
attach_file(file_path) do
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.open_action_menu
|
|
|
|
channel_page.click_action_button("chat-upload-btn")
|
2022-12-23 15:41:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(page).to have_css(".chat-composer-upload .preview .preview-img")
|
|
|
|
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.send_message("upload testing")
|
2022-12-23 15:41:10 +08:00
|
|
|
|
2023-12-06 18:02:55 +08:00
|
|
|
expect(page).to have_no_css(".chat-composer-upload")
|
2023-09-26 10:20:21 +08:00
|
|
|
|
|
|
|
expect(channel_page.messages).to have_message(
|
|
|
|
text: "upload testing\n#{File.basename(file_path)}",
|
|
|
|
persisted: true,
|
|
|
|
)
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
expect(Chat::Message.last.uploads.count).to eq(1)
|
2022-12-23 15:41:10 +08:00
|
|
|
end
|
|
|
|
|
2023-12-06 19:08:56 +08:00
|
|
|
xit "adds a thumbnail for large images" do
|
2023-12-06 14:59:18 +08:00
|
|
|
SiteSetting.create_thumbnails = true
|
|
|
|
|
|
|
|
chat.visit_channel(channel_1)
|
|
|
|
file_path = file_from_fixtures("huge.jpg", "images").path
|
|
|
|
|
|
|
|
attach_file(file_path) do
|
|
|
|
channel_page.open_action_menu
|
|
|
|
channel_page.click_action_button("chat-upload-btn")
|
|
|
|
end
|
|
|
|
|
|
|
|
expect { channel_page.send_message }.to change { Chat::Message.count }.by(1)
|
|
|
|
|
|
|
|
expect(channel_page).to have_no_css(".chat-composer-upload")
|
|
|
|
|
|
|
|
message = Chat::Message.last
|
|
|
|
|
|
|
|
try_until_success(timeout: 5) { expect(message.uploads.first.thumbnail).to be_present }
|
|
|
|
|
|
|
|
upload = message.uploads.first
|
|
|
|
|
|
|
|
# image has src attribute with thumbnail url
|
|
|
|
expect(channel_page).to have_css(".chat-uploads img[src$='#{upload.thumbnail.url}']")
|
|
|
|
|
|
|
|
# image has data-large-src with original image src
|
|
|
|
expect(channel_page).to have_css(".chat-uploads img[data-large-src$='#{upload.url}']")
|
|
|
|
end
|
|
|
|
|
2023-11-02 19:22:59 +08:00
|
|
|
it "adds dominant color attribute to images" do
|
|
|
|
chat.visit_channel(channel_1)
|
|
|
|
file_path = file_from_fixtures("logo.png", "images").path
|
|
|
|
|
|
|
|
attach_file(file_path) do
|
|
|
|
channel_page.open_action_menu
|
|
|
|
channel_page.click_action_button("chat-upload-btn")
|
|
|
|
end
|
|
|
|
|
|
|
|
channel_page.click_send_message
|
|
|
|
|
|
|
|
expect(channel_page.messages).to have_css(".chat-img-upload[data-dominant-color]", count: 1)
|
|
|
|
end
|
|
|
|
|
2023-01-04 22:26:52 +08:00
|
|
|
it "allows uploading multiple files" do
|
|
|
|
chat.visit_channel(channel_1)
|
2023-03-03 20:09:25 +08:00
|
|
|
|
2023-01-04 22:26:52 +08:00
|
|
|
file_path_1 = file_from_fixtures("logo.png", "images").path
|
|
|
|
file_path_2 = file_from_fixtures("logo.jpg", "images").path
|
2023-03-09 16:17:54 +08:00
|
|
|
attach_file([file_path_1, file_path_2]) do
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.open_action_menu
|
|
|
|
channel_page.click_action_button("chat-upload-btn")
|
2023-01-04 22:26:52 +08:00
|
|
|
end
|
2023-03-03 20:09:25 +08:00
|
|
|
|
2023-01-04 22:26:52 +08:00
|
|
|
expect(page).to have_css(".chat-composer-upload .preview .preview-img", count: 2)
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.send_message("upload testing")
|
2023-01-04 22:26:52 +08:00
|
|
|
|
2023-12-06 18:02:55 +08:00
|
|
|
expect(page).to have_no_css(".chat-composer-upload")
|
2023-09-26 10:20:21 +08:00
|
|
|
expect(channel_page.messages).to have_message(
|
|
|
|
text: "upload testing\n#{I18n.t("js.chat.uploaded_files", count: 2)}",
|
|
|
|
persisted: true,
|
2024-07-10 22:11:50 +08:00
|
|
|
wait: 5,
|
2023-09-26 10:20:21 +08:00
|
|
|
)
|
2024-07-10 22:11:50 +08:00
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
expect(Chat::Message.last.uploads.count).to eq(2)
|
2023-01-04 22:26:52 +08:00
|
|
|
end
|
|
|
|
|
2023-09-26 10:20:21 +08:00
|
|
|
it "allows uploading a huge image file with preprocessing" do
|
|
|
|
skip("This test is flaky on CI") if ENV["CI"]
|
|
|
|
|
2022-12-23 15:41:10 +08:00
|
|
|
SiteSetting.composer_media_optimization_image_bytes_optimization_threshold = 200.kilobytes
|
|
|
|
chat.visit_channel(channel_1)
|
|
|
|
file_path = file_from_fixtures("huge.jpg", "images").path
|
2023-09-26 10:20:21 +08:00
|
|
|
|
2022-12-23 15:41:10 +08:00
|
|
|
attach_file(file_path) do
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.open_action_menu
|
|
|
|
channel_page.click_action_button("chat-upload-btn")
|
2022-12-23 15:41:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(find(".chat-composer-upload")).to have_content("Processing")
|
|
|
|
|
|
|
|
# image processing clientside is slow! here we are waiting for processing
|
|
|
|
# to complete then the upload to complete as well
|
2023-05-10 06:33:40 +08:00
|
|
|
expect(page).to have_css(".chat-composer-upload .preview .preview-img", wait: 25)
|
2022-12-23 15:41:10 +08:00
|
|
|
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.send_message("upload testing")
|
2022-12-23 15:41:10 +08:00
|
|
|
|
2023-12-06 18:02:55 +08:00
|
|
|
expect(page).to have_no_css(".chat-composer-upload")
|
2023-09-26 10:20:21 +08:00
|
|
|
|
|
|
|
expect(channel_page.messages).to have_message(
|
|
|
|
text: "upload testing\n#{File.basename(file_path)}",
|
|
|
|
persisted: true,
|
|
|
|
)
|
|
|
|
|
2023-03-17 21:24:38 +08:00
|
|
|
expect(Chat::Message.last.uploads.count).to eq(1)
|
2022-12-23 15:41:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-28 22:55:01 +08:00
|
|
|
context "when editing a message with uploads" do
|
|
|
|
fab!(:message_2) { Fabricate(:chat_message, user: current_user, chat_channel: channel_1) }
|
|
|
|
fab!(:upload_reference) do
|
|
|
|
Fabricate(
|
|
|
|
:upload_reference,
|
|
|
|
target: message_2,
|
|
|
|
upload: Fabricate(:upload, user: current_user),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
channel_1.add(current_user)
|
|
|
|
sign_in(current_user)
|
|
|
|
|
|
|
|
file = file_from_fixtures("logo-dev.png", "images")
|
|
|
|
url = Discourse.store.store_upload(file, upload_reference.upload)
|
|
|
|
upload_reference.upload.update!(url: url, sha1: Upload.generate_digest(file))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "allows deleting uploads" do
|
|
|
|
chat.visit_channel(channel_1)
|
2023-10-04 22:14:37 +08:00
|
|
|
channel_page.messages.edit(message_2)
|
2023-04-25 16:23:03 +08:00
|
|
|
find(".chat-composer-upload").hover
|
|
|
|
find(".chat-composer-upload__remove-btn").click
|
2023-10-23 16:40:30 +08:00
|
|
|
expect(channel_page.message_by_id(message_2.id)).to have_no_css(".chat-uploads")
|
|
|
|
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.click_send_message
|
2023-10-23 16:40:30 +08:00
|
|
|
try_until_success(timeout: 5) { expect(message_2.reload.upload_ids).to be_empty }
|
2023-03-28 22:55:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "allows adding more uploads" do
|
|
|
|
chat.visit_channel(channel_1)
|
2023-10-04 22:14:37 +08:00
|
|
|
channel_page.messages.edit(message_2)
|
2023-03-28 22:55:01 +08:00
|
|
|
|
|
|
|
file_path = file_from_fixtures("logo.png", "images").path
|
|
|
|
attach_file(file_path) do
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.open_action_menu
|
|
|
|
channel_page.click_action_button("chat-upload-btn")
|
2023-03-28 22:55:01 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(page).to have_css(".chat-composer-upload .preview .preview-img", count: 2)
|
|
|
|
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.click_send_message
|
2023-03-28 22:55:01 +08:00
|
|
|
|
2023-12-06 18:02:55 +08:00
|
|
|
expect(page).to have_no_css(".chat-composer-upload")
|
2023-03-28 22:55:01 +08:00
|
|
|
expect(page).to have_css(".chat-img-upload", count: 2)
|
2023-04-25 16:23:03 +08:00
|
|
|
|
|
|
|
try_until_success(timeout: 5) { expect(message_2.reload.uploads.count).to eq(2) }
|
2023-03-28 22:55:01 +08:00
|
|
|
end
|
|
|
|
end
|
2022-12-23 15:41:10 +08:00
|
|
|
|
|
|
|
context "when uploads are not allowed" do
|
|
|
|
fab!(:user_2) { Fabricate(:user) }
|
|
|
|
fab!(:direct_message_channel_1) do
|
|
|
|
Fabricate(:direct_message_channel, users: [current_user, user_2])
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.chat_allow_uploads = false
|
|
|
|
channel_1.add(current_user)
|
|
|
|
sign_in(current_user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not show the action button for uploading files in public channels" do
|
|
|
|
chat.visit_channel(channel_1)
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.open_action_menu
|
2023-12-06 18:02:55 +08:00
|
|
|
expect(page).to have_no_css(".chat-upload-btn")
|
2022-12-23 15:41:10 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "does not show the action button for uploading files in direct message channels" do
|
|
|
|
chat.visit_channel(direct_message_channel_1)
|
2023-05-31 00:37:30 +08:00
|
|
|
channel_page.open_action_menu
|
2023-12-06 18:02:55 +08:00
|
|
|
expect(page).to have_no_css(".chat-upload-btn")
|
2022-12-23 15:41:10 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|