Files
2026-04-15 14:18:42 +02:00

137 lines
4.7 KiB
Ruby

# frozen_string_literal: true
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++
require "mini_magick"
RSpec.shared_examples "avatar management" do
let(:image_base_path) { File.expand_path(File.dirname(__FILE__) + "/../fixtures/") }
let(:enable_gravatars) { false }
let(:enable_local_avatars) { false }
let(:plugin_settings) do
{
"enable_gravatars" => enable_gravatars,
"enable_local_avatars" => enable_local_avatars
}
end
before do
allow(Setting)
.to receive(:plugin_openproject_avatars)
.and_return(plugin_settings)
end
describe "only gravatars enabled" do
let(:enable_gravatars) { true }
it "shows the gravatar avatar" do
visit avatar_management_path
expect(page).to have_css(".form--fieldset-legend", text: "GRAVATAR")
expect(page).to have_css(".avatars--current-gravatar")
# Local not rendered
expect(page).to have_no_css(".form--fieldset-legend", text: "CUSTOM AVATAR")
expect(page).to have_no_css(".avatars--current-local-avatar", text: "none")
end
end
describe "only local avatars enabled" do
let(:enable_local_avatars) { true }
it "can upload a new image" do
visit avatar_management_path
expect(page).to have_css(".form--fieldset-legend", text: "CUSTOM AVATAR")
expect(page).to have_css(".avatars--current-local-avatar", text: "none")
# Gravatars not rendered
expect(page).to have_no_css(".form--fieldset-legend", text: "GRAVATAR")
# Attach a new invalid image
find_by_id("avatar_file_input").set UploadedFile.load_from(File.join(image_base_path, "invalid.txt")).path
# Expect error
expect(page).to have_css(".form--label.-error")
expect(page).to have_css(".avatars--error-pane", text: "Allowed formats are jpg, png, gif")
# Attach new image
visit avatar_management_path
expect(page).to have_css(".avatars--current-local-avatar", text: "none")
find_by_id("avatar_file_input").set UploadedFile.load_from(File.join(image_base_path, "too_big.jpg")).path
# Expect not error, since ng-file-upload resizes the image
expect(page).to have_no_css(".form--label.-error")
expect(page).to have_no_css(".avatars--error-pane span")
# Expect preview
expect(page).to have_css(".preview img")
# Click button
click_on "Update"
# Expect avatar rendered
expect(page).to have_css(".form--fieldset-legend", text: "CUSTOM AVATAR")
avatar_tag = find(".avatars--current-local-avatar img")
expect(avatar_tag[:src]).to include user_avatar_path(target_user)
# Expect the avatar to be resized
avatar_path = target_user.local_avatar_attachment.file.path
content_type = OpenProject::ContentTypeDetector.new(avatar_path).detect
image = MiniMagick::Image.open(avatar_path)
expect(image.dimensions).to eq [128, 128]
expect(content_type).to eq("image/jpeg")
# Delete the avatar
accept_alert do
find(".avatars--local-avatar-delete-link").click
end
expect(page).to have_css(".avatars--current-local-avatar", text: "none", wait: 20)
end
end
describe "both local avatars enabled" do
let(:enable_gravatars) { true }
let(:enable_local_avatars) { true }
it "renders both sections" do
visit avatar_management_path
# Gravatar
expect(page).to have_css(".form--fieldset-legend", text: "GRAVATAR")
expect(page).to have_css(".avatars--current-gravatar")
# Local
expect(page).to have_css(".form--fieldset-legend", text: "CUSTOM AVATAR")
expect(page).to have_css(".avatars--current-local-avatar", text: "none")
end
end
end