mirror of
https://github.com/discourse/discourse.git
synced 2024-11-26 18:13:39 +08:00
DEV: Re-organize TopicsController#reset_new tests (#15399)
This commit is contained in:
parent
1ad47030fe
commit
9e18868c31
|
@ -3254,18 +3254,33 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
|
||||
describe '#reset_new' do
|
||||
it 'needs you to be logged in' do
|
||||
context "when a user is not signed in" do
|
||||
it 'fails' do
|
||||
put "/topics/reset-new.json"
|
||||
expect(response.status).to eq(403)
|
||||
end
|
||||
end
|
||||
|
||||
it "updates the `new_since` date" do
|
||||
context "when a user is signed in" do
|
||||
before_all do
|
||||
@old_date = 2.years.ago
|
||||
user.user_stat.update_column(:new_since, @old_date)
|
||||
|
||||
CategoryUser.set_notification_level_for_category(
|
||||
user,
|
||||
NotificationLevels.all[:tracking],
|
||||
tracked_category.id,
|
||||
)
|
||||
end
|
||||
|
||||
let!(:old_date) { @old_date }
|
||||
|
||||
before do
|
||||
sign_in(user)
|
||||
end
|
||||
|
||||
old_date = 2.years.ago
|
||||
user.user_stat.update_column(:new_since, old_date)
|
||||
user.update_column(:created_at, old_date)
|
||||
|
||||
context "when tracked is unset" do
|
||||
it "updates the `new_since` date" do
|
||||
TopicTrackingState.expects(:publish_dismiss_new)
|
||||
|
||||
put "/topics/reset-new.json"
|
||||
|
@ -3273,14 +3288,10 @@ RSpec.describe TopicsController do
|
|||
user.reload
|
||||
expect(user.user_stat.new_since.to_date).not_to eq(old_date.to_date)
|
||||
end
|
||||
end
|
||||
|
||||
describe "when tracked param is true" do
|
||||
it "does not update user_stat.new_since" do
|
||||
sign_in(user)
|
||||
|
||||
old_date = 2.years.ago
|
||||
user.user_stat.update_column(:new_since, old_date)
|
||||
|
||||
put "/topics/reset-new.json?tracked=true"
|
||||
expect(response.status).to eq(200)
|
||||
user.reload
|
||||
|
@ -3288,12 +3299,6 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
|
||||
it "creates dismissed topic user records for each new topic" do
|
||||
sign_in(user)
|
||||
user.user_stat.update_column(:new_since, 2.years.ago)
|
||||
|
||||
CategoryUser.set_notification_level_for_category(user,
|
||||
NotificationLevels.all[:tracking],
|
||||
tracked_category.id)
|
||||
tracked_topic = create_post(category: tracked_category).topic
|
||||
|
||||
create_post # This is a new post, but is not tracked so a record will not be created for it
|
||||
|
@ -3305,11 +3310,6 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
|
||||
it "creates dismissed topic user records if there are > 30 (default pagination) topics" do
|
||||
sign_in(user)
|
||||
CategoryUser.set_notification_level_for_category(user,
|
||||
NotificationLevels.all[:tracking],
|
||||
tracked_category.id)
|
||||
|
||||
topic_ids = []
|
||||
5.times do
|
||||
topic_ids << create_post(category: tracked_category).topic.id
|
||||
|
@ -3325,11 +3325,6 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
|
||||
it "creates dismissed topic user records if there are > 30 (default pagination) topics and topic_ids are provided" do
|
||||
sign_in(user)
|
||||
CategoryUser.set_notification_level_for_category(user,
|
||||
NotificationLevels.all[:tracking],
|
||||
tracked_category.id)
|
||||
|
||||
topic_ids = []
|
||||
5.times do
|
||||
topic_ids << create_post(category: tracked_category).topic.id
|
||||
|
@ -3344,14 +3339,10 @@ RSpec.describe TopicsController do
|
|||
DismissedTopicUser.where(user_id: user.id, topic_id: topic_ids).count
|
||||
}.by(4)
|
||||
end
|
||||
end
|
||||
|
||||
context "when tracked=false" do
|
||||
it "updates the user_stat new_since column and dismisses all the new topics" do
|
||||
sign_in(user)
|
||||
CategoryUser.set_notification_level_for_category(user,
|
||||
NotificationLevels.all[:tracking],
|
||||
tracked_category.id)
|
||||
|
||||
topic_ids = []
|
||||
5.times do
|
||||
topic_ids << create_post(category: tracked_category).topic.id
|
||||
|
@ -3366,10 +3357,6 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
|
||||
it "does not pass topic ids that are not new for the user to the bulk action, limit the scope to new topics" do
|
||||
sign_in(user)
|
||||
CategoryUser.set_notification_level_for_category(user,
|
||||
NotificationLevels.all[:tracking],
|
||||
tracked_category.id)
|
||||
|
||||
topic_ids = []
|
||||
5.times do
|
||||
|
@ -3389,7 +3376,6 @@ RSpec.describe TopicsController do
|
|||
}.by(5)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
context 'category' do
|
||||
fab!(:subcategory) { Fabricate(:category, parent_category_id: category.id) }
|
||||
|
@ -3397,8 +3383,6 @@ RSpec.describe TopicsController do
|
|||
fab!(:subcategory_topic) { Fabricate(:topic, category: subcategory) }
|
||||
|
||||
it 'dismisses topics for main category' do
|
||||
sign_in(user)
|
||||
|
||||
TopicTrackingState.expects(:publish_dismiss_new).with(user.id, topic_ids: [category_topic.id])
|
||||
|
||||
put "/topics/reset-new.json?category_id=#{category.id}"
|
||||
|
@ -3407,8 +3391,6 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
|
||||
it 'dismisses topics for main category and subcategories' do
|
||||
sign_in(user)
|
||||
|
||||
TopicTrackingState.expects(:publish_dismiss_new).with(user.id, topic_ids: [category_topic.id, subcategory_topic.id])
|
||||
|
||||
put "/topics/reset-new.json?category_id=#{category.id}&include_subcategories=true"
|
||||
|
@ -3422,7 +3404,6 @@ RSpec.describe TopicsController do
|
|||
fab!(:topic_tag) { Fabricate(:topic_tag, topic: tag_topic, tag: tag) }
|
||||
|
||||
it 'dismisses topics for tag' do
|
||||
sign_in(user)
|
||||
TopicTrackingState.expects(:publish_dismiss_new).with(user.id, topic_ids: [tag_topic.id])
|
||||
put "/topics/reset-new.json?tag_id=#{tag.name}"
|
||||
expect(DismissedTopicUser.where(user_id: user.id).pluck(:topic_id)).to eq([tag_topic.id])
|
||||
|
@ -3431,12 +3412,11 @@ RSpec.describe TopicsController do
|
|||
|
||||
context 'tag and category' do
|
||||
fab!(:tag_topic) { Fabricate(:topic) }
|
||||
fab!(:tag_and_category_topic) { Fabricate(:topic, category: category) }
|
||||
fab!(:topic_tag) { Fabricate(:topic_tag, topic: tag_topic, tag: tag) }
|
||||
fab!(:tag_and_category_topic) { Fabricate(:topic, category: category) }
|
||||
fab!(:topic_tag2) { Fabricate(:topic_tag, topic: tag_and_category_topic, tag: tag) }
|
||||
|
||||
it 'dismisses topics for tag' do
|
||||
sign_in(user)
|
||||
TopicTrackingState.expects(:publish_dismiss_new).with(user.id, topic_ids: [tag_and_category_topic.id])
|
||||
put "/topics/reset-new.json?tag_id=#{tag.name}&category_id=#{category.id}"
|
||||
expect(DismissedTopicUser.where(user_id: user.id).pluck(:topic_id)).to eq([tag_and_category_topic.id])
|
||||
|
@ -3448,12 +3428,6 @@ RSpec.describe TopicsController do
|
|||
fab!(:topic3) { Fabricate(:topic) }
|
||||
|
||||
it "updates the `new_since` date" do
|
||||
sign_in(user)
|
||||
|
||||
old_date = 2.years.ago
|
||||
user.user_stat.update_column(:new_since, old_date)
|
||||
user.update_column(:created_at, old_date)
|
||||
|
||||
TopicTrackingState.expects(:publish_dismiss_new).with(user.id, topic_ids: [topic2.id, topic3.id]).at_least_once
|
||||
|
||||
put "/topics/reset-new.json", **{ params: { topic_ids: [topic2.id, topic3.id] } }
|
||||
|
@ -3464,7 +3438,6 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
|
||||
it "raises an error if topic_ids is provided and it is not an array" do
|
||||
sign_in(user)
|
||||
put "/topics/reset-new.json", params: { topic_ids: topic2.id }
|
||||
expect(response.parsed_body["errors"].first).to match(/Expecting topic_ids to contain a list/)
|
||||
put "/topics/reset-new.json", params: { topic_ids: [topic2.id] }
|
||||
|
@ -3473,11 +3446,6 @@ RSpec.describe TopicsController do
|
|||
|
||||
describe "when tracked param is true" do
|
||||
it "does not update user_stat.new_since and does not dismiss untracked topics" do
|
||||
sign_in(user)
|
||||
|
||||
old_date = 2.years.ago
|
||||
user.user_stat.update_column(:new_since, old_date)
|
||||
|
||||
put "/topics/reset-new.json?tracked=true", **{ params: { topic_ids: [topic2.id, topic3.id] } }
|
||||
expect(response.status).to eq(200)
|
||||
user.reload
|
||||
|
@ -3486,12 +3454,6 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
|
||||
it "creates topic user records for each unread topic" do
|
||||
sign_in(user)
|
||||
user.user_stat.update_column(:new_since, 2.years.ago)
|
||||
|
||||
CategoryUser.set_notification_level_for_category(user,
|
||||
NotificationLevels.all[:tracking],
|
||||
tracked_category.id)
|
||||
tracked_topic = create_post.topic
|
||||
tracked_topic.update!(category_id: tracked_category.id)
|
||||
topic2.update!(category_id: tracked_category.id)
|
||||
|
@ -3505,6 +3467,7 @@ RSpec.describe TopicsController do
|
|||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe '#feature_stats' do
|
||||
it "works" do
|
||||
|
|
Loading…
Reference in New Issue
Block a user