2020-10-14 07:38:57 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
describe Bookmark do
|
2021-09-09 08:50:26 +08:00
|
|
|
fab!(:post) { Fabricate(:post) }
|
|
|
|
|
|
|
|
context 'validations' do
|
|
|
|
it 'does not allow user to bookmark a post twice' do
|
|
|
|
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
|
|
|
|
user = bookmark.user
|
|
|
|
|
|
|
|
bookmark_2 = Fabricate.build(:bookmark,
|
|
|
|
post: post,
|
|
|
|
topic: post.topic,
|
|
|
|
user: user
|
|
|
|
)
|
|
|
|
|
|
|
|
expect(bookmark_2.valid?).to eq(false)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-14 07:38:57 +08:00
|
|
|
describe "#cleanup!" do
|
|
|
|
it "deletes bookmarks attached to a deleted post which has been deleted for > 3 days" do
|
|
|
|
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
|
|
|
|
bookmark2 = Fabricate(:bookmark, post: Fabricate(:post, topic: post.topic))
|
|
|
|
post.trash!
|
|
|
|
post.update(deleted_at: 4.days.ago)
|
|
|
|
Bookmark.cleanup!
|
|
|
|
expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
|
|
|
|
expect(Bookmark.find_by(id: bookmark2.id)).to eq(bookmark2)
|
|
|
|
end
|
|
|
|
|
2021-06-16 06:30:40 +08:00
|
|
|
it "runs a SyncTopicUserBookmarked job for all deleted bookmark unique topics to make sure topic_user.bookmarked is in sync" do
|
|
|
|
post2 = Fabricate(:post)
|
|
|
|
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
|
|
|
|
bookmark2 = Fabricate(:bookmark, post: Fabricate(:post, topic: post.topic))
|
|
|
|
bookmark3 = Fabricate(:bookmark, post: post2, topic: post2.topic)
|
|
|
|
bookmark4 = Fabricate(:bookmark, post: post2, topic: post2.topic)
|
|
|
|
post.trash!
|
|
|
|
post.update(deleted_at: 4.days.ago)
|
|
|
|
post2.trash!
|
|
|
|
post2.update(deleted_at: 4.days.ago)
|
|
|
|
Bookmark.cleanup!
|
|
|
|
expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
|
|
|
|
expect(Bookmark.find_by(id: bookmark2.id)).to eq(bookmark2)
|
|
|
|
expect(Bookmark.find_by(id: bookmark3.id)).to eq(nil)
|
|
|
|
expect(Bookmark.find_by(id: bookmark4.id)).to eq(nil)
|
|
|
|
expect_job_enqueued(job: :sync_topic_user_bookmarked, args: { topic_id: post.topic_id })
|
|
|
|
expect_job_enqueued(job: :sync_topic_user_bookmarked, args: { topic_id: post2.topic_id })
|
|
|
|
end
|
|
|
|
|
2020-10-14 07:38:57 +08:00
|
|
|
it "deletes bookmarks attached to a deleted topic which has been deleted for > 3 days" do
|
|
|
|
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
|
|
|
|
bookmark2 = Fabricate(:bookmark, topic: post.topic, post: Fabricate(:post, topic: post.topic))
|
|
|
|
bookmark3 = Fabricate(:bookmark)
|
|
|
|
post.topic.trash!
|
|
|
|
post.topic.update(deleted_at: 4.days.ago)
|
|
|
|
Bookmark.cleanup!
|
|
|
|
expect(Bookmark.find_by(id: bookmark.id)).to eq(nil)
|
|
|
|
expect(Bookmark.find_by(id: bookmark2.id)).to eq(nil)
|
|
|
|
expect(Bookmark.find_by(id: bookmark3.id)).to eq(bookmark3)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "does not delete bookmarks attached to posts that are not deleted or that have not met the 3 day grace period" do
|
|
|
|
bookmark = Fabricate(:bookmark, post: post, topic: post.topic)
|
|
|
|
bookmark2 = Fabricate(:bookmark)
|
|
|
|
Bookmark.cleanup!
|
|
|
|
expect(Bookmark.find(bookmark.id)).to eq(bookmark)
|
|
|
|
post.trash!
|
|
|
|
Bookmark.cleanup!
|
|
|
|
expect(Bookmark.find(bookmark.id)).to eq(bookmark)
|
|
|
|
expect(Bookmark.find_by(id: bookmark2.id)).to eq(bookmark2)
|
|
|
|
end
|
2021-04-09 11:06:35 +08:00
|
|
|
|
|
|
|
describe "bookmark limits" do
|
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
|
|
|
it "does not get the bookmark limit error because it is not creating a new bookmark (for users already over the limit)" do
|
|
|
|
Fabricate(:bookmark, user: user)
|
|
|
|
Fabricate(:bookmark, user: user)
|
|
|
|
last_bookmark = Fabricate(:bookmark, user: user)
|
|
|
|
SiteSetting.max_bookmarks_per_user = 2
|
|
|
|
expect { last_bookmark.clear_reminder! }.not_to raise_error
|
|
|
|
end
|
|
|
|
|
|
|
|
it "gets the bookmark limit error when creating a new bookmark over the limit" do
|
|
|
|
Fabricate(:bookmark, user: user)
|
|
|
|
Fabricate(:bookmark, user: user)
|
|
|
|
SiteSetting.max_bookmarks_per_user = 2
|
|
|
|
expect { Fabricate(:bookmark, user: user) }.to raise_error(ActiveRecord::RecordInvalid)
|
|
|
|
end
|
|
|
|
end
|
2020-10-14 07:38:57 +08:00
|
|
|
end
|
|
|
|
end
|