Extract params hash for api/v1/push/subscriptions#create (#35475)

This commit is contained in:
Matt Jankowski 2025-07-24 03:49:20 -04:00 committed by GitHub
parent 469304359a
commit e54e96d61f
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 35 additions and 19 deletions

View File

@ -16,16 +16,7 @@ class Api::V1::Push::SubscriptionsController < Api::BaseController
def create
with_redis_lock("push_subscription:#{current_user.id}") do
destroy_web_push_subscriptions!
@push_subscription = Web::PushSubscription.create!(
endpoint: subscription_params[:endpoint],
key_p256dh: subscription_params[:keys][:p256dh],
key_auth: subscription_params[:keys][:auth],
standard: subscription_params[:standard] || false,
data: data_params,
user_id: current_user.id,
access_token_id: doorkeeper_token.id
)
@push_subscription = Web::PushSubscription.create!(web_push_subscription_params)
end
render json: @push_subscription, serializer: REST::WebPushSubscriptionSerializer
@ -55,6 +46,18 @@ class Api::V1::Push::SubscriptionsController < Api::BaseController
not_found if @push_subscription.nil?
end
def web_push_subscription_params
{
access_token_id: doorkeeper_token.id,
data: data_params,
endpoint: subscription_params[:endpoint],
key_auth: subscription_params[:keys][:auth],
key_p256dh: subscription_params[:keys][:p256dh],
standard: subscription_params[:standard] || false,
user_id: current_user.id,
}
end
def subscription_params
params.expect(subscription: [:endpoint, :standard, keys: [:auth, :p256dh]])
end

View File

@ -166,17 +166,30 @@ RSpec.describe 'API V1 Push Subscriptions' do
describe 'GET /api/v1/push/subscription' do
subject { get '/api/v1/push/subscription', headers: headers }
before { create_subscription_with_token }
context 'with a subscription' do
before { create_subscription_with_token }
it 'shows subscription details' do
subject
it 'shows subscription details' do
subject
expect(response)
.to have_http_status(200)
expect(response.content_type)
.to start_with('application/json')
expect(response.parsed_body)
.to include(endpoint: endpoint)
expect(response)
.to have_http_status(200)
expect(response.content_type)
.to start_with('application/json')
expect(response.parsed_body)
.to include(endpoint: endpoint)
end
end
context 'without a subscription' do
it 'returns not found' do
subject
expect(response)
.to have_http_status(404)
expect(response.content_type)
.to start_with('application/json')
end
end
end