2019-04-29 19:27:42 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-27 21:27:38 -05:00
|
|
|
RSpec.describe DraftsController do
|
2023-07-28 06:53:46 -05:00
|
|
|
fab!(:user) { Fabricate(:user) }
|
|
|
|
|
2021-09-14 07:18:01 -05:00
|
|
|
describe "#index" do
|
2023-01-09 05:18:21 -06:00
|
|
|
it "requires you to be logged in" do
|
2021-09-14 07:18:01 -05:00
|
|
|
get "/drafts.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
2023-07-28 06:53:46 -05:00
|
|
|
describe "when limit params is invalid" do
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
include_examples "invalid limit params", "/drafts.json", described_class::INDEX_LIMIT
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "returns correct stream length after adding a draft" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2023-01-09 05:18:21 -06:00
|
|
|
Draft.set(user, "xxx", 0, "{}")
|
2021-09-14 07:18:01 -05:00
|
|
|
get "/drafts.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
parsed = response.parsed_body
|
|
|
|
expect(response.parsed_body["drafts"].length).to eq(1)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "has empty stream after deleting last draft" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2023-01-09 05:18:21 -06:00
|
|
|
Draft.set(user, "xxx", 0, "{}")
|
|
|
|
Draft.clear(user, "xxx", 0)
|
2021-09-14 07:18:01 -05:00
|
|
|
get "/drafts.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["drafts"].length).to eq(0)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "does not include topic details when user cannot see topic" do
|
2021-09-14 07:18:01 -05:00
|
|
|
topic = Fabricate(:private_message_topic)
|
|
|
|
topic_user = topic.user
|
|
|
|
other_user = Fabricate(:user)
|
2023-01-09 05:18:21 -06:00
|
|
|
Draft.set(topic_user, "topic_#{topic.id}", 0, "{}")
|
|
|
|
Draft.set(other_user, "topic_#{topic.id}", 0, "{}")
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
sign_in(topic_user)
|
|
|
|
get "/drafts.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["drafts"].first["title"]).to eq(topic.title)
|
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 01:34:54 -05:00
|
|
|
|
2021-09-14 07:18:01 -05:00
|
|
|
sign_in(other_user)
|
|
|
|
get "/drafts.json"
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["drafts"].first["title"]).to eq(nil)
|
|
|
|
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 01:34:54 -05:00
|
|
|
end
|
|
|
|
|
2021-09-14 07:18:01 -05:00
|
|
|
describe "#show" do
|
|
|
|
it "returns a draft if requested" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2023-01-09 05:18:21 -06:00
|
|
|
Draft.set(user, "hello", 0, "test")
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
get "/drafts/hello.json"
|
|
|
|
expect(response.status).to eq(200)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(response.parsed_body["draft"]).to eq("test")
|
2021-09-14 07:18:01 -05: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 01:34:54 -05:00
|
|
|
end
|
2018-09-12 09:13:20 -05:00
|
|
|
|
2021-09-14 07:18:01 -05:00
|
|
|
describe "#create" do
|
2023-01-09 05:18:21 -06:00
|
|
|
it "requires you to be logged in" do
|
2021-09-14 07:18:01 -05:00
|
|
|
post "/drafts.json"
|
|
|
|
expect(response.status).to eq(403)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "saves a draft" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2021-09-14 07:18:01 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json", params: { draft_key: "xyz", data: { my: "data" }.to_json, sequence: 0 }
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(Draft.get(user, "xyz", 0)).to eq(%q({"my":"data"}))
|
2021-09-14 07:18:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it "returns 404 when the key is missing" do
|
|
|
|
sign_in(Fabricate(:user))
|
|
|
|
post "/drafts.json", params: { data: { my: "data" }.to_json, sequence: 0 }
|
|
|
|
expect(response.status).to eq(404)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "checks for an conflict on update" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2021-09-14 07:18:01 -05:00
|
|
|
post = Fabricate(:post, user: user)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "topic",
|
|
|
|
sequence: 0,
|
|
|
|
data: { postId: post.id, originalText: post.raw, action: "edit" }.to_json,
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(response.parsed_body["conflict_user"]).to eq(nil)
|
|
|
|
|
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "topic",
|
|
|
|
sequence: 0,
|
|
|
|
data: { postId: post.id, originalText: "something else", action: "edit" }.to_json,
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(response.parsed_body["conflict_user"]["id"]).to eq(post.last_editor.id)
|
|
|
|
expect(response.parsed_body["conflict_user"]).to include("avatar_template")
|
2021-09-14 07:18:01 -05:00
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "cant trivially resolve conflicts without interaction" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
DraftSequence.next!(user, "abc")
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 0,
|
|
|
|
data: { a: "test" }.to_json,
|
|
|
|
owner: "abcdefg",
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["draft_sequence"]).to eq(1)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "has a clean protocol for ownership handover" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2021-09-14 07:18:01 -05:00
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 0,
|
|
|
|
data: { a: "test" }.to_json,
|
|
|
|
owner: "abcdefg",
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["draft_sequence"]).to eq(0)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 0,
|
|
|
|
data: { b: "test" }.to_json,
|
|
|
|
owner: "hijklmnop",
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["draft_sequence"]).to eq(1)
|
|
|
|
|
|
|
|
expect(DraftSequence.current(user, "abc")).to eq(1)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 1,
|
|
|
|
data: { c: "test" }.to_json,
|
|
|
|
owner: "hijklmnop",
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["draft_sequence"]).to eq(2)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: 2,
|
|
|
|
data: { c: "test" }.to_json,
|
|
|
|
owner: "abc",
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(200)
|
|
|
|
expect(response.parsed_body["draft_sequence"]).to eq(3)
|
|
|
|
end
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
it "raises an error for out-of-sequence draft setting" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2021-09-14 07:18:01 -05:00
|
|
|
seq = DraftSequence.next!(user, "abc")
|
|
|
|
Draft.set(user, "abc", seq, { b: "test" }.to_json)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: seq - 1,
|
|
|
|
data: { a: "test" }.to_json,
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(409)
|
|
|
|
|
2023-01-09 05:18:21 -06:00
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "abc",
|
|
|
|
sequence: seq + 1,
|
|
|
|
data: { a: "test" }.to_json,
|
|
|
|
}
|
2021-09-14 07:18:01 -05:00
|
|
|
|
|
|
|
expect(response.status).to eq(409)
|
|
|
|
end
|
2023-01-25 05:50:21 -06:00
|
|
|
|
|
|
|
context "when data is too big" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:data) { "a" * (SiteSetting.max_draft_length + 1) }
|
|
|
|
|
|
|
|
before do
|
|
|
|
SiteSetting.max_draft_length = 500
|
|
|
|
sign_in(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
it "returns an error" do
|
|
|
|
post "/drafts.json",
|
|
|
|
params: {
|
|
|
|
draft_key: "xyz",
|
|
|
|
data: { reply: data }.to_json,
|
|
|
|
sequence: 0,
|
|
|
|
}
|
|
|
|
expect(response).to have_http_status :bad_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context "when data is not too big" do
|
|
|
|
context "when data is not proper JSON" do
|
|
|
|
let(:user) { Fabricate(:user) }
|
|
|
|
let(:data) { "not-proper-json" }
|
|
|
|
|
|
|
|
before { sign_in(user) }
|
|
|
|
|
|
|
|
it "returns an error" do
|
|
|
|
post "/drafts.json", params: { draft_key: "xyz", data: data, sequence: 0 }
|
|
|
|
expect(response).to have_http_status :bad_request
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-12 09:13:20 -05:00
|
|
|
end
|
2020-03-23 06:02:24 -05:00
|
|
|
|
2021-09-14 07:18:01 -05:00
|
|
|
describe "#destroy" do
|
2023-01-09 05:18:21 -06:00
|
|
|
it "destroys drafts when required" do
|
2023-07-28 06:53:46 -05:00
|
|
|
sign_in(user)
|
2023-01-09 05:18:21 -06:00
|
|
|
Draft.set(user, "xxx", 0, "hi")
|
2021-09-14 07:18:01 -05:00
|
|
|
delete "/drafts/xxx.json", params: { sequence: 0 }
|
2023-04-19 14:41:45 -05:00
|
|
|
|
2021-09-14 07:18:01 -05:00
|
|
|
expect(response.status).to eq(200)
|
2023-01-09 05:18:21 -06:00
|
|
|
expect(Draft.get(user, "xxx", 0)).to eq(nil)
|
2021-09-14 07:18:01 -05:00
|
|
|
end
|
2023-04-19 14:41:45 -05:00
|
|
|
|
|
|
|
it "denies attempts to destroy unowned draft" do
|
|
|
|
sign_in(Fabricate(:admin))
|
|
|
|
user = Fabricate(:user)
|
|
|
|
Draft.set(user, "xxx", 0, "hi")
|
|
|
|
delete "/drafts/xxx.json", params: { sequence: 0, username: user.username }
|
|
|
|
|
|
|
|
# Draft is not deleted because request is not via API
|
|
|
|
expect(Draft.get(user, "xxx", 0)).to be_present
|
|
|
|
end
|
|
|
|
|
|
|
|
shared_examples "for a passed user" do
|
|
|
|
it "deletes draft" do
|
|
|
|
api_key = Fabricate(:api_key).key
|
|
|
|
Draft.set(recipient, "xxx", 0, "hi")
|
|
|
|
|
|
|
|
delete "/drafts/xxx.json",
|
|
|
|
params: {
|
|
|
|
sequence: 0,
|
|
|
|
username: recipient.username,
|
|
|
|
},
|
|
|
|
headers: {
|
|
|
|
HTTP_API_USERNAME: caller.username,
|
|
|
|
HTTP_API_KEY: api_key,
|
|
|
|
}
|
|
|
|
|
|
|
|
expect(response.status).to eq(response_code)
|
|
|
|
|
|
|
|
if draft_deleted
|
|
|
|
expect(Draft.get(recipient, "xxx", 0)).to eq(nil)
|
|
|
|
else
|
|
|
|
expect(Draft.get(recipient, "xxx", 0)).to be_present
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "api called by admin" do
|
|
|
|
include_examples "for a passed user" do
|
|
|
|
let(:caller) { Fabricate(:admin) }
|
|
|
|
let(:recipient) { Fabricate(:user) }
|
|
|
|
let(:response_code) { 200 }
|
|
|
|
let(:draft_deleted) { true }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "api called by tl4 user" do
|
|
|
|
include_examples "for a passed user" do
|
|
|
|
let(:caller) { Fabricate(:trust_level_4) }
|
|
|
|
let(:recipient) { Fabricate(:user) }
|
|
|
|
let(:response_code) { 403 }
|
|
|
|
let(:draft_deleted) { false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "api called by regular user" do
|
|
|
|
include_examples "for a passed user" do
|
|
|
|
let(:caller) { Fabricate(:user) }
|
|
|
|
let(:recipient) { Fabricate(:user) }
|
|
|
|
let(:response_code) { 403 }
|
|
|
|
let(:draft_deleted) { false }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe "api called by admin for another admin" do
|
|
|
|
include_examples "for a passed user" do
|
|
|
|
let(:caller) { Fabricate(:admin) }
|
|
|
|
let(:recipient) { Fabricate(:admin) }
|
|
|
|
let(:response_code) { 200 }
|
|
|
|
let(:draft_deleted) { true }
|
|
|
|
end
|
|
|
|
end
|
2020-03-23 06:02:24 -05: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 01:34:54 -05:00
|
|
|
end
|