2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 17:41:23 +08:00
|
|
|
require 'rails_helper'
|
2013-02-06 03:16:51 +08:00
|
|
|
|
|
|
|
describe Draft do
|
2019-10-17 13:56:40 +08:00
|
|
|
|
|
|
|
fab!(:user) do
|
|
|
|
Fabricate(:user)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
2019-10-17 13:56:40 +08:00
|
|
|
|
2020-03-23 19:02:24 +08:00
|
|
|
fab!(:post) do
|
|
|
|
Fabricate(:post)
|
|
|
|
end
|
|
|
|
|
2020-05-26 08:07:00 +08:00
|
|
|
context 'system user' do
|
|
|
|
it "can not set drafts" do
|
|
|
|
# fake a sequence
|
|
|
|
DraftSequence.create!(user_id: Discourse.system_user.id, draft_key: "abc", sequence: 10)
|
|
|
|
|
|
|
|
seq = Draft.set(Discourse.system_user, "abc", 0, { reply: 'hi' }.to_json)
|
|
|
|
expect(seq).to eq(0)
|
|
|
|
|
|
|
|
draft = Draft.get(Discourse.system_user, "abc", 0)
|
|
|
|
expect(draft).to eq(nil)
|
|
|
|
|
|
|
|
draft = Draft.get(Discourse.system_user, "abc", 1)
|
|
|
|
expect(draft).to eq(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-10-17 13:56:40 +08:00
|
|
|
context 'backup_drafts_to_pm_length' do
|
|
|
|
it "correctly backs up drafts to a personal message" do
|
|
|
|
SiteSetting.backup_drafts_to_pm_length = 1
|
|
|
|
|
|
|
|
draft = {
|
|
|
|
reply: "this is a reply",
|
|
|
|
random_key: "random"
|
|
|
|
}
|
|
|
|
|
2020-05-12 14:55:24 +08:00
|
|
|
seq = Draft.set(user, "xyz", 0, draft.to_json)
|
2019-10-17 13:56:40 +08:00
|
|
|
draft["reply"] = "test" * 100
|
2019-10-17 14:41:28 +08:00
|
|
|
|
|
|
|
half_grace = (SiteSetting.editing_grace_period / 2 + 1).seconds
|
|
|
|
|
|
|
|
freeze_time half_grace.from_now
|
2020-05-12 14:55:24 +08:00
|
|
|
seq = Draft.set(user, "xyz", seq, draft.to_json)
|
2019-10-17 13:56:40 +08:00
|
|
|
|
2019-10-31 14:15:41 +08:00
|
|
|
draft_post = BackupDraftPost.find_by(user_id: user.id, key: "xyz").post
|
2019-10-17 13:56:40 +08:00
|
|
|
|
|
|
|
expect(draft_post.revisions.count).to eq(0)
|
|
|
|
|
2019-10-17 14:41:28 +08:00
|
|
|
freeze_time half_grace.from_now
|
2019-10-17 13:56:40 +08:00
|
|
|
|
|
|
|
# this should trigger a post revision as 10 minutes have passed
|
|
|
|
draft["reply"] = "hello"
|
2020-05-12 14:55:24 +08:00
|
|
|
Draft.set(user, "xyz", seq, draft.to_json)
|
2019-10-17 13:56:40 +08:00
|
|
|
|
|
|
|
draft_topic = BackupDraftTopic.find_by(user_id: user.id)
|
|
|
|
expect(draft_topic.topic.posts_count).to eq(2)
|
|
|
|
|
|
|
|
draft_post.reload
|
|
|
|
expect(draft_post.revisions.count).to eq(1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "can get a draft by user" do
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.set(user, "test", 0, "data")
|
|
|
|
expect(Draft.get(user, "test", 0)).to eq "data"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the user id and key correctly" do
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.set(user, "test", 0, "data")
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(Fabricate.build(:coding_horror), "test", 0)).to eq nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should overwrite draft data correctly" do
|
2020-05-12 14:55:24 +08:00
|
|
|
seq = Draft.set(user, "test", 0, "data")
|
|
|
|
seq = Draft.set(user, "test", seq, "new data")
|
|
|
|
expect(Draft.get(user, "test", seq)).to eq "new data"
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should increase the sequence on every save" do
|
|
|
|
seq = Draft.set(user, "test", 0, "data")
|
|
|
|
expect(seq).to eq(0)
|
|
|
|
seq = Draft.set(user, "test", 0, "data")
|
|
|
|
expect(seq).to eq(1)
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should clear drafts on request" do
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.set(user, "test", 0, "data")
|
|
|
|
Draft.clear(user, "test", 0)
|
|
|
|
expect(Draft.get(user, "test", 0)).to eq nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2019-10-31 14:15:41 +08:00
|
|
|
it "should cross check with DraftSequence table" do
|
|
|
|
|
|
|
|
Draft.set(user, "test", 0, "old")
|
|
|
|
expect(Draft.get(user, "test", 0)).to eq "old"
|
|
|
|
|
|
|
|
DraftSequence.next!(user, "test")
|
|
|
|
seq = DraftSequence.next!(user, "test")
|
|
|
|
expect(seq).to eq(2)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Draft.set(user, "test", seq - 1, "error")
|
|
|
|
end.to raise_error(Draft::OutOfSequence)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Draft.set(user, "test", seq + 1, "error")
|
|
|
|
end.to raise_error(Draft::OutOfSequence)
|
|
|
|
|
|
|
|
Draft.set(user, "test", seq, "data")
|
|
|
|
expect(Draft.get(user, "test", seq)).to eq "data"
|
|
|
|
|
|
|
|
expect do
|
|
|
|
expect(Draft.get(user, "test", seq - 1)).to eq "data"
|
|
|
|
end.to raise_error(Draft::OutOfSequence)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
expect(Draft.get(user, "test", seq + 1)).to eq "data"
|
|
|
|
end.to raise_error(Draft::OutOfSequence)
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should disregard old draft if sequence decreases" do
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.set(user, "test", 0, "data")
|
2019-10-31 14:15:41 +08:00
|
|
|
DraftSequence.next!(user, "test")
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.set(user, "test", 1, "hello")
|
2019-10-31 14:15:41 +08:00
|
|
|
|
|
|
|
expect do
|
|
|
|
Draft.set(user, "test", 0, "foo")
|
|
|
|
end.to raise_error(Draft::OutOfSequence)
|
|
|
|
|
|
|
|
expect do
|
|
|
|
Draft.get(user, "test", 0)
|
|
|
|
end.to raise_error(Draft::OutOfSequence)
|
|
|
|
|
2019-10-17 13:56:40 +08:00
|
|
|
expect(Draft.get(user, "test", 1)).to eq "hello"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2020-09-30 00:59:03 +08:00
|
|
|
it "should disregard draft sequence if force_save is true" do
|
|
|
|
Draft.set(user, "test", 0, "data")
|
|
|
|
DraftSequence.next!(user, "test")
|
|
|
|
Draft.set(user, "test", 1, "hello")
|
|
|
|
|
|
|
|
seq = Draft.set(user, "test", 0, "foo", nil, force_save: true)
|
|
|
|
expect(seq).to eq(2)
|
|
|
|
end
|
|
|
|
|
2015-06-03 16:52:41 +08:00
|
|
|
it 'can cleanup old drafts' do
|
2015-06-02 11:45:47 +08:00
|
|
|
key = Draft::NEW_TOPIC
|
2015-06-02 18:27:47 +08:00
|
|
|
|
|
|
|
Draft.set(user, key, 0, 'draft')
|
|
|
|
Draft.cleanup!
|
|
|
|
expect(Draft.count).to eq 1
|
2021-07-29 22:06:11 +08:00
|
|
|
expect(user.user_stat.draft_count).to eq(1)
|
2015-06-02 18:27:47 +08:00
|
|
|
|
2015-06-02 11:45:47 +08:00
|
|
|
seq = DraftSequence.next!(user, key)
|
|
|
|
|
|
|
|
Draft.set(user, key, seq, 'draft')
|
|
|
|
DraftSequence.update_all('sequence = sequence + 1')
|
|
|
|
|
|
|
|
Draft.cleanup!
|
|
|
|
|
|
|
|
expect(Draft.count).to eq 0
|
2021-07-29 22:06:11 +08:00
|
|
|
expect(user.reload.user_stat.draft_count).to eq(0)
|
2019-10-31 14:15:41 +08:00
|
|
|
|
|
|
|
Draft.set(Fabricate(:user), Draft::NEW_TOPIC, 0, 'draft')
|
2015-06-02 11:45:47 +08:00
|
|
|
|
|
|
|
Draft.cleanup!
|
|
|
|
|
|
|
|
expect(Draft.count).to eq 1
|
2015-06-03 16:52:41 +08:00
|
|
|
|
|
|
|
# should cleanup drafts more than 180 days old
|
2015-06-03 18:14:00 +08:00
|
|
|
SiteSetting.delete_drafts_older_than_n_days = 180
|
2015-06-03 16:52:41 +08:00
|
|
|
|
|
|
|
Draft.last.update_columns(updated_at: 200.days.ago)
|
|
|
|
Draft.cleanup!
|
|
|
|
expect(Draft.count).to eq 0
|
2015-06-02 11:45:47 +08:00
|
|
|
end
|
|
|
|
|
2021-07-27 19:05:33 +08:00
|
|
|
it 'updates draft count when a draft is created or destroyed' do
|
2021-12-08 21:23:44 +08:00
|
|
|
Draft.set(Fabricate(:user), Draft::NEW_TOPIC, 0, "data")
|
|
|
|
|
2021-07-27 19:05:33 +08:00
|
|
|
messages = MessageBus.track_publish("/user") do
|
2021-12-08 20:40:35 +08:00
|
|
|
Draft.set(user, Draft::NEW_TOPIC, 0, "data")
|
2021-07-27 19:05:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.first.data[:draft_count]).to eq(1)
|
2021-12-08 20:40:35 +08:00
|
|
|
expect(messages.first.data[:has_topic_draft]).to eq(true)
|
2021-07-27 19:05:33 +08:00
|
|
|
|
|
|
|
messages = MessageBus.track_publish("/user") do
|
|
|
|
Draft.where(user: user).destroy_all
|
|
|
|
end
|
|
|
|
|
|
|
|
expect(messages.first.data[:draft_count]).to eq(0)
|
2021-12-08 20:40:35 +08:00
|
|
|
expect(messages.first.data[:has_topic_draft]).to eq(false)
|
2021-07-27 19:05:33 +08:00
|
|
|
end
|
|
|
|
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 14:34:54 +08:00
|
|
|
describe '#stream' do
|
2019-05-07 11:12:20 +08:00
|
|
|
fab!(:public_post) { Fabricate(:post) }
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 14:34:54 +08:00
|
|
|
let(:public_topic) { public_post.topic }
|
|
|
|
|
|
|
|
let(:stream) do
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.stream(user: user)
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 14:34:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the correct number of drafts in the stream" do
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.set(user, "test", 0, '{"reply":"hey.","action":"createTopic","title":"Hey"}')
|
|
|
|
Draft.set(user, "test2", 0, '{"reply":"howdy"}')
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 14:34:54 +08:00
|
|
|
expect(stream.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the right topic id in a draft reply in the stream" do
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.set(user, "topic_#{public_topic.id}", 0, '{"reply":"hi"}')
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 14:34:54 +08:00
|
|
|
draft_row = stream.first
|
|
|
|
expect(draft_row.topic_id).to eq(public_topic.id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the right draft username in the stream" do
|
2019-10-17 13:56:40 +08:00
|
|
|
Draft.set(user, "topic_#{public_topic.id}", 0, '{"reply":"hey"}')
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 14:34:54 +08:00
|
|
|
draft_row = stream.first
|
2020-03-23 19:02:24 +08:00
|
|
|
expect(draft_row.user.username).to eq(user.username)
|
FEATURE: Drafts view in user profile
* add drafts.json endpoint, user profile tab with drafts stream
* improve drafts stream display in user profile
* truncate excerpts in drafts list, better handling for resume draft action
* improve draft stream SQL query, add rspec tests
* if composer is open, quietly close it when user opens another draft from drafts stream; load PM draft only when user is in /u/username/messages (instead of /u/username)
* cleanup
* linting fixes
* apply prettier styling to modified files
* add client tests for drafts, includes a fixture for drafts.json
* improvements to code following review
* refresh drafts route when user deletes a draft open in the composer while being in the drafts route; minor prettier scss fix
* added more spec tests, deleted an acceptance test for removing drafts that was too finicky, formatting and code style fixes, added appEvent for draft:destroyed
* prettier, eslint fixes
* use "username_lower" from users table, added error handling for rejected promises
* adds guardian spec for can_see_drafts, adds improvements following code review
* move DraftsController spec to its own file
* fix failing drafts qunit test, use getOwner instead of deprecated this.container
* limit test fixture for draft.json testing to new_topic request only
2018-08-01 14:34:54 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'key expiry' do
|
|
|
|
it 'nukes new topic draft after a topic is created' do
|
2020-03-23 19:02:24 +08:00
|
|
|
Draft.set(user, Draft::NEW_TOPIC, 0, 'my draft')
|
2022-02-09 16:37:38 +08:00
|
|
|
_t = Fabricate(:topic, user: user, advance_draft: true)
|
2020-03-23 19:02:24 +08:00
|
|
|
s = DraftSequence.current(user, Draft::NEW_TOPIC)
|
|
|
|
expect(Draft.get(user, Draft::NEW_TOPIC, s)).to eq nil
|
2015-06-02 11:32:25 +08:00
|
|
|
expect(Draft.count).to eq 0
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'nukes new pm draft after a pm is created' do
|
2020-03-23 19:02:24 +08:00
|
|
|
Draft.set(user, Draft::NEW_PRIVATE_MESSAGE, 0, 'my draft')
|
2022-02-09 16:37:38 +08:00
|
|
|
t = Fabricate(:topic, user: user, archetype: Archetype.private_message, category_id: nil, advance_draft: true)
|
2013-02-06 03:16:51 +08:00
|
|
|
s = DraftSequence.current(t.user, Draft::NEW_PRIVATE_MESSAGE)
|
2020-03-23 19:02:24 +08:00
|
|
|
expect(Draft.get(user, Draft::NEW_PRIVATE_MESSAGE, s)).to eq nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it 'does not nuke new topic draft after a pm is created' do
|
2020-03-23 19:02:24 +08:00
|
|
|
Draft.set(user, Draft::NEW_TOPIC, 0, 'my draft')
|
|
|
|
t = Fabricate(:topic, user: user, archetype: Archetype.private_message, category_id: nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
s = DraftSequence.current(t.user, Draft::NEW_TOPIC)
|
2020-03-23 19:02:24 +08:00
|
|
|
expect(Draft.get(user, Draft::NEW_TOPIC, s)).to eq 'my draft'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'nukes the post draft when a post is created' do
|
2013-03-19 03:12:31 +08:00
|
|
|
topic = Fabricate(:topic)
|
2019-10-31 14:15:41 +08:00
|
|
|
|
|
|
|
Draft.set(user, topic.draft_key, 0, 'hello')
|
|
|
|
|
2022-02-09 16:37:38 +08:00
|
|
|
p = PostCreator.new(user, raw: Fabricate.build(:post).raw, topic_id: topic.id, advance_draft: true).create
|
2013-03-19 03:12:31 +08:00
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(p.user, p.topic.draft_key, DraftSequence.current(p.user, p.topic.draft_key))).to eq nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'nukes the post draft when a post is revised' do
|
2020-03-23 19:02:24 +08:00
|
|
|
Draft.set(post.user, post.topic.draft_key, 0, 'hello')
|
|
|
|
post.revise(post.user, raw: 'another test')
|
|
|
|
s = DraftSequence.current(post.user, post.topic.draft_key)
|
|
|
|
expect(Draft.get(post.user, post.topic.draft_key, s)).to eq nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2015-08-03 12:29:04 +08:00
|
|
|
it 'increases revision each time you set' do
|
2020-03-23 19:02:24 +08:00
|
|
|
Draft.set(user, 'new_topic', 0, 'hello')
|
|
|
|
Draft.set(user, 'new_topic', 0, 'goodbye')
|
2015-08-03 12:29:04 +08:00
|
|
|
|
2020-03-23 19:02:24 +08:00
|
|
|
expect(Draft.find_by(user_id: user.id, draft_key: 'new_topic').revisions).to eq(2)
|
2019-10-31 14:15:41 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles owner switching gracefully' do
|
|
|
|
draft_seq = Draft.set(user, 'new_topic', 0, 'hello', _owner = 'ABCDEF')
|
|
|
|
expect(draft_seq).to eq(0)
|
|
|
|
|
|
|
|
draft_seq = Draft.set(user, 'new_topic', 0, 'hello world', _owner = 'HIJKL')
|
|
|
|
expect(draft_seq).to eq(1)
|
|
|
|
|
|
|
|
draft_seq = Draft.set(user, 'new_topic', 1, 'hello world', _owner = 'HIJKL')
|
2020-05-12 14:55:24 +08:00
|
|
|
expect(draft_seq).to eq(2)
|
2015-08-03 12:29:04 +08:00
|
|
|
end
|
2020-03-23 19:02:24 +08:00
|
|
|
|
|
|
|
it 'can correctly preload drafts' do
|
|
|
|
Draft.set(user, "#{Draft::EXISTING_TOPIC}#{post.topic_id}", 0, { raw: 'hello', postId: post.id }.to_json)
|
|
|
|
|
|
|
|
drafts = Draft.where(user_id: user.id).to_a
|
|
|
|
|
|
|
|
Draft.preload_data(drafts, user)
|
|
|
|
|
|
|
|
expect(drafts[0].topic_preloaded?).to eq(true)
|
|
|
|
expect(drafts[0].topic.id).to eq(post.topic_id)
|
|
|
|
|
|
|
|
expect(drafts[0].post_preloaded?).to eq(true)
|
|
|
|
expect(drafts[0].post.id).to eq(post.id)
|
|
|
|
end
|
|
|
|
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|