2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-10-11 04:41:23 -05:00
|
|
|
require 'rails_helper'
|
2013-02-05 13:16:51 -06:00
|
|
|
|
|
|
|
describe DraftController do
|
|
|
|
it 'requires you to be logged in' do
|
2018-06-06 23:24:20 -05:00
|
|
|
post "/draft"
|
2018-01-11 21:15:10 -06:00
|
|
|
expect(response.status).to eq(403)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves a draft on update' do
|
2018-06-06 23:24:20 -05:00
|
|
|
user = sign_in(Fabricate(:user))
|
2018-11-14 10:47:59 -06:00
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: 'xyz',
|
|
|
|
data: { my: "data" }.to_json,
|
|
|
|
sequence: 0
|
|
|
|
}
|
|
|
|
|
2018-06-06 23:24:20 -05:00
|
|
|
expect(response.status).to eq(200)
|
2018-11-14 10:47:59 -06:00
|
|
|
expect(Draft.get(user, 'xyz', 0)).to eq(%q({"my":"data"}))
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
|
2018-11-14 05:56:25 -06:00
|
|
|
it 'checks for an conflict on update' do
|
|
|
|
user = sign_in(Fabricate(:user))
|
|
|
|
post = Fabricate(:post, user: user)
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
username: user.username,
|
|
|
|
draft_key: "topic",
|
|
|
|
sequence: 0,
|
2018-11-14 10:47:59 -06:00
|
|
|
data: {
|
|
|
|
postId: post.id,
|
2018-11-14 20:14:07 -06:00
|
|
|
originalText: post.raw,
|
|
|
|
action: "edit"
|
2018-11-14 10:47:59 -06:00
|
|
|
}.to_json
|
2018-11-14 05:56:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
expect(JSON.parse(response.body)['conflict_user']).to eq(nil)
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
username: user.username,
|
|
|
|
draft_key: "topic",
|
|
|
|
sequence: 0,
|
2018-11-14 10:47:59 -06:00
|
|
|
data: {
|
|
|
|
postId: post.id,
|
2018-11-14 20:14:07 -06:00
|
|
|
originalText: "something else",
|
|
|
|
action: "edit"
|
2018-11-14 10:47:59 -06:00
|
|
|
}.to_json
|
2018-11-14 05:56:25 -06:00
|
|
|
}
|
|
|
|
|
2018-11-14 10:47:59 -06:00
|
|
|
json = JSON.parse(response.body)
|
|
|
|
|
|
|
|
expect(json['conflict_user']['id']).to eq(post.last_editor.id)
|
|
|
|
expect(json['conflict_user']).to include('avatar_template')
|
2018-11-14 05:56:25 -06:00
|
|
|
end
|
|
|
|
|
2013-02-25 10:42:20 -06:00
|
|
|
it 'destroys drafts when required' do
|
2018-06-06 23:24:20 -05:00
|
|
|
user = sign_in(Fabricate(:user))
|
2013-02-05 13:16:51 -06:00
|
|
|
Draft.set(user, 'xxx', 0, 'hi')
|
2018-06-06 23:24:20 -05:00
|
|
|
delete "/draft.json", params: { draft_key: 'xxx', sequence: 0 }
|
|
|
|
expect(response.status).to eq(200)
|
2015-01-09 11:04:02 -06:00
|
|
|
expect(Draft.get(user, 'xxx', 0)).to eq(nil)
|
2013-02-05 13:16:51 -06:00
|
|
|
end
|
|
|
|
end
|