API: allow post creation without a body when there are photos

This commit is contained in:
Jonne Haß 2020-02-20 18:49:21 +01:00
parent e9242d7754
commit cd6e02ccec
3 changed files with 71 additions and 5 deletions

View file

@ -46,7 +46,7 @@ module Api
def normalized_create_params def normalized_create_params
mapped_parameters = { mapped_parameters = {
status_message: { status_message: {
text: params.require(:body) text: params[:body]
}, },
public: params.require(:public), public: params.require(:public),
aspect_ids: normalize_aspect_ids(params.permit(aspects: [])) aspect_ids: normalize_aspect_ids(params.permit(aspects: []))

View file

@ -8,6 +8,8 @@ class StatusMessageCreationService
end end
def create(params) def create(params)
validate_content(params)
build_status_message(params).tap do |status_message| build_status_message(params).tap do |status_message|
load_aspects(params[:aspect_ids]) unless status_message.public? load_aspects(params[:aspect_ids]) unless status_message.public?
add_attachments(status_message, params) add_attachments(status_message, params)
@ -20,6 +22,10 @@ class StatusMessageCreationService
attr_reader :user, :aspects attr_reader :user, :aspects
def validate_content(params)
raise MissingContent unless params[:status_message][:text].present? || params[:photos].present?
end
def build_status_message(params) def build_status_message(params)
public = params[:public] || false public = params[:public] || false
user.build_post(:status_message, params[:status_message].merge(public: public)) user.build_post(:status_message, params[:status_message].merge(public: public))
@ -80,4 +86,7 @@ class StatusMessageCreationService
class BadAspectsIDs < RuntimeError class BadAspectsIDs < RuntimeError
end end
class MissingContent < RuntimeError
end
end end

View file

@ -238,6 +238,7 @@ describe Api::V1::PostsController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
post = response_body(response) post = response_body(response)
confirm_post_format(post, auth.user, post_for_ref_only) confirm_post_format(post, auth.user, post_for_ref_only)
expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end end
it "or creates a private post" do it "or creates a private post" do
@ -260,6 +261,7 @@ describe Api::V1::PostsController do
post = response_body(response) post = response_body(response)
expect(response.status).to eq(200) expect(response.status).to eq(200)
confirm_post_format(post, auth.user, post_for_ref_only) confirm_post_format(post, auth.user, post_for_ref_only)
expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end end
it "doesn't creates a private post without private:modify scope in token" do it "doesn't creates a private post without private:modify scope in token" do
@ -274,7 +276,7 @@ describe Api::V1::PostsController do
} }
) )
expect(response.status).to eq(422) confirm_api_error(response, 422, "Failed to create the post")
end end
end end
@ -303,6 +305,7 @@ describe Api::V1::PostsController do
post_for_ref_only = StatusMessageCreationService.new(auth.user).create(merged_params) post_for_ref_only = StatusMessageCreationService.new(auth.user).create(merged_params)
confirm_post_format(post, auth.user, post_for_ref_only) confirm_post_format(post, auth.user, post_for_ref_only)
expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end end
it "fails to add other's photos" do it "fails to add other's photos" do
@ -317,7 +320,7 @@ describe Api::V1::PostsController do
photos: @alice_photo_guids photos: @alice_photo_guids
} }
) )
expect(response.status).to eq(422) confirm_api_error(response, 422, "Failed to create the post")
end end
it "fails to add non-pending photos" do it "fails to add non-pending photos" do
@ -332,7 +335,7 @@ describe Api::V1::PostsController do
photos: [@user_photo3.guid] photos: [@user_photo3.guid]
} }
) )
expect(response.status).to eq(422) confirm_api_error(response, 422, "Failed to create the post")
end end
it "fails to add bad photo guids" do it "fails to add bad photo guids" do
@ -372,6 +375,7 @@ describe Api::V1::PostsController do
post = response_body(response) post = response_body(response)
expect(response.status).to eq(200) expect(response.status).to eq(200)
confirm_post_format(post, auth.user, post_for_ref_only) confirm_post_format(post, auth.user, post_for_ref_only)
expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end end
it "fails poll with no answers" do it "fails poll with no answers" do
@ -447,6 +451,7 @@ describe Api::V1::PostsController do
post = response_body(response) post = response_body(response)
expect(response.status).to eq(200) expect(response.status).to eq(200)
confirm_post_format(post, auth.user, post_for_ref_only) confirm_post_format(post, auth.user, post_for_ref_only)
expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end end
it "creates with mentions" do it "creates with mentions" do
@ -485,10 +490,62 @@ describe Api::V1::PostsController do
expect(response.status).to eq(200) expect(response.status).to eq(200)
post = response_body(response) post = response_body(response)
expect(post["nsfw"]).to be_truthy expect(post["nsfw"]).to be_truthy
expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end end
end end
context "when given missing format" do context "when given just photos" do
it "creates the post" do
post(
api_v1_posts_path,
params: {
access_token: access_token,
public: true,
photos: @user_photo_guids
}
)
expect(response.status).to eq(200)
expect(response.body).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post")
end
it "fails to add other's photos" do
post(
api_v1_posts_path,
params: {
access_token: access_token,
public: true,
photos: @alice_photo_guids
}
)
confirm_api_error(response, 422, "Failed to create the post")
end
it "fails to add non-pending photos" do
post(
api_v1_posts_path,
params: {
access_token: access_token,
public: true,
photos: [@user_photo3.guid]
}
)
confirm_api_error(response, 422, "Failed to create the post")
end
it "fails to add bad photo guids" do
post(
api_v1_posts_path,
params: {
access_token: access_token,
public: true,
photos: ["999_999_999"]
}
)
confirm_api_error(response, 422, "Failed to create the post")
end
end
context "when given bad post" do
it "fails when no body" do it "fails when no body" do
post( post(
api_v1_posts_path, api_v1_posts_path,