2019-04-30 08:27:42 +08:00
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
|
RSpec.describe UserUpdater do
|
2023-11-10 06:47:59 +08:00
|
|
|
|
fab!(:user)
|
2021-12-23 01:09:43 +08:00
|
|
|
|
fab!(:u1) { Fabricate(:user) }
|
|
|
|
|
fab!(:u2) { Fabricate(:user) }
|
|
|
|
|
fab!(:u3) { Fabricate(:user) }
|
2013-12-11 01:46:35 +08:00
|
|
|
|
|
|
|
|
|
let(:acting_user) { Fabricate.build(:user) }
|
|
|
|
|
|
2015-03-31 07:16:11 +08:00
|
|
|
|
describe "#update_muted_users" do
|
|
|
|
|
it "has no cross talk" do
|
|
|
|
|
updater = UserUpdater.new(u1, u1)
|
|
|
|
|
updater.update_muted_users("#{u2.username},#{u3.username}")
|
|
|
|
|
|
|
|
|
|
updater = UserUpdater.new(u2, u2)
|
|
|
|
|
updater.update_muted_users("#{u3.username},#{u1.username}")
|
|
|
|
|
|
|
|
|
|
updater = UserUpdater.new(u3, u3)
|
|
|
|
|
updater.update_muted_users("")
|
|
|
|
|
|
2019-03-21 03:55:46 +08:00
|
|
|
|
expect(MutedUser.where(user_id: u2.id).pluck(:muted_user_id)).to match_array([u3.id, u1.id])
|
|
|
|
|
expect(MutedUser.where(user_id: u1.id).pluck(:muted_user_id)).to match_array([u2.id, u3.id])
|
|
|
|
|
expect(MutedUser.where(user_id: u3.id).count).to eq(0)
|
2019-03-06 19:21:58 +08:00
|
|
|
|
end
|
2019-03-20 18:18:46 +08:00
|
|
|
|
|
|
|
|
|
it "excludes acting user" do
|
|
|
|
|
updater = UserUpdater.new(u1, u1)
|
|
|
|
|
updater.update_muted_users("#{u1.username},#{u2.username}")
|
|
|
|
|
|
2019-03-21 03:55:46 +08:00
|
|
|
|
expect(MutedUser.where(muted_user_id: u2.id).pluck(:muted_user_id)).to match_array([u2.id])
|
2019-03-20 18:18:46 +08:00
|
|
|
|
end
|
2019-03-06 19:21:58 +08:00
|
|
|
|
end
|
|
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
|
describe "#update" do
|
2023-11-10 06:47:59 +08:00
|
|
|
|
fab!(:category)
|
|
|
|
|
fab!(:tag)
|
2019-05-07 11:12:20 +08:00
|
|
|
|
fab!(:tag2) { Fabricate(:tag) }
|
2018-03-30 03:08:22 +08:00
|
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
|
it "saves user" do
|
|
|
|
|
user = Fabricate(:user, name: "Billy Bob")
|
2021-07-28 14:07:18 +08:00
|
|
|
|
updater = UserUpdater.new(user, user)
|
2013-11-02 05:06:20 +08:00
|
|
|
|
|
|
|
|
|
updater.update(name: "Jim Tom")
|
|
|
|
|
|
|
|
|
|
expect(user.reload.name).to eq "Jim Tom"
|
|
|
|
|
end
|
|
|
|
|
|
2023-05-26 08:26:38 +08:00
|
|
|
|
describe "the within_user_updater_transaction event" do
|
|
|
|
|
it "allows plugins to perform additional updates" do
|
|
|
|
|
update_attributes = { name: "Jimmmy Johnny" }
|
|
|
|
|
handler =
|
|
|
|
|
Proc.new do |user, attrs|
|
|
|
|
|
user.user_profile.update!(bio_raw: "hello world I'm Jimmmy")
|
|
|
|
|
expect(attrs).to eq(update_attributes)
|
|
|
|
|
end
|
|
|
|
|
DiscourseEvent.on(:within_user_updater_transaction, &handler)
|
|
|
|
|
|
|
|
|
|
updater = UserUpdater.new(user, user)
|
|
|
|
|
updater.update(update_attributes)
|
|
|
|
|
|
|
|
|
|
expect(user.reload.name).to eq("Jimmmy Johnny")
|
|
|
|
|
expect(user.user_profile.bio_raw).to eq("hello world I'm Jimmmy")
|
|
|
|
|
ensure
|
|
|
|
|
DiscourseEvent.off(:within_user_updater_transaction, &handler)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "can cancel the whole update transaction if a handler raises" do
|
|
|
|
|
error_class = Class.new(StandardError)
|
|
|
|
|
handler = Proc.new { raise error_class.new }
|
|
|
|
|
|
|
|
|
|
DiscourseEvent.on(:within_user_updater_transaction, &handler)
|
|
|
|
|
|
|
|
|
|
old_name = user.name
|
|
|
|
|
updater = UserUpdater.new(user, user)
|
|
|
|
|
|
|
|
|
|
expect { updater.update(name: "Failure McClario") }.to raise_error(error_class)
|
|
|
|
|
|
|
|
|
|
expect(user.reload.name).to eq(old_name)
|
|
|
|
|
ensure
|
|
|
|
|
DiscourseEvent.off(:within_user_updater_transaction, &handler)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2016-07-08 10:58:18 +08:00
|
|
|
|
it "can update categories and tags" do
|
2021-07-17 02:50:40 +08:00
|
|
|
|
updater = UserUpdater.new(user, user)
|
2018-03-30 03:08:22 +08:00
|
|
|
|
updater.update(watched_tags: "#{tag.name},#{tag2.name}", muted_category_ids: [category.id])
|
2016-07-08 10:58:18 +08:00
|
|
|
|
|
|
|
|
|
expect(
|
|
|
|
|
TagUser.where(
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
tag_id: tag.id,
|
|
|
|
|
notification_level: TagUser.notification_levels[:watching],
|
2018-03-30 03:08:22 +08:00
|
|
|
|
).exists?,
|
|
|
|
|
).to eq(true)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
|
2018-03-30 03:08:22 +08:00
|
|
|
|
expect(
|
|
|
|
|
TagUser.where(
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
tag_id: tag2.id,
|
|
|
|
|
notification_level: TagUser.notification_levels[:watching],
|
|
|
|
|
).exists?,
|
|
|
|
|
).to eq(true)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
|
2016-07-08 10:58:18 +08:00
|
|
|
|
expect(
|
|
|
|
|
CategoryUser.where(
|
|
|
|
|
user_id: user.id,
|
|
|
|
|
category_id: category.id,
|
|
|
|
|
notification_level: CategoryUser.notification_levels[:muted],
|
|
|
|
|
).count,
|
|
|
|
|
).to eq(1)
|
2021-07-17 02:50:40 +08:00
|
|
|
|
end
|
2016-07-08 10:58:18 +08:00
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
|
context "with a staged user" do
|
2021-07-17 02:50:40 +08:00
|
|
|
|
let(:staged_user) { Fabricate(:staged) }
|
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
|
context "when allow_changing_staged_user_tracking is false" do
|
2021-07-17 02:50:40 +08:00
|
|
|
|
before { SiteSetting.allow_changing_staged_user_tracking = false }
|
|
|
|
|
|
|
|
|
|
it "doesn't update muted categories and watched tags" do
|
|
|
|
|
updater = UserUpdater.new(Fabricate(:admin), staged_user)
|
|
|
|
|
updater.update(watched_tags: "#{tag.name}", muted_category_ids: [category.id])
|
|
|
|
|
expect(TagUser.exists?(user_id: staged_user.id)).to eq(false)
|
|
|
|
|
expect(CategoryUser.exists?(user_id: staged_user.id)).to eq(false)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
|
context "when allow_changing_staged_user_tracking is true" do
|
2021-07-17 02:50:40 +08:00
|
|
|
|
before { SiteSetting.allow_changing_staged_user_tracking = true }
|
|
|
|
|
|
|
|
|
|
it "updates muted categories and watched tags" do
|
|
|
|
|
updater = UserUpdater.new(Fabricate(:admin), staged_user)
|
|
|
|
|
updater.update(watched_tags: "#{tag.name}", muted_category_ids: [category.id])
|
|
|
|
|
expect(
|
|
|
|
|
TagUser.exists?(
|
|
|
|
|
user_id: staged_user.id,
|
|
|
|
|
tag_id: tag.id,
|
|
|
|
|
notification_level: TagUser.notification_levels[:watching],
|
2023-01-09 19:18:21 +08:00
|
|
|
|
),
|
2021-07-17 02:50:40 +08:00
|
|
|
|
).to eq(true)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
|
2021-07-17 02:50:40 +08:00
|
|
|
|
expect(
|
|
|
|
|
CategoryUser.exists?(
|
|
|
|
|
user_id: staged_user.id,
|
|
|
|
|
category_id: category.id,
|
|
|
|
|
notification_level: CategoryUser.notification_levels[:muted],
|
2023-01-09 19:18:21 +08:00
|
|
|
|
),
|
2021-07-17 02:50:40 +08:00
|
|
|
|
).to eq(true)
|
|
|
|
|
end
|
|
|
|
|
end
|
2016-07-08 10:58:18 +08:00
|
|
|
|
end
|
|
|
|
|
|
2018-03-30 03:08:22 +08:00
|
|
|
|
it "doesn't remove notification prefs when updating something else" do
|
|
|
|
|
TagUser.create!(
|
|
|
|
|
user: user,
|
|
|
|
|
tag: tag,
|
|
|
|
|
notification_level: TagUser.notification_levels[:watching],
|
|
|
|
|
)
|
|
|
|
|
CategoryUser.create!(
|
|
|
|
|
user: user,
|
|
|
|
|
category: category,
|
|
|
|
|
notification_level: CategoryUser.notification_levels[:muted],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
updater.update(name: "Steve Dave")
|
|
|
|
|
|
|
|
|
|
expect(TagUser.where(user: user).count).to eq(1)
|
|
|
|
|
expect(CategoryUser.where(user: user).count).to eq(1)
|
|
|
|
|
end
|
|
|
|
|
|
2016-02-17 12:46:19 +08:00
|
|
|
|
it "updates various fields" do
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2016-11-15 16:10:20 +08:00
|
|
|
|
date_of_birth = Time.zone.now
|
2021-03-05 04:24:37 +08:00
|
|
|
|
SiteSetting.disable_mailing_list_mode = false
|
2016-02-17 12:46:19 +08:00
|
|
|
|
|
2018-08-08 12:46:34 +08:00
|
|
|
|
theme = Fabricate(:theme, user_selectable: true)
|
2017-05-16 00:48:08 +08:00
|
|
|
|
|
|
|
|
|
seq = user.user_option.theme_key_seq
|
|
|
|
|
|
2019-04-29 11:58:52 +08:00
|
|
|
|
val =
|
|
|
|
|
updater.update(
|
|
|
|
|
bio_raw: "my new bio",
|
|
|
|
|
email_level: UserOption.email_level_types[:always],
|
|
|
|
|
mailing_list_mode: true,
|
|
|
|
|
digest_after_minutes: "45",
|
|
|
|
|
new_topic_duration_minutes: 100,
|
|
|
|
|
auto_track_topics_after_msecs: 101,
|
|
|
|
|
notification_level_when_replying: 3,
|
|
|
|
|
email_in_reply_to: false,
|
|
|
|
|
date_of_birth: date_of_birth,
|
|
|
|
|
theme_ids: [theme.id],
|
2020-08-18 00:37:45 +08:00
|
|
|
|
allow_private_messages: false,
|
2019-04-29 11:58:52 +08:00
|
|
|
|
)
|
2017-10-06 15:56:58 +08:00
|
|
|
|
|
2016-08-01 13:29:28 +08:00
|
|
|
|
expect(val).to be_truthy
|
|
|
|
|
|
2016-02-17 12:46:19 +08:00
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
|
|
expect(user.user_profile.bio_raw).to eq "my new bio"
|
2019-03-15 22:55:11 +08:00
|
|
|
|
expect(user.user_option.email_level).to eq UserOption.email_level_types[:always]
|
2016-02-17 12:46:19 +08:00
|
|
|
|
expect(user.user_option.mailing_list_mode).to eq true
|
2016-03-03 04:26:27 +08:00
|
|
|
|
expect(user.user_option.digest_after_minutes).to eq 45
|
2016-02-18 13:57:22 +08:00
|
|
|
|
expect(user.user_option.new_topic_duration_minutes).to eq 100
|
|
|
|
|
expect(user.user_option.auto_track_topics_after_msecs).to eq 101
|
2016-10-01 00:36:43 +08:00
|
|
|
|
expect(user.user_option.notification_level_when_replying).to eq 3
|
2016-02-25 21:05:40 +08:00
|
|
|
|
expect(user.user_option.email_in_reply_to).to eq false
|
2018-07-12 12:18:21 +08:00
|
|
|
|
expect(user.user_option.theme_ids.first).to eq theme.id
|
2017-05-16 00:48:08 +08:00
|
|
|
|
expect(user.user_option.theme_key_seq).to eq(seq + 1)
|
2017-10-06 15:56:58 +08:00
|
|
|
|
expect(user.user_option.allow_private_messages).to eq(false)
|
2016-11-15 16:10:20 +08:00
|
|
|
|
expect(user.date_of_birth).to eq(date_of_birth.to_date)
|
2020-08-18 00:37:45 +08:00
|
|
|
|
end
|
2019-05-02 16:53:17 +08:00
|
|
|
|
|
2020-08-18 00:37:45 +08:00
|
|
|
|
it "allows user to update profile header when the user has required trust level" do
|
|
|
|
|
user = Fabricate(:user, trust_level: 2)
|
|
|
|
|
updater = UserUpdater.new(user, user)
|
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
|
SiteSetting.min_trust_level_to_allow_profile_background = 2
|
|
|
|
|
val = updater.update(profile_background_upload_url: upload.url)
|
|
|
|
|
expect(val).to be_truthy
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.profile_background_upload).to eq(upload)
|
|
|
|
|
success = updater.update(profile_background_upload_url: "")
|
|
|
|
|
expect(success).to eq(true)
|
2019-05-02 16:53:17 +08:00
|
|
|
|
user.reload
|
2020-08-18 00:37:45 +08:00
|
|
|
|
expect(user.profile_background_upload).to eq(nil)
|
|
|
|
|
end
|
2019-05-02 16:53:17 +08:00
|
|
|
|
|
2020-08-18 00:37:45 +08:00
|
|
|
|
it "allows user to update user card background when the user has required trust level" do
|
2023-12-14 10:57:58 +08:00
|
|
|
|
user = Fabricate(:user, trust_level: 2, refresh_auto_groups: true)
|
2020-08-18 00:37:45 +08:00
|
|
|
|
updater = UserUpdater.new(user, user)
|
|
|
|
|
upload = Fabricate(:upload)
|
|
|
|
|
SiteSetting.min_trust_level_to_allow_user_card_background = 2
|
|
|
|
|
val = updater.update(card_background_upload_url: upload.url)
|
|
|
|
|
expect(val).to be_truthy
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.card_background_upload).to eq(upload)
|
|
|
|
|
success = updater.update(card_background_upload_url: "")
|
2019-05-02 16:53:17 +08:00
|
|
|
|
expect(success).to eq(true)
|
2020-08-18 00:37:45 +08:00
|
|
|
|
user.reload
|
2019-05-02 16:53:17 +08:00
|
|
|
|
expect(user.card_background_upload).to eq(nil)
|
2014-06-10 13:19:08 +08:00
|
|
|
|
end
|
|
|
|
|
|
2016-11-28 22:52:35 +08:00
|
|
|
|
it "disables email_digests when enabling mailing_list_mode" do
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2021-03-05 04:24:37 +08:00
|
|
|
|
SiteSetting.disable_mailing_list_mode = false
|
2016-11-28 22:52:35 +08:00
|
|
|
|
|
|
|
|
|
val = updater.update(mailing_list_mode: true, email_digests: true)
|
|
|
|
|
expect(val).to be_truthy
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
|
|
|
|
|
expect(user.user_option.email_digests).to eq false
|
|
|
|
|
expect(user.user_option.mailing_list_mode).to eq true
|
|
|
|
|
end
|
|
|
|
|
|
2021-05-21 09:43:47 +08:00
|
|
|
|
it "filters theme_ids blank values before updating preferences" do
|
2018-08-10 19:12:02 +08:00
|
|
|
|
user.user_option.update!(theme_ids: [1])
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
|
|
|
|
|
updater.update(theme_ids: [""])
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_option.theme_ids).to eq([])
|
|
|
|
|
|
|
|
|
|
updater.update(theme_ids: [nil])
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_option.theme_ids).to eq([])
|
|
|
|
|
|
|
|
|
|
theme = Fabricate(:theme)
|
2018-08-24 09:30:00 +08:00
|
|
|
|
child = Fabricate(:theme, component: true)
|
2019-11-28 13:19:01 +08:00
|
|
|
|
theme.add_relative_theme!(:child, child)
|
2018-08-10 19:12:02 +08:00
|
|
|
|
theme.set_default!
|
|
|
|
|
|
|
|
|
|
updater.update(theme_ids: [theme.id.to_s, child.id.to_s, "", nil])
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_option.theme_ids).to eq([theme.id, child.id])
|
|
|
|
|
end
|
|
|
|
|
|
2021-01-21 00:31:52 +08:00
|
|
|
|
let(:schedule_attrs) do
|
|
|
|
|
{
|
|
|
|
|
enabled: true,
|
|
|
|
|
day_0_start_time: 30,
|
|
|
|
|
day_0_end_time: 60,
|
|
|
|
|
day_1_start_time: 30,
|
|
|
|
|
day_1_end_time: 60,
|
|
|
|
|
day_2_start_time: 30,
|
|
|
|
|
day_2_end_time: 60,
|
|
|
|
|
day_3_start_time: 30,
|
|
|
|
|
day_3_end_time: 60,
|
|
|
|
|
day_4_start_time: 30,
|
|
|
|
|
day_4_end_time: 60,
|
|
|
|
|
day_5_start_time: 30,
|
|
|
|
|
day_5_end_time: 60,
|
|
|
|
|
day_6_start_time: 30,
|
|
|
|
|
day_6_end_time: 60,
|
|
|
|
|
}
|
2023-01-09 19:18:21 +08:00
|
|
|
|
end
|
2021-01-21 00:31:52 +08:00
|
|
|
|
|
|
|
|
|
context "with user_notification_schedule" do
|
|
|
|
|
it "allows users to create their notification schedule when it doesn't exist previously" do
|
|
|
|
|
expect(user.user_notification_schedule).to be_nil
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
|
|
|
|
|
updater.update(user_notification_schedule: schedule_attrs)
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_notification_schedule.enabled).to eq(true)
|
|
|
|
|
expect(user.user_notification_schedule.day_0_start_time).to eq(30)
|
|
|
|
|
expect(user.user_notification_schedule.day_0_end_time).to eq(60)
|
|
|
|
|
expect(user.user_notification_schedule.day_6_start_time).to eq(30)
|
|
|
|
|
expect(user.user_notification_schedule.day_6_end_time).to eq(60)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "allows users to update their notification schedule" do
|
|
|
|
|
UserNotificationSchedule.create({ user: user }.merge(UserNotificationSchedule::DEFAULT))
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
updater.update(user_notification_schedule: schedule_attrs)
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_notification_schedule.enabled).to eq(true)
|
|
|
|
|
expect(user.user_notification_schedule.day_0_start_time).to eq(30)
|
|
|
|
|
expect(user.user_notification_schedule.day_0_end_time).to eq(60)
|
|
|
|
|
expect(user.user_notification_schedule.day_6_start_time).to eq(30)
|
|
|
|
|
expect(user.user_notification_schedule.day_6_end_time).to eq(60)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "processes the schedule and do_not_disturb_timings are created" do
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
|
|
|
|
|
expect { updater.update(user_notification_schedule: schedule_attrs) }.to change {
|
|
|
|
|
user.do_not_disturb_timings.count
|
|
|
|
|
}.by(4)
|
|
|
|
|
end
|
2021-01-23 03:02:11 +08:00
|
|
|
|
|
|
|
|
|
it "removes do_not_disturb_timings when the schedule is disabled" do
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
updater.update(user_notification_schedule: schedule_attrs)
|
|
|
|
|
expect(user.user_notification_schedule.enabled).to eq(true)
|
|
|
|
|
|
|
|
|
|
schedule_attrs[:enabled] = false
|
|
|
|
|
updater.update(user_notification_schedule: schedule_attrs)
|
|
|
|
|
|
|
|
|
|
expect(user.user_notification_schedule.enabled).to eq(false)
|
|
|
|
|
expect(user.do_not_disturb_timings.count).to eq(0)
|
|
|
|
|
end
|
2021-01-21 00:31:52 +08:00
|
|
|
|
end
|
|
|
|
|
|
2016-08-01 13:29:28 +08:00
|
|
|
|
context "when sso overrides bio" do
|
|
|
|
|
it "does not change bio" do
|
2021-02-08 18:04:33 +08:00
|
|
|
|
SiteSetting.discourse_connect_url = "https://www.example.com/sso"
|
|
|
|
|
SiteSetting.enable_discourse_connect = true
|
|
|
|
|
SiteSetting.discourse_connect_overrides_bio = true
|
2016-08-01 13:29:28 +08:00
|
|
|
|
|
2016-02-17 12:46:19 +08:00
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2013-11-02 05:06:20 +08:00
|
|
|
|
|
2016-08-01 13:29:28 +08:00
|
|
|
|
expect(updater.update(bio_raw: "new bio")).to be_truthy
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_profile.bio_raw).not_to eq "new bio"
|
2013-11-02 05:06:20 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2020-04-28 14:06:35 +08:00
|
|
|
|
context "when sso overrides location" do
|
|
|
|
|
it "does not change location" do
|
2021-02-08 18:04:33 +08:00
|
|
|
|
SiteSetting.discourse_connect_url = "https://www.example.com/sso"
|
|
|
|
|
SiteSetting.enable_discourse_connect = true
|
|
|
|
|
SiteSetting.discourse_connect_overrides_location = true
|
2020-04-28 14:06:35 +08:00
|
|
|
|
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
|
|
|
|
|
expect(updater.update(location: "new location")).to be_truthy
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_profile.location).not_to eq "new location"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when sso overrides website" do
|
|
|
|
|
it "does not change website" do
|
2021-02-08 18:04:33 +08:00
|
|
|
|
SiteSetting.discourse_connect_url = "https://www.example.com/sso"
|
|
|
|
|
SiteSetting.enable_discourse_connect = true
|
|
|
|
|
SiteSetting.discourse_connect_overrides_website = true
|
2020-04-28 14:06:35 +08:00
|
|
|
|
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
|
|
|
|
|
expect(updater.update(website: "https://google.com")).to be_truthy
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.user_profile.website).not_to eq "https://google.com"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-29 01:46:27 +08:00
|
|
|
|
context "when updating primary group" do
|
|
|
|
|
let(:new_group) { Group.create(name: "new_group") }
|
|
|
|
|
|
|
|
|
|
it "updates when setting is enabled" do
|
|
|
|
|
SiteSetting.user_selected_primary_groups = true
|
|
|
|
|
user.groups << new_group
|
|
|
|
|
user.update(primary_group_id: nil)
|
|
|
|
|
UserUpdater.new(acting_user, user).update(primary_group_id: new_group.id)
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.primary_group_id).to eq new_group.id
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "does not update when setting is disabled" do
|
|
|
|
|
SiteSetting.user_selected_primary_groups = false
|
|
|
|
|
user.groups << new_group
|
|
|
|
|
user.update(primary_group_id: nil)
|
|
|
|
|
UserUpdater.new(acting_user, user).update(primary_group_id: new_group.id)
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.primary_group_id).to eq nil
|
|
|
|
|
end
|
2020-01-18 06:43:54 +08:00
|
|
|
|
|
2020-02-08 07:26:33 +08:00
|
|
|
|
it "does not update when changing other profile data" do
|
|
|
|
|
SiteSetting.user_selected_primary_groups = true
|
|
|
|
|
user.groups << new_group
|
|
|
|
|
user.update(primary_group_id: new_group.id)
|
|
|
|
|
UserUpdater.new(acting_user, user).update(website: "http://example.com")
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.primary_group_id).to eq new_group.id
|
|
|
|
|
end
|
|
|
|
|
|
2020-01-18 06:43:54 +08:00
|
|
|
|
it "can be removed by the user when setting is enabled" do
|
|
|
|
|
SiteSetting.user_selected_primary_groups = true
|
|
|
|
|
user.groups << new_group
|
|
|
|
|
user.update(primary_group_id: new_group.id)
|
|
|
|
|
UserUpdater.new(acting_user, user).update(primary_group_id: "")
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.primary_group_id).to eq nil
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "cannot be removed by the user when setting is disabled" do
|
|
|
|
|
SiteSetting.user_selected_primary_groups = false
|
|
|
|
|
user.groups << new_group
|
|
|
|
|
user.update(primary_group_id: new_group.id)
|
|
|
|
|
UserUpdater.new(acting_user, user).update(primary_group_id: "")
|
|
|
|
|
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.primary_group_id).to eq new_group.id
|
|
|
|
|
end
|
2019-10-29 01:46:27 +08:00
|
|
|
|
end
|
|
|
|
|
|
2021-07-09 12:41:26 +08:00
|
|
|
|
context "when updating flair group" do
|
|
|
|
|
let(:group) do
|
|
|
|
|
Fabricate(
|
|
|
|
|
:group,
|
|
|
|
|
name: "Group",
|
|
|
|
|
flair_bg_color: "#111111",
|
|
|
|
|
flair_color: "#999999",
|
|
|
|
|
flair_icon: "icon",
|
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
end
|
2021-07-09 12:41:26 +08:00
|
|
|
|
|
|
|
|
|
it "updates when setting is enabled" do
|
|
|
|
|
group.add(user)
|
|
|
|
|
|
|
|
|
|
UserUpdater.new(acting_user, user).update(flair_group_id: group.id)
|
|
|
|
|
expect(user.reload.flair_group_id).to eq(group.id)
|
|
|
|
|
|
|
|
|
|
UserUpdater.new(acting_user, user).update(flair_group_id: "")
|
|
|
|
|
expect(user.reload.flair_group_id).to eq(nil)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
|
context "when update fails" do
|
|
|
|
|
it "returns false" do
|
|
|
|
|
user.stubs(save: false)
|
2016-02-17 12:46:19 +08:00
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2013-11-02 05:06:20 +08:00
|
|
|
|
|
2014-09-25 23:44:48 +08:00
|
|
|
|
expect(updater.update).to be_falsey
|
2013-11-02 05:06:20 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "with permission to update title" do
|
|
|
|
|
it "allows user to change title" do
|
|
|
|
|
user = Fabricate(:user, title: "Emperor")
|
2020-08-18 00:37:45 +08:00
|
|
|
|
Guardian.any_instance.stubs(:can_grant_title?).with(user, "Minion").returns(true)
|
2016-02-17 12:46:19 +08:00
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2013-11-02 05:06:20 +08:00
|
|
|
|
|
|
|
|
|
updater.update(title: "Minion")
|
|
|
|
|
|
|
|
|
|
expect(user.reload.title).to eq "Minion"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
|
context "when title is from a badge" do
|
2019-05-07 11:12:20 +08:00
|
|
|
|
fab!(:user) { Fabricate(:user, title: "Emperor") }
|
|
|
|
|
fab!(:badge) { Fabricate(:badge, name: "Minion") }
|
2018-04-27 04:50:50 +08:00
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
|
context "when badge can be used as a title" do
|
2019-04-29 15:32:25 +08:00
|
|
|
|
before { badge.update(allow_title: true) }
|
2018-04-27 04:50:50 +08:00
|
|
|
|
|
2023-03-08 20:37:20 +08:00
|
|
|
|
it "can use as title, sets granted_title_badge_id" do
|
2018-04-27 04:50:50 +08:00
|
|
|
|
BadgeGranter.grant(badge, user)
|
|
|
|
|
updater = UserUpdater.new(user, user)
|
|
|
|
|
updater.update(title: badge.name)
|
|
|
|
|
user.reload
|
2023-03-08 20:37:20 +08:00
|
|
|
|
expect(user.user_profile.granted_title_badge_id).to eq(badge.id)
|
2018-04-27 04:50:50 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "badge has not been granted, does not change title" do
|
2019-04-29 15:32:25 +08:00
|
|
|
|
badge.update(allow_title: true)
|
2018-04-27 04:50:50 +08:00
|
|
|
|
updater = UserUpdater.new(user, user)
|
|
|
|
|
updater.update(title: badge.name)
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.title).not_to eq(badge.name)
|
2023-03-08 20:37:20 +08:00
|
|
|
|
expect(user.user_profile.granted_title_badge_id).to be_nil
|
2018-04-27 04:50:50 +08:00
|
|
|
|
end
|
|
|
|
|
|
2023-03-08 20:37:20 +08:00
|
|
|
|
it "changing to a title that is not from a badge, unsets granted_title_badge_id" do
|
2019-04-29 15:32:25 +08:00
|
|
|
|
user.update(title: badge.name)
|
2023-03-08 20:37:20 +08:00
|
|
|
|
user.user_profile.update(granted_title_badge_id: badge.id)
|
2018-04-27 04:50:50 +08:00
|
|
|
|
|
2020-08-18 00:37:45 +08:00
|
|
|
|
Guardian.any_instance.stubs(:can_grant_title?).with(user, "Dancer").returns(true)
|
2018-04-27 04:50:50 +08:00
|
|
|
|
|
|
|
|
|
updater = UserUpdater.new(user, user)
|
|
|
|
|
updater.update(title: "Dancer")
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.title).to eq("Dancer")
|
2023-03-08 20:37:20 +08:00
|
|
|
|
expect(user.user_profile.granted_title_badge_id).to be_nil
|
2018-04-27 04:50:50 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
it "cannot use as title, does not change title" do
|
|
|
|
|
BadgeGranter.grant(badge, user)
|
|
|
|
|
updater = UserUpdater.new(user, user)
|
|
|
|
|
updater.update(title: badge.name)
|
|
|
|
|
user.reload
|
|
|
|
|
expect(user.title).not_to eq(badge.name)
|
2023-03-08 20:37:20 +08:00
|
|
|
|
expect(user.user_profile.granted_title_badge_id).to be_nil
|
2018-04-27 04:50:50 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2013-11-02 05:06:20 +08:00
|
|
|
|
context "without permission to update title" do
|
|
|
|
|
it "does not allow user to change title" do
|
|
|
|
|
user = Fabricate(:user, title: "Emperor")
|
2020-08-18 00:37:45 +08:00
|
|
|
|
Guardian.any_instance.stubs(:can_grant_title?).with(user, "Minion").returns(false)
|
2017-02-28 04:02:00 +08:00
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2013-11-02 05:06:20 +08:00
|
|
|
|
|
|
|
|
|
updater.update(title: "Minion")
|
|
|
|
|
|
|
|
|
|
expect(user.reload.title).not_to eq "Minion"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when website includes http" do
|
|
|
|
|
it "does not add http before updating" do
|
2017-02-28 04:02:00 +08:00
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2013-11-02 05:06:20 +08:00
|
|
|
|
|
|
|
|
|
updater.update(website: "http://example.com")
|
|
|
|
|
|
2014-06-07 12:54:32 +08:00
|
|
|
|
expect(user.reload.user_profile.website).to eq "http://example.com"
|
2013-11-02 05:06:20 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when website does not include http" do
|
|
|
|
|
it "adds http before updating" do
|
2017-02-28 04:02:00 +08:00
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2013-11-02 05:06:20 +08:00
|
|
|
|
|
|
|
|
|
updater.update(website: "example.com")
|
|
|
|
|
|
2014-06-07 12:54:32 +08:00
|
|
|
|
expect(user.reload.user_profile.website).to eq "http://example.com"
|
2013-11-02 05:06:20 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2014-09-18 01:09:39 +08:00
|
|
|
|
|
2020-04-23 03:50:45 +08:00
|
|
|
|
context "when website is invalid" do
|
|
|
|
|
it "returns an error" do
|
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
|
|
|
|
|
|
|
|
|
expect(updater.update(website: "ʔ<")).to eq nil
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-09-18 01:09:39 +08:00
|
|
|
|
context "when custom_fields is empty string" do
|
|
|
|
|
it "update is successful" do
|
|
|
|
|
user.custom_fields = { "import_username" => "my_old_username" }
|
|
|
|
|
user.save
|
2017-02-28 04:02:00 +08:00
|
|
|
|
updater = UserUpdater.new(acting_user, user)
|
2014-09-18 01:09:39 +08:00
|
|
|
|
|
|
|
|
|
updater.update(website: "example.com", custom_fields: "")
|
2015-04-25 23:18:35 +08:00
|
|
|
|
expect(user.reload.custom_fields).to eq("import_username" => "my_old_username")
|
2014-09-18 01:09:39 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
2017-02-23 13:48:57 +08:00
|
|
|
|
|
2022-10-12 23:38:45 +08:00
|
|
|
|
context "when skip_new_user_tips is edited" do
|
2022-11-22 02:57:02 +08:00
|
|
|
|
it "updates seen_popups too" do
|
|
|
|
|
messages =
|
2023-01-30 19:48:09 +08:00
|
|
|
|
MessageBus.track_publish("/user-tips/#{user.id}") do
|
2022-11-22 02:57:02 +08:00
|
|
|
|
UserUpdater.new(Discourse.system_user, user).update(skip_new_user_tips: true)
|
|
|
|
|
end
|
2022-10-12 23:38:45 +08:00
|
|
|
|
|
|
|
|
|
expect(user.user_option.skip_new_user_tips).to eq(true)
|
2022-11-10 02:20:34 +08:00
|
|
|
|
expect(user.user_option.seen_popups).to eq([-1])
|
2022-11-22 02:57:02 +08:00
|
|
|
|
expect(messages.map(&:data)).to contain_exactly([-1])
|
2022-12-08 00:27:10 +08:00
|
|
|
|
end
|
2022-10-12 23:38:45 +08:00
|
|
|
|
|
2022-12-08 00:27:10 +08:00
|
|
|
|
it "does not reset seen_popups" do
|
|
|
|
|
user.user_option.update!(seen_popups: [1, 2, 3])
|
|
|
|
|
|
|
|
|
|
UserUpdater.new(Discourse.system_user, user).update(skip_new_user_tips: false)
|
2022-10-12 23:38:45 +08:00
|
|
|
|
|
|
|
|
|
expect(user.user_option.skip_new_user_tips).to eq(false)
|
2022-12-08 00:27:10 +08:00
|
|
|
|
expect(user.user_option.seen_popups).to eq([1, 2, 3])
|
2022-11-22 02:57:02 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
context "when seen_popups is edited" do
|
|
|
|
|
it "publishes a message" do
|
|
|
|
|
messages =
|
2023-01-30 19:48:09 +08:00
|
|
|
|
MessageBus.track_publish("/user-tips/#{user.id}") do
|
2022-11-22 02:57:02 +08:00
|
|
|
|
UserUpdater.new(Discourse.system_user, user).update(seen_popups: [1])
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
expect(user.user_option.seen_popups).to eq([1])
|
|
|
|
|
expect(messages.map(&:data)).to contain_exactly([1])
|
2022-10-12 23:38:45 +08:00
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2017-02-23 13:48:57 +08:00
|
|
|
|
it "logs the action" do
|
|
|
|
|
user = Fabricate(:user, name: "Billy Bob")
|
2018-11-22 10:10:07 +08:00
|
|
|
|
|
2021-07-28 14:07:18 +08:00
|
|
|
|
expect do UserUpdater.new(user, user).update(name: "Jim Tom") end.to change {
|
2017-10-23 11:16:14 +08:00
|
|
|
|
UserHistory.count
|
|
|
|
|
}.by(1)
|
|
|
|
|
|
2018-11-22 10:10:07 +08:00
|
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:change_name])
|
|
|
|
|
|
2021-07-28 14:07:18 +08:00
|
|
|
|
expect do UserUpdater.new(user, user).update(name: "JiM TOm") end.to_not change {
|
2017-10-23 11:16:14 +08:00
|
|
|
|
UserHistory.count
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 14:07:18 +08:00
|
|
|
|
expect do UserUpdater.new(user, user).update(bio_raw: "foo bar") end.to_not change {
|
2018-11-30 18:00:34 +08:00
|
|
|
|
UserHistory.count
|
|
|
|
|
}
|
|
|
|
|
|
2021-07-28 14:07:18 +08:00
|
|
|
|
user_without_name = Fabricate(:user, name: nil)
|
|
|
|
|
|
2017-10-23 11:16:14 +08:00
|
|
|
|
expect do
|
2021-07-28 14:07:18 +08:00
|
|
|
|
UserUpdater.new(user_without_name, user_without_name).update(bio_raw: "foo bar")
|
2017-10-23 11:16:14 +08:00
|
|
|
|
end.to_not change { UserHistory.count }
|
|
|
|
|
|
|
|
|
|
expect do
|
2021-07-28 14:07:18 +08:00
|
|
|
|
UserUpdater.new(user_without_name, user_without_name).update(name: "Jim Tom")
|
2017-10-23 11:16:14 +08:00
|
|
|
|
end.to change { UserHistory.count }.by(1)
|
|
|
|
|
|
2018-11-22 10:10:07 +08:00
|
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:change_name])
|
|
|
|
|
|
2021-07-28 14:07:18 +08:00
|
|
|
|
expect do UserUpdater.new(user, user).update(name: "") end.to change { UserHistory.count }.by(
|
2023-01-09 19:18:21 +08:00
|
|
|
|
1,
|
2018-11-22 10:10:07 +08:00
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
|
2018-11-22 10:10:07 +08:00
|
|
|
|
expect(UserHistory.last.action).to eq(UserHistory.actions[:change_name])
|
2017-02-23 13:48:57 +08:00
|
|
|
|
end
|
2013-11-02 05:06:20 +08:00
|
|
|
|
end
|
|
|
|
|
end
|