discourse-translator/spec/lib/guardian_extension_spec.rb
Natalie Tay cdb3a59b8a
FIX: Ensure old feature works with new and show translate button in correct scenarios (#215)
In the older implementation of translation, there exists these two site settings are part of the criteria that determines if 🌐 shows up per post

* `restrict translation by group` - Only allowed groups can translate
* `restrict translation by poster group` - Only allow translation of posts made by users in allowed groups. If empty, allow translations of posts from all users.

In a recent update with experimental features, we updated the cases where 🌐 will show up and caused a regression where the 🌐 shows up for everyone in `restrict translation by group`, ignoring `restrict translation by poster group`.

This commit makes sure these two site settings are adhered to when the experimental topic setting is turned off.
2025-02-18 15:09:16 +08:00

244 lines
8.2 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
describe "#can_detect_language?" do
fab!(:group)
fab!(:user) { Fabricate(:user, groups: [group]) }
fab!(:post) { Fabricate(:post, user: user, raw: "Hello, world!") }
let(:guardian) { Guardian.new(user) }
it "returns false when the post user is not in restrict_translation_by_poster_group" do
SiteSetting.restrict_translation_by_poster_group = "#{Fabricate(:group).id}"
expect(guardian.can_detect_language?(post)).to eq(false)
end
context "when post author is in allowed groups" do
before do
SiteSetting.restrict_translation_by_group = "#{group.id}"
SiteSetting.restrict_translation_by_poster_group = "#{group.id}"
end
it "returns true when the post is not a small action post" do
expect(guardian.can_detect_language?(post)).to eq(true)
end
it "returns false when the post is a small action post" do
post.update!(post_type: Post.types[:small_action])
expect(guardian.can_detect_language?(post)).to eq(false)
end
it "returns false when the post raw is empty" do
expect { post.update(raw: "") }.to change { guardian.can_detect_language?(post) }.from(
true,
).to(false)
end
end
end
describe "#can_translate?" do
fab!(:group)
fab!(:user) { Fabricate(:user, locale: "en", groups: [group]) }
fab!(:post)
let(:guardian) { Guardian.new(user) }
it "returns false when translator disabled" do
SiteSetting.translator_enabled = false
expect(guardian.can_translate?(post)).to eq(false)
end
describe "when translator enabled" do
before { SiteSetting.translator_enabled = true }
describe "when experimental_topic_translation enabled" do
before { SiteSetting.experimental_topic_translation = true }
describe "anon user" do
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
it "cannot translate" do
expect(Guardian.new.can_translate?(post)).to eq(false)
end
end
describe "logged in user" do
it "cannot translate when user is not in restrict_translation_by_group" do
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
expect(guardian.can_translate?(post)).to eq(false)
end
describe "user is in restrict_translation_by_group" do
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
describe "locale is :pt" do
before { I18n.stubs(:locale).returns(:pt) }
it "cannot translate when post detected locale matches i18n locale" do
post.set_detected_locale("pt")
expect(guardian.can_translate?(post)).to eq(false)
end
it "can translate when post's detected locale does not match i18n locale" do
post.set_detected_locale("jp")
expect(guardian.can_translate?(post)).to eq(true)
end
it "cannot translate when post has translation for user locale" do
post.set_detected_locale("jp")
post.set_translation("pt", "Olá, mundo!")
expect(guardian.can_translate?(post)).to eq(false)
end
it "can translate when post does not have translation for user locale" do
post.set_detected_locale("jp")
expect(guardian.can_translate?(post)).to eq(true)
end
end
end
end
end
describe "when experimental topic translation disabled" do
before { SiteSetting.experimental_topic_translation = false }
describe "anon user" do
before { SiteSetting.restrict_translation_by_group = "#{Group::AUTO_GROUPS[:everyone]}" }
it "cannot translate" do
expect(Guardian.new.can_translate?(post)).to eq(false)
end
end
describe "logged in user" do
it "cannot translate when user is not in restrict_translation_by_group" do
SiteSetting.restrict_translation_by_group = "#{group.id + 1}"
expect(guardian.can_translate?(post)).to eq(false)
end
describe "user is in restrict_translation_by_group" do
before { SiteSetting.restrict_translation_by_group = "#{group.id}" }
describe "locale is :pt" do
before { I18n.stubs(:locale).returns(:pt) }
it "cannot translate when post detected locale matches i18n locale" do
post.set_detected_locale("pt")
expect(guardian.can_translate?(post)).to eq(false)
end
it "can translate when post's detected locale does not match i18n locale" do
post.set_detected_locale("jp")
expect(guardian.can_translate?(post)).to eq(true)
end
it "cannot translate if poster is not in restrict_translation_by_poster_group" do
SiteSetting.restrict_translation_by_poster_group = "#{Group::AUTO_GROUPS[:staff]}"
expect(guardian.can_translate?(post)).to eq(false)
end
it "can translate if poster is in restrict_translation_by_poster_group" do
poster = post.user
poster_group = Fabricate(:group, users: [poster])
SiteSetting.restrict_translation_by_poster_group = "#{poster_group.id}"
expect(guardian.can_translate?(post)).to eq(true)
SiteSetting.restrict_translation_by_poster_group = ""
expect(guardian.can_translate?(post)).to eq(true)
end
end
end
end
end
end
end
end