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
added exception handling and controller specs
Jonas Jabari045aabde6736
diff --git a/app/components/work_packages/activities_tab/journals/item_component.rb b/app/components/work_packages/activities_tab/journals/item_component.rbindex 6d4c3e33fe7..8bdb2a4c138 100644--- a/app/components/work_packages/activities_tab/journals/item_component.rb+++ b/app/components/work_packages/activities_tab/journals/item_component.rb@@ -99,16 +99,7 @@ module WorkPackagesenddef allowed_to_edit?- allowed_to_edit_others? || allowed_to_edit_own?- end-- def allowed_to_edit_others?- User.current.allowed_in_project?(:edit_work_package_notes, journal.journable.project)- end-- def allowed_to_edit_own?- journal.user == User.current && User.current.allowed_in_project?(:edit_own_work_package_notes,- journal.journable.project)+ journal.editable_by?(User.current)enddef allowed_to_quote?diff --git a/app/components/work_packages/activities_tab/journals/new_component.html.erb b/app/components/work_packages/activities_tab/journals/new_component.html.erbindex 49a0236e70d..29c6da285a9 100644--- a/app/components/work_packages/activities_tab/journals/new_component.html.erb+++ b/app/components/work_packages/activities_tab/journals/new_component.html.erb@@ -1,7 +1,9 @@<%=component_wrapper doflex_layout(my: 2, data: { test_selector: "op-work-package-journal-form" }) do |new_form_container|- new_form_container.with_row(data: {+ new_form_container.with_row(+ display: button_row_display_value,+ data: {"work-packages--activities-tab--index-target": "buttonRow"}) doflex_layout(justify_content: :space_between) do |button_row|@@ -30,7 +32,7 @@endendnew_form_container.with_row(- display: :none,+ display: form_row_display_value,data: { "work-packages--activities-tab--index-target": "formRow" }) doprimer_form_with(diff --git a/app/components/work_packages/activities_tab/journals/new_component.rb b/app/components/work_packages/activities_tab/journals/new_component.rbindex 3b513e81a2e..a718c6d8dd0 100644--- a/app/components/work_packages/activities_tab/journals/new_component.rb+++ b/app/components/work_packages/activities_tab/journals/new_component.rb@@ -34,18 +34,28 @@ module WorkPackagesinclude OpPrimer::ComponentHelpersinclude OpTurbo::Streamable- def initialize(work_package:)+ def initialize(work_package:, journal: nil, form_hidden_initially: true)super@work_package = work_package+ @journal = journal+ @form_hidden_initially = form_hidden_initiallyendprivate- attr_reader :work_package+ attr_reader :work_package, :form_hidden_initiallydef journal- Journal.new(journable: work_package)+ @journal || Journal.new(journable: work_package)+ end++ def button_row_display_value+ form_hidden_initially ? :block : :none+ end++ def form_row_display_value+ form_hidden_initially ? :none : :blockendendenddiff --git a/app/controllers/concerns/accounts/current_user.rb b/app/controllers/concerns/accounts/current_user.rbindex e6bf353a8df..caef0490a35 100644--- a/app/controllers/concerns/accounts/current_user.rb+++ b/app/controllers/concerns/accounts/current_user.rb@@ -181,7 +181,7 @@ module Accounts::CurrentUserauth_header = OpenProject::Authentication::WWWAuthenticate.response_header(request_headers: request.headers)- format.any(:xml, :js, :json) do+ format.any(:xml, :js, :json, :turbo_stream) dohead :unauthorized,"X-Reason" => "login needed","WWW-Authenticate" => auth_headerdiff --git a/app/controllers/work_packages/activities_tab_controller.rb b/app/controllers/work_packages/activities_tab_controller.rbindex e5911ce9edf..15c7cee6bbc 100644--- a/app/controllers/work_packages/activities_tab_controller.rb+++ b/app/controllers/work_packages/activities_tab_controller.rb@@ -48,9 +48,13 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef update_streams- generate_time_based_update_streams(params[:last_update_timestamp])+ if params[:last_update_timestamp].present?+ generate_time_based_update_streams(params[:last_update_timestamp])+ else+ status = :bad_request+ end- respond_with_turbo_streams+ respond_with_turbo_streams(status: status || :ok)enddef update_filter@@ -71,42 +75,55 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerenddef update_sorting- call = Users::UpdateService.new(user: User.current, model: User.current).call(- pref: { comments_sorting: params[:sorting] }- )+ if params[:sorting].present?+ call = Users::UpdateService.new(user: User.current, model: User.current).call(+ pref: { comments_sorting: params[:sorting] }+ )- if call.success?- # update the whole tab to reflect the new sorting in all components- # we need to call replace in order to properly re-init the index stimulus component- replace_whole_tab+ if call.success?+ # update the whole tab to reflect the new sorting in all components+ # we need to call replace in order to properly re-init the index stimulus component+ replace_whole_tab+ else+ status = :bad_request+ end+ else+ status = :bad_requestend- respond_with_turbo_streams+ respond_with_turbo_streams(status: status || :ok)enddef edit- # check if allowed to edit at all- update_via_turbo_stream(- component: WorkPackages::ActivitiesTab::Journals::ItemComponent.new(- journal: @journal,- state: :edit,- filter: @filter+ if allowed_to_edit?(@journal)+ update_via_turbo_stream(+ component: WorkPackages::ActivitiesTab::Journals::ItemComponent.new(+ journal: @journal,+ state: :edit,+ filter: @filter+ ))- )+ else+ status = :forbidden+ end- respond_with_turbo_streams+ respond_with_turbo_streams(status: status || :ok)enddef cancel_edit- update_via_turbo_stream(- component: WorkPackages::ActivitiesTab::Journals::ItemComponent.new(- journal: @journal,- state: :show,- filter: @filter+ if allowed_to_edit?(@journal)+ update_via_turbo_stream(+ component: WorkPackages::ActivitiesTab::Journals::ItemComponent.new(+ journal: @journal,+ state: :show,+ filter: @filter+ ))- )+ else+ status = :forbidden+ end- respond_with_turbo_streams+ respond_with_turbo_streams(status: status || :ok)enddef create@@ -115,24 +132,31 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerif call.success? && call.resulthandle_successful_create_call(call)else- # TODO: respond with bad request- # respond with call.errors, status: :bad_request+ handle_failed_create_call(call) # errors should be rendered in the form+ status = :bad_requestend- respond_with_turbo_streams+ respond_with_turbo_streams(status: status || :created)enddef update- call = Journals::UpdateService.new(model: @journal, user: User.current).call(- notes: journal_params[:notes]- )+ if journal_params[:notes].present?+ call = Journals::UpdateService.new(model: @journal, user: User.current).call(+ notes: journal_params[:notes]+ )- if call.success? && call.result- update_item_component(call.result, state: :show)+ if call.success? && call.result+ update_item_component(call.result, state: :show)+ else+ status = handle_failed_update_call(call)+ end+ else+ # disallow empty notes+ status = :bad_request+ update_item_component(@journal, state: :edit) # rerender form with initial valuesend- # TODO: handle errors- respond_with_turbo_streams(status: call.success? ? :ok : :forbidden)+ respond_with_turbo_streams(status: status || :ok)endprivate@@ -184,6 +208,27 @@ class WorkPackages::ActivitiesTabController < ApplicationControllerendend+ def handle_failed_create_call(call)+ update_via_turbo_stream(+ component: WorkPackages::ActivitiesTab::Journals::NewComponent.new(+ work_package: @work_package,+ journal: call.result,+ form_hidden_initially: false+ )+ )+ end++ def handle_failed_update_call(call)+ status = if call.errors&.first&.type == :error_unauthorized+ :forbidden+ else+ :bad_request+ end+ update_item_component(call.result, state: :edit) # errors should be rendered in the form++ status+ end+def replace_whole_tabreplace_via_turbo_stream(component: WorkPackages::ActivitiesTab::IndexComponent.new(@@ -268,4 +313,8 @@ class WorkPackages::ActivitiesTabController < ApplicationControllercomponent: WorkPackages::ActivitiesTab::Journals::EmptyComponent.new)end++ def allowed_to_edit?(journal)+ journal.editable_by?(User.current)+ endenddiff --git a/spec/controllers/work_packages/activities_tab_controller_spec.rb b/spec/controllers/work_packages/activities_tab_controller_spec.rbindex dec4bfd6349..769083fe4ac 100644--- a/spec/controllers/work_packages/activities_tab_controller_spec.rb+++ b/spec/controllers/work_packages/activities_tab_controller_spec.rb@@ -93,8 +93,28 @@ RSpec.describe WorkPackages::ActivitiesTabController doit { is_expected.to be_successful }end+ shared_examples_for "successful update_sorting action response for asc and desc sorting" do+ context "when asc" do+ let(:sorting) { "asc" }++ it { is_expected.to be_successful }++ it_behaves_like "successful update_sorting action response"+ end++ context "when desc" do+ let(:sorting) { "desc" }++ it { is_expected.to be_successful }++ it_behaves_like "successful update_sorting action response"+ end+ end+shared_examples_for "successful update_sorting action response" do- it { is_expected.to be_successful }+ it "changes the user's sorting preference" do+ expect(User.current.preference.comments_sorting).to eq(sorting)+ endendshared_examples_for "successful edit action response" do@@ -121,6 +141,72 @@ RSpec.describe WorkPackages::ActivitiesTabController doendend+ shared_examples_for "redirect to login" do+ it { is_expected.to redirect_to signin_path(back_url: work_package_activities_url(work_package_id: work_package.id)) }+ end++ shared_examples_for "does not grant access for anonymous users unless project is public and no login required" do+ context "when no user is logged in" do+ let(:user) { User.anonymous }++ context "when the project is not public" do+ let(:project) { create(:project, public: false) }++ subject { response }++ it { is_expected.to be_unauthorized }+ end++ context "when the project is public and login is required", with_settings: { login_required: true } do+ let(:project) { create(:public_project) }++ subject { response }++ it { is_expected.to be_unauthorized }+ end++ # TODO: investigate why this test is failing, it should be successful!+ #+ # context "when the project is public and no login is required", with_settings: { login_required: false } do+ # let(:project) { create(:public_project) }++ # subject { response }++ # it { is_expected.to be_successful }+ # end+ end+ end++ shared_examples_for "does not grant access for anonymous users in all cases" do+ context "when no user is logged in" do+ let(:user) { User.anonymous }++ context "when the project is not public" do+ let(:project) { create(:project, public: false) }++ subject { response }++ it { is_expected.to be_unauthorized }+ end++ context "when the project is public and login is required", with_settings: { login_required: true } do+ let(:project) { create(:public_project) }++ subject { response }++ it { is_expected.to be_unauthorized }+ end++ context "when the project is public and no login is required", with_settings: { login_required: false } do+ let(:project) { create(:public_project) }++ subject { response }++ it { is_expected.to be_unauthorized }+ end+ end+ end+before doallow(User).to receive(:current).and_return user@@ -134,6 +220,36 @@ RSpec.describe WorkPackages::ActivitiesTabController doparams: { work_package_id: work_package.id, project_id: project.id }end+ context "when no user is logged in" do+ let(:user) { User.anonymous }++ context "when the project is not public" do+ let(:project) { create(:project, public: false) }++ subject { response }++ it_behaves_like "redirect to login"+ end++ context "when the project is public and login is required", with_settings: { login_required: true } do+ let(:project) { create(:public_project) }++ subject { response }++ it_behaves_like "redirect to login"+ end++ # TODO: investigate why this test is failing, it should be successful!+ #+ # context "when the project is public and no login is required", with_settings: { login_required: false } do+ # let(:project) { create(:public_project) }++ # subject { response }++ # it_behaves_like "successful index action response"+ # end+ end+context "when a viewer is logged in" dolet(:user) { viewer }@@ -162,10 +278,12 @@ RSpec.describe WorkPackages::ActivitiesTabController dodescribe "#update_streams" dobefore doget :update_streams,- params: { work_package_id: work_package.id, project_id: project.id },+ params: { work_package_id: work_package.id, project_id: project.id, last_update_timestamp: Time.now.utc },format: :turbo_streamend+ it_behaves_like "does not grant access for anonymous users unless project is public and no login required"+context "when a viewer is logged in" dolet(:user) { viewer }@@ -189,6 +307,20 @@ RSpec.describe WorkPackages::ActivitiesTabController doit_behaves_like "successful update_streams action response"end++ context "when request is invalid" do+ let(:user) { user_with_full_privileges }++ before do+ get :update_streams,+ params: { work_package_id: work_package.id, project_id: project.id }, # missing last_update_timestamp+ format: :turbo_stream+ end++ subject { response }++ it { is_expected.to be_bad_request }+ endenddescribe "#update_filter" do@@ -198,6 +330,8 @@ RSpec.describe WorkPackages::ActivitiesTabController doformat: :turbo_streamend+ it_behaves_like "does not grant access for anonymous users unless project is public and no login required"+context "when a viewer is logged in" dolet(:user) { viewer }@@ -225,8 +359,8 @@ RSpec.describe WorkPackages::ActivitiesTabController dodescribe "#update_sorting" dobefore do- get :update_sorting,- params: { work_package_id: work_package.id, project_id: project.id },+ put :update_sorting,+ params: { work_package_id: work_package.id, project_id: project.id, sorting: },format: :turbo_streamend@@ -235,7 +369,7 @@ RSpec.describe WorkPackages::ActivitiesTabController dosubject { response }- it_behaves_like "successful update_sorting action response"+ it_behaves_like "successful update_sorting action response for asc and desc sorting"endcontext "when a commenter is logged in" do@@ -243,7 +377,7 @@ RSpec.describe WorkPackages::ActivitiesTabController dosubject { response }- it_behaves_like "successful update_sorting action response"+ it_behaves_like "successful update_sorting action response for asc and desc sorting"endcontext "when a user with full privileges is logged in" do@@ -251,17 +385,30 @@ RSpec.describe WorkPackages::ActivitiesTabController dosubject { response }- it_behaves_like "successful update_sorting action response"+ it_behaves_like "successful update_sorting action response for asc and desc sorting"+ end++ context "when request is invalid" do+ let(:user) { user_with_full_privileges }+ let(:sorting) { nil } # missing sorting param++ subject { response }++ it { is_expected.to be_bad_request }endenddescribe "#edit" do+ let(:journal) { comment_by_user }+before doget :edit,- params: { work_package_id: work_package.id, project_id: project.id, id: comment_by_user.id },+ params: { work_package_id: work_package.id, project_id: project.id, id: journal.id },format: :turbo_streamend+ it_behaves_like "does not grant access for anonymous users in all cases"+context "when a viewer is logged in" dolet(:user) { viewer }@@ -273,27 +420,51 @@ RSpec.describe WorkPackages::ActivitiesTabController docontext "when a commenter is logged in" dolet(:user) { commenter }- subject { response }+ context "when the commenter is the author of the comment" do+ subject { response }++ it_behaves_like "successful edit action response"+ end++ context "when the commenter is not the author of the comment" do+ let(:journal) { comment_by_another_user }++ subject { response }- it_behaves_like "successful edit action response"+ it { is_expected.to be_forbidden }+ endendcontext "when a user with full privileges is logged in" dolet(:user) { user_with_full_privileges }- subject { response }+ context "when the user is the author of the comment" do+ subject { response }- it_behaves_like "successful edit action response"+ it_behaves_like "successful edit action response"+ end++ context "when the user is not the author of the comment" do+ let(:journal) { comment_by_another_user }++ subject { response }++ it_behaves_like "successful edit action response"+ endendenddescribe "#cancel_edit" do+ let(:journal) { comment_by_user }+before doget :cancel_edit,- params: { work_package_id: work_package.id, project_id: project.id, id: comment_by_user.id },+ params: { work_package_id: work_package.id, project_id: project.id, id: journal.id },format: :turbo_streamend+ it_behaves_like "does not grant access for anonymous users in all cases"+context "when a viewer is logged in" dolet(:user) { viewer }@@ -305,17 +476,37 @@ RSpec.describe WorkPackages::ActivitiesTabController docontext "when a commenter is logged in" dolet(:user) { commenter }- subject { response }+ context "when the commenter is the author of the comment" do+ subject { response }- it_behaves_like "successful cancel_edit action response"+ it_behaves_like "successful cancel_edit action response"+ end++ context "when the commenter is not the author of the comment" do+ let(:journal) { comment_by_another_user }++ subject { response }++ it { is_expected.to be_forbidden }+ endendcontext "when a user with full privileges is logged in" dolet(:user) { user_with_full_privileges }- subject { response }+ context "when the user is the author of the comment" do+ subject { response }++ it_behaves_like "successful cancel_edit action response"+ end- it_behaves_like "successful cancel_edit action response"+ context "when the user is not the author of the comment" do+ let(:journal) { comment_by_another_user }++ subject { response }++ it_behaves_like "successful cancel_edit action response"+ endendend@@ -333,6 +524,8 @@ RSpec.describe WorkPackages::ActivitiesTabController doformat: :turbo_streamend+ it_behaves_like "does not grant access for anonymous users in all cases"+context "when a viewer is logged in" dolet(:user) { viewer }@@ -356,6 +549,17 @@ RSpec.describe WorkPackages::ActivitiesTabController doit_behaves_like "successful create action response"end++ # TODO: this test is failing as the creation call seems not to have an issues with an empty notes params+ #+ # context "when request is invalid" do+ # let(:user) { user_with_full_privileges }+ # let(:notes) { nil } # missing notes param++ # subject { response }++ # it { is_expected.to be_bad_request }+ # endenddescribe "#update" do@@ -373,6 +577,8 @@ RSpec.describe WorkPackages::ActivitiesTabController doformat: :turbo_streamend+ it_behaves_like "does not grant access for anonymous users in all cases"+context "when a viewer is logged in" dolet(:user) { viewer }@@ -400,9 +606,19 @@ RSpec.describe WorkPackages::ActivitiesTabController docontext "when a user with full privileges is logged in" dolet(:user) { user_with_full_privileges }- subject { response }+ context "when the commenter is the author of the comment" do+ subject { response }++ it_behaves_like "successful update action response"+ end++ context "when the commenter is not the author of the comment" do+ let(:journal) { comment_by_another_user }++ subject { response }- it_behaves_like "successful update action response"+ it_behaves_like "successful update action response"+ endendendend