Skip to content

Commit

Permalink
Fix deleted user error in schema (decidim#13681)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
alecslupu authored Jan 22, 2025
1 parent 6486c77 commit c0a9d5c
Show file tree
Hide file tree
Showing 19 changed files with 147 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 0 additions & 18 deletions decidim-blogs/lib/decidim/blogs/post_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def serialize
private

attr_reader :post
alias resource post

def author
case post.author.class.name
Expand All @@ -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

Expand All @@ -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

Expand Down
25 changes: 20 additions & 5 deletions decidim-blogs/spec/lib/decidim/blogs/post_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions decidim-blogs/spec/system/explore_posts_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 0 additions & 4 deletions decidim-budgets/lib/decidim/budgets/project_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion decidim-core/app/serializers/decidim/exporters/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 0 additions & 18 deletions decidim-debates/lib/decidim/debates/debate_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ def last_comment_by_fields
}
end

def component
debate.component
end

def url
Decidim::ResourceLocatorPresenter.new(debate).url
end
Expand All @@ -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
28 changes: 21 additions & 7 deletions decidim-debates/spec/lib/decidim/debates/debate_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions decidim-debates/spec/system/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 0 additions & 18 deletions decidim-meetings/lib/decidim/meetings/meeting_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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

Expand All @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions decidim-meetings/spec/system/show_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 0 additions & 18 deletions decidim-pages/lib/decidim/pages/page_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Loading

0 comments on commit c0a9d5c

Please sign in to comment.