From c0a9d5cc289dfb68fde3bb0c463ac90d14021a6d Mon Sep 17 00:00:00 2001 From: Alexandru Emil Lupu Date: Wed, 22 Jan 2025 11:51:52 +0200 Subject: [PATCH] Fix deleted user error in schema (#13681) * Fix deleted user error in schema * Fix failing specs * Remove method override * Refactor * Fix specs * Refactor page serializer * Fix specs on meetings and proposals * Add specs for blogs and debates * Fix debates spec * Fix spec --- .../accountability/result_serializer.rb | 4 --- .../lib/decidim/blogs/post_serializer.rb | 18 ----------- ...schema_org_blog_posting_post_serializer.rb | 5 ++-- .../lib/decidim/blogs/post_serializer_spec.rb | 25 ++++++++++++---- ...a_org_blog_posting_post_serializer_spec.rb | 10 +++++++ .../spec/system/explore_posts_spec.rb | 8 +++++ .../lib/decidim/budgets/project_serializer.rb | 4 --- .../decidim/exporters/serializer.rb | 18 ++++++++++- .../lib/decidim/debates/debate_serializer.rb | 18 ----------- .../decidim/debates/debate_serializer_spec.rb | 28 ++++++++++++----- decidim-debates/spec/system/show_spec.rb | 9 ++++++ .../decidim/meetings/meeting_serializer.rb | 18 ----------- .../schema_org_event_meeting_serializer.rb | 5 ++-- ...chema_org_event_meeting_serializer_spec.rb | 13 ++++++++ decidim-meetings/spec/system/show_spec.rb | 9 ++++++ .../lib/decidim/pages/page_serializer.rb | 18 ----------- .../decidim/proposals/proposal_serializer.rb | 18 ----------- .../proposals/proposal_serializer_spec.rb | 30 ++++++++++++++----- .../spec/system/proposal_show_spec.rb | 14 +++++++-- 19 files changed, 147 insertions(+), 125 deletions(-) diff --git a/decidim-accountability/lib/decidim/accountability/result_serializer.rb b/decidim-accountability/lib/decidim/accountability/result_serializer.rb index f7bfe23e5c785..e423f791ddcd5 100644 --- a/decidim-accountability/lib/decidim/accountability/result_serializer.rb +++ b/decidim-accountability/lib/decidim/accountability/result_serializer.rb @@ -50,10 +50,6 @@ def serialize attr_reader :result alias resource result - def component - result.component - end - def proposals result.linked_resources(:proposals, "included_proposals").map do |proposal| Decidim::ResourceLocatorPresenter.new(proposal).url diff --git a/decidim-blogs/lib/decidim/blogs/post_serializer.rb b/decidim-blogs/lib/decidim/blogs/post_serializer.rb index 0b69052d76373..195ffdf2c6003 100644 --- a/decidim-blogs/lib/decidim/blogs/post_serializer.rb +++ b/decidim-blogs/lib/decidim/blogs/post_serializer.rb @@ -42,10 +42,6 @@ def serialize attr_reader :post alias resource post - def component - post.component - end - def url Decidim::ResourceLocatorPresenter.new(post).url end @@ -69,20 +65,6 @@ def author_url(author) root_url # is a Decidim::Organization end end - - def profile_url(author) - return "" if author.respond_to?(:deleted?) && author.deleted? - - Decidim::Core::Engine.routes.url_helpers.profile_url(author.nickname, host:) - end - - def root_url - Decidim::Core::Engine.routes.url_helpers.root_url(host:) - end - - def host - resource.organization.host - end end end end diff --git a/decidim-blogs/lib/decidim/blogs/schema_org_blog_posting_post_serializer.rb b/decidim-blogs/lib/decidim/blogs/schema_org_blog_posting_post_serializer.rb index 8946ba2c078ae..bc8e430fd88c5 100644 --- a/decidim-blogs/lib/decidim/blogs/schema_org_blog_posting_post_serializer.rb +++ b/decidim-blogs/lib/decidim/blogs/schema_org_blog_posting_post_serializer.rb @@ -32,6 +32,7 @@ def serialize private attr_reader :post + alias resource post def author case post.author.class.name @@ -48,7 +49,7 @@ def author_user_group { "@type": "Organization", name: post.author.name, - url: EngineRouter.new("decidim", router_options).profile_url(post.author.nickname) + url: profile_url(post.author) } end @@ -64,7 +65,7 @@ def author_user { "@type": "Person", name: decidim_escape_translated(post.author.name), - url: EngineRouter.new("decidim", router_options).profile_url(post.author.nickname) + url: profile_url(post.author) } end diff --git a/decidim-blogs/spec/lib/decidim/blogs/post_serializer_spec.rb b/decidim-blogs/spec/lib/decidim/blogs/post_serializer_spec.rb index 7f0a94a50b125..966df045ac111 100644 --- a/decidim-blogs/spec/lib/decidim/blogs/post_serializer_spec.rb +++ b/decidim-blogs/spec/lib/decidim/blogs/post_serializer_spec.rb @@ -23,10 +23,8 @@ module Blogs describe "author" do context "when it is a user" do - before do - post.author.update!(name: "John Doe") - post.reload - end + let(:author) { create(:user, name: "John Doe", organization: component.organization) } + let!(:post) { create(:post, component:, author:) } it "serializes the user name" do expect(serialized[:author]).to include(name: "John Doe") @@ -35,6 +33,23 @@ module Blogs it "serializes the link to its profile" do expect(serialized[:author]).to include(url: profile_url(post.author.nickname)) end + + context "when author is deleted" do + let(:author) { create(:user, :deleted, organization: component.organization) } + let!(:debate) { create(:post, component:, author:) } + + it "serializes the user id" do + expect(serialized[:author]).to include(id: author.id) + end + + it "serializes the user name" do + expect(serialized[:author]).to include(name: "") + end + + it "serializes the link to its profile" do + expect(serialized[:author]).to include(url: "") + end + end end context "when it is a user group" do @@ -91,7 +106,7 @@ module Blogs end def profile_url(nickname) - Decidim::Core::Engine.routes.url_helpers.profile_url(nickname, host:) + Decidim::Core::Engine.routes.url_helpers.profile_url(nickname, host:, port: Capybara.server_port) end def root_url diff --git a/decidim-blogs/spec/lib/decidim/blogs/schema_org_blog_posting_post_serializer_spec.rb b/decidim-blogs/spec/lib/decidim/blogs/schema_org_blog_posting_post_serializer_spec.rb index 3f10f907c223b..4668c266becfb 100644 --- a/decidim-blogs/spec/lib/decidim/blogs/schema_org_blog_posting_post_serializer_spec.rb +++ b/decidim-blogs/spec/lib/decidim/blogs/schema_org_blog_posting_post_serializer_spec.rb @@ -72,6 +72,16 @@ module Decidim::Blogs expect(serialized[:author][:name]).to eq(post.author.name) expect(serialized[:author][:url]).to eq("http://#{organization.host}:#{Capybara.server_port}/profiles/#{post.author.nickname}") end + + context "when author is deleted" do + let(:author) { create(:user, :deleted, organization:) } + + it "serializes the author" do + expect(serialized[:author][:@type]).to eq("Person") + expect(serialized[:author][:name]).to eq(post.author.name) + expect(serialized[:author][:url]).to eq("") + end + end end context "with user group author" do diff --git a/decidim-blogs/spec/system/explore_posts_spec.rb b/decidim-blogs/spec/system/explore_posts_spec.rb index e847238e3b5df..1fb26fecfc66f 100644 --- a/decidim-blogs/spec/system/explore_posts_spec.rb +++ b/decidim-blogs/spec/system/explore_posts_spec.rb @@ -107,6 +107,14 @@ expect(page).to have_content(user.name) end end + + context "when participant is deleted" do + let(:author) { create(:user, :deleted, organization: component.organization) } + + it "successfully shows the page" do + expect(page).to have_content("Deleted participant") + end + end end it "show post info" do diff --git a/decidim-budgets/lib/decidim/budgets/project_serializer.rb b/decidim-budgets/lib/decidim/budgets/project_serializer.rb index 5174ca7722430..0779beb5c0083 100644 --- a/decidim-budgets/lib/decidim/budgets/project_serializer.rb +++ b/decidim-budgets/lib/decidim/budgets/project_serializer.rb @@ -51,10 +51,6 @@ def serialize attr_reader :project alias resource project - def component - project.component - end - def related_proposals project.linked_resources(:proposals, "included_proposals").map(&:id) end diff --git a/decidim-core/app/serializers/decidim/exporters/serializer.rb b/decidim-core/app/serializers/decidim/exporters/serializer.rb index e7b070dd2e7c9..c36a5348ca1e1 100644 --- a/decidim-core/app/serializers/decidim/exporters/serializer.rb +++ b/decidim-core/app/serializers/decidim/exporters/serializer.rb @@ -55,7 +55,23 @@ def event_name ActiveSupport::Inflector.underscore(self.class.to_s).sub("/", ".serialize.").gsub("/", ".") end - private + protected + + delegate :component, to: :resource + + def profile_url(user) + return "" if user.respond_to?(:deleted?) && user.deleted? + + EngineRouter.new("decidim", { host: }).profile_url(user.nickname) + end + + def root_url + Decidim::Core::Engine.routes.url_helpers.root_url(host:) + end + + def host + resource.organization.host + end # helper method to serialize taxonomies for any resource def taxonomies diff --git a/decidim-debates/lib/decidim/debates/debate_serializer.rb b/decidim-debates/lib/decidim/debates/debate_serializer.rb index 262362ce6455b..dd0508fd5d8d3 100644 --- a/decidim-debates/lib/decidim/debates/debate_serializer.rb +++ b/decidim-debates/lib/decidim/debates/debate_serializer.rb @@ -64,10 +64,6 @@ def last_comment_by_fields } end - def component - debate.component - end - def url Decidim::ResourceLocatorPresenter.new(debate).url end @@ -91,20 +87,6 @@ def user_url(author) root_url # is a Decidim::Organization end end - - def profile_url(author) - return "" if author.respond_to?(:deleted?) && author.deleted? - - Decidim::Core::Engine.routes.url_helpers.profile_url(author.nickname, host:) - end - - def root_url - Decidim::Core::Engine.routes.url_helpers.root_url(host:) - end - - def host - resource.organization.host - end end end end diff --git a/decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb b/decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb index 32f39dd79ef81..465f81fedf031 100644 --- a/decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb +++ b/decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb @@ -52,12 +52,9 @@ module Debates end context "when it is a user" do - let!(:debate) { create(:debate, :participant_author) } - - before do - debate.author.update!(name: "John Doe") - debate.reload - end + let(:author) { create(:user, name: "John Doe", organization: component.organization) } + let(:component) { create(:debates_component) } + let!(:debate) { create(:debate, component:, author:) } it "serializes the user name" do expect(serialized[:author]).to include(name: "John Doe") @@ -66,6 +63,23 @@ module Debates it "serializes the link to its profile" do expect(serialized[:author]).to include(url: profile_url(debate.author.nickname)) end + + context "when author is deleted" do + let(:author) { create(:user, :deleted, organization: component.organization) } + let!(:debate) { create(:debate, component:, author:) } + + it "serializes the user id" do + expect(serialized[:author]).to include(id: author.id) + end + + it "serializes the user name" do + expect(serialized[:author]).to include(name: "") + end + + it "serializes the link to its profile" do + expect(serialized[:author]).to include(url: "") + end + end end context "when it is a user group" do @@ -208,7 +222,7 @@ module Debates end def profile_url(nickname) - Decidim::Core::Engine.routes.url_helpers.profile_url(nickname, host:) + Decidim::Core::Engine.routes.url_helpers.profile_url(nickname, host:, port: Capybara.server_port) end def root_url diff --git a/decidim-debates/spec/system/show_spec.rb b/decidim-debates/spec/system/show_spec.rb index 16d9659f32048..2364f26f209fa 100644 --- a/decidim-debates/spec/system/show_spec.rb +++ b/decidim-debates/spec/system/show_spec.rb @@ -53,6 +53,15 @@ it { expect(page).to have_no_selector("iframe") } end + + context "when participant is deleted" do + let(:author) { create(:user, :deleted, organization: component.organization) } + let!(:debate) { create(:debate, component:, author:) } + + it "successfully shows the page" do + expect(page).to have_content("Deleted participant") + end + end end context "when shows the debate component" do diff --git a/decidim-meetings/lib/decidim/meetings/meeting_serializer.rb b/decidim-meetings/lib/decidim/meetings/meeting_serializer.rb index e872f7d9f73e3..172afe6ba87e1 100644 --- a/decidim-meetings/lib/decidim/meetings/meeting_serializer.rb +++ b/decidim-meetings/lib/decidim/meetings/meeting_serializer.rb @@ -73,24 +73,6 @@ def author_url(author) end end - def profile_url(author) - return "" if author.respond_to?(:deleted?) && author.deleted? - - Decidim::Core::Engine.routes.url_helpers.profile_url(author.nickname, host:) - end - - def root_url - Decidim::Core::Engine.routes.url_helpers.root_url(host:) - end - - def host - resource.organization.host - end - - def component - meeting.component - end - def related_proposals meeting.linked_resources(:proposals, "proposals_from_meeting").map do |proposal| Decidim::ResourceLocatorPresenter.new(proposal).url diff --git a/decidim-meetings/lib/decidim/meetings/schema_org_event_meeting_serializer.rb b/decidim-meetings/lib/decidim/meetings/schema_org_event_meeting_serializer.rb index 260253012ffe3..28020abeb7e38 100644 --- a/decidim-meetings/lib/decidim/meetings/schema_org_event_meeting_serializer.rb +++ b/decidim-meetings/lib/decidim/meetings/schema_org_event_meeting_serializer.rb @@ -37,6 +37,7 @@ def serialize private attr_reader :meeting + alias resource meeting def organizer return organizer_user_group if meeting.decidim_user_group_id? @@ -53,7 +54,7 @@ def organizer_user_group { "@type": "Organization", name: meeting.author.name, - url: EngineRouter.new("decidim", router_options).profile_url(meeting.user_group.nickname) + url: profile_url(meeting.user_group) } end @@ -69,7 +70,7 @@ def organizer_user { "@type": "Person", name: decidim_escape_translated(meeting.author.name), - url: EngineRouter.new("decidim", router_options).profile_url(meeting.author.nickname) + url: profile_url(meeting.author) } end diff --git a/decidim-meetings/spec/lib/decidim/meetings/schema_org_event_meeting_serializer_spec.rb b/decidim-meetings/spec/lib/decidim/meetings/schema_org_event_meeting_serializer_spec.rb index 6dc9043830884..569097f51d4b9 100644 --- a/decidim-meetings/spec/lib/decidim/meetings/schema_org_event_meeting_serializer_spec.rb +++ b/decidim-meetings/spec/lib/decidim/meetings/schema_org_event_meeting_serializer_spec.rb @@ -73,6 +73,19 @@ module Decidim::Meetings expect(serialized[:organizer][:name]).to eq(meeting.author.name) expect(serialized[:organizer][:url]).to eq("http://#{organization.host}:#{Capybara.server_port}/profiles/#{meeting.author.nickname}") end + + context "with deleted author" do + let(:organization) { create(:organization) } + let(:user) { create(:user, :deleted, organization:) } + let(:component) { create(:meeting_component, :published, organization:) } + let!(:meeting) { create(:meeting, :published, author: user.reload, component:, latitude:, longitude:) } + + it "serializes the organizer" do + expect(serialized[:organizer][:@type]).to eq("Person") + expect(serialized[:organizer][:name]).to eq(meeting.author.name) + expect(serialized[:organizer][:url]).to eq("") + end + end end context "with user group author" do diff --git a/decidim-meetings/spec/system/show_spec.rb b/decidim-meetings/spec/system/show_spec.rb index 87302de921a2d..ce42ae5e8efcd 100644 --- a/decidim-meetings/spec/system/show_spec.rb +++ b/decidim-meetings/spec/system/show_spec.rb @@ -34,5 +34,14 @@ expect(page).to have_content("HST") end end + + context "when participant is deleted" do + let(:user) { create(:user, :deleted, organization:) } + let!(:meeting) { create(:meeting, :published, author: user.reload, component:) } + + it "successfully shows the page" do + expect(page).to have_content("Deleted participant") + end + end end end diff --git a/decidim-pages/lib/decidim/pages/page_serializer.rb b/decidim-pages/lib/decidim/pages/page_serializer.rb index 3c653902ecbe5..c353b8455bc19 100644 --- a/decidim-pages/lib/decidim/pages/page_serializer.rb +++ b/decidim-pages/lib/decidim/pages/page_serializer.rb @@ -35,10 +35,6 @@ def serialize attr_reader :page alias resource page - def component - page.component - end - def url Decidim::ResourceLocatorPresenter.new(page).url end @@ -62,20 +58,6 @@ def author_url(author) root_url # is a Decidim::Organization end end - - def profile_url(author) - return "" if author.respond_to?(:deleted?) && author.deleted? - - Decidim::Core::Engine.routes.url_helpers.profile_url(author.nickname, host:) - end - - def root_url - Decidim::Core::Engine.routes.url_helpers.root_url(host:) - end - - def host - resource.organization.host - end end end end diff --git a/decidim-proposals/lib/decidim/proposals/proposal_serializer.rb b/decidim-proposals/lib/decidim/proposals/proposal_serializer.rb index 34c18ea4a3fed..881ca1552c9fb 100644 --- a/decidim-proposals/lib/decidim/proposals/proposal_serializer.rb +++ b/decidim-proposals/lib/decidim/proposals/proposal_serializer.rb @@ -73,10 +73,6 @@ def serialize attr_reader :proposal alias resource proposal - def component - proposal.component - end - def meetings proposal.linked_resources(:meetings, "proposals_from_meeting").map do |meeting| Decidim::ResourceLocatorPresenter.new(meeting).url @@ -142,23 +138,9 @@ def author_url(author) end end - def profile_url(author) - return "" if author.respond_to?(:deleted?) && author.deleted? - - Decidim::Core::Engine.routes.url_helpers.profile_url(author.nickname, host:) - end - def meeting_url(meeting) Decidim::EngineRouter.main_proxy(meeting.component).meeting_url(id: meeting.id, host:) end - - def root_url - Decidim::Core::Engine.routes.url_helpers.root_url(host:) - end - - def host - resource.organization.host - end end end end diff --git a/decidim-proposals/spec/services/decidim/proposals/proposal_serializer_spec.rb b/decidim-proposals/spec/services/decidim/proposals/proposal_serializer_spec.rb index 1c5163a36ace8..5e90d43341a84 100644 --- a/decidim-proposals/spec/services/decidim/proposals/proposal_serializer_spec.rb +++ b/decidim-proposals/spec/services/decidim/proposals/proposal_serializer_spec.rb @@ -17,7 +17,7 @@ module Proposals let!(:meetings_component) { create(:component, manifest_name: "meetings", participatory_space: participatory_process) } let(:meetings) { create_list(:meeting, 2, :published, component: meetings_component) } - let!(:proposals_component) { create(:component, manifest_name: "proposals", participatory_space: participatory_process) } + let!(:proposals_component) { create(:proposal_component, participatory_space: participatory_process) } let(:other_proposals) { create_list(:proposal, 2, component: proposals_component) } let(:serialized) { subject.serialize } @@ -72,12 +72,9 @@ module Proposals end context "when it is a user" do - let!(:proposal) { create(:proposal, :participant_author) } - - before do - proposal.creator_author.update!(name: "John Doe") - proposal.reload - end + let!(:user) { create(:user, name: "John Doe", organization: component.organization) } + let(:component) { create(:proposal_component) } + let!(:proposal) { create(:proposal, component:, users: [user]) } it "serializes the user name" do expect(serialized[:author]).to include(name: ["John Doe"]) @@ -86,6 +83,23 @@ module Proposals it "serializes the link to its profile" do expect(serialized[:author]).to include(url: [profile_url(proposal.creator_author.nickname)]) end + + context "when author is deleted" do + let!(:user) { create(:user, :deleted, organization: component.organization) } + let!(:proposal) { create(:proposal, component:, users: [user]) } + + it "serializes the user id" do + expect(serialized[:author]).to include(id: [user.id]) + end + + it "serializes the user name" do + expect(serialized[:author]).to include(name: [""]) + end + + it "serializes the link to its profile" do + expect(serialized[:author]).to include(url: [""]) + end + end end context "when it is multiple users" do @@ -421,7 +435,7 @@ module Proposals end def profile_url(nickname) - Decidim::Core::Engine.routes.url_helpers.profile_url(nickname, host:) + Decidim::Core::Engine.routes.url_helpers.profile_url(nickname, host:, port: Capybara.server_port) end def meeting_url(meeting) diff --git a/decidim-proposals/spec/system/proposal_show_spec.rb b/decidim-proposals/spec/system/proposal_show_spec.rb index c6819c030d511..e91aa11c1b167 100644 --- a/decidim-proposals/spec/system/proposal_show_spec.rb +++ b/decidim-proposals/spec/system/proposal_show_spec.rb @@ -31,7 +31,7 @@ def visit_proposal end context "when I am an admin user" do - let(:user) { create(:user, :admin, :confirmed, organization:) } + let!(:user) { create(:user, :admin, :confirmed, organization:) } it "has a link to answer to the proposal at the admin" do within "header" do @@ -42,7 +42,7 @@ def visit_proposal end context "when I am a regular user" do - let(:user) { create(:user, :confirmed, organization:) } + let!(:user) { create(:user, :confirmed, organization:) } it "does not have a link to answer the proposal at the admin" do within "header" do @@ -57,6 +57,7 @@ def visit_proposal let(:user) { create(:user, :confirmed, organization:) } before do + visit_proposal login_as user, scope: :user visit current_path end @@ -69,6 +70,15 @@ def visit_proposal expect(page).to have_link("Send private message") end end + + context "when participant is deleted" do + let!(:author) { create(:user, :deleted, organization: component.organization) } + let!(:proposal) { create(:proposal, component:, users: [author]) } + + it "successfully shows the page" do + expect(page).to have_content("Deleted participant") + end + end end end end