discourse-translator/spec/lib/guardian_extension_spec.rb
Ted Johansson 17bd836196
FIX: Don't error out on deleted users (#149)
The GuardianExtension#poster_group_allow_translate? method restricts and determines whether a post can be translated based on the poster's group membership. There are reports that this errors out when deleting spammers.

The problem is that when a spammer is deleted, their post user_id is nullified, and because the above method doesn't account for this case, we try to call #in_any_groups? on nil, which errors out.

This PR fixes that by explicitly accounting for the case of a post with a nil user ID.
2024-06-18 09:56:35 +08:00

80 lines
2.5 KiB
Ruby

# frozen_string_literal: true
require "rails_helper"
describe DiscourseTranslator::GuardianExtension do
describe "anon user" do
let!(:guardian) { Guardian.new }
fab!(:post)
before do
SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}"
SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:everyone]}"
end
describe "#user_group_allow_translate?" do
it "returns false" do
expect(guardian.user_group_allow_translate?).to eq(false)
end
end
describe "#poster_group_allow_translate?" do
it "returns false" do
expect(guardian.poster_group_allow_translate?(post)).to eq(false)
end
end
end
describe "deleted poster" do
fab!(:group)
fab!(:user)
fab!(:poster) { Fabricate(:user, groups: [group]) }
fab!(:post) { Fabricate(:post, user: poster) }
let!(:guardian) { Guardian.new(user) }
describe "#poster_group_allow_translate?" do
it "returns false when the post user has been deleted" do
SiteSetting.restrict_translation_by_poster_group = "#{group.id}"
post.update(user: nil)
expect(guardian.poster_group_allow_translate?(post)).to eq(false)
end
end
end
describe "logged in user" do
fab!(:group)
fab!(:user) { Fabricate(:user, groups: [group]) }
fab!(:post) { Fabricate(:post, user: user) }
let(:guardian) { Guardian.new(user) }
describe "#user_group_allow_translate?" do
it "returns true when the user is in restrict_translation_by_group" do
SiteSetting.restrict_translation_by_group = "#{group.id}"
expect(guardian.user_group_allow_translate?).to eq(true)
end
it "returns false when the user is not in restrict_translation_by_group" do
SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:moderators]}"
expect(guardian.user_group_allow_translate?).to eq(false)
end
end
describe "#poster_group_allow_translate?" do
it "returns true when the post user is in restrict_translation_by_poster_group" do
SiteSetting.restrict_translation_by_poster_group = "#{group.id}"
expect(guardian.poster_group_allow_translate?(post)).to eq(true)
end
it "returns false when the post user is not in restrict_translation_by_poster_group" do
SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:moderators]}"
expect(guardian.poster_group_allow_translate?(post)).to eq(false)
end
end
end
end