diff --git a/app/controllers/api/v1/notifications_controller.rb b/app/controllers/api/v1/notifications_controller.rb index 5fad84cfd..139f456c2 100644 --- a/app/controllers/api/v1/notifications_controller.rb +++ b/app/controllers/api/v1/notifications_controller.rb @@ -3,6 +3,8 @@ module Api module V1 class NotificationsController < Api::V1::BaseController + BOOLEAN_TYPE = ActiveModel::Type::Boolean.new + before_action do require_access_token %w[notifications] end @@ -24,25 +26,25 @@ module Api def index after_date = Date.iso8601(params[:only_after]) if params.has_key?(:only_after) - notifications_query = service.index(params[:only_unread], after_date) + notifications_query = service.index(BOOLEAN_TYPE.cast(params[:only_unread]), after_date) notifications_page = time_pager(notifications_query).response notifications_page[:data] = notifications_page[:data].map do |note| NotificationPresenter.new(note, default_serializer_options).as_api_json end render_paged_api_response notifications_page rescue ArgumentError - render_error 422, "Couldnt process the notifications requestt process the notifications request" + render_error 422, "Could not process the notifications request" end def update - read = ActiveModel::Type::Boolean.new.cast(params.require(:read)) + read = BOOLEAN_TYPE.cast(params.require(:read)) if service.update_status_by_guid(params[:id], read) head :no_content else - render_error 422, "Couldnt process the notifications requestt process the notifications request" + render_error 422, "Could not process the notifications request" end rescue ActionController::ParameterMissing - render_error 422, "Couldnt process the notifications requestt process the notifications request" + render_error 422, "Could not process the notifications request" end private diff --git a/spec/integration/api/notifications_controller_spec.rb b/spec/integration/api/notifications_controller_spec.rb index d362bb354..d61eba2b0 100644 --- a/spec/integration/api/notifications_controller_spec.rb +++ b/spec/integration/api/notifications_controller_spec.rb @@ -62,6 +62,20 @@ describe Api::V1::NotificationsController do expect(notification.length).to eq(1) end + it "with proper credentials and unread only explicitly false" do + @notification.set_read_state(true) + get( + api_v1_notifications_path, + params: {only_unread: false, access_token: access_token} + ) + expect(response.status).to eq(200) + notifications = response_body_data(response) + expect(notifications.length).to eq(2) + confirm_notification_format(notifications[1], @notification, "also_commented", nil) + + expect(notifications.to_json).to match_json_schema(:api_v1_schema, fragment: "#/definitions/notifications") + end + it "with proper credentials and after certain date" do get( api_v1_notifications_path, @@ -87,7 +101,7 @@ describe Api::V1::NotificationsController do api_v1_notifications_path, params: {only_after: "January 1, 2018", access_token: access_token} ) - confirm_api_error(response, 422, "Couldnt process the notifications requestt process the notifications request") + confirm_api_error(response, 422, "Could not process the notifications request") end it "with insufficient credentials" do @@ -190,7 +204,7 @@ describe Api::V1::NotificationsController do api_v1_notification_path("999_999_999"), params: {access_token: access_token} ) - confirm_api_error(response, 422, "Couldnt process the notifications requestt process the notifications request") + confirm_api_error(response, 422, "Could not process the notifications request") end it "with proper missing read field" do @@ -198,7 +212,7 @@ describe Api::V1::NotificationsController do api_v1_notification_path(@notification.guid), params: {access_token: access_token} ) - confirm_api_error(response, 422, "Couldnt process the notifications requestt process the notifications request") + confirm_api_error(response, 422, "Could not process the notifications request") end it "with insufficient credentials" do