2013-02-06 03:16:51 +08:00
|
|
|
require 'spec_helper'
|
|
|
|
|
|
|
|
describe Draft do
|
2013-02-26 00:42:20 +08:00
|
|
|
before do
|
2013-02-06 03:16:51 +08:00
|
|
|
@user = Fabricate(:user)
|
|
|
|
end
|
2013-02-26 00:42:20 +08:00
|
|
|
it "can get a draft by user" do
|
2013-02-06 03:16:51 +08:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
2014-12-31 22:55:03 +08:00
|
|
|
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
|
|
|
|
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
|
2013-02-06 03:16:51 +08:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
|
|
|
Draft.set(@user, "test", 0, "new data")
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(@user, "test", 0)).to eq "new data"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should clear drafts on request" do
|
2013-02-06 03:16:51 +08:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
|
|
|
Draft.clear(@user, "test", 0)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(@user, "test", 0)).to eq nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
it "should disregard old draft if sequence decreases" do
|
2013-02-06 03:16:51 +08:00
|
|
|
Draft.set(@user, "test", 0, "data")
|
|
|
|
Draft.set(@user, "test", 1, "hello")
|
|
|
|
Draft.set(@user, "test", 0, "foo")
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(@user, "test", 0)).to eq nil
|
|
|
|
expect(Draft.get(@user, "test", 1)).to eq "hello"
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2013-02-26 00:42:20 +08:00
|
|
|
context 'key expiry' do
|
|
|
|
it 'nukes new topic draft after a topic is created' do
|
2013-02-06 03:16:51 +08:00
|
|
|
u = Fabricate(:user)
|
2013-02-26 00:42:20 +08:00
|
|
|
Draft.set(u, Draft::NEW_TOPIC, 0, 'my draft')
|
2014-09-11 15:39:20 +08:00
|
|
|
_t = Fabricate(:topic, user: u)
|
2013-02-06 03:16:51 +08:00
|
|
|
s = DraftSequence.current(u, Draft::NEW_TOPIC)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(u, Draft::NEW_TOPIC, s)).to eq nil
|
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
|
2013-02-06 03:16:51 +08:00
|
|
|
u = Fabricate(:user)
|
2013-02-26 00:42:20 +08:00
|
|
|
Draft.set(u, Draft::NEW_PRIVATE_MESSAGE, 0, 'my draft')
|
2014-09-11 15:39:20 +08:00
|
|
|
t = Fabricate(:topic, user: u, archetype: Archetype.private_message, category_id: nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
s = DraftSequence.current(t.user, Draft::NEW_PRIVATE_MESSAGE)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(u, 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
|
2013-02-06 03:16:51 +08:00
|
|
|
u = Fabricate(:user)
|
2013-02-26 00:42:20 +08:00
|
|
|
Draft.set(u, Draft::NEW_TOPIC, 0, 'my draft')
|
2014-09-11 15:39:20 +08:00
|
|
|
t = Fabricate(:topic, user: u, archetype: Archetype.private_message, category_id: nil)
|
2013-02-06 03:16:51 +08:00
|
|
|
s = DraftSequence.current(t.user, Draft::NEW_TOPIC)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(u, 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
|
|
|
user = Fabricate(:user)
|
|
|
|
topic = Fabricate(:topic)
|
|
|
|
p = PostCreator.new(user, raw: Fabricate.build(:post).raw, topic_id: topic.id).create
|
2013-02-06 03:16:51 +08:00
|
|
|
Draft.set(p.user, p.topic.draft_key, 0,'hello')
|
2013-03-19 03:12:31 +08:00
|
|
|
|
|
|
|
PostCreator.new(user, raw: Fabricate.build(:post).raw).create
|
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
|
|
|
|
p = Fabricate(:post)
|
|
|
|
Draft.set(p.user, p.topic.draft_key, 0,'hello')
|
2014-10-28 05:06:43 +08:00
|
|
|
p.revise(p.user, { raw: 'another test' })
|
2013-02-06 03:16:51 +08:00
|
|
|
s = DraftSequence.current(p.user, p.topic.draft_key)
|
2014-12-31 22:55:03 +08:00
|
|
|
expect(Draft.get(p.user, p.topic.draft_key, s)).to eq nil
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
|
2014-12-31 22:55:03 +08:00
|
|
|
it 'increases the sequence number when a post is revised'
|
2013-02-06 03:16:51 +08:00
|
|
|
end
|
|
|
|
end
|