OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
add tests and add stub for upcoming feature tests
Klaus Zanders1dcea35945ca
diff --git a/modules/costs/app/controllers/time_entries_controller.rb b/modules/costs/app/controllers/time_entries_controller.rbindex cfb0d802f3d..d2b9de27bcf 100644--- a/modules/costs/app/controllers/time_entries_controller.rb+++ b/modules/costs/app/controllers/time_entries_controller.rb@@ -148,13 +148,13 @@ class TimeEntriesController < ApplicationControllerendend- rescue ActiveRecord::NotFound+ rescue ActiveRecord::RecordNotFounddeny_access(not_found: true)enddef load_and_authorize_optional_work_packageif params[:work_package_id].present?- @work_package = WorkPackage.visible.find_by(id: params[:work_package_id])+ @work_package = WorkPackage.visible.find(params[:work_package_id])@project = @work_package.projectif !User.current.allowed_in_project?(:log_time, @project) &&@@ -162,14 +162,14 @@ class TimeEntriesController < ApplicationControllerdeny_accessendend- rescue ActiveRecord::NotFound+ rescue ActiveRecord::RecordNotFounddeny_access(not_found: true)enddef load_or_build_and_authorize_time_entry@time_entry = if params[:id]TimeEntry.visible.find_by(id: params[:id]).tap do |entry|- if entry && !TimeEntries::UpdateContract.new(entry, current_user).user_allowed_to_update?+ if entry.blank? || !TimeEntries::UpdateContract.new(entry, current_user).user_allowed_to_update?deny_access(not_found: true)endenddiff --git a/modules/costs/spec/controllers/time_entries_controller_spec.rb b/modules/costs/spec/controllers/time_entries_controller_spec.rbnew file mode 100644index 00000000000..38f0727c2c1--- /dev/null+++ b/modules/costs/spec/controllers/time_entries_controller_spec.rb@@ -0,0 +1,292 @@+# 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 File.expand_path("#{File.dirname(__FILE__)}/../spec_helper.rb")++RSpec.describe TimeEntriesController do+ let(:user) { create(:user) }+ let(:other_user) { create(:user) }++ let!(:project1) { create(:project) }+ let!(:work_package1) { create(:work_package, project: project1) }+ let!(:work_package2) { create(:work_package, project: project1) }++ let(:project2) { create(:project) }+ let(:work_package3) { create(:work_package, project: project2) }+ let(:work_package4) { create(:work_package, project: project2) }++ before do+ login_as user+ end++ describe "#dialog" do+ describe "authorization checks" do+ context "when opening dialog on an existing time entry" do+ let!(:time_entry1) { create(:time_entry, user: user, work_package: work_package1) }+ let!(:time_entry2) { create(:time_entry, user: other_user, work_package: work_package1) }++ context "and the user has the edit_own_time_entries permission on the work package" do+ before do+ role = create(:work_package_role, permissions: %i[view_own_time_entries edit_own_time_entries])+ create(:work_package_member, user: user, entity: work_package1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { id: time_entry1.id }, format: :turbo_stream+ expect(response).to be_successful+ expect(assigns(:time_entry)).to eq time_entry1+ end++ it "does not allow opening the dialog when the time entry belongs to another user" do+ get :dialog, params: { id: time_entry2.id }, format: :turbo_stream+ expect(response).to be_not_found+ end+ end++ context "and the user has the edit_own_time_entries permission on the project" do+ before do+ role = create(:project_role, permissions: %i[view_own_time_entries edit_own_time_entries])+ create(:member, user: user, project: project1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { id: time_entry1.id }, format: :turbo_stream+ expect(response).to be_successful+ expect(assigns(:time_entry)).to eq time_entry1+ end++ it "does not allow opening the dialog when the time entry belongs to another user" do+ get :dialog, params: { id: time_entry2.id }, format: :turbo_stream+ expect(response).to be_not_found+ end+ end++ context "and the user has the edit_time_entries permission on the project" do+ before do+ role = create(:project_role, permissions: %i[view_time_entries edit_time_entries])+ create(:member, user: user, project: project1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { id: time_entry1.id }, format: :turbo_stream+ expect(response).to be_successful+ expect(assigns(:time_entry)).to eq time_entry1+ end++ it "allows opening the dialog when the time entry belongs to another user" do+ get :dialog, params: { id: time_entry2.id }, format: :turbo_stream+ expect(response).to be_successful+ expect(assigns(:time_entry)).to eq time_entry2+ end+ end++ context "and the user has no edit permissions" do+ it "renders a 404" do+ get :dialog, params: { id: time_entry1.id }, format: :turbo_stream++ expect(response).to be_not_found+ end+ end+ end++ context "when opening dialog on a project" do+ context "and the user has the log_own_time permission on a work package within the project" do+ before do+ role = create(:work_package_role, permissions: %i[log_own_time])+ create(:work_package_member, user: user, entity: work_package1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { project_id: project1 }, format: :turbo_stream+ expect(response).to be_successful++ time_entry = assigns(:time_entry)+ expect(time_entry).to be_new_record+ expect(time_entry.project).to eq(project1)+ end++ it "does not allow to open the dialog for another project" do+ get :dialog, params: { project_id: project2 }, format: :turbo_stream+ expect(response).to be_not_found+ end+ end++ context "and the user has the log_own_time permission on the project" do+ before do+ role = create(:project_role, permissions: %i[view_project log_own_time])+ create(:member, user: user, project: project1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { project_id: project1 }, format: :turbo_stream+ expect(response).to be_successful++ time_entry = assigns(:time_entry)+ expect(time_entry).to be_new_record+ expect(time_entry.project).to eq(project1)+ end+ end++ context "and the user has the log_time permission on the project" do+ before do+ role = create(:project_role, permissions: %i[view_project log_time])+ create(:member, user: user, project: project1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { project_id: project1 }, format: :turbo_stream+ expect(response).to be_successful++ time_entry = assigns(:time_entry)+ expect(time_entry).to be_new_record+ expect(time_entry.project).to eq(project1)+ end+ end++ context "and the user has no log permissions, but can view the project" do+ before do+ role = create(:project_role, permissions: %i[view_project])+ create(:member, user: user, project: project1, roles: [role])+ end++ it "does not allow to open the dialog" do+ get :dialog, params: { project_id: project1 }, format: :turbo_stream+ expect(response).to be_forbidden+ end+ end+ end++ context "when opening a dialog on a work package" do+ context "and the user has the log_own_time permission on the work package" do+ before do+ role = create(:work_package_role, permissions: %i[view_work_packages log_own_time])+ create(:work_package_member, user: user, entity: work_package1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { work_package_id: work_package1.id }, format: :turbo_stream+ expect(response).to be_successful++ time_entry = assigns(:time_entry)+ expect(time_entry).to be_new_record+ expect(time_entry.project).to eq(project1)+ expect(time_entry.work_package).to eq(work_package1)+ end++ it "does not allow to open the dialog for another work package" do+ get :dialog, params: { work_package_id: work_package2.id }, format: :turbo_stream+ expect(response).to be_not_found+ end+ end++ context "and the user has the log_own_time permission on the project" do+ before do+ role = create(:project_role, permissions: %i[view_work_packages log_own_time])+ create(:member, user: user, project: project1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { work_package_id: work_package1.id }, format: :turbo_stream+ expect(response).to be_successful+ end++ it "does not allow to open the dialog for work package from another project" do+ get :dialog, params: { work_package_id: work_package3.id }, format: :turbo_stream+ expect(response).to be_not_found+ end+ end++ context "and the user has the log_time permission on the project" do+ before do+ role = create(:project_role, permissions: %i[view_work_packages log_time])+ create(:member, user: user, project: project1, roles: [role])+ end++ it "allows the user to open the dialog" do+ get :dialog, params: { work_package_id: work_package1.id }, format: :turbo_stream+ expect(response).to be_successful+ end++ it "does not allow to open the dialog for work package from another project" do+ get :dialog, params: { work_package_id: work_package3.id }, format: :turbo_stream+ expect(response).to be_not_found+ end+ end++ context "and the user has no log permission but can view the work package" do+ before do+ role = create(:work_package_role, permissions: %i[view_work_packages])+ create(:work_package_member, user: user, entity: work_package1, roles: [role])+ end++ it "does not allow to open the dialog" do+ get :dialog, params: { work_package_id: work_package1.id }, format: :turbo_stream+ expect(response).to be_forbidden+ end+ end+ end+ end+ end++ describe "#user_tz_caption" do+ let(:user) { create(:admin) } # so we don't have to mock permissions'++ render_views++ context "when current user and other user have different timezones" do+ before do+ user.pref.time_zone = "Europe/Berlin"+ other_user.pref.time_zone = "Asia/Tokyo"+ other_user.save+ end++ it "returns a notice about the different timezone" do+ get :user_tz_caption, params: { user_id: other_user.id }, format: :turbo_stream+ expect(response.body).to include(+ "caption=\"#{I18n.t('notice_different_time_zones',+ tz: described_class.helpers.friendly_timezone_name(other_user.time_zone))}\""+ )+ end+ end++ context "when current user and other user have the same timezone" do+ before do+ user.pref.time_zone = "Europe/Berlin"+ other_user.pref.time_zone = "Europe/Berlin"+ other_user.save+ end++ it "returns no notice" do+ get :user_tz_caption, params: { user_id: other_user.id }, format: :turbo_stream+ expect(response.body).to include("caption=\"\"")+ end+ end+ end+enddiff --git a/modules/costs/spec/features/time_entries_spec.rb b/modules/costs/spec/features/time_entries_spec.rbindex a754a7dc51f..1ea9a23c973 100644--- a/modules/costs/spec/features/time_entries_spec.rb+++ b/modules/costs/spec/features/time_entries_spec.rb@@ -28,7 +28,7 @@require_relative "../spec_helper"-RSpec.describe "Work Package table cost entries", :js do+RSpec.describe "Work Package table time entries", :js doshared_let(:project) { create(:project_with_types) }shared_let(:user) { create(:admin) }diff --git a/modules/costs/spec/features/time_entry_dialog_spec.rb b/modules/costs/spec/features/time_entry_dialog_spec.rbnew file mode 100644index 00000000000..db8a50da54e--- /dev/null+++ b/modules/costs/spec/features/time_entry_dialog_spec.rb@@ -0,0 +1,76 @@+# 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 File.expand_path("#{File.dirname(__FILE__)}/../spec_helper.rb")++RSpec.describe "time entry dialog", :js, with_flag: :track_start_and_end_times_for_time_entries do+ shared_let(:project) { create(:project_with_types) }++ shared_let(:work_package_a) { create(:work_package, subject: "WP A", project:) }+ shared_let(:work_package_b) { create(:work_package, subject: "WP B", project:) }++ let(:wp_view_a) { Pages::FullWorkPackage.new(work_package_a) }+ let(:wp_view_b) { Pages::FullWorkPackage.new(work_package_b) }+ let(:time_logging_modal) { Components::TimeLoggingModal.new }++ let(:user) { create(:user, member_with_permissions: { project => permissions }) }++ before do+ login_as user+ end++ context "when user has permission to log own time" do+ let(:permissions) { %i[log_own_time view_own_time_entries view_work_packages] }++ context "when start and end time is not allowed", with_settings: { allow_tracking_start_and_end_times: false } do+ end++ context "when start and end time is allowed", with_settings: { allow_tracking_start_and_end_times: true } do+ end++ context "when start and end time is enforced",+ with_settings: { allow_tracking_start_and_end_times: true, enforce_tracking_start_and_end_times: true } do+ end+ end++ context "when user has permission to log time for others" do+ let!(:other_user) { create(:user, member_with_permissions: { project => [:view_project] }) }+ let(:permissions) { %i[log_time view_time_entries view_work_packages] }+ end++ context "when user has permission to edit own time entries" do+ let(:permissions) { %i[log_own_time view_own_time_entries edit_own_time_entries view_work_packages] }+ end++ context "when user has permission to edit time entries for others" do+ let!(:other_user) { create(:user, member_with_permissions: { project => [:view_project] }) }+ let(:permissions) { %i[log_time view_time_entries edit_time_entries view_work_packages] }+ end+end