From e8b9a70fbf7fa97d786733ee030e1fd0af2b6181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Wed, 29 Jan 2020 23:32:33 +0100 Subject: [PATCH] Ensure API responses adhere to documented error format --- app/controllers/api/v1/aspects_controller.rb | 14 +++--- app/controllers/api/v1/base_controller.rb | 4 ++ app/controllers/api/v1/comments_controller.rb | 14 +++--- app/controllers/api/v1/contacts_controller.rb | 10 ++--- .../api/v1/conversations_controller.rb | 4 +- app/controllers/api/v1/likes_controller.rb | 11 ++--- app/controllers/api/v1/messages_controller.rb | 4 +- .../api/v1/notifications_controller.rb | 10 ++--- app/controllers/api/v1/photos_controller.rb | 6 +-- .../api/v1/post_interactions_controller.rb | 22 +++++----- app/controllers/api/v1/posts_controller.rb | 6 +-- app/controllers/api/v1/reshares_controller.rb | 6 +-- app/controllers/api/v1/search_controller.rb | 2 +- .../api/v1/tag_followings_controller.rb | 2 +- app/controllers/api/v1/users_controller.rb | 8 ++-- spec/integration/api/api_spec_helper.rb | 8 ++++ .../api/aspects_controller_spec.rb | 20 +++------ .../api/comments_controller_spec.rb | 41 ++++++----------- .../api/contacts_controller_spec.rb | 32 +++++--------- .../api/conversations_controller_spec.rb | 32 +++++--------- spec/integration/api/likes_controller_spec.rb | 23 ++++------ .../api/messages_controller_spec.rb | 11 ++--- .../api/notifications_controller_spec.rb | 17 +++---- .../integration/api/photos_controller_spec.rb | 26 ++++------- .../api/post_interactions_controller_spec.rb | 29 +++++------- spec/integration/api/posts_controller_spec.rb | 44 +++++++------------ .../api/reshares_controller_spec.rb | 23 ++++------ .../integration/api/search_controller_spec.rb | 11 ++--- .../api/streams_controller_spec.rb | 2 +- .../api/tag_followings_controller_spec.rb | 8 ++-- spec/integration/api/users_controller_spec.rb | 17 +++---- 31 files changed, 189 insertions(+), 278 deletions(-) create mode 100644 spec/integration/api/api_spec_helper.rb diff --git a/app/controllers/api/v1/aspects_controller.rb b/app/controllers/api/v1/aspects_controller.rb index d18263847..a05f4cbae 100644 --- a/app/controllers/api/v1/aspects_controller.rb +++ b/app/controllers/api/v1/aspects_controller.rb @@ -23,7 +23,7 @@ module Api if aspect render json: aspect_as_json(aspect, true) else - render json: I18n.t("api.endpoint_errors.aspects.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.aspects.not_found") end end @@ -33,24 +33,24 @@ module Api if aspect&.save render json: aspect_as_json(aspect, true) else - render json: I18n.t("api.endpoint_errors.aspects.cant_create"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.aspects.cant_create") end rescue ActionController::ParameterMissing - render json: I18n.t("api.endpoint_errors.aspects.cant_create"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.aspects.cant_create") end def update aspect = current_user.aspects.where(id: params[:id]).first if !aspect - render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.aspects.cant_update") elsif aspect.update!(aspect_params(true)) render json: aspect_as_json(aspect, true) else - render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.aspects.cant_update") end rescue ActionController::ParameterMissing, ActiveRecord::RecordInvalid - render json: I18n.t("api.endpoint_errors.aspects.cant_update"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.aspects.cant_update") end def destroy @@ -58,7 +58,7 @@ module Api if aspect&.destroy head :no_content else - render json: I18n.t("api.endpoint_errors.aspects.cant_delete"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.aspects.cant_delete") end end diff --git a/app/controllers/api/v1/base_controller.rb b/app/controllers/api/v1/base_controller.rb index e112b06f1..970af0729 100644 --- a/app/controllers/api/v1/base_controller.rb +++ b/app/controllers/api/v1/base_controller.rb @@ -58,6 +58,10 @@ module Api render json: page[:data] end + def render_error(code, message) + render json: {code: code, message: message}, status: code + end + def time_pager(query) Api::Paging::RestPaginatorBuilder.new(query, request).time_pager(params) end diff --git a/app/controllers/api/v1/comments_controller.rb b/app/controllers/api/v1/comments_controller.rb index be00f9e7e..405df9e8f 100644 --- a/app/controllers/api/v1/comments_controller.rb +++ b/app/controllers/api/v1/comments_controller.rb @@ -12,18 +12,18 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found") end rescue_from ActiveRecord::RecordInvalid do - render json: I18n.t("api.endpoint_errors.comments.not_allowed"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.comments.not_allowed") end def create find_post comment = comment_service.create(params.require(:post_id), params.require(:body)) rescue ActiveRecord::RecordNotFound - render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found") else render json: comment_as_json(comment), status: :created end @@ -45,7 +45,7 @@ module Api head :no_content end rescue ActiveRecord::RecordInvalid - render json: I18n.t("api.endpoint_errors.comments.no_delete"), status: :forbidden + render_error 403, I18n.t("api.endpoint_errors.comments.no_delete") end def report @@ -64,7 +64,7 @@ module Api if report.save head :no_content else - render json: I18n.t("api.endpoint_errors.comments.duplicate_report"), status: :conflict + render_error 409, I18n.t("api.endpoint_errors.comments.duplicate_report") end end @@ -72,10 +72,10 @@ module Api def comment_and_post_validate(post_guid, comment_guid) if !comment_exists(comment_guid) - render json: I18n.t("api.endpoint_errors.comments.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.comments.not_found") false elsif !comment_is_for_post(post_guid, comment_guid) - render json: I18n.t("api.endpoint_errors.comments.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.comments.not_found") false else true diff --git a/app/controllers/api/v1/contacts_controller.rb b/app/controllers/api/v1/contacts_controller.rb index 50760d0ea..41028204f 100644 --- a/app/controllers/api/v1/contacts_controller.rb +++ b/app/controllers/api/v1/contacts_controller.rb @@ -14,7 +14,7 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.aspects.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.aspects.not_found") end def index @@ -34,10 +34,10 @@ module Api if aspect_membership head :no_content else - render json: I18n.t("api.endpoint_errors.contacts.cant_create"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.contacts.cant_create") end rescue ActiveRecord::RecordNotUnique - render json: I18n.t("api.endpoint_errors.contacts.cant_create"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.contacts.cant_create") end def destroy @@ -48,10 +48,10 @@ module Api if result && result[:success] head :no_content else - render json: I18n.t("api.endpoint_errors.contacts.cant_delete"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.contacts.cant_delete") end rescue ActiveRecord::RecordNotFound - render json: I18n.t("api.endpoint_errors.contacts.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.contacts.not_found") end def aspects_membership_service diff --git a/app/controllers/api/v1/conversations_controller.rb b/app/controllers/api/v1/conversations_controller.rb index 23bdcf210..61649013c 100644 --- a/app/controllers/api/v1/conversations_controller.rb +++ b/app/controllers/api/v1/conversations_controller.rb @@ -10,7 +10,7 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.conversations.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.conversations.not_found") end def index @@ -40,7 +40,7 @@ module Api Diaspora::Federation::Dispatcher.defer_dispatch(current_user, conversation) render json: conversation_as_json(conversation), status: :created rescue ActiveRecord::RecordInvalid, ActionController::ParameterMissing, ActiveRecord::RecordNotFound - render json: I18n.t("api.endpoint_errors.conversations.cant_process"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.conversations.cant_process") end def destroy diff --git a/app/controllers/api/v1/likes_controller.rb b/app/controllers/api/v1/likes_controller.rb index ba524c236..0b4180776 100644 --- a/app/controllers/api/v1/likes_controller.rb +++ b/app/controllers/api/v1/likes_controller.rb @@ -12,11 +12,11 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found") end rescue_from ActiveRecord::RecordInvalid do - render json: I18n.t("api.endpoint_errors.likes.user_not_allowed_to_like"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.likes.user_not_allowed_to_like") end def show @@ -35,8 +35,9 @@ module Api like_service.create(params[:post_id]) rescue ActiveRecord::RecordInvalid => e - return render json: I18n.t("api.endpoint_errors.likes.like_exists"), status: :unprocessable_entity if - e.message == "Validation failed: Target has already been taken" + if e.message == "Validation failed: Target has already been taken" + return render_error 422, I18n.t("api.endpoint_errors.likes.like_exists") + end raise else @@ -51,7 +52,7 @@ module Api if success head :no_content else - render json: I18n.t("api.endpoint_errors.likes.no_like"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.likes.no_like") end end diff --git a/app/controllers/api/v1/messages_controller.rb b/app/controllers/api/v1/messages_controller.rb index dc2839447..f6db1ba2a 100644 --- a/app/controllers/api/v1/messages_controller.rb +++ b/app/controllers/api/v1/messages_controller.rb @@ -8,7 +8,7 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.conversations.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.conversations.not_found") end def create @@ -19,7 +19,7 @@ module Api Diaspora::Federation::Dispatcher.defer_dispatch(current_user, message) render json: message_json(message), status: :created rescue ActionController::ParameterMissing - render json: I18n.t("api.endpoint_errors.conversations.cant_process"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.conversations.cant_process") end def index diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb index a00063b4e..94a5b41a2 100644 --- a/app/controllers/api/v1/notifications_controller.rb +++ b/app/controllers/api/v1/notifications_controller.rb @@ -8,7 +8,7 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.notifications.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.notifications.not_found") end def show @@ -17,7 +17,7 @@ module Api if notification render json: NotificationPresenter.new(notification).as_api_json(true) else - render json: I18n.t("api.endpoint_errors.notifications.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.notifications.not_found") end end @@ -31,7 +31,7 @@ module Api end render_paged_api_response notifications_page rescue ArgumentError - render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.notifications.cant_process") end def update @@ -39,10 +39,10 @@ module Api if service.update_status_by_guid(params[:id], read) head :no_content else - render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.notifications.cant_process") end rescue ActionController::ParameterMissing - render json: I18n.t("api.endpoint_errors.notifications.cant_process"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.notifications.cant_process") end private diff --git a/app/controllers/api/v1/photos_controller.rb b/app/controllers/api/v1/photos_controller.rb index d56012311..b97f12ed3 100644 --- a/app/controllers/api/v1/photos_controller.rb +++ b/app/controllers/api/v1/photos_controller.rb @@ -12,7 +12,7 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.photos.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.photos.not_found") end def index @@ -46,7 +46,7 @@ module Api render json: photo_json(photo) rescue CarrierWave::IntegrityError, ActionController::ParameterMissing, RuntimeError - render json: I18n.t("api.endpoint_errors.photos.failed_create"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.photos.failed_create") end def destroy @@ -58,7 +58,7 @@ module Api if current_user.retract(photo) head :no_content else - render json: I18n.t("api.endpoint_errors.photos.failed_delete"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.photos.failed_delete") end end diff --git a/app/controllers/api/v1/post_interactions_controller.rb b/app/controllers/api/v1/post_interactions_controller.rb index 14809ebb8..5fa9557c3 100644 --- a/app/controllers/api/v1/post_interactions_controller.rb +++ b/app/controllers/api/v1/post_interactions_controller.rb @@ -10,7 +10,7 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found") end def subscribe @@ -18,7 +18,7 @@ module Api current_user.participate!(post) head :no_content rescue ActiveRecord::RecordInvalid - render json: I18n.t("api.endpoint_errors.interactions.cant_subscribe"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.interactions.cant_subscribe") end def hide @@ -45,27 +45,27 @@ module Api if report.save head :no_content else - render json: I18n.t("api.endpoint_errors.posts.cant_report"), status: :conflict + render_error 409, I18n.t("api.endpoint_errors.posts.cant_report") end rescue ActionController::ParameterMissing - render json: I18n.t("api.endpoint_errors.posts.cant_report"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.posts.cant_report") end def vote + post = find_post begin - post = find_post + poll_vote = poll_service.vote(post.id, params[:poll_answer_id]) rescue ActiveRecord::RecordNotFound - render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found - return + # This, but not the find_post above, should return a 422, + # we just keep poll_vote nil so it goes into the else below end - poll_vote = poll_service.vote(post.id, params[:poll_answer_id]) if poll_vote head :no_content else - render json: I18n.t("api.endpoint_errors.interactions.cant_vote"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.interactions.cant_vote") end - rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotFound - render json: I18n.t("api.endpoint_errors.interactions.cant_vote"), status: :unprocessable_entity + rescue ActiveRecord::RecordInvalid + render_error 422, I18n.t("api.endpoint_errors.interactions.cant_vote") end private diff --git a/app/controllers/api/v1/posts_controller.rb b/app/controllers/api/v1/posts_controller.rb index 27c05ebec..eb854368f 100644 --- a/app/controllers/api/v1/posts_controller.rb +++ b/app/controllers/api/v1/posts_controller.rb @@ -14,7 +14,7 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found") end def show @@ -31,14 +31,14 @@ module Api @status_message = creation_service.create(creation_params) render json: PostPresenter.new(@status_message, current_user).as_api_response rescue StandardError - render json: I18n.t("api.endpoint_errors.posts.failed_create"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.posts.failed_create") end def destroy post_service.destroy(params[:id], private_modify?) head :no_content rescue Diaspora::NotMine, Diaspora::NonPublic - render json: I18n.t("api.endpoint_errors.posts.failed_delete"), status: :forbidden + render_error 403, I18n.t("api.endpoint_errors.posts.failed_delete") end private diff --git a/app/controllers/api/v1/reshares_controller.rb b/app/controllers/api/v1/reshares_controller.rb index 8caca9e7e..40e9fdf04 100644 --- a/app/controllers/api/v1/reshares_controller.rb +++ b/app/controllers/api/v1/reshares_controller.rb @@ -12,11 +12,11 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found") end rescue_from Diaspora::NonPublic do - render json: I18n.t("api.endpoint_errors.posts.post_not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.posts.post_not_found") end def show @@ -35,7 +35,7 @@ module Api def create reshare = reshare_service.create(params.require(:post_id)) rescue ActiveRecord::RecordNotFound, ActiveRecord::RecordInvalid, RuntimeError - render plain: I18n.t("reshares.create.error"), status: :unprocessable_entity + render_error 422, I18n.t("reshares.create.error") else render json: PostPresenter.new(reshare, current_user).as_api_response end diff --git a/app/controllers/api/v1/search_controller.rb b/app/controllers/api/v1/search_controller.rb index 512e9dba4..555c04c7c 100644 --- a/app/controllers/api/v1/search_controller.rb +++ b/app/controllers/api/v1/search_controller.rb @@ -8,7 +8,7 @@ module Api end rescue_from ActionController::ParameterMissing, RuntimeError do - render json: I18n.t("api.endpoint_errors.search.cant_process"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.search.cant_process") end def user_index diff --git a/app/controllers/api/v1/tag_followings_controller.rb b/app/controllers/api/v1/tag_followings_controller.rb index ab51c70fa..5579a32f6 100755 --- a/app/controllers/api/v1/tag_followings_controller.rb +++ b/app/controllers/api/v1/tag_followings_controller.rb @@ -19,7 +19,7 @@ module Api tag_followings_service.create(params.require(:name)) head :no_content rescue StandardError - render json: I18n.t("api.endpoint_errors.tags.cant_process"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.tags.cant_process") end def destroy diff --git a/app/controllers/api/v1/users_controller.rb b/app/controllers/api/v1/users_controller.rb index 576cc1fc6..64f3b77ba 100644 --- a/app/controllers/api/v1/users_controller.rb +++ b/app/controllers/api/v1/users_controller.rb @@ -22,7 +22,7 @@ module Api end rescue_from ActiveRecord::RecordNotFound do - render json: I18n.t("api.endpoint_errors.users.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.users.not_found") end def show @@ -42,15 +42,15 @@ module Api if params_to_update && current_user.update_profile(params_to_update) render json: PersonPresenter.new(current_user.person, current_user).profile_hash_as_api_json else - render json: I18n.t("api.endpoint_errors.users.cant_update"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.users.cant_update") end rescue RuntimeError - render json: I18n.t("api.endpoint_errors.users.cant_update"), status: :unprocessable_entity + render_error 422, I18n.t("api.endpoint_errors.users.cant_update") end def contacts if params.require(:user_id) != current_user.guid - render json: I18n.t("api.endpoint_errors.users.not_found"), status: :not_found + render_error 404, I18n.t("api.endpoint_errors.users.not_found") return end diff --git a/spec/integration/api/api_spec_helper.rb b/spec/integration/api/api_spec_helper.rb new file mode 100644 index 000000000..02f6c4bf9 --- /dev/null +++ b/spec/integration/api/api_spec_helper.rb @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +require "spec_helper" + +def confirm_api_error(response, code, message) + expect(response.status).to eq(code) + expect(JSON.parse(response.body)).to eq("code" => code, "message" => message) +end diff --git a/spec/integration/api/aspects_controller_spec.rb b/spec/integration/api/aspects_controller_spec.rb index 9a9621cf1..0ed32e2fb 100644 --- a/spec/integration/api/aspects_controller_spec.rb +++ b/spec/integration/api/aspects_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_sTring_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::AspectsController do let(:auth) { @@ -91,8 +91,7 @@ describe Api::V1::AspectsController do api_v1_aspect_path("-1"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.aspects.not_found")) end end @@ -137,8 +136,7 @@ describe Api::V1::AspectsController do params: {name: @aspect1.name, access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.cant_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.aspects.cant_create")) end end @@ -149,8 +147,7 @@ describe Api::V1::AspectsController do params: {order: 0, access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.cant_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.aspects.cant_create")) end end @@ -237,8 +234,7 @@ describe Api::V1::AspectsController do params: {name: @aspect1.name, access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.cant_update")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.aspects.cant_update")) end it "fails with bad id" do @@ -247,8 +243,7 @@ describe Api::V1::AspectsController do params: {name: "NewAspectName", access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.cant_update")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.aspects.cant_update")) end end @@ -289,8 +284,7 @@ describe Api::V1::AspectsController do api_v1_aspect_path("-1"), params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.cant_delete")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.aspects.cant_delete")) end end diff --git a/spec/integration/api/comments_controller_spec.rb b/spec/integration/api/comments_controller_spec.rb index ae6a5cf81..aae3f3490 100644 --- a/spec/integration/api/comments_controller_spec.rb +++ b/spec/integration/api/comments_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::CommentsController do let(:auth) { @@ -80,8 +80,7 @@ describe Api::V1::CommentsController do api_v1_post_comments_path(post_id: "999_999_999"), params: {body: "text", access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -102,8 +101,7 @@ describe Api::V1::CommentsController do api_v1_post_comments_path(post_id: @private_post.guid), params: {body: "comment text", access_token: access_token_public_only} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "fails without interactions scope" do @@ -154,8 +152,7 @@ describe Api::V1::CommentsController do api_v1_post_comments_path(post_id: "999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -165,8 +162,7 @@ describe Api::V1::CommentsController do api_v1_post_comments_path(post_id: @private_post.guid), params: {access_token: access_token_public_only} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "fails without valid token" do @@ -210,8 +206,7 @@ describe Api::V1::CommentsController do ), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -237,8 +232,7 @@ describe Api::V1::CommentsController do ), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.comments.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.comments.not_found")) end end @@ -252,8 +246,7 @@ describe Api::V1::CommentsController do ), params: {access_token: access_token} ) - expect(response.status).to eq(403) - expect(response.body).to eq(I18n.t("api.endpoint_errors.comments.no_delete")) + confirm_api_error(response, 403, I18n.t("api.endpoint_errors.comments.no_delete")) end end @@ -316,8 +309,7 @@ describe Api::V1::CommentsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.comments.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.comments.not_found")) end end @@ -333,8 +325,7 @@ describe Api::V1::CommentsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -350,8 +341,7 @@ describe Api::V1::CommentsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -367,8 +357,7 @@ describe Api::V1::CommentsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.comments.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.comments.not_found")) end end @@ -396,8 +385,7 @@ describe Api::V1::CommentsController do access_token: access_token } ) - expect(response.status).to eq(409) - expect(response.body).to eq(I18n.t("api.endpoint_errors.comments.duplicate_report")) + confirm_api_error(response, 409, I18n.t("api.endpoint_errors.comments.duplicate_report")) end end @@ -413,8 +401,7 @@ describe Api::V1::CommentsController do access_token: access_token_public_only } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "fails without valid token" do diff --git a/spec/integration/api/contacts_controller_spec.rb b/spec/integration/api/contacts_controller_spec.rb index 5e5b76c1f..1a07e1eb5 100644 --- a/spec/integration/api/contacts_controller_spec.rb +++ b/spec/integration/api/contacts_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::ContactsController do let(:auth) { @@ -68,8 +68,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contacts_path(-1), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.aspects.not_found")) end it "fails for other user's Aspect ID" do @@ -77,8 +76,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contacts_path(@eve_aspect.id), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.aspects.not_found")) end end @@ -119,8 +117,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contacts_path(@aspect2.id), params: {person_guid: alice.guid, access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.contacts.cant_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.contacts.cant_create")) end end @@ -130,8 +127,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contacts_path(-1), params: {person_guid: alice.guid, access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.aspects.not_found")) end it "fails for other user's Aspect ID" do @@ -139,8 +135,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contacts_path(@eve_aspect.id), params: {person_guid: alice.guid, access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.aspects.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.aspects.not_found")) end end @@ -150,8 +145,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contacts_path(@aspect2.id), params: {person_guid: "999_999_999", access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.contacts.cant_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.contacts.cant_create")) end end @@ -194,8 +188,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contact_path(@aspect2.id, eve.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.contacts.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.contacts.not_found")) end end @@ -205,8 +198,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contact_path(-1, eve.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.contacts.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.contacts.not_found")) end it "fails for other user's Aspect ID" do @@ -214,8 +206,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contact_path(@eve_aspect.id, eve.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.contacts.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.contacts.not_found")) end end @@ -225,8 +216,7 @@ describe Api::V1::ContactsController do api_v1_aspect_contact_path(@aspect2.id, "999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.contacts.cant_delete")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.contacts.cant_delete")) end end diff --git a/spec/integration/api/conversations_controller_spec.rb b/spec/integration/api/conversations_controller_spec.rb index 4d903b4ab..ab03c77fe 100644 --- a/spec/integration/api/conversations_controller_spec.rb +++ b/spec/integration/api/conversations_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::ConversationsController do let(:auth) { @@ -57,8 +57,7 @@ describe Api::V1::ConversationsController do context "without valid data" do it "fails with empty body" do post api_v1_conversations_path, params: {access_token: access_token} - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.conversations.cant_process")) end it "fails with missing subject " do @@ -68,8 +67,7 @@ describe Api::V1::ConversationsController do access_token: access_token } post api_v1_conversations_path, params: incomplete_conversation - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.conversations.cant_process")) end it "fails with missing body " do @@ -79,8 +77,7 @@ describe Api::V1::ConversationsController do access_token: access_token } post api_v1_conversations_path, params: incomplete_conversation - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.conversations.cant_process")) end it "fails with missing recipients " do @@ -90,8 +87,7 @@ describe Api::V1::ConversationsController do access_token: access_token } post api_v1_conversations_path, params: incomplete_conversation - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.conversations.cant_process")) end it "fails with bad recipient ID " do @@ -102,8 +98,7 @@ describe Api::V1::ConversationsController do access_token: access_token } post api_v1_conversations_path, params: incomplete_conversation - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.conversations.cant_process")) end it "fails with invalid recipient (not allowed to message) " do @@ -114,8 +109,7 @@ describe Api::V1::ConversationsController do access_token: access_token } post api_v1_conversations_path, params: incomplete_conversation - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.conversations.cant_process")) end end @@ -237,8 +231,7 @@ describe Api::V1::ConversationsController do api_v1_conversation_path(-1), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.conversations.not_found")) end end @@ -291,8 +284,7 @@ describe Api::V1::ConversationsController do @conversation_guid, params: {access_token: access_token} ) - expect(response.status).to eq 404 - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.conversations.not_found")) get api_v1_conversation_path( @conversation_guid, params: {access_token: access_token_participant} @@ -317,8 +309,7 @@ describe Api::V1::ConversationsController do @conversation_guid, params: {access_token: access_token_participant} ) - expect(response.status).to eq 404 - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.conversations.not_found")) expect { Conversation.find(guid: @conversation_guid) @@ -332,8 +323,7 @@ describe Api::V1::ConversationsController do api_v1_conversation_path(42), params: {access_token: access_token} ) - expect(response.status).to eq 404 - expect(response.body).to eq(I18n.t("api.endpoint_errors.conversations.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.conversations.not_found")) end end diff --git a/spec/integration/api/likes_controller_spec.rb b/spec/integration/api/likes_controller_spec.rb index d25d82265..faf5d3686 100644 --- a/spec/integration/api/likes_controller_spec.rb +++ b/spec/integration/api/likes_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::LikesController do let(:auth) { @@ -85,8 +85,7 @@ describe Api::V1::LikesController do api_v1_post_likes_path(post_id: "badguid"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -97,8 +96,7 @@ describe Api::V1::LikesController do api_v1_post_likes_path(post_id: @private_status.guid), params: {access_token: access_token_public_only} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.likes.user_not_allowed_to_like")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.likes.user_not_allowed_to_like")) end end @@ -136,8 +134,7 @@ describe Api::V1::LikesController do api_v1_post_likes_path(post_id: @status.guid), params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.likes.like_exists")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.likes.like_exists")) likes = like_service.find_for_post(@status.guid) expect(likes.length).to eq(1) @@ -151,8 +148,7 @@ describe Api::V1::LikesController do api_v1_post_likes_path(post_id: 99_999_999), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -210,8 +206,7 @@ describe Api::V1::LikesController do api_v1_post_likes_path(post_id: @status.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.likes.no_like")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.likes.no_like")) likes = like_service.find_for_post(@status.guid) expect(likes.length).to eq(0) @@ -224,8 +219,7 @@ describe Api::V1::LikesController do api_v1_post_likes_path(post_id: 99_999_999), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -236,8 +230,7 @@ describe Api::V1::LikesController do api_v1_post_likes_path(post_id: @private_status.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "fails in unliking post without interactions" do diff --git a/spec/integration/api/messages_controller_spec.rb b/spec/integration/api/messages_controller_spec.rb index d2956b017..120f691e8 100644 --- a/spec/integration/api/messages_controller_spec.rb +++ b/spec/integration/api/messages_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::MessagesController do let(:auth) { @@ -71,8 +71,7 @@ describe Api::V1::MessagesController do api_v1_conversation_messages_path(@conversation_guid), params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq I18n.t("api.endpoint_errors.conversations.cant_process") + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.conversations.cant_process")) end it "empty string returns a unprocessable entity (422)" do @@ -80,8 +79,7 @@ describe Api::V1::MessagesController do api_v1_conversation_messages_path(@conversation_guid), params: {body: "", access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq I18n.t("api.endpoint_errors.conversations.cant_process") + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.conversations.cant_process")) end end @@ -91,8 +89,7 @@ describe Api::V1::MessagesController do api_v1_conversation_messages_path(42), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq I18n.t("api.endpoint_errors.conversations.not_found") + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.conversations.not_found")) end end diff --git a/spec/integration/api/notifications_controller_spec.rb b/spec/integration/api/notifications_controller_spec.rb index 0edbd857e..e38be1e59 100644 --- a/spec/integration/api/notifications_controller_spec.rb +++ b/spec/integration/api/notifications_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::NotificationsController do let(:auth) { @@ -87,8 +87,7 @@ describe Api::V1::NotificationsController do api_v1_notifications_path, params: {only_after: "January 1, 2018", access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.notifications.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.notifications.cant_process")) end it "with insufficient credentials" do @@ -130,8 +129,7 @@ describe Api::V1::NotificationsController do api_v1_notification_path("999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.notifications.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.notifications.not_found")) end it "on someone else's notification" do @@ -147,8 +145,7 @@ describe Api::V1::NotificationsController do api_v1_notification_path(alice_notification.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.notifications.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.notifications.not_found")) end it "with insufficient credentials" do @@ -193,8 +190,7 @@ describe Api::V1::NotificationsController do api_v1_notification_path("999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.notifications.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.notifications.cant_process")) end it "with proper missing read field" do @@ -202,8 +198,7 @@ describe Api::V1::NotificationsController do api_v1_notification_path(@notification.guid), params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.notifications.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.notifications.cant_process")) end it "with insufficient credentials" do diff --git a/spec/integration/api/photos_controller_spec.rb b/spec/integration/api/photos_controller_spec.rb index 457d67db9..ec7686ca6 100644 --- a/spec/integration/api/photos_controller_spec.rb +++ b/spec/integration/api/photos_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_sTring_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::PhotosController do let(:auth) { @@ -109,8 +109,7 @@ describe Api::V1::PhotosController do api_v1_photo_path(@shared_photo1.guid), params: {access_token: access_token_public_only_read_only} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.photos.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.photos.not_found")) end it "with other user's private photo" do @@ -118,8 +117,7 @@ describe Api::V1::PhotosController do api_v1_photo_path(@private_photo1.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.photos.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.photos.not_found")) end it "with invalid GUID" do @@ -127,8 +125,7 @@ describe Api::V1::PhotosController do api_v1_photo_path("999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.photos.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.photos.not_found")) end it "with invalid access token" do @@ -244,8 +241,7 @@ describe Api::V1::PhotosController do api_v1_photos_path, params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.photos.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.photos.failed_create")) end it "with non-image file" do @@ -257,8 +253,7 @@ describe Api::V1::PhotosController do api_v1_photos_path, params: {image: text_file, access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.photos.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.photos.failed_create")) end it "with impromperly identified file" do @@ -270,8 +265,7 @@ describe Api::V1::PhotosController do api_v1_photos_path, params: {image: text_file, access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.photos.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.photos.failed_create")) end it "with invalid access token" do @@ -319,8 +313,7 @@ describe Api::V1::PhotosController do api_v1_photo_path(@alice_public_photo.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.photos.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.photos.not_found")) end it "with other invalid GUID" do @@ -328,8 +321,7 @@ describe Api::V1::PhotosController do api_v1_photo_path("999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.photos.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.photos.not_found")) end it "with invalid access token" do diff --git a/spec/integration/api/post_interactions_controller_spec.rb b/spec/integration/api/post_interactions_controller_spec.rb index 0cbef3b5f..742180cdb 100644 --- a/spec/integration/api/post_interactions_controller_spec.rb +++ b/spec/integration/api/post_interactions_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_sTring_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::PostInteractionsController do let(:auth) { @@ -82,8 +82,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "with insufficient token" do @@ -141,8 +140,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "with insufficient token" do @@ -210,8 +208,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "when not subscribed already" do @@ -288,8 +285,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "when already reported" do @@ -308,8 +304,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(409) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.cant_report")) + confirm_api_error(response, 409, I18n.t("api.endpoint_errors.posts.cant_report")) end it "when missing reason" do @@ -319,8 +314,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.cant_report")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.cant_report")) end it "with insufficient token" do @@ -395,8 +389,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.interactions.cant_vote")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.interactions.cant_vote")) end it "fails with bad answer id" do @@ -407,8 +400,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.interactions.cant_vote")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.interactions.cant_vote")) end it "fails with bad post id" do @@ -419,8 +411,7 @@ describe Api::V1::PostInteractionsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "with insufficient token" do diff --git a/spec/integration/api/posts_controller_spec.rb b/spec/integration/api/posts_controller_spec.rb index 6cecda8b8..35af66452 100644 --- a/spec/integration/api/posts_controller_spec.rb +++ b/spec/integration/api/posts_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::PostsController do let(:auth) { @@ -128,8 +128,7 @@ describe Api::V1::PostsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -146,8 +145,7 @@ describe Api::V1::PostsController do access_token: access_token_public_only_read_only } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) get( api_v1_post_path(shared_post.guid), @@ -167,8 +165,7 @@ describe Api::V1::PostsController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -322,8 +319,7 @@ describe Api::V1::PostsController do photos: ["999_999_999"] } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end it "creates with poll" do @@ -364,8 +360,7 @@ describe Api::V1::PostsController do } } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end it "fails poll with blank answer" do @@ -382,8 +377,7 @@ describe Api::V1::PostsController do } } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end it "fails poll with blank question and message text" do @@ -399,8 +393,7 @@ describe Api::V1::PostsController do } } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end it "creates with location" do @@ -476,8 +469,7 @@ describe Api::V1::PostsController do public: true } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end it "fails when no public field and no aspects" do @@ -489,8 +481,7 @@ describe Api::V1::PostsController do body: message_text } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end it "fails when private no aspects" do @@ -503,8 +494,7 @@ describe Api::V1::PostsController do public: false } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end it "fails when unknown aspect IDs" do @@ -518,8 +508,7 @@ describe Api::V1::PostsController do aspects: ["-1"] } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end it "fails when no public field but aspects" do @@ -534,8 +523,7 @@ describe Api::V1::PostsController do aspects: [aspect.id] } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_create")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.posts.failed_create")) end end @@ -638,8 +626,7 @@ describe Api::V1::PostsController do api_v1_post_path("999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -656,8 +643,7 @@ describe Api::V1::PostsController do api_v1_post_path(status.guid), params: {access_token: access_token} ) - expect(response.status).to eq(403) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.failed_delete")) + confirm_api_error(response, 403, I18n.t("api.endpoint_errors.posts.failed_delete")) end end end diff --git a/spec/integration/api/reshares_controller_spec.rb b/spec/integration/api/reshares_controller_spec.rb index 328426238..67a4ae54a 100644 --- a/spec/integration/api/reshares_controller_spec.rb +++ b/spec/integration/api/reshares_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_sTring_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::ResharesController do let(:auth) { @@ -85,8 +85,7 @@ describe Api::V1::ResharesController do params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end it "fails with private post it shouldn't see" do @@ -97,8 +96,7 @@ describe Api::V1::ResharesController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end @@ -139,8 +137,7 @@ describe Api::V1::ResharesController do params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("reshares.create.error")) + confirm_api_error(response, 422, I18n.t("reshares.create.error")) end end @@ -151,8 +148,7 @@ describe Api::V1::ResharesController do params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("reshares.create.error")) + confirm_api_error(response, 422, I18n.t("reshares.create.error")) end it "fails with own post" do @@ -161,8 +157,7 @@ describe Api::V1::ResharesController do params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("reshares.create.error")) + confirm_api_error(response, 422, I18n.t("reshares.create.error")) end it "fails with private post it shouldn't see" do @@ -173,8 +168,7 @@ describe Api::V1::ResharesController do access_token: access_token } ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("reshares.create.error")) + confirm_api_error(response, 422, I18n.t("reshares.create.error")) end it "fails with private post it can see" do @@ -185,8 +179,7 @@ describe Api::V1::ResharesController do access_token: access_token } ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.posts.post_not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.posts.post_not_found")) end end diff --git a/spec/integration/api/search_controller_spec.rb b/spec/integration/api/search_controller_spec.rb index 73d1b084e..80c1bb454 100644 --- a/spec/integration/api/search_controller_spec.rb +++ b/spec/integration/api/search_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::SearchController do let(:auth) { @@ -128,8 +128,7 @@ describe Api::V1::SearchController do "/api/v1/search/users", params: {tag: "tag1", name_or_handle: "name", access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.search.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.search.cant_process")) end it "fails with no fields" do @@ -137,8 +136,7 @@ describe Api::V1::SearchController do "/api/v1/search/users", params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.search.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.search.cant_process")) end it "fails with bad credentials" do @@ -210,8 +208,7 @@ describe Api::V1::SearchController do "/api/v1/search/posts", params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.search.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.search.cant_process")) end it "fails with bad credentials" do diff --git a/spec/integration/api/streams_controller_spec.rb b/spec/integration/api/streams_controller_spec.rb index ea80d1539..a3af82201 100644 --- a/spec/integration/api/streams_controller_spec.rb +++ b/spec/integration/api/streams_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::StreamsController do let(:auth_read_only) { diff --git a/spec/integration/api/tag_followings_controller_spec.rb b/spec/integration/api/tag_followings_controller_spec.rb index 76ccc1588..3262d7384 100755 --- a/spec/integration/api/tag_followings_controller_spec.rb +++ b/spec/integration/api/tag_followings_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_string_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::TagFollowingsController do let(:auth) { @@ -56,8 +56,7 @@ describe Api::V1::TagFollowingsController do params: {access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.tags.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.tags.cant_process")) end end @@ -68,8 +67,7 @@ describe Api::V1::TagFollowingsController do params: {name: "tag3", access_token: access_token} ) - expect(response.status).to eq(422) - expect(response.body).to eq(I18n.t("api.endpoint_errors.tags.cant_process")) + confirm_api_error(response, 422, I18n.t("api.endpoint_errors.tags.cant_process")) end end diff --git a/spec/integration/api/users_controller_spec.rb b/spec/integration/api/users_controller_spec.rb index d13d0af24..6edc7739b 100644 --- a/spec/integration/api/users_controller_spec.rb +++ b/spec/integration/api/users_controller_spec.rb @@ -1,6 +1,6 @@ # frozen_sTring_literal: true -require "spec_helper" +require_relative "api_spec_helper" describe Api::V1::UsersController do include PeopleHelper @@ -168,8 +168,7 @@ describe Api::V1::UsersController do "/api/v1/users/999_999_999", params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.users.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.users.not_found")) end end end @@ -335,8 +334,7 @@ describe Api::V1::UsersController do api_v1_user_contacts_path("999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.users.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.users.not_found")) end it "fails with other user's GUID" do @@ -344,8 +342,7 @@ describe Api::V1::UsersController do api_v1_user_contacts_path(alice.guid), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.users.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.users.not_found")) end it "fails if insufficient scope token" do @@ -427,8 +424,7 @@ describe Api::V1::UsersController do api_v1_user_photos_path("999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.users.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.users.not_found")) end it "fails if invalid token" do @@ -501,8 +497,7 @@ describe Api::V1::UsersController do api_v1_user_posts_path("999_999_999"), params: {access_token: access_token} ) - expect(response.status).to eq(404) - expect(response.body).to eq(I18n.t("api.endpoint_errors.users.not_found")) + confirm_api_error(response, 404, I18n.t("api.endpoint_errors.users.not_found")) end it "fails if invalid token" do