2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe GroupShowSerializer do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
2017-07-27 16:51:25 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with an admin user" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:admin) }
|
|
|
|
fab!(:group) { Fabricate(:group, users: [user]) }
|
2016-11-29 16:25:02 +08:00
|
|
|
|
|
|
|
it "should return the right attributes" do
|
|
|
|
json = GroupShowSerializer.new(group, scope: Guardian.new(user)).as_json
|
|
|
|
|
2020-08-19 00:30:08 +08:00
|
|
|
expect(json[:group_show][:is_group_owner]).to eq(nil)
|
2016-11-29 16:25:02 +08:00
|
|
|
expect(json[:group_show][:is_group_user]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with a group owner" do
|
2016-11-29 16:25:02 +08:00
|
|
|
before { group.add_owner(user) }
|
|
|
|
|
|
|
|
it "should return the right attributes" do
|
|
|
|
json = GroupShowSerializer.new(group, scope: Guardian.new(user)).as_json
|
|
|
|
|
|
|
|
expect(json[:group_show][:is_group_owner]).to eq(true)
|
|
|
|
expect(json[:group_show][:is_group_user]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2017-07-27 16:51:25 +08:00
|
|
|
|
|
|
|
describe "#mentionable" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:group) { Fabricate(:group, mentionable_level: Group::ALIAS_LEVELS[:everyone]) }
|
2017-07-27 16:51:25 +08:00
|
|
|
|
|
|
|
it "should return the right value" do
|
|
|
|
json = GroupShowSerializer.new(group, scope: Guardian.new).as_json
|
|
|
|
|
|
|
|
expect(json[:group_show][:mentionable]).to eq(nil)
|
|
|
|
|
|
|
|
json = GroupShowSerializer.new(group, scope: Guardian.new(user)).as_json
|
|
|
|
|
|
|
|
expect(json[:group_show][:mentionable]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2020-09-17 02:58:52 +08:00
|
|
|
|
|
|
|
describe "#automatic_membership_email_domains" do
|
|
|
|
fab!(:group) { Fabricate(:group, automatic_membership_email_domains: "ilovediscourse.com") }
|
|
|
|
let(:admin_guardian) { Guardian.new(Fabricate(:admin)) }
|
|
|
|
|
|
|
|
it "should include email domains for admin" do
|
|
|
|
subject =
|
|
|
|
described_class.new(group, scope: admin_guardian, root: false, owner_group_ids: [group.id])
|
|
|
|
expect(subject.as_json[:automatic_membership_email_domains]).to eq("ilovediscourse.com")
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not include email domains for other users" do
|
|
|
|
subject =
|
|
|
|
described_class.new(group, scope: Guardian.new, root: false, owner_group_ids: [group.id])
|
|
|
|
expect(subject.as_json[:automatic_membership_email_domains]).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "admin only fields" do
|
2023-06-21 22:00:19 +08:00
|
|
|
subject(:serializer) { described_class.new(group, scope: guardian, root: false) }
|
|
|
|
|
2020-09-17 02:58:52 +08:00
|
|
|
fab!(:group) { Fabricate(:group, email_username: "foo@bar.com", email_password: "pa$$w0rd") }
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "for a user" do
|
2020-09-17 02:58:52 +08:00
|
|
|
let(:guardian) { Guardian.new(Fabricate(:user)) }
|
|
|
|
|
|
|
|
it "are not visible" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(serializer.as_json[:email_username]).to be_nil
|
|
|
|
expect(serializer.as_json[:email_password]).to be_nil
|
2020-09-17 02:58:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "for an admin" do
|
2020-09-17 02:58:52 +08:00
|
|
|
let(:guardian) { Guardian.new(Fabricate(:admin)) }
|
|
|
|
|
|
|
|
it "are visible" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(serializer.as_json[:email_username]).to eq("foo@bar.com")
|
|
|
|
expect(serializer.as_json[:email_password]).to eq("pa$$w0rd")
|
|
|
|
expect(serializer.as_json[:message_count]).to eq(0)
|
2020-09-17 02:58:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "default notification settings" do
|
2023-06-21 22:00:19 +08:00
|
|
|
subject(:serializer) { described_class.new(group, scope: guardian, root: false) }
|
|
|
|
|
2020-09-17 02:58:52 +08:00
|
|
|
let(:category1) { Fabricate(:category) }
|
|
|
|
let(:category2) { Fabricate(:category) }
|
|
|
|
let(:tag1) { Fabricate(:tag) }
|
|
|
|
let(:tag2) { Fabricate(:tag) }
|
|
|
|
|
|
|
|
before do
|
2020-09-17 04:02:28 +08:00
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
|
2020-09-17 02:58:52 +08:00
|
|
|
GroupCategoryNotificationDefault.create!(
|
|
|
|
group: group,
|
|
|
|
category: category1,
|
|
|
|
notification_level: GroupCategoryNotificationDefault.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
GroupCategoryNotificationDefault.create!(
|
|
|
|
group: group,
|
|
|
|
category: category2,
|
|
|
|
notification_level: GroupCategoryNotificationDefault.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
GroupTagNotificationDefault.create!(
|
|
|
|
group: group,
|
|
|
|
tag: tag1,
|
|
|
|
notification_level: GroupTagNotificationDefault.notification_levels[:watching],
|
|
|
|
)
|
|
|
|
GroupTagNotificationDefault.create!(
|
|
|
|
group: group,
|
|
|
|
tag: tag2,
|
|
|
|
notification_level: GroupTagNotificationDefault.notification_levels[:tracking],
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "for a user" do
|
2020-09-17 02:58:52 +08:00
|
|
|
let(:guardian) { Guardian.new(Fabricate(:user)) }
|
|
|
|
|
|
|
|
it "are not visible" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(
|
|
|
|
serializer.as_json.keys.select { |k| k.to_s.ends_with?("_category_ids") },
|
|
|
|
).to be_empty
|
|
|
|
expect(serializer.as_json.keys.select { |k| k.to_s.ends_with?("_tags") }).to be_empty
|
2020-09-17 02:58:52 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "for admin" do
|
2020-09-17 02:58:52 +08:00
|
|
|
let(:guardian) { Guardian.new(Fabricate(:admin)) }
|
|
|
|
|
|
|
|
it "are correct" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(serializer.as_json[:watching_category_ids]).to eq([category1.id])
|
|
|
|
expect(serializer.as_json[:tracking_category_ids]).to eq([category2.id])
|
|
|
|
expect(serializer.as_json[:watching_first_post_category_ids]).to eq([])
|
|
|
|
expect(serializer.as_json[:regular_category_ids]).to eq([])
|
|
|
|
expect(serializer.as_json[:muted_category_ids]).to eq([])
|
|
|
|
|
|
|
|
expect(serializer.as_json[:watching_tags]).to eq([tag1.name])
|
|
|
|
expect(serializer.as_json[:tracking_tags]).to eq([tag2.name])
|
|
|
|
expect(serializer.as_json[:watching_first_post_tags]).to eq([])
|
|
|
|
expect(serializer.as_json[:regular_tags]).to eq([])
|
|
|
|
expect(serializer.as_json[:muted_tags]).to eq([])
|
2020-09-17 02:58:52 +08:00
|
|
|
end
|
2020-09-17 04:02:28 +08:00
|
|
|
|
|
|
|
it "doesn't include tag fields if tags are disabled" do
|
|
|
|
SiteSetting.tagging_enabled = false
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(
|
|
|
|
serializer.as_json.keys.select { |k| k.to_s.ends_with?("_category_ids") }.length,
|
|
|
|
).to eq(5)
|
|
|
|
expect(serializer.as_json.keys.select { |k| k.to_s.ends_with?("_tags") }).to be_empty
|
2020-09-17 04:02:28 +08:00
|
|
|
end
|
2020-09-17 02:58:52 +08:00
|
|
|
end
|
|
|
|
end
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|