2024-07-02 11:51:59 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2024-07-03 20:48:18 -04:00
|
|
|
describe DiscourseAi::GuardianExtensions do
|
2024-07-02 11:51:59 -04:00
|
|
|
fab!(:user)
|
|
|
|
fab!(:group)
|
|
|
|
fab!(:topic)
|
|
|
|
|
|
|
|
before do
|
|
|
|
group.add(user)
|
2024-07-03 20:48:18 -04:00
|
|
|
assign_fake_provider_to(:ai_summarization_model)
|
|
|
|
SiteSetting.ai_summarization_enabled = true
|
2024-11-26 11:44:12 -05:00
|
|
|
SiteSetting.ai_summary_gists_enabled = true
|
2024-07-02 11:51:59 -04:00
|
|
|
end
|
|
|
|
|
2024-10-21 14:15:25 -04:00
|
|
|
let(:anon_guardian) { Guardian.new }
|
|
|
|
let(:guardian) { Guardian.new(user) }
|
2024-07-03 20:48:18 -04:00
|
|
|
|
2024-10-21 14:15:25 -04:00
|
|
|
describe "#can_see_summary?" do
|
2024-07-02 11:51:59 -04:00
|
|
|
context "when the user cannot generate a summary" do
|
|
|
|
before { SiteSetting.ai_custom_summarization_allowed_groups = "" }
|
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
SiteSetting.ai_custom_summarization_allowed_groups = ""
|
|
|
|
|
2024-10-21 14:15:25 -04:00
|
|
|
expect(guardian.can_see_summary?(topic)).to eq(false)
|
2024-07-02 11:51:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true if there is a cached summary" do
|
|
|
|
AiSummary.create!(
|
|
|
|
target: topic,
|
|
|
|
summarized_text: "test",
|
|
|
|
original_content_sha: "123",
|
|
|
|
algorithm: "test",
|
2024-10-15 12:53:26 -04:00
|
|
|
summary_type: AiSummary.summary_types[:complete],
|
2024-07-02 11:51:59 -04:00
|
|
|
)
|
|
|
|
|
2024-10-21 14:15:25 -04:00
|
|
|
expect(guardian.can_see_summary?(topic)).to eq(true)
|
2024-07-02 11:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the user can generate a summary" do
|
|
|
|
before { SiteSetting.ai_custom_summarization_allowed_groups = group.id }
|
|
|
|
|
|
|
|
it "returns true if the user group is present in the ai_custom_summarization_allowed_groups_map setting" do
|
2024-10-21 14:15:25 -04:00
|
|
|
expect(guardian.can_see_summary?(topic)).to eq(true)
|
2024-07-03 20:48:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when the topic is a PM" do
|
|
|
|
before { SiteSetting.ai_custom_summarization_allowed_groups = group.id }
|
|
|
|
let(:pm) { Fabricate(:private_message_topic) }
|
|
|
|
|
|
|
|
it "returns false" do
|
2024-10-21 14:15:25 -04:00
|
|
|
expect(guardian.can_see_summary?(pm)).to eq(false)
|
2024-07-02 11:51:59 -04:00
|
|
|
end
|
2024-08-20 17:58:24 -04:00
|
|
|
|
|
|
|
it "returns true if user is in a group that is allowed summaries" do
|
|
|
|
SiteSetting.ai_pm_summarization_allowed_groups = group.id
|
2024-10-21 14:15:25 -04:00
|
|
|
expect(guardian.can_see_summary?(pm)).to eq(true)
|
2024-08-20 17:58:24 -04:00
|
|
|
end
|
2024-07-02 11:51:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context "when there is no user" do
|
|
|
|
it "returns false for anons" do
|
2024-10-21 14:15:25 -04:00
|
|
|
expect(anon_guardian.can_see_summary?(topic)).to eq(false)
|
2024-07-02 11:51:59 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true for anons when there is a cached summary" do
|
|
|
|
AiSummary.create!(
|
|
|
|
target: topic,
|
|
|
|
summarized_text: "test",
|
|
|
|
original_content_sha: "123",
|
|
|
|
algorithm: "test",
|
2024-10-15 12:53:26 -04:00
|
|
|
summary_type: AiSummary.summary_types[:complete],
|
2024-07-02 11:51:59 -04:00
|
|
|
)
|
|
|
|
|
2024-10-21 14:15:25 -04:00
|
|
|
expect(guardian.can_see_summary?(topic)).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "#can_see_gists?" do
|
2024-11-26 11:44:12 -05:00
|
|
|
before { SiteSetting.ai_summary_gists_allowed_groups = group.id }
|
2024-10-21 14:15:25 -04:00
|
|
|
let(:guardian) { Guardian.new(user) }
|
|
|
|
|
|
|
|
context "when there is no user" do
|
|
|
|
it "returns false for anons" do
|
|
|
|
expect(anon_guardian.can_see_gists?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-11-19 18:54:49 -05:00
|
|
|
context "when setting is set to everyone" do
|
2024-11-26 11:44:12 -05:00
|
|
|
before { SiteSetting.ai_summary_gists_allowed_groups = Group::AUTO_GROUPS[:everyone] }
|
2024-11-19 18:54:49 -05:00
|
|
|
|
|
|
|
it "returns true" do
|
|
|
|
expect(guardian.can_see_gists?).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-10-21 14:15:25 -04:00
|
|
|
context "when there is a user but it's not a member of the allowed groups" do
|
2024-11-26 11:44:12 -05:00
|
|
|
before { SiteSetting.ai_summary_gists_allowed_groups = "" }
|
2024-10-21 14:15:25 -04:00
|
|
|
|
|
|
|
it "returns false" do
|
|
|
|
expect(guardian.can_see_gists?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when there is a user who is a member of an allowed group" do
|
|
|
|
it "returns false" do
|
|
|
|
expect(guardian.can_see_gists?).to eq(true)
|
2024-07-02 11:51:59 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|