discourse/spec/models/bookmark_spec.rb
Martin Brennan 1ba5ccd8af
FIX: When user has already hit bookmark limit, do not error for clear_reminder! or other updates (#12658)
We introduced a cap on the number of bookmarks the user can add in be145ccf2f. However this has caused unintended side effects; when the `jobs/scheduled/bookmark_reminder_notifications.rb` runs we get this error for users who already had more bookmarks than the limit:

> Job exception: Validation failed: Sorry, you have too many bookmarks, visit #{url}/my/activity/bookmarks to remove some.

This is because the `clear_reminder!` call was triggering a bookmark validation, which raised an error because the user already had to many, holding up other reminders.

This PR also adds `max_bookmarks_per_user` hidden site setting (default 2000). This replaces the BOOKMARK_LIMIT const so we can raise it for certain sites.
2021-04-09 13:06:35 +10:00

63 lines
2.5 KiB
Ruby

# frozen_string_literal: true
require 'rails_helper'
describe Bookmark do
describe "#cleanup!" do
it "deletes bookmarks attached to a deleted post which has been deleted for > 3 days" do
post = Fabricate(:post)
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
it "deletes bookmarks attached to a deleted topic which has been deleted for > 3 days" do
post = Fabricate(:post)
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
post = Fabricate(:post)
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
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
end
end