From cd6e02ccec636f5fa109f32c1a5b36ba15671f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Thu, 20 Feb 2020 18:49:21 +0100 Subject: [PATCH] API: allow post creation without a body when there are photos --- app/controllers/api/v1/posts_controller.rb | 2 +- .../status_message_creation_service.rb | 9 +++ spec/integration/api/posts_controller_spec.rb | 65 +++++++++++++++++-- 3 files changed, 71 insertions(+), 5 deletions(-) diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb index 527bc181a..196b497de 100644 --- a/app/controllers/api/v1/posts_controller.rb +++ b/app/controllers/api/v1/posts_controller.rb @@ -46,7 +46,7 @@ module Api def normalized_create_params mapped_parameters = { status_message: { - text: params.require(:body) + text: params[:body] }, public: params.require(:public), aspect_ids: normalize_aspect_ids(params.permit(aspects: [])) diff --git a/app/services/status_message_creation_service.rb b/app/services/status_message_creation_service.rb index 5f53c440e..951b89596 100644 --- a/app/services/status_message_creation_service.rb +++ b/app/services/status_message_creation_service.rb @@ -8,6 +8,8 @@ class StatusMessageCreationService end def create(params) + validate_content(params) + build_status_message(params).tap do |status_message| load_aspects(params[:aspect_ids]) unless status_message.public? add_attachments(status_message, params) @@ -20,6 +22,10 @@ class StatusMessageCreationService 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) public = params[:public] || false user.build_post(:status_message, params[:status_message].merge(public: public)) @@ -80,4 +86,7 @@ class StatusMessageCreationService class BadAspectsIDs < RuntimeError end + + class MissingContent < RuntimeError + end end diff --git a/spec/integration/api/posts_controller_spec.rb b/spec/integration/api/posts_controller_spec.rb index b858cbfd9..0a6d416b0 100644 --- a/spec/integration/api/posts_controller_spec.rb +++ b/spec/integration/api/posts_controller_spec.rb @@ -238,6 +238,7 @@ describe Api::V1::PostsController do expect(response.status).to eq(200) post = response_body(response) 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 it "or creates a private post" do @@ -260,6 +261,7 @@ describe Api::V1::PostsController do post = response_body(response) expect(response.status).to eq(200) 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 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 @@ -303,6 +305,7 @@ describe Api::V1::PostsController do post_for_ref_only = StatusMessageCreationService.new(auth.user).create(merged_params) 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 it "fails to add other's photos" do @@ -317,7 +320,7 @@ describe Api::V1::PostsController do photos: @alice_photo_guids } ) - expect(response.status).to eq(422) + confirm_api_error(response, 422, "Failed to create the post") end it "fails to add non-pending photos" do @@ -332,7 +335,7 @@ describe Api::V1::PostsController do photos: [@user_photo3.guid] } ) - expect(response.status).to eq(422) + confirm_api_error(response, 422, "Failed to create the post") end it "fails to add bad photo guids" do @@ -372,6 +375,7 @@ describe Api::V1::PostsController do post = response_body(response) expect(response.status).to eq(200) 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 it "fails poll with no answers" do @@ -447,6 +451,7 @@ describe Api::V1::PostsController do post = response_body(response) expect(response.status).to eq(200) 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 it "creates with mentions" do @@ -485,10 +490,62 @@ describe Api::V1::PostsController do expect(response.status).to eq(200) post = response_body(response) expect(post["nsfw"]).to be_truthy + expect(post.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/post") 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 post( api_v1_posts_path,