API: Fix fetching explicitly not only unread notifications

This commit is contained in:
Jonne Haß 2020-02-08 21:54:08 +01:00
parent cd0995abf3
commit 8068d8747b
2 changed files with 24 additions and 8 deletions

View file

@ -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

View file

@ -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