2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe UserOption do
|
2016-03-17 07:03:56 +08:00
|
|
|
describe "#ensure_consistency!" do
|
|
|
|
it "recreates missing user option records" do
|
|
|
|
user = Fabricate(:user)
|
|
|
|
user.user_option.destroy
|
|
|
|
UserOption.ensure_consistency!
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
2019-03-15 22:55:11 +08:00
|
|
|
expect(user.user_option.email_level).to eq(SiteSetting.default_email_level)
|
|
|
|
expect(user.user_option.email_messages_level).to eq(SiteSetting.default_email_messages_level)
|
2016-03-17 07:03:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-05-31 13:43:17 +08:00
|
|
|
describe "defaults" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2016-02-18 13:57:22 +08:00
|
|
|
|
|
|
|
it "should be redirected to top when there is a reason to" do
|
|
|
|
user.user_option.expects(:redirected_to_top).returns(reason: "42")
|
|
|
|
expect(user.user_option.should_be_redirected_to_top).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not be redirected to top when there is no reason to" do
|
|
|
|
user.user_option.expects(:redirected_to_top).returns(nil)
|
|
|
|
expect(user.user_option.should_be_redirected_to_top).to eq(false)
|
|
|
|
end
|
|
|
|
|
2018-10-11 01:00:08 +08:00
|
|
|
it "should not hide the profile and presence by default" do
|
|
|
|
expect(user.user_option.hide_profile_and_presence).to eq(false)
|
|
|
|
end
|
2020-06-02 05:39:16 +08:00
|
|
|
|
|
|
|
it "should correctly set digest frequency" do
|
|
|
|
SiteSetting.default_email_digest_frequency = 1440
|
|
|
|
user = Fabricate(:user)
|
|
|
|
expect(user.user_option.email_digests).to eq(true)
|
|
|
|
expect(user.user_option.digest_after_minutes).to eq(1440)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should correctly set digest frequency when disabled" do
|
|
|
|
SiteSetting.default_email_digest_frequency = 0
|
|
|
|
user = Fabricate(:user)
|
|
|
|
expect(user.user_option.email_digests).to eq(false)
|
|
|
|
expect(user.user_option.digest_after_minutes).to eq(0)
|
|
|
|
end
|
2016-02-18 13:57:22 +08:00
|
|
|
end
|
|
|
|
|
2019-05-31 13:43:17 +08:00
|
|
|
describe "site settings" do
|
|
|
|
it "should apply defaults from site settings" do
|
|
|
|
SiteSetting.default_other_enable_quoting = false
|
|
|
|
SiteSetting.default_other_enable_defer = true
|
|
|
|
SiteSetting.default_other_external_links_in_new_tab = true
|
|
|
|
SiteSetting.default_other_dynamic_favicon = true
|
2020-08-14 21:40:56 +08:00
|
|
|
SiteSetting.default_other_skip_new_user_tips = true
|
2019-05-31 13:43:17 +08:00
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
|
|
|
|
expect(user.user_option.enable_quoting).to eq(false)
|
|
|
|
expect(user.user_option.enable_defer).to eq(true)
|
|
|
|
expect(user.user_option.external_links_in_new_tab).to eq(true)
|
|
|
|
expect(user.user_option.dynamic_favicon).to eq(true)
|
2020-08-14 21:40:56 +08:00
|
|
|
expect(user.user_option.skip_new_user_tips).to eq(true)
|
2019-05-31 13:43:17 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-08 04:25:14 +08:00
|
|
|
describe "#mailing_list_mode" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:forum_user) { Fabricate(:user) }
|
|
|
|
fab!(:mailing_list_user) { Fabricate(:user) }
|
2016-03-05 05:53:28 +08:00
|
|
|
|
|
|
|
before do
|
|
|
|
forum_user.user_option.update(mailing_list_mode: false)
|
|
|
|
mailing_list_user.user_option.update(mailing_list_mode: true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return false when `SiteSetting.disable_mailing_list_mode` is enabled" do
|
2016-03-08 04:25:14 +08:00
|
|
|
SiteSetting.disable_mailing_list_mode = true
|
2016-03-05 05:53:28 +08:00
|
|
|
expect(forum_user.user_option.mailing_list_mode).to eq(false)
|
|
|
|
expect(mailing_list_user.user_option.mailing_list_mode).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should return the stored value when `SiteSetting.disable_mailing_list_mode` is disabled" do
|
2016-03-08 04:25:14 +08:00
|
|
|
SiteSetting.disable_mailing_list_mode = false
|
2016-03-05 05:53:28 +08:00
|
|
|
expect(forum_user.user_option.mailing_list_mode).to eq(false)
|
|
|
|
expect(mailing_list_user.user_option.mailing_list_mode).to eq(true)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-18 13:57:22 +08:00
|
|
|
describe ".redirected_to_top" do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
2016-02-18 13:57:22 +08:00
|
|
|
|
|
|
|
it "should have no reason when `SiteSetting.redirect_users_to_top_page` is disabled" do
|
2017-10-05 04:08:41 +08:00
|
|
|
SiteSetting.redirect_users_to_top_page = false
|
2016-02-18 13:57:22 +08:00
|
|
|
expect(user.user_option.redirected_to_top).to eq(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when `SiteSetting.redirect_users_to_top_page` is enabled" do
|
2017-10-05 04:08:41 +08:00
|
|
|
before { SiteSetting.redirect_users_to_top_page = true }
|
2016-02-18 13:57:22 +08:00
|
|
|
|
|
|
|
it "should have no reason when top is not in the `SiteSetting.top_menu`" do
|
2017-10-05 04:08:41 +08:00
|
|
|
SiteSetting.top_menu = "latest"
|
2016-02-18 13:57:22 +08:00
|
|
|
expect(user.user_option.redirected_to_top).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when top is in the `SiteSetting.top_menu`" do
|
2017-10-05 04:08:41 +08:00
|
|
|
before { SiteSetting.top_menu = "latest|top" }
|
2016-02-18 13:57:22 +08:00
|
|
|
|
|
|
|
it "should have no reason when there are not enough topics" do
|
|
|
|
SiteSetting.expects(:min_redirected_to_top_period).returns(nil)
|
|
|
|
expect(user.user_option.redirected_to_top).to eq(nil)
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when there are enough topics" do
|
2016-02-18 13:57:22 +08:00
|
|
|
before { SiteSetting.expects(:min_redirected_to_top_period).returns(:monthly) }
|
|
|
|
|
|
|
|
describe "a new user" do
|
|
|
|
before do
|
|
|
|
user.stubs(:trust_level).returns(0)
|
|
|
|
user.stubs(:last_seen_at).returns(5.minutes.ago)
|
|
|
|
end
|
|
|
|
|
2022-01-09 06:39:46 +08:00
|
|
|
after { Discourse.redis.flushdb }
|
2020-07-24 17:16:52 +08:00
|
|
|
|
2016-02-18 13:57:22 +08:00
|
|
|
it "should have a reason for the first visit" do
|
2017-10-05 04:08:41 +08:00
|
|
|
freeze_time do
|
|
|
|
delay = SiteSetting.active_user_rate_limit_secs / 2
|
|
|
|
|
2020-07-24 17:16:52 +08:00
|
|
|
expect_enqueued_with(
|
|
|
|
job: :update_top_redirection,
|
|
|
|
args: {
|
|
|
|
user_id: user.id,
|
|
|
|
redirected_at: Time.zone.now.to_s,
|
|
|
|
},
|
|
|
|
at: Time.zone.now + delay,
|
|
|
|
) do
|
|
|
|
expect(user.user_option.redirected_to_top).to eq(
|
|
|
|
reason: I18n.t("redirected_to_top_reasons.new_user"),
|
|
|
|
period: :monthly,
|
|
|
|
)
|
|
|
|
end
|
2017-10-05 04:08:41 +08:00
|
|
|
end
|
2016-02-18 13:57:22 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should not have a reason for next visits" do
|
|
|
|
user.user_option.expects(:last_redirected_to_top_at).returns(10.minutes.ago)
|
|
|
|
user.user_option.expects(:update_last_redirected_to_top!).never
|
|
|
|
|
|
|
|
expect(user.user_option.redirected_to_top).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "an older user" do
|
|
|
|
before { user.stubs(:trust_level).returns(1) }
|
|
|
|
|
|
|
|
it "should have a reason when the user hasn't been seen in a month" do
|
|
|
|
user.last_seen_at = 2.months.ago
|
|
|
|
user.user_option.expects(:update_last_redirected_to_top!).once
|
|
|
|
|
|
|
|
expect(user.user_option.redirected_to_top).to eq(
|
|
|
|
reason: I18n.t("redirected_to_top_reasons.not_seen_in_a_month"),
|
|
|
|
period: :monthly,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-08-20 03:56:14 +08:00
|
|
|
|
|
|
|
describe ".user_tzinfo" do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with user with valid timezone given" do
|
2021-08-20 03:56:14 +08:00
|
|
|
before { user.user_option.update(timezone: "Europe/Paris") }
|
|
|
|
|
|
|
|
it "returns the expect timezone" do
|
|
|
|
expect(UserOption.user_tzinfo(user.id)).to eq(
|
|
|
|
ActiveSupport::TimeZone.find_tzinfo("Europe/Paris"),
|
|
|
|
)
|
|
|
|
end
|
2023-02-22 00:52:04 +08:00
|
|
|
|
|
|
|
it "works for Europe/Kyiv" do
|
|
|
|
user.user_option.update(timezone: "Europe/Kyiv")
|
|
|
|
expect(UserOption.user_tzinfo(user.id)).to eq(
|
|
|
|
ActiveSupport::TimeZone.find_tzinfo("Europe/Kyiv"),
|
|
|
|
)
|
|
|
|
end
|
2021-08-20 03:56:14 +08:00
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with user with invalid timezone given" do
|
2021-08-20 03:56:14 +08:00
|
|
|
before { user.user_option.update(timezone: "Catopia/Catcity") }
|
|
|
|
|
|
|
|
it "fallbacks to UTC" do
|
|
|
|
expect(UserOption.user_tzinfo(user.id)).to eq(ActiveSupport::TimeZone.find_tzinfo("UTC"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-18 13:57:22 +08:00
|
|
|
end
|