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 DraftController do
|
|
|
|
it 'requires you to be logged in' do
|
2018-06-07 00:24:20 -04:00
|
|
|
post "/draft"
|
2018-01-11 22:15:10 -05:00
|
|
|
expect(response.status).to eq(403)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'saves a draft on update' do
|
2018-06-07 00:24:20 -04:00
|
|
|
user = sign_in(Fabricate(:user))
|
2018-11-14 11:47:59 -05:00
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: 'xyz',
|
|
|
|
data: { my: "data" }.to_json,
|
|
|
|
sequence: 0
|
|
|
|
}
|
|
|
|
|
2018-06-07 00:24:20 -04:00
|
|
|
expect(response.status).to eq(200)
|
2018-11-14 11:47:59 -05:00
|
|
|
expect(Draft.get(user, 'xyz', 0)).to eq(%q({"my":"data"}))
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
|
2020-02-14 10:48:56 -05:00
|
|
|
it "returns 404 when the key is missing" do
|
2020-05-12 02:55:24 -04:00
|
|
|
_user = sign_in(Fabricate(:user))
|
2020-02-14 10:48:56 -05:00
|
|
|
post "/draft.json", params: { data: { my: "data" }.to_json, sequence: 0 }
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
2020-02-14 11:06:12 -05:00
|
|
|
it "returns a draft if requested" do
|
|
|
|
user = sign_in(Fabricate(:user))
|
|
|
|
Draft.set(user, 'hello', 0, 'test')
|
|
|
|
|
|
|
|
get "/draft.json", params: { draft_key: 'hello' }
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2020-02-14 11:06:12 -05:00
|
|
|
expect(json['draft']).to eq('test')
|
|
|
|
|
|
|
|
get "/draft.json"
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
2018-11-14 06:56:25 -05:00
|
|
|
it 'checks for an conflict on update' do
|
|
|
|
user = sign_in(Fabricate(:user))
|
|
|
|
post = Fabricate(:post, user: user)
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "topic",
|
|
|
|
sequence: 0,
|
2018-11-14 11:47:59 -05:00
|
|
|
data: {
|
|
|
|
postId: post.id,
|
2018-11-14 21:14:07 -05:00
|
|
|
originalText: post.raw,
|
|
|
|
action: "edit"
|
2018-11-14 11:47:59 -05:00
|
|
|
}.to_json
|
2018-11-14 06:56:25 -05:00
|
|
|
}
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
expect(response.parsed_body['conflict_user']).to eq(nil)
|
2018-11-14 06:56:25 -05:00
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "topic",
|
|
|
|
sequence: 0,
|
2018-11-14 11:47:59 -05:00
|
|
|
data: {
|
|
|
|
postId: post.id,
|
2018-11-14 21:14:07 -05:00
|
|
|
originalText: "something else",
|
|
|
|
action: "edit"
|
2018-11-14 11:47:59 -05:00
|
|
|
}.to_json
|
2018-11-14 06:56:25 -05:00
|
|
|
}
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2018-11-14 11:47:59 -05:00
|
|
|
|
|
|
|
expect(json['conflict_user']['id']).to eq(post.last_editor.id)
|
|
|
|
expect(json['conflict_user']).to include('avatar_template')
|
2018-11-14 06:56:25 -05:00
|
|
|
end
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
it 'destroys drafts when required' do
|
2018-06-07 00:24:20 -04:00
|
|
|
user = sign_in(Fabricate(:user))
|
2013-02-05 14:16:51 -05:00
|
|
|
Draft.set(user, 'xxx', 0, 'hi')
|
2018-06-07 00:24:20 -04:00
|
|
|
delete "/draft.json", params: { draft_key: 'xxx', sequence: 0 }
|
|
|
|
expect(response.status).to eq(200)
|
2015-01-09 12:04:02 -05:00
|
|
|
expect(Draft.get(user, 'xxx', 0)).to eq(nil)
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
2019-10-31 02:15:41 -04:00
|
|
|
|
|
|
|
it 'cant trivially resolve conflicts without interaction' do
|
|
|
|
|
|
|
|
user = sign_in(Fabricate(:user))
|
|
|
|
|
|
|
|
DraftSequence.next!(user, "abc")
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 0,
|
|
|
|
data: { a: "test" }.to_json,
|
|
|
|
owner: "abcdefg"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2019-10-31 02:15:41 -04:00
|
|
|
expect(json["draft_sequence"]).to eq(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'has a clean protocol for ownership handover' do
|
|
|
|
user = sign_in(Fabricate(:user))
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 0,
|
|
|
|
data: { a: "test" }.to_json,
|
|
|
|
owner: "abcdefg"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2019-10-31 02:15:41 -04:00
|
|
|
expect(json["draft_sequence"]).to eq(0)
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 0,
|
|
|
|
data: { b: "test" }.to_json,
|
|
|
|
owner: "hijklmnop"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2019-10-31 02:15:41 -04:00
|
|
|
expect(json["draft_sequence"]).to eq(1)
|
|
|
|
|
|
|
|
expect(DraftSequence.current(user, "abc")).to eq(1)
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 1,
|
|
|
|
data: { c: "test" }.to_json,
|
|
|
|
owner: "hijklmnop"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2020-05-12 02:55:24 -04:00
|
|
|
expect(json["draft_sequence"]).to eq(2)
|
2019-10-31 02:15:41 -04:00
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "abc",
|
2020-05-12 02:55:24 -04:00
|
|
|
sequence: 2,
|
2019-10-31 02:15:41 -04:00
|
|
|
data: { c: "test" }.to_json,
|
|
|
|
owner: "abc"
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2020-05-07 11:04:12 -04:00
|
|
|
json = response.parsed_body
|
2020-05-12 02:55:24 -04:00
|
|
|
expect(json["draft_sequence"]).to eq(3)
|
2019-10-31 02:15:41 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'raises an error for out-of-sequence draft setting' do
|
|
|
|
|
|
|
|
user = sign_in(Fabricate(:user))
|
|
|
|
seq = DraftSequence.next!(user, "abc")
|
|
|
|
Draft.set(user, "abc", seq, { b: "test" }.to_json)
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: seq - 1,
|
|
|
|
data: { a: "test" }.to_json
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(409)
|
|
|
|
|
|
|
|
post "/draft.json", params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: seq + 1,
|
|
|
|
data: { a: "test" }.to_json
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(409)
|
|
|
|
|
|
|
|
end
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|