2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe UserNotifications do
|
2013-06-19 03:54:02 +08:00
|
|
|
let(:user) { Fabricate(:admin) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-01-31 13:37:40 +08:00
|
|
|
describe "#get_context_posts" do
|
|
|
|
it "does not include hidden/deleted/user_deleted posts in context" do
|
2016-02-12 19:10:30 +08:00
|
|
|
post1 = create_post
|
2016-02-19 10:56:52 +08:00
|
|
|
_post2 = Fabricate(:post, topic: post1.topic, deleted_at: 1.day.ago)
|
|
|
|
_post3 = Fabricate(:post, topic: post1.topic, user_deleted: true)
|
|
|
|
_post4 = Fabricate(:post, topic: post1.topic, hidden: true)
|
|
|
|
_post5 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:moderator_action])
|
|
|
|
_post6 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:small_action])
|
|
|
|
_post7 = Fabricate(:post, topic: post1.topic, post_type: Post.types[:whisper])
|
2016-02-12 19:10:30 +08:00
|
|
|
last = Fabricate(:post, topic: post1.topic)
|
|
|
|
|
2016-02-25 21:05:40 +08:00
|
|
|
post1.user.user_option.email_previous_replies = UserOption.previous_replies_type[:always]
|
|
|
|
|
2016-02-12 19:10:30 +08:00
|
|
|
# default is only post #1
|
2016-02-25 21:05:40 +08:00
|
|
|
expect(UserNotifications.get_context_posts(last, nil, post1.user).count).to eq(1)
|
2016-02-12 19:10:30 +08:00
|
|
|
# staff members can also see the whisper
|
2016-02-25 21:05:40 +08:00
|
|
|
moderator = build(:moderator)
|
|
|
|
moderator.user_option = UserOption.new
|
|
|
|
moderator.user_option.email_previous_replies = UserOption.previous_replies_type[:always]
|
|
|
|
tu = TopicUser.new(topic: post1.topic, user: moderator)
|
|
|
|
expect(UserNotifications.get_context_posts(last, tu, tu.user).count).to eq(2)
|
2014-01-31 13:37:40 +08:00
|
|
|
end
|
2016-02-19 10:56:52 +08:00
|
|
|
|
|
|
|
it "allows users to control context" do
|
|
|
|
post1 = create_post
|
|
|
|
_post2 = Fabricate(:post, topic: post1.topic)
|
|
|
|
post3 = Fabricate(:post, topic: post1.topic)
|
|
|
|
|
|
|
|
user = Fabricate(:user)
|
|
|
|
TopicUser.change(user.id, post1.topic_id, last_emailed_post_number: 1)
|
|
|
|
topic_user = TopicUser.find_by(user_id: user.id, topic_id: post1.topic_id)
|
|
|
|
# to avoid reloads after update_columns
|
|
|
|
user = topic_user.user
|
2016-02-25 21:05:40 +08:00
|
|
|
user.user_option.update_columns(
|
|
|
|
email_previous_replies: UserOption.previous_replies_type[:unless_emailed],
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(UserNotifications.get_context_posts(post3, topic_user, user).count).to eq(1)
|
2016-02-19 10:56:52 +08:00
|
|
|
|
|
|
|
user.user_option.update_columns(
|
|
|
|
email_previous_replies: UserOption.previous_replies_type[:never],
|
|
|
|
)
|
2016-02-25 21:05:40 +08:00
|
|
|
expect(UserNotifications.get_context_posts(post3, topic_user, user).count).to eq(0)
|
2016-02-19 10:56:52 +08:00
|
|
|
|
|
|
|
user.user_option.update_columns(
|
|
|
|
email_previous_replies: UserOption.previous_replies_type[:always],
|
|
|
|
)
|
2016-02-25 21:05:40 +08:00
|
|
|
expect(UserNotifications.get_context_posts(post3, topic_user, user).count).to eq(2)
|
2016-02-19 10:56:52 +08:00
|
|
|
|
2017-04-25 03:26:06 +08:00
|
|
|
SiteSetting.private_email = true
|
|
|
|
expect(UserNotifications.get_context_posts(post3, topic_user, user).count).to eq(0)
|
2016-02-19 10:56:52 +08:00
|
|
|
end
|
2014-01-31 13:37:40 +08:00
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
describe ".signup" do
|
2023-06-21 22:00:19 +08:00
|
|
|
subject(:email) { UserNotifications.signup(user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-10-29 23:06:50 +08:00
|
|
|
it "works" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
expect(email.subject).to be_present
|
|
|
|
expect(email.from).to eq([SiteSetting.notification_email])
|
|
|
|
expect(email.body).to be_present
|
2014-10-29 23:06:50 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".forgot_password" do
|
2023-06-21 22:00:19 +08:00
|
|
|
subject(:email) { UserNotifications.forgot_password(user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-10-29 23:06:50 +08:00
|
|
|
it "works" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
expect(email.subject).to be_present
|
|
|
|
expect(email.from).to eq([SiteSetting.notification_email])
|
|
|
|
expect(email.body).to be_present
|
2014-10-29 23:06:50 +08:00
|
|
|
end
|
2020-10-07 11:02:24 +08:00
|
|
|
end
|
|
|
|
|
2021-04-12 23:08:23 +08:00
|
|
|
describe ".post_approved" do
|
2023-11-10 06:47:59 +08:00
|
|
|
fab!(:post)
|
2021-04-12 23:08:23 +08:00
|
|
|
|
|
|
|
it "works" do
|
|
|
|
subject =
|
|
|
|
UserNotifications.post_approved(user, { notification_data_hash: { post_url: post.url } })
|
|
|
|
|
|
|
|
expect(subject.to).to eq([user.email])
|
|
|
|
expect(subject.subject).to be_present
|
|
|
|
expect(subject.from).to eq([SiteSetting.notification_email])
|
|
|
|
expect(subject.body).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-07 11:02:24 +08:00
|
|
|
describe ".confirm_new_email" do
|
|
|
|
let(:opts) { { requested_by_admin: requested_by_admin, email_token: token } }
|
|
|
|
let(:token) { "test123" }
|
|
|
|
|
|
|
|
context "when requested by admin" do
|
|
|
|
let(:requested_by_admin) { true }
|
2014-10-29 23:06:50 +08:00
|
|
|
|
2020-10-07 11:02:24 +08:00
|
|
|
it "uses the requested by admin template" do
|
|
|
|
expect(UserNotifications.confirm_new_email(user, opts).body).to include(
|
|
|
|
"This email change was requested by a site admin.",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when not requested by admin" do
|
|
|
|
let(:requested_by_admin) { false }
|
|
|
|
|
|
|
|
it "uses the normal template" do
|
|
|
|
expect(UserNotifications.confirm_new_email(user, opts).body).not_to include(
|
|
|
|
"This email change was requested by a site admin.",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-04-20 23:17:24 +08:00
|
|
|
describe ".email_login" do
|
2023-06-21 22:00:19 +08:00
|
|
|
subject(:email) { UserNotifications.email_login(user, email_token: email_token) }
|
|
|
|
|
2021-11-25 15:34:39 +08:00
|
|
|
let(:email_token) do
|
|
|
|
Fabricate(:email_token, user: user, scope: EmailToken.scopes[:email_login]).token
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2017-04-20 23:17:24 +08:00
|
|
|
|
|
|
|
it "generates the right email" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
expect(email.from).to eq([SiteSetting.notification_email])
|
2017-04-20 23:17:24 +08:00
|
|
|
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.subject).to eq(
|
2017-04-20 23:17:24 +08:00
|
|
|
I18n.t("user_notifications.email_login.subject_template", email_prefix: SiteSetting.title),
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.body.to_s).to match(
|
2017-04-20 23:17:24 +08:00
|
|
|
I18n.t(
|
|
|
|
"user_notifications.email_login.text_body_template",
|
|
|
|
site_name: SiteSetting.title,
|
|
|
|
base_url: Discourse.base_url,
|
|
|
|
email_token: email_token,
|
2023-01-09 19:18:21 +08:00
|
|
|
),
|
2017-04-20 23:17:24 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-11 04:46:08 +08:00
|
|
|
describe ".digest" do
|
2023-06-21 22:00:19 +08:00
|
|
|
subject(:email) { UserNotifications.digest(user) }
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2019-12-03 17:05:53 +08:00
|
|
|
after { Discourse.redis.keys("summary-new-users:*").each { |key| Discourse.redis.del(key) } }
|
2019-10-26 04:33:05 +08:00
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context "without new topics" do
|
2014-10-29 23:06:50 +08:00
|
|
|
it "doesn't send the email" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.to).to be_blank
|
2014-10-29 23:06:50 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2016-12-20 03:53:53 +08:00
|
|
|
context "with topics only from new users" do
|
2019-01-18 06:46:04 +08:00
|
|
|
let!(:new_today) do
|
|
|
|
Fabricate(
|
|
|
|
:topic,
|
|
|
|
user: Fabricate(:user, trust_level: TrustLevel[0], created_at: 10.minutes.ago),
|
|
|
|
title: "Hey everyone look at me",
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2016-12-20 03:53:53 +08:00
|
|
|
let!(:new_yesterday) do
|
|
|
|
Fabricate(
|
|
|
|
:topic,
|
|
|
|
user: Fabricate(:user, trust_level: TrustLevel[0], created_at: 25.hours.ago),
|
|
|
|
created_at: 25.hours.ago,
|
|
|
|
title: "This topic is of interest to you",
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2016-12-20 03:53:53 +08:00
|
|
|
|
|
|
|
it "returns topics from new users if they're more than 24 hours old" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
html = email.html_part.body.to_s
|
2016-12-20 03:53:53 +08:00
|
|
|
expect(html).to include(new_yesterday.title)
|
|
|
|
expect(html).to_not include(new_today.title)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
context "with new topics" do
|
2018-08-08 04:38:05 +08:00
|
|
|
let!(:popular_topic) do
|
|
|
|
Fabricate(:topic, user: Fabricate(:coding_horror), created_at: 1.hour.ago)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2014-10-29 23:06:50 +08:00
|
|
|
it "works" do
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.to).to eq([user.email])
|
|
|
|
expect(email.subject).to be_present
|
|
|
|
expect(email.from).to eq([SiteSetting.notification_email])
|
|
|
|
expect(email.html_part.body.to_s).to be_present
|
|
|
|
expect(email.text_part.body.to_s).to be_present
|
|
|
|
expect(email.header["List-Unsubscribe"].to_s).to match(/\/email\/unsubscribe\/\h{64}/)
|
|
|
|
expect(email.html_part.body.to_s).to include("New Users")
|
2019-10-26 04:33:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "doesn't include new user count if digest_after_minutes is low" do
|
|
|
|
user.user_option.digest_after_minutes = 60
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.html_part.body.to_s).to_not include("New Users")
|
2019-10-26 04:33:05 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "works with min_date string" do
|
|
|
|
digest = UserNotifications.digest(user, since: 1.month.ago.to_date.to_s)
|
|
|
|
expect(digest.html_part.body.to_s).to be_present
|
|
|
|
expect(digest.text_part.body.to_s).to be_present
|
|
|
|
expect(digest.html_part.body.to_s).to include("New Users")
|
2013-11-29 06:20:56 +08:00
|
|
|
end
|
|
|
|
|
2015-06-04 00:49:51 +08:00
|
|
|
it "includes email_prefix in email subject instead of site title" do
|
|
|
|
SiteSetting.email_prefix = "Try Discourse"
|
|
|
|
SiteSetting.title = "Discourse Meta"
|
|
|
|
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.subject).to match(/Try Discourse/)
|
|
|
|
expect(email.subject).not_to match(/Discourse Meta/)
|
2015-06-04 00:49:51 +08:00
|
|
|
end
|
2016-11-23 02:23:21 +08:00
|
|
|
|
2023-05-10 01:19:26 +08:00
|
|
|
it "includes unread likes received count within the since date" do
|
|
|
|
Fabricate(
|
|
|
|
:notification,
|
|
|
|
user: user,
|
|
|
|
notification_type: Notification.types[:liked],
|
|
|
|
created_at: 2.months.ago,
|
|
|
|
)
|
|
|
|
Fabricate(
|
|
|
|
:notification,
|
|
|
|
user: user,
|
|
|
|
notification_type: Notification.types[:liked],
|
|
|
|
read: true,
|
|
|
|
)
|
|
|
|
Fabricate(:notification, user: user, notification_type: Notification.types[:liked])
|
|
|
|
Fabricate(:notification, user: user, notification_type: Notification.types[:liked])
|
|
|
|
digest = UserNotifications.digest(user, since: 1.month.ago.to_date.to_s)
|
|
|
|
parsed_html = Nokogiri::HTML5.fragment(digest.html_part.body.to_s)
|
|
|
|
expect(parsed_html.css(".header-stat-count #likes_received_stat_count strong").text).to eq(
|
|
|
|
"2",
|
|
|
|
)
|
|
|
|
expect(
|
|
|
|
parsed_html.css(".header-stat-description #likes_received_stat_description strong").text,
|
|
|
|
).to eq("Likes Received")
|
|
|
|
end
|
|
|
|
|
2016-11-23 02:23:21 +08:00
|
|
|
it "excludes deleted topics and their posts" do
|
2017-08-15 00:47:33 +08:00
|
|
|
deleted =
|
|
|
|
Fabricate(
|
|
|
|
:topic,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
title: "Delete this topic plz",
|
|
|
|
created_at: 1.hour.ago,
|
|
|
|
)
|
|
|
|
post =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: deleted,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 2,
|
|
|
|
raw: "Your wish is my command",
|
|
|
|
created_at: 1.hour.ago,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2016-11-23 02:23:21 +08:00
|
|
|
deleted.trash!
|
2023-06-21 22:00:19 +08:00
|
|
|
html = email.html_part.body.to_s
|
2016-11-23 02:23:21 +08:00
|
|
|
expect(html).to_not include deleted.title
|
|
|
|
expect(html).to_not include post.raw
|
|
|
|
end
|
2016-12-02 01:01:23 +08:00
|
|
|
|
2020-08-05 01:35:48 +08:00
|
|
|
it "excludes shared drafts" do
|
|
|
|
cat = Fabricate(:category)
|
|
|
|
SiteSetting.shared_drafts_category = cat.id
|
|
|
|
topic =
|
|
|
|
Fabricate(:topic, title: "This is a draft", category_id: cat.id, created_at: 1.hour.ago)
|
|
|
|
post =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: topic,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 2,
|
|
|
|
raw: "secret draft content",
|
|
|
|
created_at: 1.hour.ago,
|
|
|
|
)
|
2023-06-21 22:00:19 +08:00
|
|
|
html = email.html_part.body.to_s
|
2020-08-05 01:35:48 +08:00
|
|
|
expect(html).to_not include topic.title
|
|
|
|
expect(html).to_not include post.raw
|
|
|
|
end
|
|
|
|
|
2016-12-02 01:01:23 +08:00
|
|
|
it "excludes whispers and other post types that don't belong" do
|
2017-08-15 00:47:33 +08:00
|
|
|
t =
|
|
|
|
Fabricate(
|
|
|
|
:topic,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
title: "Who likes the same stuff I like?",
|
|
|
|
created_at: 1.hour.ago,
|
|
|
|
)
|
|
|
|
whisper =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: t,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 2,
|
|
|
|
raw: "You like weird stuff",
|
|
|
|
post_type: Post.types[:whisper],
|
|
|
|
created_at: 1.hour.ago,
|
|
|
|
)
|
|
|
|
mod_action =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: t,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 3,
|
|
|
|
raw: "This topic unlisted",
|
|
|
|
post_type: Post.types[:moderator_action],
|
|
|
|
created_at: 1.hour.ago,
|
|
|
|
)
|
|
|
|
small_action =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: t,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 4,
|
|
|
|
raw: "A small action",
|
|
|
|
post_type: Post.types[:small_action],
|
|
|
|
created_at: 1.hour.ago,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2023-06-21 22:00:19 +08:00
|
|
|
html = email.html_part.body.to_s
|
2016-12-02 01:01:23 +08:00
|
|
|
expect(html).to_not include whisper.raw
|
|
|
|
expect(html).to_not include mod_action.raw
|
|
|
|
expect(html).to_not include small_action.raw
|
|
|
|
end
|
|
|
|
|
|
|
|
it "excludes deleted and hidden posts" do
|
2017-08-15 00:47:33 +08:00
|
|
|
t =
|
|
|
|
Fabricate(
|
|
|
|
:topic,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
title: "Post objectionable stuff here",
|
|
|
|
created_at: 1.hour.ago,
|
|
|
|
)
|
|
|
|
deleted =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: t,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 2,
|
|
|
|
raw: "This post is uncalled for",
|
|
|
|
deleted_at: 5.minutes.ago,
|
|
|
|
created_at: 1.hour.ago,
|
|
|
|
)
|
|
|
|
hidden =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: t,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 3,
|
|
|
|
raw: "Try to find this post",
|
|
|
|
hidden: true,
|
|
|
|
hidden_at: 5.minutes.ago,
|
|
|
|
hidden_reason_id: Post.hidden_reasons[:flagged_by_tl3_user],
|
|
|
|
created_at: 1.hour.ago,
|
|
|
|
)
|
|
|
|
user_deleted =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
topic: t,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 4,
|
|
|
|
raw: "I regret this post",
|
|
|
|
user_deleted: true,
|
|
|
|
created_at: 1.hour.ago,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2023-06-21 22:00:19 +08:00
|
|
|
html = email.html_part.body.to_s
|
2016-12-02 01:01:23 +08:00
|
|
|
expect(html).to_not include deleted.raw
|
|
|
|
expect(html).to_not include hidden.raw
|
|
|
|
expect(html).to_not include user_deleted.raw
|
|
|
|
end
|
2017-05-04 02:35:13 +08:00
|
|
|
|
2017-08-15 00:47:33 +08:00
|
|
|
it "excludes posts that are newer than editing grace period" do
|
|
|
|
SiteSetting.editing_grace_period = 5.minutes
|
|
|
|
too_new =
|
|
|
|
Fabricate(
|
|
|
|
:topic,
|
|
|
|
user: Fabricate(:user),
|
|
|
|
title: "Oops I need to edit this",
|
2019-04-30 08:27:42 +08:00
|
|
|
created_at: 1.minute.ago,
|
|
|
|
)
|
|
|
|
_too_new_post =
|
|
|
|
Fabricate(
|
|
|
|
:post,
|
|
|
|
user: too_new.user,
|
|
|
|
topic: too_new,
|
|
|
|
score: 100.0,
|
|
|
|
post_number: 1,
|
2017-08-15 00:47:33 +08:00
|
|
|
created_at: 1.minute.ago,
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2023-06-21 22:00:19 +08:00
|
|
|
html = email.html_part.body.to_s
|
2017-08-15 00:47:33 +08:00
|
|
|
expect(html).to_not include too_new.title
|
|
|
|
end
|
|
|
|
|
2017-05-04 02:35:13 +08:00
|
|
|
it "uses theme color" do
|
|
|
|
cs =
|
|
|
|
Fabricate(
|
|
|
|
:color_scheme,
|
|
|
|
name: "Fancy",
|
|
|
|
color_scheme_colors: [
|
2019-01-18 06:46:04 +08:00
|
|
|
Fabricate(:color_scheme_color, name: "header_primary", hex: "F0F0F0"),
|
2020-02-12 05:09:41 +08:00
|
|
|
Fabricate(:color_scheme_color, name: "header_background", hex: "1E1E1E"),
|
2018-08-08 12:46:34 +08:00
|
|
|
],
|
2023-01-09 19:18:21 +08:00
|
|
|
)
|
2018-08-08 12:46:34 +08:00
|
|
|
theme =
|
2019-01-18 06:46:04 +08:00
|
|
|
Fabricate(:theme, user_selectable: true, user: Fabricate(:admin), color_scheme_id: cs.id)
|
2018-08-08 12:46:34 +08:00
|
|
|
|
2017-05-04 02:35:13 +08:00
|
|
|
theme.set_default!
|
|
|
|
|
2023-06-21 22:00:19 +08:00
|
|
|
html = email.html_part.body.to_s
|
2017-05-04 02:35:13 +08:00
|
|
|
expect(html).to include "F0F0F0"
|
|
|
|
expect(html).to include "1E1E1E"
|
|
|
|
end
|
2018-08-08 04:38:05 +08:00
|
|
|
|
|
|
|
it "supports subfolder" do
|
2019-11-15 13:48:24 +08:00
|
|
|
set_subfolder "/forum"
|
2023-06-21 22:00:19 +08:00
|
|
|
html = email.html_part.body.to_s
|
|
|
|
text = email.text_part.body.to_s
|
2018-08-08 04:38:05 +08:00
|
|
|
expect(html).to be_present
|
|
|
|
expect(text).to be_present
|
|
|
|
expect(html).to_not include("/forum/forum")
|
|
|
|
expect(text).to_not include("/forum/forum")
|
2023-06-21 22:00:19 +08:00
|
|
|
expect(email.header["List-Unsubscribe"].to_s).to match(
|
2018-08-08 04:38:05 +08:00
|
|
|
/http:\/\/test.localhost\/forum\/email\/unsubscribe\/\h{64}/,
|
|
|
|
)
|
|
|
|
|
|
|
|
topic_url = "http://test.localhost/forum/t/#{popular_topic.slug}/#{popular_topic.id}"
|
|
|
|
expect(html).to include(topic_url)
|
|
|
|
expect(text).to include(topic_url)
|
|
|
|
end
|
2020-07-13 22:39:40 +08:00
|
|
|
|
|
|
|
it "applies lang/xml:lang html attributes" do
|
|
|
|
SiteSetting.default_locale = "pl_PL"
|
2023-06-21 22:00:19 +08:00
|
|
|
html = email.html_part.to_s
|
2020-07-13 22:39:40 +08:00
|
|
|
|
|
|
|
expect(html).to match(' lang="pl-PL"')
|
|
|
|
expect(html).to match(' xml:lang="pl-PL"')
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-26 15:27:46 +08:00
|
|
|
describe ".user_replied" do
|
2014-10-21 22:06:55 +08:00
|
|
|
let(:response_by_user) { Fabricate(:user, name: "John Doe") }
|
2014-10-03 19:44:08 +08:00
|
|
|
let(:category) { Fabricate(:category, name: "India") }
|
2023-07-13 15:39:58 +08:00
|
|
|
let(:tag1) { Fabricate(:tag, name: "Taggo", public_topic_count: 1) }
|
|
|
|
let(:tag2) { Fabricate(:tag, name: "Taggie", public_topic_count: 3) }
|
|
|
|
let(:tag3) { Fabricate(:tag, name: "Teggo", public_topic_count: 2) }
|
2022-11-30 00:35:41 +08:00
|
|
|
|
|
|
|
let(:hidden_tag) { Fabricate(:tag, name: "hidden") }
|
|
|
|
let!(:hidden_tag_group) do
|
|
|
|
Fabricate(:tag_group, permissions: { "staff" => 1 }, tag_names: [hidden_tag.name])
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:topic) do
|
|
|
|
Fabricate(
|
|
|
|
:topic,
|
|
|
|
category: category,
|
2023-06-15 01:22:14 +08:00
|
|
|
tags: [tag1, tag2, tag3, hidden_tag],
|
2022-11-30 00:35:41 +08:00
|
|
|
title: "Super cool topic",
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
end
|
2016-02-25 21:05:40 +08:00
|
|
|
let(:post) { Fabricate(:post, topic: topic, raw: "This is My super duper cool topic") }
|
2019-01-18 06:46:04 +08:00
|
|
|
let(:response) { Fabricate(:basic_reply, topic: post.topic, user: response_by_user) }
|
2014-02-12 13:51:26 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
2019-01-18 06:46:04 +08:00
|
|
|
let(:notification) { Fabricate(:replied_notification, user: user, post: response) }
|
2014-02-12 13:51:26 +08:00
|
|
|
|
2013-07-26 15:27:46 +08:00
|
|
|
it "generates a correct email" do
|
2020-07-06 08:56:19 +08:00
|
|
|
SiteSetting.default_email_in_reply_to = true
|
|
|
|
|
2016-02-25 21:05:40 +08:00
|
|
|
# Fabricator is not fabricating this ...
|
2018-07-11 10:24:07 +08:00
|
|
|
SiteSetting.email_subject =
|
|
|
|
"[%{site_name}] %{optional_pm}%{optional_cat}%{optional_tags}%{topic_title}"
|
2015-02-18 03:37:22 +08:00
|
|
|
SiteSetting.enable_names = true
|
|
|
|
SiteSetting.display_name_on_posts = true
|
2019-01-18 06:46:04 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
|
2014-10-21 22:06:55 +08:00
|
|
|
# from should include full user name
|
2019-01-04 23:06:21 +08:00
|
|
|
expect(mail[:from].display_names).to eql(["John Doe via Discourse"])
|
2014-10-21 22:06:55 +08:00
|
|
|
|
2014-10-03 19:44:08 +08:00
|
|
|
# subject should include category name
|
|
|
|
expect(mail.subject).to match(/India/)
|
|
|
|
|
2018-07-11 10:24:07 +08:00
|
|
|
# subject should include tag names
|
|
|
|
expect(mail.subject).to match(/Taggo/)
|
|
|
|
expect(mail.subject).to match(/Taggie/)
|
|
|
|
|
2019-07-31 03:05:08 +08:00
|
|
|
mail_html = mail.html_part.body.to_s
|
2016-02-25 21:05:40 +08:00
|
|
|
|
|
|
|
expect(mail_html.scan(/My super duper cool topic/).count).to eq(1)
|
|
|
|
expect(mail_html.scan(/In Reply To/).count).to eq(1)
|
|
|
|
|
2016-02-09 16:09:10 +08:00
|
|
|
# 2 "visit topic" link
|
2016-02-25 21:05:40 +08:00
|
|
|
expect(mail_html.scan(/Visit Topic/).count).to eq(2)
|
2016-02-09 16:09:10 +08:00
|
|
|
|
2013-07-26 15:27:46 +08:00
|
|
|
# 2 respond to links cause we have 1 context post
|
2016-02-25 21:05:40 +08:00
|
|
|
expect(mail_html.scan(/to respond/).count).to eq(2)
|
2013-07-26 15:27:46 +08:00
|
|
|
|
|
|
|
# 1 unsubscribe
|
2016-02-25 21:05:40 +08:00
|
|
|
expect(mail_html.scan(/To unsubscribe/).count).to eq(1)
|
2013-07-26 15:27:46 +08:00
|
|
|
|
2013-12-30 10:02:12 +08:00
|
|
|
# side effect, topic user is updated with post number
|
2019-01-18 06:46:04 +08:00
|
|
|
tu = TopicUser.get(post.topic_id, user)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tu.last_emailed_post_number).to eq(response.post_number)
|
2016-02-25 21:05:40 +08:00
|
|
|
|
|
|
|
# no In Reply To if user opts out
|
2019-01-18 06:46:04 +08:00
|
|
|
user.user_option.email_in_reply_to = false
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
2016-02-25 21:05:40 +08:00
|
|
|
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s.scan(/In Reply To/).count).to eq(0)
|
2016-11-14 08:09:24 +08:00
|
|
|
|
|
|
|
SiteSetting.enable_names = true
|
|
|
|
SiteSetting.display_name_on_posts = true
|
|
|
|
SiteSetting.prioritize_username_in_ux = false
|
|
|
|
|
|
|
|
response.user.username = "bobmarley"
|
|
|
|
response.user.name = "Bob Marley"
|
|
|
|
response.user.save
|
|
|
|
|
2019-01-18 06:46:04 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
2016-11-14 08:09:24 +08:00
|
|
|
|
2019-07-31 03:05:08 +08:00
|
|
|
mail_html = mail.html_part.body.to_s
|
2016-11-14 08:09:24 +08:00
|
|
|
expect(mail_html.scan(/>Bob Marley/).count).to eq(1)
|
|
|
|
expect(mail_html.scan(/>bobmarley/).count).to eq(0)
|
|
|
|
|
2022-11-30 00:35:41 +08:00
|
|
|
expect(mail.subject.scan(/#{tag1.name}/).count).to eq(1)
|
|
|
|
expect(mail.subject.scan(/#{hidden_tag.name}/).count).to eq(0)
|
|
|
|
|
2016-11-14 08:09:24 +08:00
|
|
|
SiteSetting.prioritize_username_in_ux = true
|
|
|
|
|
2019-01-18 06:46:04 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
2016-11-14 08:09:24 +08:00
|
|
|
|
2019-07-31 03:05:08 +08:00
|
|
|
mail_html = mail.html_part.body.to_s
|
2016-11-14 08:09:24 +08:00
|
|
|
expect(mail_html.scan(/>Bob Marley/).count).to eq(0)
|
|
|
|
expect(mail_html.scan(/>bobmarley/).count).to eq(1)
|
2013-07-26 15:27:46 +08:00
|
|
|
end
|
2017-04-25 03:26:06 +08:00
|
|
|
|
2023-06-15 01:22:14 +08:00
|
|
|
describe "number of tags shown in subject line" do
|
|
|
|
describe "max_tags_per_email_subject siteSetting enabled" do
|
|
|
|
before { SiteSetting.enable_max_tags_per_email_subject = true }
|
|
|
|
|
|
|
|
it "should match max_tags_per_email_subject" do
|
|
|
|
SiteSetting.email_subject =
|
|
|
|
"[%{site_name}] %{optional_pm}%{optional_cat}%{optional_tags}%{topic_title}"
|
|
|
|
SiteSetting.max_tags_per_topic = 1
|
|
|
|
SiteSetting.max_tags_per_email_subject = 2
|
2023-07-13 15:39:58 +08:00
|
|
|
|
2023-06-15 01:22:14 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
2023-07-13 15:39:58 +08:00
|
|
|
|
|
|
|
expect(mail.subject).to eq(
|
|
|
|
"[Discourse] [#{category.name}] #{tag2.name} #{tag3.name} #{topic.title}",
|
|
|
|
)
|
2023-06-15 01:22:14 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "max_tags_per_email_subject siteSetting disabled" do
|
|
|
|
before { SiteSetting.enable_max_tags_per_email_subject = false }
|
|
|
|
|
|
|
|
it "should match max_tags_per_topic" do
|
|
|
|
SiteSetting.email_subject =
|
|
|
|
"[%{site_name}] %{optional_pm}%{optional_cat}%{optional_tags}%{topic_title}"
|
|
|
|
SiteSetting.max_tags_per_topic = 2
|
|
|
|
SiteSetting.max_tags_per_email_subject = 1
|
|
|
|
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
2023-07-13 15:39:58 +08:00
|
|
|
|
|
|
|
expect(mail.subject).to eq(
|
|
|
|
"[Discourse] [#{category.name}] #{tag2.name} #{tag3.name} #{topic.title}",
|
|
|
|
)
|
2023-06-15 01:22:14 +08:00
|
|
|
end
|
|
|
|
end
|
2023-01-06 08:03:02 +08:00
|
|
|
end
|
|
|
|
|
2017-04-25 03:26:06 +08:00
|
|
|
it "doesn't include details when private_email is enabled" do
|
|
|
|
SiteSetting.private_email = true
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
2019-01-18 06:46:04 +08:00
|
|
|
user,
|
2017-04-25 03:26:06 +08:00
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s).to_not include(response.raw)
|
|
|
|
expect(mail.html_part.body.to_s).to_not include(topic.url)
|
2017-04-25 03:26:06 +08:00
|
|
|
expect(mail.text_part.to_s).to_not include(response.raw)
|
|
|
|
expect(mail.text_part.to_s).to_not include(topic.url)
|
|
|
|
end
|
2019-08-07 00:45:28 +08:00
|
|
|
|
|
|
|
it "includes excerpt when post_excerpts_in_emails is enabled" do
|
|
|
|
paragraphs = [
|
|
|
|
"This is the first paragraph, but you should read more.",
|
|
|
|
"And here is its friend, the second paragraph.",
|
|
|
|
]
|
|
|
|
SiteSetting.post_excerpts_in_emails = true
|
|
|
|
SiteSetting.post_excerpt_maxlength = paragraphs.first.length
|
2019-09-12 08:41:50 +08:00
|
|
|
response.update!(raw: paragraphs.join("\n\n"))
|
2019-08-07 00:45:28 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
mail_html = mail.html_part.body.to_s
|
|
|
|
expect(mail_html.scan(/#{paragraphs[0]}/).count).to eq(1)
|
|
|
|
expect(mail_html.scan(/#{paragraphs[1]}/).count).to eq(0)
|
|
|
|
end
|
2013-07-26 15:27:46 +08:00
|
|
|
end
|
|
|
|
|
2014-09-29 13:16:55 +08:00
|
|
|
describe ".user_posted" do
|
2014-12-03 15:42:05 +08:00
|
|
|
let(:response_by_user) { Fabricate(:user, name: "John Doe", username: "john") }
|
2019-01-18 06:46:04 +08:00
|
|
|
let(:topic) { Fabricate(:topic, title: "Super cool topic") }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
|
|
|
let(:response) { Fabricate(:post, topic: topic, user: response_by_user) }
|
2014-09-29 13:16:55 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
2019-01-18 06:46:04 +08:00
|
|
|
let(:notification) { Fabricate(:posted_notification, user: user, post: response) }
|
2014-09-29 13:16:55 +08:00
|
|
|
|
|
|
|
it "generates a correct email" do
|
2015-02-18 03:37:22 +08:00
|
|
|
SiteSetting.enable_names = false
|
2019-01-18 06:46:04 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_posted(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
2014-09-29 13:16:55 +08:00
|
|
|
|
2014-10-21 22:06:55 +08:00
|
|
|
# from should not include full user name if "show user full names" is disabled
|
|
|
|
expect(mail[:from].display_names).to_not eql(["John Doe"])
|
|
|
|
|
2014-12-03 15:42:05 +08:00
|
|
|
# from should include username if "show user full names" is disabled
|
2019-01-04 23:06:21 +08:00
|
|
|
expect(mail[:from].display_names).to eql(["john via Discourse"])
|
2014-12-03 15:42:05 +08:00
|
|
|
|
2014-10-03 19:44:08 +08:00
|
|
|
# subject should not include category name
|
|
|
|
expect(mail.subject).not_to match(/Uncategorized/)
|
|
|
|
|
2016-02-25 21:05:40 +08:00
|
|
|
# 1 respond to links as no context by default
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s.scan(/to respond/).count).to eq(1)
|
2014-09-29 13:16:55 +08:00
|
|
|
|
|
|
|
# 1 unsubscribe link
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s.scan(/To unsubscribe/).count).to eq(1)
|
2014-09-29 13:16:55 +08:00
|
|
|
|
|
|
|
# side effect, topic user is updated with post number
|
2019-01-18 06:46:04 +08:00
|
|
|
tu = TopicUser.get(post.topic_id, user)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tu.last_emailed_post_number).to eq(response.post_number)
|
2014-09-29 13:16:55 +08:00
|
|
|
end
|
2017-04-25 03:26:06 +08:00
|
|
|
|
|
|
|
it "doesn't include details when private_email is enabled" do
|
|
|
|
SiteSetting.private_email = true
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_posted(
|
2019-01-18 06:46:04 +08:00
|
|
|
user,
|
2017-04-25 03:26:06 +08:00
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s).to_not include(response.raw)
|
2017-04-25 03:26:06 +08:00
|
|
|
expect(mail.text_part.to_s).to_not include(response.raw)
|
|
|
|
end
|
2019-01-18 17:52:48 +08:00
|
|
|
|
|
|
|
it "uses the original subject for staged users" do
|
|
|
|
incoming_email =
|
|
|
|
Fabricate(
|
|
|
|
:incoming_email,
|
|
|
|
subject: "Original Subject",
|
|
|
|
post: post,
|
|
|
|
topic: post.topic,
|
|
|
|
user: user,
|
|
|
|
)
|
|
|
|
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_posted(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
expect(mail.subject).to match(/Super cool topic/)
|
|
|
|
|
|
|
|
user.update!(staged: true)
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_posted(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
expect(mail.subject).to eq("Re: Original Subject")
|
|
|
|
|
|
|
|
another_post = Fabricate(:post, topic: topic)
|
|
|
|
incoming_email.update!(post_id: another_post.id)
|
|
|
|
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_private_message(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
expect(mail.subject).to match(/Super cool topic/)
|
|
|
|
end
|
2014-09-29 13:16:55 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".user_private_message" do
|
2014-12-03 15:42:05 +08:00
|
|
|
let(:response_by_user) { Fabricate(:user, name: "", username: "john") }
|
2019-01-18 17:52:48 +08:00
|
|
|
let(:topic) { Fabricate(:private_message_topic, title: "Super cool topic") }
|
|
|
|
let(:post) { Fabricate(:post, topic: topic) }
|
2014-10-21 22:06:55 +08:00
|
|
|
let(:response) { Fabricate(:post, topic: topic, user: response_by_user) }
|
2014-09-29 13:16:55 +08:00
|
|
|
let(:user) { Fabricate(:user) }
|
2019-01-18 06:46:04 +08:00
|
|
|
let(:notification) { Fabricate(:private_message_notification, user: user, post: response) }
|
2014-09-29 13:16:55 +08:00
|
|
|
|
|
|
|
it "generates a correct email" do
|
2015-02-18 03:37:22 +08:00
|
|
|
SiteSetting.enable_names = true
|
2016-01-27 09:19:49 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_private_message(
|
2019-01-18 06:46:04 +08:00
|
|
|
user,
|
2016-01-27 09:19:49 +08:00
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
2014-09-29 13:16:55 +08:00
|
|
|
|
2014-12-03 15:42:05 +08:00
|
|
|
# from should include username if full user name is not provided
|
2019-01-04 23:06:21 +08:00
|
|
|
expect(mail[:from].display_names).to eql(["john via Discourse"])
|
2014-10-21 22:06:55 +08:00
|
|
|
|
2014-09-29 13:16:55 +08:00
|
|
|
# subject should include "[PM]"
|
2018-02-19 17:20:17 +08:00
|
|
|
expect(mail.subject).to include("[PM] ")
|
2014-09-29 13:16:55 +08:00
|
|
|
|
2016-02-09 16:09:10 +08:00
|
|
|
# 1 "visit message" link
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s.scan(/Visit Message/).count).to eq(1)
|
2016-02-09 16:09:10 +08:00
|
|
|
|
2014-09-29 13:16:55 +08:00
|
|
|
# 1 respond to link
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s.scan(/to respond/).count).to eq(1)
|
2014-09-29 13:16:55 +08:00
|
|
|
|
|
|
|
# 1 unsubscribe link
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s.scan(/To unsubscribe/).count).to eq(1)
|
2014-09-29 13:16:55 +08:00
|
|
|
|
|
|
|
# side effect, topic user is updated with post number
|
2019-01-18 06:46:04 +08:00
|
|
|
tu = TopicUser.get(topic.id, user)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(tu.last_emailed_post_number).to eq(response.post_number)
|
2014-09-29 13:16:55 +08:00
|
|
|
end
|
2017-04-25 03:26:06 +08:00
|
|
|
|
|
|
|
it "doesn't include details when private_email is enabled" do
|
|
|
|
SiteSetting.private_email = true
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_private_message(
|
2019-01-18 06:46:04 +08:00
|
|
|
user,
|
2017-04-25 03:26:06 +08:00
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s).to_not include(response.raw)
|
|
|
|
expect(mail.html_part.body.to_s).to_not include(topic.url)
|
2017-04-25 03:26:06 +08:00
|
|
|
expect(mail.text_part.to_s).to_not include(response.raw)
|
|
|
|
expect(mail.text_part.to_s).to_not include(topic.url)
|
|
|
|
end
|
2018-02-19 17:20:17 +08:00
|
|
|
|
|
|
|
it "doesn't include group name in subject" do
|
|
|
|
group = Fabricate(:group)
|
2019-01-18 06:46:04 +08:00
|
|
|
topic.allowed_groups = [group]
|
2018-02-19 17:20:17 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_private_message(
|
2019-01-18 06:46:04 +08:00
|
|
|
user,
|
2018-02-19 17:20:17 +08:00
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(mail.subject).to include("[PM] ")
|
|
|
|
end
|
|
|
|
|
2021-10-19 13:26:22 +08:00
|
|
|
it "includes a list of participants (except for the destination user), groups first with member lists" do
|
2018-05-07 11:25:01 +08:00
|
|
|
group1 = Fabricate(:group, name: "group1")
|
|
|
|
group2 = Fabricate(:group, name: "group2")
|
|
|
|
|
|
|
|
user1 = Fabricate(:user, username: "one", groups: [group1, group2])
|
2022-04-21 00:05:17 +08:00
|
|
|
user2 = Fabricate(:user, username: "two", groups: [group1], staged: true)
|
2018-05-07 11:25:01 +08:00
|
|
|
|
2021-10-19 13:26:22 +08:00
|
|
|
topic.allowed_users = [user, user1, user2]
|
2018-05-07 11:25:01 +08:00
|
|
|
topic.allowed_groups = [group1, group2]
|
|
|
|
|
2018-05-04 06:50:06 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_private_message(
|
2019-01-18 06:46:04 +08:00
|
|
|
user,
|
2018-05-04 06:50:06 +08:00
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
|
2022-04-13 21:53:54 +08:00
|
|
|
expect(mail.body).to include(
|
|
|
|
"[group1 (2)](http://test.localhost/g/group1), [group2 (1)](http://test.localhost/g/group2), [one](http://test.localhost/u/one), [two](http://test.localhost/u/two)",
|
|
|
|
)
|
2018-05-04 06:50:06 +08:00
|
|
|
end
|
|
|
|
|
2018-02-19 17:20:17 +08:00
|
|
|
context "when SiteSetting.group_name_in_subject is true" do
|
|
|
|
before { SiteSetting.group_in_subject = true }
|
|
|
|
|
|
|
|
let(:group) { Fabricate(:group, name: "my_group") }
|
2019-01-18 06:46:04 +08:00
|
|
|
let(:mail) do
|
|
|
|
UserNotifications.user_private_message(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
end
|
2018-02-19 17:20:17 +08:00
|
|
|
|
|
|
|
shared_examples "includes first group name" do
|
|
|
|
it "includes first group name in subject" do
|
|
|
|
expect(mail.subject).to include("[my_group] ")
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when first group has full name" do
|
|
|
|
it "includes full name in subject" do
|
|
|
|
group.full_name = "My Group"
|
|
|
|
group.save
|
|
|
|
expect(mail.subject).to include("[My Group] ")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with one group in pm" do
|
2019-01-18 06:46:04 +08:00
|
|
|
before { topic.allowed_groups = [group] }
|
2018-02-19 17:20:17 +08:00
|
|
|
|
|
|
|
include_examples "includes first group name"
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with multiple groups in pm" do
|
2018-02-19 17:20:17 +08:00
|
|
|
let(:group2) { Fabricate(:group) }
|
|
|
|
|
2019-01-18 06:46:04 +08:00
|
|
|
before { topic.allowed_groups = [group, group2] }
|
2018-02-19 17:20:17 +08:00
|
|
|
|
|
|
|
include_examples "includes first group name"
|
|
|
|
end
|
2018-03-11 20:22:11 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with no groups in pm" do
|
2018-03-11 20:22:11 +08:00
|
|
|
it "includes %{optional_pm} in subject" do
|
|
|
|
expect(mail.subject).to include("[PM] ")
|
|
|
|
end
|
|
|
|
end
|
2018-02-19 17:20:17 +08:00
|
|
|
end
|
2019-01-18 17:52:48 +08:00
|
|
|
|
|
|
|
it "uses the original subject for staged users when topic was started via email" do
|
|
|
|
incoming_email =
|
|
|
|
Fabricate(
|
|
|
|
:incoming_email,
|
|
|
|
subject: "Original Subject",
|
|
|
|
post: post,
|
|
|
|
topic: topic,
|
|
|
|
user: user,
|
|
|
|
)
|
|
|
|
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_private_message(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
expect(mail.subject).to match(/Super cool topic/)
|
|
|
|
|
|
|
|
user.update!(staged: true)
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_private_message(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
expect(mail.subject).to eq("Re: Original Subject")
|
|
|
|
|
|
|
|
another_post = Fabricate(:post, topic: topic)
|
|
|
|
incoming_email.update!(post_id: another_post.id)
|
|
|
|
|
|
|
|
mail =
|
|
|
|
UserNotifications.user_private_message(
|
|
|
|
user,
|
|
|
|
post: response,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
expect(mail.subject).to match(/Super cool topic/)
|
|
|
|
end
|
2014-09-29 13:16:55 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2016-03-23 12:08:34 +08:00
|
|
|
it "adds a warning when mail limit is reached" do
|
|
|
|
SiteSetting.max_emails_per_day_per_user = 2
|
|
|
|
user = Fabricate(:user)
|
2018-07-27 12:32:07 +08:00
|
|
|
|
|
|
|
user.email_logs.create!(email_type: "blah", to_address: user.email, user_id: user.id)
|
2016-03-23 12:08:34 +08:00
|
|
|
|
|
|
|
post = Fabricate(:post)
|
|
|
|
reply = Fabricate(:post, topic_id: post.topic_id)
|
|
|
|
|
2019-01-18 06:46:04 +08:00
|
|
|
notification =
|
|
|
|
Fabricate(
|
|
|
|
:notification,
|
|
|
|
topic_id: post.topic_id,
|
|
|
|
post_number: reply.post_number,
|
|
|
|
user: post.user,
|
|
|
|
data: { original_username: "bob" }.to_json,
|
|
|
|
)
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2016-03-23 12:08:34 +08:00
|
|
|
mail =
|
|
|
|
UserNotifications.user_replied(
|
|
|
|
user,
|
|
|
|
post: reply,
|
|
|
|
notification_type: notification.notification_type,
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
)
|
|
|
|
|
|
|
|
# WARNING: you reached the limit of 100 email notifications per day. Further emails will be suppressed.
|
|
|
|
# Consider watching less topics or disabling mailing list mode.
|
2019-07-31 03:05:08 +08:00
|
|
|
expect(mail.html_part.body.to_s).to match(I18n.t("user_notifications.reached_limit", count: 2))
|
2017-05-17 19:30:13 +08:00
|
|
|
expect(mail.body.to_s).to match(I18n.t("user_notifications.reached_limit", count: 2))
|
2016-03-23 12:08:34 +08:00
|
|
|
end
|
|
|
|
|
2013-06-13 04:35:46 +08:00
|
|
|
def expects_build_with(condition)
|
|
|
|
UserNotifications.any_instance.expects(:build_email).with(user.email, condition)
|
2019-05-07 09:27:05 +08:00
|
|
|
mailer =
|
|
|
|
UserNotifications.public_send(
|
|
|
|
mail_type,
|
|
|
|
user,
|
|
|
|
notification_type: Notification.types[notification.notification_type],
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
post: notification.post,
|
|
|
|
)
|
|
|
|
|
2016-01-27 09:19:49 +08:00
|
|
|
mailer.message
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "supports reply by email" do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with reply_by_email" do
|
2013-06-13 04:35:46 +08:00
|
|
|
it "should have allow_reply_by_email set when that feature is enabled" do
|
|
|
|
expects_build_with(has_entry(:allow_reply_by_email, true))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "no reply by email" do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with reply_by_email" do
|
2013-06-13 22:56:16 +08:00
|
|
|
it "doesn't support reply by email" do
|
2013-06-13 04:35:46 +08:00
|
|
|
expects_build_with(Not(has_entry(:allow_reply_by_email, true)))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-25 03:26:06 +08:00
|
|
|
shared_examples "respect for private_email" do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with private_email" do
|
2017-04-25 03:26:06 +08:00
|
|
|
it "doesn't support reply by email" do
|
|
|
|
SiteSetting.private_email = true
|
2019-05-07 09:27:05 +08:00
|
|
|
|
|
|
|
mailer =
|
|
|
|
UserNotifications.public_send(
|
2017-04-25 03:26:06 +08:00
|
|
|
mail_type,
|
|
|
|
user,
|
|
|
|
notification_type: Notification.types[notification.notification_type],
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
post: notification.post,
|
|
|
|
)
|
|
|
|
message = mailer.message
|
|
|
|
|
|
|
|
topic = notification.post.topic
|
|
|
|
expect(message.html_part.body.to_s).not_to include(topic.title)
|
|
|
|
expect(message.html_part.body.to_s).not_to include(topic.slug)
|
|
|
|
expect(message.text_part.body.to_s).not_to include(topic.title)
|
|
|
|
expect(message.text_part.body.to_s).not_to include(topic.slug)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-10 07:54:13 +08:00
|
|
|
# The parts of emails that are derived from templates are translated
|
|
|
|
shared_examples "sets user locale" do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "with set locale for translating templates" do
|
2016-02-10 07:54:13 +08:00
|
|
|
it "sets the locale" do
|
|
|
|
expects_build_with(has_key(:locale))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-13 04:35:46 +08:00
|
|
|
shared_examples "notification email building" do
|
2013-02-06 03:16:51 +08:00
|
|
|
let(:post) { Fabricate(:post, user: user) }
|
2013-06-13 04:35:46 +08:00
|
|
|
let(:mail_type) { "user_#{notification_type}" }
|
2017-07-26 14:51:44 +08:00
|
|
|
let(:mail_template) { "user_notifications.#{mail_type}" }
|
2013-02-28 07:30:14 +08:00
|
|
|
let(:username) { "walterwhite" }
|
2013-02-06 03:16:51 +08:00
|
|
|
let(:notification) do
|
2013-06-13 04:35:46 +08:00
|
|
|
Fabricate(
|
|
|
|
:notification,
|
|
|
|
user: user,
|
|
|
|
topic: post.topic,
|
|
|
|
notification_type: Notification.types[notification_type],
|
|
|
|
post_number: post.post_number,
|
2014-02-04 09:56:28 +08:00
|
|
|
data: { original_username: username }.to_json,
|
|
|
|
)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2017-03-11 03:07:41 +08:00
|
|
|
describe "email building" do
|
2013-06-13 04:35:46 +08:00
|
|
|
it "has a username" do
|
|
|
|
expects_build_with(has_entry(:username, username))
|
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
|
2013-06-13 04:35:46 +08:00
|
|
|
it "has a url" do
|
|
|
|
expects_build_with(has_key(:url))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a template" do
|
2017-07-26 14:51:44 +08:00
|
|
|
expects_build_with(has_entry(:template, mail_template))
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
|
|
|
|
2017-03-11 03:07:41 +08:00
|
|
|
it "overrides the html part" do
|
|
|
|
expects_build_with(has_key(:html_override))
|
|
|
|
end
|
|
|
|
|
2013-06-13 04:35:46 +08:00
|
|
|
it "has a message" do
|
2013-07-23 03:06:37 +08:00
|
|
|
expects_build_with(has_key(:message))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has a context" do
|
|
|
|
expects_build_with(has_key(:context))
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
2013-02-28 07:30:14 +08:00
|
|
|
|
2013-06-13 04:35:46 +08:00
|
|
|
it "has an unsubscribe link" do
|
|
|
|
expects_build_with(has_key(:add_unsubscribe_link))
|
|
|
|
end
|
|
|
|
|
2013-06-14 06:11:10 +08:00
|
|
|
it "has an post_id" do
|
|
|
|
expects_build_with(has_key(:post_id))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "has an topic_id" do
|
|
|
|
expects_build_with(has_key(:topic_id))
|
|
|
|
end
|
|
|
|
|
2015-02-18 03:37:22 +08:00
|
|
|
it "should have user name as from_alias" do
|
|
|
|
SiteSetting.enable_names = true
|
|
|
|
SiteSetting.display_name_on_posts = true
|
2019-01-04 23:06:21 +08:00
|
|
|
expects_build_with(has_entry(:from_alias, "#{user.name} via Discourse"))
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
2014-05-07 03:01:19 +08:00
|
|
|
|
2015-02-18 03:37:22 +08:00
|
|
|
it "should not have user name as from_alias if display_name_on_posts is disabled" do
|
|
|
|
SiteSetting.enable_names = false
|
|
|
|
SiteSetting.display_name_on_posts = false
|
2019-01-04 23:06:21 +08:00
|
|
|
expects_build_with(has_entry(:from_alias, "walterwhite via Discourse"))
|
2015-02-18 03:37:22 +08:00
|
|
|
end
|
|
|
|
|
2014-05-07 03:01:19 +08:00
|
|
|
it "should explain how to respond" do
|
|
|
|
expects_build_with(Not(has_entry(:include_respond_instructions, false)))
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should not explain how to respond if the user is suspended" do
|
|
|
|
User.any_instance.stubs(:suspended?).returns(true)
|
|
|
|
expects_build_with(has_entry(:include_respond_instructions, false))
|
|
|
|
end
|
2017-03-11 03:07:41 +08:00
|
|
|
|
|
|
|
context "when customized" do
|
2017-06-16 08:39:54 +08:00
|
|
|
let(:custom_body) do
|
2019-04-30 08:27:42 +08:00
|
|
|
body = +<<~BODY
|
2019-01-18 06:46:04 +08:00
|
|
|
You are now officially notified.
|
|
|
|
%{header_instructions}
|
|
|
|
%{message} %{respond_instructions}
|
|
|
|
%{topic_title_url_encoded}
|
|
|
|
%{site_title_url_encoded}
|
2017-06-16 08:39:54 +08:00
|
|
|
BODY
|
|
|
|
|
|
|
|
body << "%{context}" if notification_type != :invited_to_topic
|
|
|
|
body
|
|
|
|
end
|
2017-03-11 03:07:41 +08:00
|
|
|
|
|
|
|
before do
|
2017-07-26 14:51:44 +08:00
|
|
|
TranslationOverride.upsert!(
|
2019-05-16 05:43:00 +08:00
|
|
|
SiteSetting.default_locale,
|
2017-07-26 14:51:44 +08:00
|
|
|
"#{mail_template}.text_body_template",
|
|
|
|
custom_body,
|
|
|
|
)
|
2017-03-11 03:07:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "shouldn't use the default html_override" do
|
|
|
|
expects_build_with(Not(has_key(:html_override)))
|
|
|
|
end
|
|
|
|
end
|
2013-02-28 07:30:14 +08:00
|
|
|
end
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
2013-02-28 07:30:14 +08:00
|
|
|
|
2013-06-13 04:35:46 +08:00
|
|
|
describe "user mentioned email" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :mentioned }
|
2017-04-25 03:26:06 +08:00
|
|
|
include_examples "respect for private_email"
|
2013-06-13 04:35:46 +08:00
|
|
|
include_examples "supports reply by email"
|
2016-02-10 07:54:13 +08:00
|
|
|
include_examples "sets user locale"
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
|
|
|
end
|
2013-02-28 07:30:14 +08:00
|
|
|
|
2021-09-17 01:37:45 +08:00
|
|
|
describe "group mentioned email" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :group_mentioned }
|
|
|
|
let(:post) { Fabricate(:private_message_post) }
|
|
|
|
let(:user) { post.user }
|
|
|
|
let(:mail_type) { "group_mentioned" }
|
|
|
|
let(:mail_template) { "user_notifications.user_#{notification_type}_pm" }
|
|
|
|
|
|
|
|
include_examples "respect for private_email"
|
|
|
|
include_examples "supports reply by email"
|
|
|
|
include_examples "sets user locale"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-13 04:35:46 +08:00
|
|
|
describe "user replied" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :replied }
|
2017-04-25 03:26:06 +08:00
|
|
|
include_examples "respect for private_email"
|
2013-06-13 04:35:46 +08:00
|
|
|
include_examples "supports reply by email"
|
2016-02-10 07:54:13 +08:00
|
|
|
include_examples "sets user locale"
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-06-13 04:35:46 +08:00
|
|
|
describe "user quoted" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :quoted }
|
2017-04-25 03:26:06 +08:00
|
|
|
include_examples "respect for private_email"
|
2013-06-13 04:35:46 +08:00
|
|
|
include_examples "supports reply by email"
|
2016-02-10 07:54:13 +08:00
|
|
|
include_examples "sets user locale"
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "user posted" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :posted }
|
2017-04-25 03:26:06 +08:00
|
|
|
include_examples "respect for private_email"
|
2013-06-13 04:35:46 +08:00
|
|
|
include_examples "supports reply by email"
|
2016-02-10 07:54:13 +08:00
|
|
|
include_examples "sets user locale"
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "user invited to a private message" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :invited_to_private_message }
|
2017-07-26 14:51:44 +08:00
|
|
|
let(:post) { Fabricate(:private_message_post) }
|
|
|
|
let(:user) { post.user }
|
|
|
|
let(:mail_template) { "user_notifications.user_#{notification_type}_pm" }
|
|
|
|
|
|
|
|
include_examples "respect for private_email"
|
|
|
|
include_examples "no reply by email"
|
|
|
|
include_examples "sets user locale"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "group invited to a private message" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :invited_to_private_message }
|
|
|
|
let(:post) { Fabricate(:private_message_post) }
|
|
|
|
let(:user) { post.user }
|
|
|
|
let(:group) { Fabricate(:group) }
|
|
|
|
let(:mail_template) { "user_notifications.user_#{notification_type}_pm_group" }
|
|
|
|
|
|
|
|
before do
|
|
|
|
notification.data_hash[:group_id] = group.id
|
|
|
|
notification.save!
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the group name" do
|
|
|
|
expects_build_with(has_entry(:group_name, group.name))
|
|
|
|
end
|
|
|
|
|
2017-04-25 03:26:06 +08:00
|
|
|
include_examples "respect for private_email"
|
2013-06-13 04:35:46 +08:00
|
|
|
include_examples "no reply by email"
|
2016-02-10 07:54:13 +08:00
|
|
|
include_examples "sets user locale"
|
2013-06-13 04:35:46 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-31 02:36:47 +08:00
|
|
|
describe "user invited to a topic" do
|
2021-05-26 10:55:07 +08:00
|
|
|
let(:notification_type) { :invited_to_topic }
|
|
|
|
|
2015-03-31 02:36:47 +08:00
|
|
|
include_examples "notification email building" do
|
2017-04-25 03:26:06 +08:00
|
|
|
include_examples "respect for private_email"
|
2016-07-08 00:23:19 +08:00
|
|
|
include_examples "no reply by email"
|
|
|
|
include_examples "sets user locale"
|
|
|
|
end
|
2021-05-26 10:55:07 +08:00
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when showing the right name in 'From' field" do
|
2021-05-26 10:55:07 +08:00
|
|
|
let(:inviter) { Fabricate(:user) }
|
|
|
|
let(:invitee) { Fabricate(:user) }
|
|
|
|
|
|
|
|
let(:notification) do
|
|
|
|
Fabricate(
|
|
|
|
:notification,
|
|
|
|
notification_type: Notification.types[:invited_to_topic],
|
|
|
|
user: invitee,
|
|
|
|
topic: post.topic,
|
|
|
|
post_number: post.post_number,
|
|
|
|
data: {
|
|
|
|
topic_title: post.topic.title,
|
|
|
|
display_username: inviter.username,
|
|
|
|
original_user_id: inviter.id,
|
|
|
|
original_username: inviter.username,
|
|
|
|
}.to_json,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:mailer) do
|
|
|
|
UserNotifications.public_send(
|
|
|
|
"user_invited_to_topic",
|
|
|
|
invitee,
|
|
|
|
notification_type: Notification.types[notification.notification_type],
|
|
|
|
notification_data_hash: notification.data_hash,
|
|
|
|
post: notification.post,
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends the email as the inviter" do
|
|
|
|
SiteSetting.enable_names = false
|
|
|
|
|
|
|
|
expect(mailer.message.to_s).to include(
|
|
|
|
"From: #{inviter.username} via #{SiteSetting.title} <#{SiteSetting.notification_email}>",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "sends the email as the inviter" do
|
|
|
|
expect(mailer.message.to_s).to include(
|
|
|
|
"From: #{inviter.name} via #{SiteSetting.title} <#{SiteSetting.notification_email}>",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2016-07-08 00:23:19 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe "watching first post" do
|
|
|
|
include_examples "notification email building" do
|
|
|
|
let(:notification_type) { :invited_to_topic }
|
2017-04-25 03:26:06 +08:00
|
|
|
include_examples "respect for private_email"
|
2015-03-31 02:36:47 +08:00
|
|
|
include_examples "no reply by email"
|
2016-02-10 07:54:13 +08:00
|
|
|
include_examples "sets user locale"
|
2015-03-31 02:36:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-10 07:54:13 +08:00
|
|
|
# notification emails derived from templates are translated into the user's locale
|
2022-07-28 00:14:14 +08:00
|
|
|
shared_context "with notification derived from template" do
|
2016-02-10 07:54:13 +08:00
|
|
|
let(:user) { Fabricate(:user, locale: locale) }
|
|
|
|
let(:mail_type) { mail_type }
|
|
|
|
let(:notification) { Fabricate(:notification, user: user) }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "notifications from template" do
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when user locale is allowed" do
|
2018-09-05 17:44:26 +08:00
|
|
|
before { SiteSetting.allow_user_locale = true }
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2016-03-08 03:40:11 +08:00
|
|
|
%w[
|
|
|
|
signup
|
|
|
|
signup_after_approval
|
|
|
|
confirm_old_email
|
|
|
|
notify_old_email
|
|
|
|
confirm_new_email
|
|
|
|
forgot_password
|
|
|
|
admin_login
|
|
|
|
account_created
|
|
|
|
].each do |mail_type|
|
2022-07-28 00:14:14 +08:00
|
|
|
include_examples "with notification derived from template" do
|
2016-02-10 07:54:13 +08:00
|
|
|
let(:locale) { "fr" }
|
|
|
|
let(:mail_type) { mail_type }
|
|
|
|
it "sets the locale" do
|
|
|
|
expects_build_with(has_entry(:locale, "fr"))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when user locale is not allowed" do
|
2018-09-05 17:44:26 +08:00
|
|
|
before { SiteSetting.allow_user_locale = false }
|
2023-01-09 19:18:21 +08:00
|
|
|
|
2018-09-05 17:44:26 +08:00
|
|
|
%w[
|
|
|
|
signup
|
|
|
|
signup_after_approval
|
|
|
|
notify_old_email
|
|
|
|
confirm_old_email
|
|
|
|
confirm_new_email
|
2016-03-08 03:40:11 +08:00
|
|
|
forgot_password
|
|
|
|
admin_login
|
|
|
|
account_created
|
|
|
|
].each do |mail_type|
|
2022-07-28 00:14:14 +08:00
|
|
|
include_examples "with notification derived from template" do
|
2018-09-05 17:44:26 +08:00
|
|
|
let(:locale) { "fr" }
|
2016-03-05 05:21:30 +08:00
|
|
|
let(:mail_type) { mail_type }
|
|
|
|
it "sets the locale" do
|
2021-01-21 04:32:22 +08:00
|
|
|
expects_build_with(has_entry(:locale, "en"))
|
2016-03-05 05:21:30 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-02-10 07:54:13 +08:00
|
|
|
end
|
2022-04-21 00:05:17 +08:00
|
|
|
|
|
|
|
describe "#participants" do
|
|
|
|
fab!(:group1) { Fabricate(:group, name: "group1") }
|
|
|
|
fab!(:group2) { Fabricate(:group, name: "group2") }
|
|
|
|
fab!(:group3) { Fabricate(:group, name: "group3") }
|
|
|
|
fab!(:user1) { Fabricate(:user, username: "one", name: nil, groups: [group1, group2]) }
|
|
|
|
fab!(:user2) { Fabricate(:user, username: "two", name: nil, groups: [group1]) }
|
|
|
|
fab!(:user3) { Fabricate(:user, username: "three", name: nil, groups: [group3]) }
|
|
|
|
fab!(:user4) { Fabricate(:user, username: "four", name: nil, groups: [group1, group3]) }
|
|
|
|
fab!(:admin) { Fabricate(:admin, username: "admin", name: nil) }
|
|
|
|
|
|
|
|
fab!(:topic) do
|
|
|
|
t = Fabricate(:private_message_topic, title: "Super cool topic")
|
|
|
|
t.allowed_users = [user1, user2, user3, user4, admin]
|
|
|
|
t.allowed_groups = [group1]
|
|
|
|
t
|
|
|
|
end
|
|
|
|
fab!(:posts) do
|
|
|
|
[
|
|
|
|
Fabricate(:post, topic: topic, post_number: 1, user: user2),
|
|
|
|
Fabricate(:post, topic: topic, post_number: 2, user: user1),
|
|
|
|
Fabricate(:post, topic: topic, post_number: 3, user: user2),
|
|
|
|
Fabricate(:small_action, topic: topic, post_number: 4, user: admin),
|
|
|
|
Fabricate(:post, topic: topic, post_number: 5, user: user4),
|
|
|
|
Fabricate(:post, topic: topic, post_number: 6, user: user3),
|
|
|
|
Fabricate(:post, topic: topic, post_number: 7, user: user4),
|
|
|
|
]
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns a list of participants (except for the recipient), groups first, followed by users in order of their last reply" do
|
|
|
|
expect(UserNotifications.participants(posts.last, user3)).to eq(
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), " \
|
|
|
|
"[four](http://test.localhost/u/four), [two](http://test.localhost/u/two), [one](http://test.localhost/u/one), " \
|
|
|
|
"[admin](http://test.localhost/u/admin)",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "caps the list according to site setting" do
|
|
|
|
SiteSetting.max_participant_names = 3
|
|
|
|
list =
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), [four](http://test.localhost/u/four), [two](http://test.localhost/u/two)"
|
|
|
|
expect(UserNotifications.participants(posts.last, user3)).to eq(
|
|
|
|
I18n.t("user_notifications.more_pm_participants", participants: list, count: 2),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "orders groups by user count" do
|
|
|
|
SiteSetting.max_participant_names = 3
|
|
|
|
topic.allowed_groups = [group1, group2, group3]
|
|
|
|
|
|
|
|
list =
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), [group3 (2)](http://test.localhost/g/group3), [group2 (1)](http://test.localhost/g/group2)"
|
|
|
|
expect(UserNotifications.participants(posts.last, user3)).to eq(
|
|
|
|
I18n.t("user_notifications.more_pm_participants", participants: list, count: 4),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "orders users by their last reply and user id" do
|
|
|
|
expect(UserNotifications.participants(posts[-3], user4)).to eq(
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), " \
|
|
|
|
"[two](http://test.localhost/u/two), [one](http://test.localhost/u/one), [three](http://test.localhost/u/three), " \
|
|
|
|
"[admin](http://test.localhost/u/admin)",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "prefers full group names when available" do
|
|
|
|
SiteSetting.max_participant_names = 2
|
|
|
|
topic.allowed_groups = [group1, group2]
|
|
|
|
|
|
|
|
group2.update!(full_name: "Awesome Group")
|
|
|
|
|
|
|
|
list =
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), [Awesome Group (1)](http://test.localhost/g/group2)"
|
|
|
|
expect(UserNotifications.participants(posts.last, user3)).to eq(
|
|
|
|
I18n.t("user_notifications.more_pm_participants", participants: list, count: 4),
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "always uses usernames when prioritize_username_in_ux is enabled" do
|
|
|
|
user4.update!(name: "James Bond")
|
|
|
|
user1.update!(name: "Indiana Jones")
|
|
|
|
|
|
|
|
SiteSetting.prioritize_username_in_ux = true
|
|
|
|
expect(UserNotifications.participants(posts.last, user3)).to eq(
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), " \
|
|
|
|
"[four](http://test.localhost/u/four), [two](http://test.localhost/u/two), [one](http://test.localhost/u/one), " \
|
|
|
|
"[admin](http://test.localhost/u/admin)",
|
|
|
|
)
|
|
|
|
|
|
|
|
SiteSetting.prioritize_username_in_ux = false
|
|
|
|
expect(UserNotifications.participants(posts.last, user3)).to eq(
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), " \
|
|
|
|
"[James Bond](http://test.localhost/u/four), [two](http://test.localhost/u/two), [Indiana Jones](http://test.localhost/u/one), " \
|
|
|
|
"[admin](http://test.localhost/u/admin)",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "reveals the email address of staged users if enabled" do
|
|
|
|
user4.update!(staged: true, email: "james.bond@mi6.invalid")
|
|
|
|
user1.update!(staged: true, email: "indiana.jones@example.com")
|
|
|
|
|
|
|
|
SiteSetting.prioritize_username_in_ux = true
|
|
|
|
expect(UserNotifications.participants(posts.last, user3, reveal_staged_email: true)).to eq(
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), james.bond@mi6.invalid, [two](http://test.localhost/u/two), " \
|
|
|
|
"indiana.jones@example.com, [admin](http://test.localhost/u/admin)",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does only include human users" do
|
|
|
|
topic.allowed_users << Discourse.system_user
|
|
|
|
|
|
|
|
expect(UserNotifications.participants(posts.last, user3)).to eq(
|
|
|
|
"[group1 (3)](http://test.localhost/g/group1), " \
|
|
|
|
"[four](http://test.localhost/u/four), [two](http://test.localhost/u/two), [one](http://test.localhost/u/one), " \
|
|
|
|
"[admin](http://test.localhost/u/admin)",
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-05-27 17:58:54 +08:00
|
|
|
|
|
|
|
describe ".account_silenced" do
|
|
|
|
fab!(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:silence_user]) }
|
|
|
|
|
|
|
|
it "adds the silenced_till date in user's timezone" do
|
|
|
|
user.user_option.timezone = "Asia/Tbilisi" # GMT+4
|
|
|
|
user.silenced_till = DateTime.parse("May 25, 2020, 12:00pm")
|
|
|
|
|
|
|
|
mail = UserNotifications.account_silenced(user, { user_history: user_history })
|
|
|
|
|
|
|
|
expect(mail.body).to include("May 25, 2020, 4:00pm")
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when user doesn't have timezone set" do
|
2022-05-27 17:58:54 +08:00
|
|
|
before { user.user_option.timezone = nil }
|
|
|
|
|
|
|
|
it "doesn't raise error" do
|
|
|
|
expect { UserNotifications.account_silenced(user) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the silenced_till date in UTC" do
|
|
|
|
date = "May 25, 2020, 12:00pm"
|
|
|
|
user.silenced_till = DateTime.parse(date)
|
|
|
|
|
|
|
|
mail = UserNotifications.account_silenced(user, { user_history: user_history })
|
|
|
|
|
|
|
|
expect(mail.body).to include(date)
|
|
|
|
end
|
|
|
|
end
|
2024-04-12 22:53:44 +08:00
|
|
|
|
|
|
|
context "when user timezone is invalid" do
|
|
|
|
before { user.user_option.timezone = "" }
|
|
|
|
|
|
|
|
it "doesn't raise error" do
|
|
|
|
expect { UserNotifications.account_silenced(user) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the silenced_till date in UTC" do
|
|
|
|
date = "May 25, 2020, 12:00pm"
|
|
|
|
user.silenced_till = DateTime.parse(date)
|
|
|
|
|
|
|
|
mail = UserNotifications.account_silenced(user, { user_history: user_history })
|
|
|
|
|
|
|
|
expect(mail.body).to include(date)
|
|
|
|
end
|
|
|
|
end
|
2022-05-27 17:58:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
describe ".account_suspended" do
|
|
|
|
fab!(:user_history) { Fabricate(:user_history, action: UserHistory.actions[:suspend_user]) }
|
|
|
|
|
|
|
|
it "adds the suspended_till date in user's timezone" do
|
|
|
|
user.user_option.timezone = "Asia/Tbilisi" # GMT+4
|
|
|
|
user.suspended_till = DateTime.parse("May 25, 2020, 12:00pm")
|
|
|
|
|
|
|
|
mail = UserNotifications.account_suspended(user, { user_history: user_history })
|
|
|
|
|
|
|
|
expect(mail.body).to include("May 25, 2020, 4:00pm")
|
|
|
|
end
|
|
|
|
|
2022-07-28 00:14:14 +08:00
|
|
|
context "when user doesn't have timezone set" do
|
2022-05-27 17:58:54 +08:00
|
|
|
before { user.user_option.timezone = nil }
|
|
|
|
|
|
|
|
it "doesn't raise error" do
|
|
|
|
expect { UserNotifications.account_suspended(user) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the suspended_till date in UTC" do
|
|
|
|
date = "May 25, 2020, 12:00pm"
|
|
|
|
user.suspended_till = DateTime.parse(date)
|
|
|
|
|
|
|
|
mail = UserNotifications.account_suspended(user, { user_history: user_history })
|
|
|
|
|
|
|
|
expect(mail.body).to include(date)
|
|
|
|
end
|
|
|
|
end
|
2024-04-12 22:53:44 +08:00
|
|
|
|
|
|
|
context "when user timezone is invalid" do
|
|
|
|
before { user.user_option.timezone = "" }
|
|
|
|
|
|
|
|
it "doesn't raise error" do
|
|
|
|
expect { UserNotifications.account_suspended(user) }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "adds the suspended_till date in UTC" do
|
|
|
|
date = "May 25, 2020, 12:00pm"
|
|
|
|
user.suspended_till = DateTime.parse(date)
|
|
|
|
|
|
|
|
mail = UserNotifications.account_suspended(user, { user_history: user_history })
|
|
|
|
|
|
|
|
expect(mail.body).to include(date)
|
|
|
|
end
|
|
|
|
end
|
2022-05-27 17:58:54 +08:00
|
|
|
end
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|