2019-04-29 20:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 05:41:23 -04:00
|
|
|
require 'rails_helper'
|
2013-02-05 14:16:51 -05:00
|
|
|
|
|
|
|
describe Draft do
|
2013-02-25 11:42:20 -05:00
|
|
|
before do
|
2013-02-05 14:16:51 -05:00
|
|
|
@user = Fabricate(:user)
|
|
|
|
end
|
2013-02-25 11:42:20 -05:00
|
|
|
it "can get a draft by user" do
|
2013-02-05 14:16:51 -05:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(@user, "test", 0)).to eq "data"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "uses the user id and key correctly" do
|
2017-07-27 21:20:09 -04:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(Fabricate.build(:coding_horror), "test", 0)).to eq nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should overwrite draft data correctly" do
|
2013-02-05 14:16:51 -05:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
|
|
|
Draft.set(@user, "test", 0, "new data")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(@user, "test", 0)).to eq "new data"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should clear drafts on request" do
|
2013-02-05 14:16:51 -05:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
|
|
|
Draft.clear(@user, "test", 0)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(@user, "test", 0)).to eq nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it "should disregard old draft if sequence decreases" do
|
2013-02-05 14:16:51 -05:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
|
|
|
Draft.set(@user, "test", 1, "hello")
|
|
|
|
Draft.set(@user, "test", 0, "foo")
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(@user, "test", 0)).to eq nil
|
|
|
|
expect(Draft.get(@user, "test", 1)).to eq "hello"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-06-03 04:52:41 -04:00
|
|
|
it 'can cleanup old drafts' do
|
2015-06-01 23:45:47 -04:00
|
|
|
user = Fabricate(:user)
|
|
|
|
key = Draft::NEW_TOPIC
|
2015-06-02 06:27:47 -04:00
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
Draft.set(user, key, 0, 'draft')
|
2015-06-02 06:27:47 -04:00
|
|
|
Draft.cleanup!
|
|
|
|
expect(Draft.count).to eq 1
|
|
|
|
|
2015-06-01 23:45:47 -04:00
|
|
|
seq = DraftSequence.next!(user, key)
|
|
|
|
|
2017-07-27 21:20:09 -04:00
|
|
|
Draft.set(user, key, seq, 'draft')
|
2015-06-01 23:45:47 -04:00
|
|
|
DraftSequence.update_all('sequence = sequence + 1')
|
|
|
|
|
|
|
|
Draft.cleanup!
|
|
|
|
|
|
|
|
expect(Draft.count).to eq 0
|
2017-07-27 21:20:09 -04:00
|
|
|
Draft.set(Fabricate(:user), Draft::NEW_TOPIC, seq + 1, 'draft')
|
2015-06-01 23:45:47 -04:00
|
|
|
|
|
|
|
Draft.cleanup!
|
|
|
|
|
|
|
|
expect(Draft.count).to eq 1
|
2015-06-03 04:52:41 -04:00
|
|
|
|
|
|
|
# should cleanup drafts more than 180 days old
|
2015-06-03 06:14:00 -04:00
|
|
|
SiteSetting.delete_drafts_older_than_n_days = 180
|
2015-06-03 04:52:41 -04:00
|
|
|
|
|
|
|
Draft.last.update_columns(updated_at: 200.days.ago)
|
|
|
|
Draft.cleanup!
|
|
|
|
expect(Draft.count).to eq 0
|
2015-06-01 23:45:47 -04: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 02:34:54 -04:00
|
|
|
describe '#stream' do
|
|
|
|
let(:public_post) { Fabricate(:post) }
|
|
|
|
let(:public_topic) { public_post.topic }
|
|
|
|
|
|
|
|
let(:stream) do
|
|
|
|
Draft.stream(user: @user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the correct number of drafts in the stream" do
|
2018-08-01 17:41:27 -04: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 02:34:54 -04:00
|
|
|
expect(stream.count).to eq(2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "should include the right topic id in a draft reply in the stream" do
|
2018-08-01 17:41:27 -04: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 02:34:54 -04: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
|
2018-08-01 17:41:27 -04: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 02:34:54 -04:00
|
|
|
draft_row = stream.first
|
|
|
|
expect(draft_row.draft_username).to eq(@user.username)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
context 'key expiry' do
|
|
|
|
it 'nukes new topic draft after a topic is created' do
|
2013-02-05 14:16:51 -05:00
|
|
|
u = Fabricate(:user)
|
2013-02-25 11:42:20 -05:00
|
|
|
Draft.set(u, Draft::NEW_TOPIC, 0, 'my draft')
|
2014-09-11 03:39:20 -04:00
|
|
|
_t = Fabricate(:topic, user: u)
|
2013-02-05 14:16:51 -05:00
|
|
|
s = DraftSequence.current(u, Draft::NEW_TOPIC)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(u, Draft::NEW_TOPIC, s)).to eq nil
|
2015-06-01 23:32:25 -04:00
|
|
|
expect(Draft.count).to eq 0
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'nukes new pm draft after a pm is created' do
|
2013-02-05 14:16:51 -05:00
|
|
|
u = Fabricate(:user)
|
2013-02-25 11:42:20 -05:00
|
|
|
Draft.set(u, Draft::NEW_PRIVATE_MESSAGE, 0, 'my draft')
|
2014-09-11 03:39:20 -04:00
|
|
|
t = Fabricate(:topic, user: u, archetype: Archetype.private_message, category_id: nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
s = DraftSequence.current(t.user, Draft::NEW_PRIVATE_MESSAGE)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(u, Draft::NEW_PRIVATE_MESSAGE, s)).to eq nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'does not nuke new topic draft after a pm is created' do
|
2013-02-05 14:16:51 -05:00
|
|
|
u = Fabricate(:user)
|
2013-02-25 11:42:20 -05:00
|
|
|
Draft.set(u, Draft::NEW_TOPIC, 0, 'my draft')
|
2014-09-11 03:39:20 -04:00
|
|
|
t = Fabricate(:topic, user: u, archetype: Archetype.private_message, category_id: nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
s = DraftSequence.current(t.user, Draft::NEW_TOPIC)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(u, Draft::NEW_TOPIC, s)).to eq 'my draft'
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'nukes the post draft when a post is created' do
|
2013-03-18 15:12:31 -04:00
|
|
|
user = Fabricate(:user)
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
p = PostCreator.new(user, raw: Fabricate.build(:post).raw, topic_id: topic.id).create
|
2017-07-27 21:20:09 -04:00
|
|
|
Draft.set(p.user, p.topic.draft_key, 0, 'hello')
|
2013-03-18 15:12:31 -04:00
|
|
|
|
|
|
|
PostCreator.new(user, raw: Fabricate.build(:post).raw).create
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(p.user, p.topic.draft_key, DraftSequence.current(p.user, p.topic.draft_key))).to eq nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'nukes the post draft when a post is revised' do
|
|
|
|
p = Fabricate(:post)
|
2017-07-27 21:20:09 -04:00
|
|
|
Draft.set(p.user, p.topic.draft_key, 0, 'hello')
|
|
|
|
p.revise(p.user, raw: 'another test')
|
2013-02-05 14:16:51 -05:00
|
|
|
s = DraftSequence.current(p.user, p.topic.draft_key)
|
2014-12-31 09:55:03 -05:00
|
|
|
expect(Draft.get(p.user, p.topic.draft_key, s)).to eq nil
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2015-08-03 00:29:04 -04:00
|
|
|
it 'increases revision each time you set' do
|
|
|
|
u = User.first
|
|
|
|
Draft.set(u, 'new_topic', 0, 'hello')
|
|
|
|
Draft.set(u, 'new_topic', 0, 'goodbye')
|
|
|
|
|
|
|
|
expect(Draft.find_draft(u, 'new_topic').revisions).to eq(2)
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|