2019-09-11 10:52:06 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
require "rails_helper"
|
2019-09-11 10:09:41 -04:00
|
|
|
|
|
|
|
describe Guardian do
|
2019-09-11 10:52:06 -04:00
|
|
|
before { SiteSetting.data_explorer_enabled = true }
|
|
|
|
|
2019-09-11 10:09:41 -04:00
|
|
|
def make_query(group_ids = [])
|
2022-12-29 07:31:29 -05:00
|
|
|
query =
|
2023-03-22 17:29:08 -04:00
|
|
|
DiscourseDataExplorer::Query.create!(
|
2022-12-29 07:31:29 -05:00
|
|
|
name: "Query number #{Fabrication::Sequencer.sequence("query-id", 1)}",
|
|
|
|
sql: "SELECT 1",
|
|
|
|
)
|
2020-08-26 21:36:51 -04:00
|
|
|
|
2022-12-29 07:31:29 -05:00
|
|
|
group_ids.each { |group_id| query.query_groups.create!(group_id: group_id) }
|
2020-08-26 21:36:51 -04:00
|
|
|
|
2020-08-26 20:29:57 -04:00
|
|
|
query
|
2019-09-11 10:09:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
let(:user) { build(:user) }
|
|
|
|
let(:admin) { build(:admin) }
|
2024-02-29 12:15:57 -05:00
|
|
|
fab!(:group)
|
2019-09-11 10:52:06 -04:00
|
|
|
|
2019-09-11 10:09:41 -04:00
|
|
|
describe "#user_is_a_member_of_group?" do
|
|
|
|
it "is true when the user is an admin" do
|
|
|
|
expect(Guardian.new(admin).user_is_a_member_of_group?(group)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is true when the user is not an admin, but is a member of the group" do
|
|
|
|
group.add(user)
|
|
|
|
|
|
|
|
expect(Guardian.new(user).user_is_a_member_of_group?(group)).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is false when the user is not an admin, and is not a member of the group" do
|
|
|
|
expect(Guardian.new(user).user_is_a_member_of_group?(group)).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-26 20:29:57 -04:00
|
|
|
describe "#group_and_user_can_access_query?" do
|
2019-09-11 10:09:41 -04:00
|
|
|
it "is true if the user is an admin" do
|
2020-08-26 20:29:57 -04:00
|
|
|
expect(Guardian.new(admin).group_and_user_can_access_query?(group, make_query)).to eq(true)
|
2019-09-11 10:09:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is true if the user is a member of the group, and query contains the group id" do
|
|
|
|
query = make_query(["#{group.id}"])
|
|
|
|
group.add(user)
|
|
|
|
|
2020-08-26 20:29:57 -04:00
|
|
|
expect(Guardian.new(user).group_and_user_can_access_query?(group, query)).to eq(true)
|
2019-09-11 10:09:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if the query does not contain the group id" do
|
|
|
|
group.add(user)
|
2019-09-11 10:52:06 -04:00
|
|
|
|
2020-08-26 20:29:57 -04:00
|
|
|
expect(Guardian.new(user).group_and_user_can_access_query?(group, make_query)).to eq(false)
|
2019-09-11 10:09:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if the user is not member of the group" do
|
|
|
|
query = make_query(["#{group.id}"])
|
2019-09-11 10:52:06 -04:00
|
|
|
|
2020-08-26 20:29:57 -04:00
|
|
|
expect(Guardian.new(user).group_and_user_can_access_query?(group, query)).to eq(false)
|
2019-09-11 10:09:41 -04:00
|
|
|
end
|
|
|
|
end
|
2019-09-11 10:52:06 -04:00
|
|
|
end
|