404 page for a suspended user with no published content (#19789)

* feat: update the logic in the user profile

* feat: remove use case for banished user

* feat: update has_no_content? to has_no_accessible_content? and write a test

* spec: write more tests

* refactor: rename variable

* refactor: small tweaks

---------

Co-authored-by: Philip How <philip.j.how@gmail.com>
This commit is contained in:
Ridhwana 2023-07-26 10:58:17 +02:00 committed by GitHub
parent 84b2f0145a
commit 8c7d592b96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 19 deletions

View file

@ -184,6 +184,9 @@ class StoriesController < ApplicationController
end
not_found if @user.username.include?("spam_") && @user.decorate.fully_banished?
not_found unless @user.registered
if !user_signed_in? && (@user.suspended? && @user.has_no_published_content?)
not_found
end
assign_user_comments
assign_user_stories
@list_of = "articles"

View file

@ -567,6 +567,10 @@ class User < ApplicationRecord
Tag.followed_by(self)
end
def has_no_published_content?
articles.published.empty? && comments_count.zero?
end
protected
# Send emails asynchronously

View file

@ -870,6 +870,18 @@ RSpec.describe User do
end
end
describe "#has_no_published_content?" do
it "returns true if the user has no published articles or comments" do
create(:article, user_id: user.id, published: false, published_at: Date.tomorrow)
expect(user.has_no_published_content?).to be(true)
end
it "returns false if the user has any published articles or comments" do
create(:article, user_id: user.id, published: true)
expect(user.has_no_published_content?).to be(false)
end
end
describe ".above_average and .average_x_count" do
context "when there are not yet any articles with score above 0" do
it "works as expected" do

View file

@ -3,8 +3,9 @@ require "rails_helper"
RSpec.describe "UserProfiles" do
let(:user) { create(:user) }
let(:organization) { create(:organization) }
let(:current_user) { create(:user) }
describe "GET /user" do
describe "GET /:username" do
it "renders to appropriate page" do
get "/#{user.username}"
expect(response.body).to include CGI.escapeHTML(user.name)
@ -47,24 +48,6 @@ RSpec.describe "UserProfiles" do
expect(response).to redirect_to("/#{user.username}")
end
it "raises not found for banished users" do
banishable_user = create(:user)
Moderator::BanishUser.call(admin: user, user: banishable_user)
expect { get "/#{banishable_user.reload.old_username}" }.to raise_error(ActiveRecord::RecordNotFound)
expect { get "/#{banishable_user.reload.username}" }.to raise_error(ActiveRecord::RecordNotFound)
end
it "raises not found if user not registered" do
user.update_column(:registered, false)
expect { get "/#{user.username}" }.to raise_error(ActiveRecord::RecordNotFound)
end
it "renders noindex meta if suspended" do
user.add_role(:suspended)
get "/#{user.username}"
expect(response.body).to include("<meta name=\"robots\" content=\"noindex\">")
end
it "does not render noindex meta if not suspended" do
get "/#{user.username}"
expect(response.body).not_to include("<meta name=\"robots\" content=\"noindex\">")
@ -194,6 +177,52 @@ RSpec.describe "UserProfiles" do
expect(response.body).not_to include("github-repos-container")
end
end
# rubocop:disable RSpec/NestedGroups
describe "not found and no index behaviour" do
it "raises not found for banished users" do
banishable_user = create(:user)
Moderator::BanishUser.call(admin: user, user: banishable_user)
expect { get "/#{banishable_user.reload.old_username}" }.to raise_error(ActiveRecord::RecordNotFound)
expect { get "/#{banishable_user.reload.username}" }.to raise_error(ActiveRecord::RecordNotFound)
end
context "when a user is signed in" do
it "does not raise not found for suspended users who have no current content" do
sign_in current_user
suspended_user = create(:user)
suspended_user.add_role(:suspended)
create(:article, user_id: user.id, published: false, published_at: Date.tomorrow)
expect { get "/#{suspended_user.username}" }.not_to raise_error(ActiveRecord::RecordNotFound)
end
end
context "when a user is not signed in" do
it "raises not found for suspended users who do not have published content" do
suspended_user = create(:user)
suspended_user.add_role(:suspended)
create(:article, user_id: user.id, published: false, published_at: Date.tomorrow)
expect { get "/#{suspended_user.username}" }.to raise_error(ActiveRecord::RecordNotFound)
end
end
it "renders noindex meta if suspended (and has published content)" do
user.add_role(:suspended)
create(:article, user_id: user.id)
get "/#{user.username}"
expect(response.body).to include("<meta name=\"robots\" content=\"noindex\">")
end
it "raises not found if user not registered" do
user.update_column(:registered, false)
expect { get "/#{user.username}" }.to raise_error(ActiveRecord::RecordNotFound)
end
end
# rubocop:enable RSpec/NestedGroups
end
describe "redirect to moderation" do