2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-07 22:54:31 -05:00
|
|
|
RSpec.describe CurrentUserSerializer do
|
2022-06-30 02:54:20 -04:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2021-08-26 12:16:00 -04:00
|
|
|
subject(:serializer) { described_class.new(user, scope: guardian, root: false) }
|
|
|
|
|
2022-06-30 02:54:20 -04:00
|
|
|
let(:guardian) { Guardian.new(user) }
|
2021-08-26 12:16:00 -04:00
|
|
|
|
2018-03-07 22:54:31 -05:00
|
|
|
context "when SSO is not enabled" do
|
|
|
|
it "should not include the external_id field" do
|
|
|
|
payload = serializer.as_json
|
|
|
|
expect(payload).not_to have_key(:external_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when SSO is enabled" do
|
|
|
|
let :user do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
SingleSignOnRecord.create!(user_id: user.id, external_id: '12345', last_payload: '')
|
|
|
|
user
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the external_id" do
|
2021-02-08 05:04:33 -05:00
|
|
|
SiteSetting.discourse_connect_url = "http://example.com/discourse_sso"
|
|
|
|
SiteSetting.discourse_connect_secret = "12345678910"
|
|
|
|
SiteSetting.enable_discourse_connect = true
|
2018-03-07 22:54:31 -05:00
|
|
|
payload = serializer.as_json
|
|
|
|
expect(payload[:external_id]).to eq("12345")
|
|
|
|
end
|
|
|
|
end
|
2018-07-27 02:41:07 -04:00
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#top_category_ids" do
|
2019-05-06 23:12:20 -04:00
|
|
|
fab!(:category1) { Fabricate(:category) }
|
|
|
|
fab!(:category2) { Fabricate(:category) }
|
|
|
|
fab!(:category3) { Fabricate(:category) }
|
2018-07-27 02:41:07 -04:00
|
|
|
|
|
|
|
it "should include empty top_category_ids array" do
|
|
|
|
payload = serializer.as_json
|
|
|
|
expect(payload[:top_category_ids]).to eq([])
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include correct id in top_category_ids array" do
|
2019-04-30 02:58:18 -04:00
|
|
|
_category = Category.first
|
2018-07-27 02:41:07 -04:00
|
|
|
CategoryUser.create!(user_id: user.id,
|
|
|
|
category_id: category1.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:tracking])
|
|
|
|
|
|
|
|
CategoryUser.create!(user_id: user.id,
|
|
|
|
category_id: category2.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:watching])
|
2018-07-30 06:36:36 -04:00
|
|
|
|
|
|
|
CategoryUser.create!(user_id: user.id,
|
|
|
|
category_id: category3.id,
|
|
|
|
notification_level: CategoryUser.notification_levels[:regular])
|
|
|
|
|
2018-07-27 02:41:07 -04:00
|
|
|
payload = serializer.as_json
|
|
|
|
expect(payload[:top_category_ids]).to eq([category2.id, category1.id])
|
|
|
|
end
|
|
|
|
end
|
2018-12-18 02:41:42 -05:00
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#muted_tag" do
|
2019-12-09 17:50:05 -05:00
|
|
|
fab!(:tag) { Fabricate(:tag) }
|
2022-06-14 03:39:56 -04:00
|
|
|
|
2019-12-09 17:50:05 -05:00
|
|
|
let!(:tag_user) do
|
2022-06-14 03:39:56 -04:00
|
|
|
TagUser.create!(
|
|
|
|
user_id: user.id,
|
|
|
|
notification_level: TagUser.notification_levels[:muted],
|
|
|
|
tag_id: tag.id
|
|
|
|
)
|
2019-12-09 17:50:05 -05:00
|
|
|
end
|
|
|
|
|
2022-06-14 03:39:56 -04:00
|
|
|
it 'includes muted tag names' do
|
2019-12-09 17:50:05 -05:00
|
|
|
payload = serializer.as_json
|
2022-06-14 03:39:56 -04:00
|
|
|
expect(payload[:muted_tags]).to eq([tag.name])
|
2019-12-09 17:50:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#second_factor_enabled" do
|
2021-08-26 12:16:00 -04:00
|
|
|
let(:guardian) { Guardian.new(user) }
|
2019-11-08 00:11:53 -05:00
|
|
|
let(:json) { serializer.as_json }
|
|
|
|
|
|
|
|
it "is false by default" do
|
|
|
|
expect(json[:second_factor_enabled]).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when totp enabled" do
|
|
|
|
before do
|
|
|
|
User.any_instance.stubs(:totp_enabled?).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is true" do
|
|
|
|
expect(json[:second_factor_enabled]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when security_keys enabled" do
|
|
|
|
before do
|
|
|
|
User.any_instance.stubs(:security_keys_enabled?).returns(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is true" do
|
|
|
|
expect(json[:second_factor_enabled]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#groups" do
|
2018-12-18 02:41:42 -05:00
|
|
|
it "should only show visible groups" do
|
|
|
|
Fabricate.build(:group, visibility_level: Group.visibility_levels[:public])
|
|
|
|
hidden_group = Fabricate.build(:group, visibility_level: Group.visibility_levels[:owners])
|
2020-09-17 13:03:42 -04:00
|
|
|
public_group = Fabricate.build(:group, visibility_level: Group.visibility_levels[:public], name: "UppercaseGroupName")
|
2021-08-26 12:16:00 -04:00
|
|
|
hidden_group.add(user)
|
2018-12-18 02:41:42 -05:00
|
|
|
hidden_group.save!
|
2021-08-26 12:16:00 -04:00
|
|
|
public_group.add(user)
|
2018-12-18 02:41:42 -05:00
|
|
|
public_group.save!
|
|
|
|
payload = serializer.as_json
|
|
|
|
|
2021-09-07 00:30:40 -04:00
|
|
|
expect(payload[:groups]).to contain_exactly(
|
|
|
|
{ id: public_group.id, name: public_group.name, has_messages: false }
|
|
|
|
)
|
2018-12-18 02:41:42 -05:00
|
|
|
end
|
|
|
|
end
|
2021-03-19 09:19:15 -04:00
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#has_topic_draft" do
|
2021-03-19 09:19:15 -04:00
|
|
|
it "is not included by default" do
|
|
|
|
payload = serializer.as_json
|
|
|
|
expect(payload).not_to have_key(:has_topic_draft)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns true when user has a draft" do
|
|
|
|
Draft.set(user, Draft::NEW_TOPIC, 0, "test1")
|
|
|
|
|
|
|
|
payload = serializer.as_json
|
|
|
|
expect(payload[:has_topic_draft]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "clearing a draft removes has_topic_draft from payload" do
|
|
|
|
sequence = Draft.set(user, Draft::NEW_TOPIC, 0, "test1")
|
|
|
|
Draft.clear(user, Draft::NEW_TOPIC, sequence)
|
|
|
|
|
|
|
|
payload = serializer.as_json
|
|
|
|
expect(payload).not_to have_key(:has_topic_draft)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
2021-03-19 15:20:41 -04:00
|
|
|
|
2022-07-27 06:21:10 -04:00
|
|
|
describe "#can_review" do
|
2021-08-26 12:16:00 -04:00
|
|
|
let(:guardian) { Guardian.new(user) }
|
|
|
|
let(:payload) { serializer.as_json }
|
|
|
|
|
|
|
|
context "when user is a regular one" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
2021-03-19 15:20:41 -04:00
|
|
|
|
2021-08-26 12:16:00 -04:00
|
|
|
it "return false for regular users" do
|
|
|
|
expect(payload[:can_review]).to eq(false)
|
|
|
|
end
|
2021-03-19 15:20:41 -04:00
|
|
|
end
|
|
|
|
|
2021-08-26 12:16:00 -04:00
|
|
|
context "when user is a staff member" do
|
|
|
|
let(:user) { Fabricate(:admin) }
|
2021-03-19 15:20:41 -04:00
|
|
|
|
2021-08-26 12:16:00 -04:00
|
|
|
it "returns true" do
|
|
|
|
expect(payload[:can_review]).to eq(true)
|
|
|
|
end
|
2021-03-19 15:20:41 -04:00
|
|
|
end
|
2021-08-26 12:16:00 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "#pending_posts_count" do
|
|
|
|
subject(:pending_posts_count) { serializer.pending_posts_count }
|
|
|
|
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
before { user.user_stat.pending_posts_count = 3 }
|
2021-03-19 15:20:41 -04:00
|
|
|
|
2021-08-26 12:16:00 -04:00
|
|
|
it "serializes 'pending_posts_count'" do
|
|
|
|
expect(pending_posts_count).to eq 3
|
2021-03-19 15:20:41 -04:00
|
|
|
end
|
|
|
|
end
|
2022-05-27 05:15:14 -04:00
|
|
|
|
|
|
|
describe "#status" do
|
|
|
|
fab!(:user_status) { Fabricate(:user_status) }
|
|
|
|
fab!(:user) { Fabricate(:user, user_status: user_status) }
|
|
|
|
let(:serializer) { described_class.new(user, scope: Guardian.new(user), root: false) }
|
|
|
|
|
2022-07-05 11:12:22 -04:00
|
|
|
it "adds user status when enabled" do
|
2022-05-27 05:15:14 -04:00
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:status]).to_not be_nil do |status|
|
|
|
|
expect(status.description).to eq(user_status.description)
|
|
|
|
expect(status.emoji).to eq(user_status.emoji)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-05 11:12:22 -04:00
|
|
|
it "doesn't add user status when disabled" do
|
2022-05-27 05:15:14 -04:00
|
|
|
SiteSetting.enable_user_status = false
|
|
|
|
json = serializer.as_json
|
|
|
|
expect(json.keys).not_to include :status
|
|
|
|
end
|
2022-07-05 11:12:22 -04:00
|
|
|
|
|
|
|
it "doesn't add expired user status" do
|
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
|
|
|
user.user_status.ends_at = 1.minutes.ago
|
|
|
|
serializer = described_class.new(user, scope: Guardian.new(user), root: false)
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json.keys).not_to include :status
|
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't return status if user doesn't have it set" do
|
|
|
|
SiteSetting.enable_user_status = true
|
|
|
|
|
|
|
|
user.clear_status!
|
|
|
|
user.reload
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json.keys).not_to include :status
|
|
|
|
end
|
2022-05-27 05:15:14 -04:00
|
|
|
end
|
2022-06-30 02:54:20 -04:00
|
|
|
|
2022-08-11 23:26:56 -04:00
|
|
|
describe '#sidebar_tags' do
|
2022-08-21 18:46:20 -04:00
|
|
|
fab!(:tag_1) { Fabricate(:tag, name: "foo") }
|
|
|
|
fab!(:tag_2) { Fabricate(:tag, name: "bar") }
|
|
|
|
fab!(:hidden_tag) { Fabricate(:tag, name: "secret") }
|
|
|
|
fab!(:staff_tag_group) { Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: ["secret"]) }
|
|
|
|
let(:tag_sidebar_section_link) { Fabricate(:tag_sidebar_section_link, user: user) }
|
|
|
|
let(:tag_sidebar_section_link_2) { Fabricate(:tag_sidebar_section_link, user: user) }
|
2022-06-30 02:54:20 -04:00
|
|
|
|
2022-08-11 23:26:56 -04:00
|
|
|
it "is not included when experimental sidebar has not been enabled" do
|
2022-08-21 18:46:20 -04:00
|
|
|
tag_sidebar_section_link
|
2022-07-27 01:42:26 -04:00
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = false
|
2022-08-11 23:26:56 -04:00
|
|
|
SiteSetting.tagging_enabled = true
|
2022-06-30 02:54:20 -04:00
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
2022-08-11 23:26:56 -04:00
|
|
|
expect(json[:sidebar_tags]).to eq(nil)
|
2022-06-30 02:54:20 -04:00
|
|
|
end
|
|
|
|
|
2022-08-11 23:26:56 -04:00
|
|
|
it "is not included when tagging has not been enabled" do
|
2022-08-21 18:46:20 -04:00
|
|
|
tag_sidebar_section_link
|
2022-07-27 01:42:26 -04:00
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = true
|
2022-06-30 02:54:20 -04:00
|
|
|
SiteSetting.tagging_enabled = false
|
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
2022-08-11 23:26:56 -04:00
|
|
|
expect(json[:sidebar_tags]).to eq(nil)
|
2022-06-30 02:54:20 -04:00
|
|
|
end
|
|
|
|
|
2022-08-11 23:26:56 -04:00
|
|
|
it "is present when experimental sidebar and tagging has been enabled" do
|
2022-08-21 18:46:20 -04:00
|
|
|
tag_sidebar_section_link
|
2022-07-27 01:42:26 -04:00
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = true
|
2022-06-30 02:54:20 -04:00
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
|
2022-08-11 23:26:56 -04:00
|
|
|
tag_sidebar_section_link_2.linkable.update!(pm_topic_count: 5, topic_count: 0)
|
|
|
|
|
2022-06-30 02:54:20 -04:00
|
|
|
json = serializer.as_json
|
|
|
|
|
2022-08-11 23:26:56 -04:00
|
|
|
expect(json[:sidebar_tags]).to contain_exactly(
|
|
|
|
{ name: tag_sidebar_section_link.linkable.name, pm_only: false },
|
|
|
|
{ name: tag_sidebar_section_link_2.linkable.name, pm_only: true }
|
2022-06-30 02:54:20 -04:00
|
|
|
)
|
|
|
|
end
|
2022-08-21 18:46:20 -04:00
|
|
|
|
|
|
|
it 'includes visible default sidebar tags' do
|
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = true
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.default_sidebar_tags = "foo|bar|secret"
|
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:sidebar_tags]).to eq([
|
|
|
|
{ name: "foo", pm_only: false },
|
|
|
|
{ name: "bar", pm_only: false }
|
|
|
|
])
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'includes tags choosen by user' do
|
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = true
|
|
|
|
SiteSetting.tagging_enabled = true
|
|
|
|
SiteSetting.default_sidebar_tags = "foo|bar|secret"
|
|
|
|
tag_sidebar_section_link = Fabricate(:tag_sidebar_section_link, user: user)
|
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:sidebar_tags]).to eq([
|
|
|
|
{ name: tag_sidebar_section_link.linkable.name, pm_only: false }
|
|
|
|
])
|
|
|
|
end
|
2022-06-30 02:54:20 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe '#sidebar_category_ids' do
|
2022-08-21 18:46:20 -04:00
|
|
|
fab!(:category) { Fabricate(:category) }
|
|
|
|
fab!(:category_2) { Fabricate(:category) }
|
|
|
|
fab!(:private_category) { Fabricate(:private_category, group: Fabricate(:group)) }
|
|
|
|
let(:category_sidebar_section_link) { Fabricate(:category_sidebar_section_link, user: user) }
|
|
|
|
let(:category_sidebar_section_link_2) { Fabricate(:category_sidebar_section_link, user: user) }
|
2022-06-30 02:54:20 -04:00
|
|
|
|
2022-07-27 01:42:26 -04:00
|
|
|
it "is not included when SiteSeting.enable_experimental_sidebar_hamburger is false" do
|
2022-08-21 18:46:20 -04:00
|
|
|
category_sidebar_section_link
|
2022-07-27 01:42:26 -04:00
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = false
|
2022-06-30 02:54:20 -04:00
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:sidebar_category_ids]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2022-07-27 01:42:26 -04:00
|
|
|
it "is not included when experimental sidebar has not been enabled" do
|
2022-08-21 18:46:20 -04:00
|
|
|
category_sidebar_section_link
|
2022-07-27 01:42:26 -04:00
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = false
|
2022-06-30 02:54:20 -04:00
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
|
|
|
|
expect(json[:sidebar_category_ids]).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2022-08-21 18:46:20 -04:00
|
|
|
it 'includes visible default sidebar categories' do
|
2022-07-27 01:42:26 -04:00
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = true
|
2022-08-21 18:46:20 -04:00
|
|
|
SiteSetting.default_sidebar_categories = "#{category.id}|#{category_2.id}|#{private_category.id}"
|
2022-06-30 02:54:20 -04:00
|
|
|
|
|
|
|
json = serializer.as_json
|
2022-08-21 18:46:20 -04:00
|
|
|
expect(json[:sidebar_category_ids]).to eq([category.id, category_2.id])
|
|
|
|
end
|
2022-06-30 02:54:20 -04:00
|
|
|
|
2022-08-21 18:46:20 -04:00
|
|
|
it 'includes categories choosen by user' do
|
|
|
|
SiteSetting.enable_experimental_sidebar_hamburger = true
|
|
|
|
SiteSetting.default_sidebar_categories = "#{category.id}|#{category_2.id}|#{private_category.id}"
|
|
|
|
|
|
|
|
category_sidebar_section_link
|
|
|
|
category_sidebar_section_link_2
|
|
|
|
|
|
|
|
json = serializer.as_json
|
|
|
|
expect(json[:sidebar_category_ids]).to eq([category_sidebar_section_link.linkable.id, category_sidebar_section_link_2.linkable.id])
|
2022-06-30 02:54:20 -04:00
|
|
|
end
|
|
|
|
end
|
2022-07-25 08:19:53 -04:00
|
|
|
|
|
|
|
describe "#likes_notifications_disabled" do
|
|
|
|
it "is true if the user disables likes notifications" do
|
|
|
|
user.user_option.update!(like_notification_frequency: UserOption.like_notification_frequency_type[:never])
|
|
|
|
expect(serializer.as_json[:likes_notifications_disabled]).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "is false if the user doesn't disable likes notifications" do
|
|
|
|
user.user_option.update!(like_notification_frequency: UserOption.like_notification_frequency_type[:always])
|
|
|
|
expect(serializer.as_json[:likes_notifications_disabled]).to eq(false)
|
|
|
|
user.user_option.update!(like_notification_frequency: UserOption.like_notification_frequency_type[:first_time_and_daily])
|
|
|
|
expect(serializer.as_json[:likes_notifications_disabled]).to eq(false)
|
|
|
|
user.user_option.update!(like_notification_frequency: UserOption.like_notification_frequency_type[:first_time])
|
|
|
|
expect(serializer.as_json[:likes_notifications_disabled]).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
2022-09-21 00:32:47 -04:00
|
|
|
|
|
|
|
describe '#redesigned_user_page_nav_enabled' do
|
|
|
|
fab!(:group) { Fabricate(:group) }
|
|
|
|
fab!(:group2) { Fabricate(:group) }
|
|
|
|
|
|
|
|
it "is false when enable_new_user_profile_nav_groups site setting has not been set" do
|
|
|
|
expect(serializer.as_json[:redesigned_user_page_nav_enabled]).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is false if user does not belong to any of the configured groups in the enable_new_user_profile_nav_groups site setting' do
|
|
|
|
SiteSetting.enable_new_user_profile_nav_groups = "#{group.id}|#{group2.id}"
|
|
|
|
|
|
|
|
expect(serializer.as_json[:redesigned_user_page_nav_enabled]).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is true if user belongs one of the configured groups in the enable_new_user_profile_nav_groups site setting' do
|
|
|
|
SiteSetting.enable_new_user_profile_nav_groups = "#{group.id}|#{group2.id}"
|
|
|
|
group.add(user)
|
|
|
|
|
|
|
|
expect(serializer.as_json[:redesigned_user_page_nav_enabled]).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
2018-03-07 22:54:31 -05:00
|
|
|
end
|