Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: Fix for anon users visiting posts when the plugin is enabled #106

Merged
merged 2 commits into from May 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/discourse_translator/guardian_extension.rb
Expand Up @@ -3,6 +3,7 @@ module DiscourseTranslator::GuardianExtension
def user_group_allowed?
authorized_groups = SiteSetting.restrict_translation_by_group.split("|").map(&:to_i)

return false if !current_user
user_groups = current_user.groups.pluck :id
authorized = authorized_groups.intersection user_groups
!authorized.empty?
Expand Down
6 changes: 6 additions & 0 deletions spec/lib/guardian_extension_spec.rb
Expand Up @@ -23,5 +23,11 @@

expect(guardian.user_group_allowed?).to eq(false)
end

it "returns false when the user is not logged in" do
SiteSetting.restrict_translation_by_group = "not_in_the_list"
non_logged_guardian = Guardian.new
expect(non_logged_guardian.user_group_allowed?).to eq(false)
end
end
end
76 changes: 46 additions & 30 deletions spec/serializers/post_serializer_spec.rb
Expand Up @@ -4,51 +4,67 @@

RSpec.describe PostSerializer do
let(:post) { Fabricate(:post) }
let!(:user) do
user = Fabricate(:user, locale: "en")
user.group_users << Fabricate(:group_user, user: user, group: Group[:trust_level_1])
user
end
let(:serializer) { PostSerializer.new(post, scope: Guardian.new(user)) }

before do
SiteSetting.translator_enabled = true
SiteSetting.queue_jobs = true
end

describe "#can_translate" do
describe "when user is in a allowlisted group"
before do
SiteSetting.restrict_translation_by_group =
"#{Group.find_by(name: user.groups.first.name).id}|not_in_the_list"
end
describe "when post detected lang does not match user's locale" do
before do
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "ja"
post.save
describe "logged in user" do
let(:user) do
user = Fabricate(:user, locale: "en")
user.group_users << Fabricate(:group_user, user: user, group: Group[:trust_level_1])
user
end
let(:serializer) { PostSerializer.new(post, scope: Guardian.new(user)) }

describe "when user is in a allowlisted group" do
before do
SiteSetting.restrict_translation_by_group =
"#{Group.find_by(name: user.groups.first.name).id}|not_in_the_list"
end
describe "when post detected lang does not match user's locale" do
before do
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "ja"
post.save
end

it { expect(serializer.can_translate).to eq(true) }
end

describe "when post detected lang matches user's locale" do
before do
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "en"
post.save
end
it { expect(serializer.can_translate).to eq(false) }
end
end
describe "when user is not in a allowlisted group" do
before do
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "ja"
post.save
SiteSetting.restrict_translation_by_group = "not_in_the_list"
end

it { expect(serializer.can_translate).to eq(true) }
it "should not translate even if the post detected lang does not match the user's locale" do
expect(serializer.can_translate).to eq(false)
end
end
end

describe "when post detected lang matches user's locale" do
describe "when user is not logged in" do
let(:serializer) { PostSerializer.new(post, scope: Guardian.new) }
before do
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "en"
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "ja"
post.save
SiteSetting.restrict_translation_by_group = "11|not_in_the_list"
end
it { expect(serializer.can_translate).to eq(false) }
end
end

describe "when user is not in a allowlisted group" do
before do
post.custom_fields[DiscourseTranslator::DETECTED_LANG_CUSTOM_FIELD] = "ja"
post.save
SiteSetting.restrict_translation_by_group = "not_in_the_list"
end

it "should not translate even if the post detected lang does not match the user's locale" do
expect(serializer.can_translate).to eq(false)
it "should not translate" do
expect(serializer.can_translate).to eq(false)
end
end
end
end