Convert request-based setup into factory setup in push/subscriptions request spec (#29489)

pull/2674/head
Matt Jankowski 2024-03-11 11:46:25 -04:00 committed by GitHub
parent a38e424185
commit 24319836de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 56 additions and 34 deletions

View File

@ -37,66 +37,88 @@ describe 'API V1 Push Subscriptions' do
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } } let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'POST /api/v1/push/subscription' do describe 'POST /api/v1/push/subscription' do
before do subject { post '/api/v1/push/subscription', params: create_payload, headers: headers }
post '/api/v1/push/subscription', params: create_payload, headers: headers
end
it 'saves push subscriptions' do it 'saves push subscriptions and returns expected JSON' do
push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint]) subject
expect(push_subscription.endpoint).to eq(create_payload[:subscription][:endpoint]) expect(endpoint_push_subscription)
expect(push_subscription.key_p256dh).to eq(create_payload[:subscription][:keys][:p256dh]) .to have_attributes(
expect(push_subscription.key_auth).to eq(create_payload[:subscription][:keys][:auth]) endpoint: eq(create_payload[:subscription][:endpoint]),
expect(push_subscription.user_id).to eq user.id key_p256dh: eq(create_payload[:subscription][:keys][:p256dh]),
expect(push_subscription.access_token_id).to eq token.id key_auth: eq(create_payload[:subscription][:keys][:auth]),
end user_id: eq(user.id),
access_token_id: eq(token.id)
)
it 'replaces old subscription on repeat calls' do
post '/api/v1/push/subscription', params: create_payload, headers: headers
expect(Web::PushSubscription.where(endpoint: create_payload[:subscription][:endpoint]).count).to eq 1
end
it 'returns the expected JSON' do
expect(body_as_json.with_indifferent_access) expect(body_as_json.with_indifferent_access)
.to include( .to include(
{ endpoint: create_payload[:subscription][:endpoint], alerts: {}, policy: 'all' } { endpoint: create_payload[:subscription][:endpoint], alerts: {}, policy: 'all' }
) )
end end
it 'replaces old subscription on repeat calls' do
2.times { subject }
expect(endpoint_push_subscriptions.count)
.to eq(1)
end
end end
describe 'PUT /api/v1/push/subscription' do describe 'PUT /api/v1/push/subscription' do
before do subject { put '/api/v1/push/subscription', params: alerts_payload, headers: headers }
post '/api/v1/push/subscription', params: create_payload, headers: headers
put '/api/v1/push/subscription', params: alerts_payload, headers: headers
end
it 'changes alert settings' do before { create_subscription_with_token }
push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
expect(push_subscription.data['policy']).to eq(alerts_payload[:data][:policy]) it 'changes data policy and alert settings and returns expected JSON' do
expect { subject }
.to change { endpoint_push_subscription.reload.data }
.from(nil)
.to(include('policy' => alerts_payload[:data][:policy]))
%w(follow follow_request favourite reblog mention poll status).each do |type| %w(follow follow_request favourite reblog mention poll status).each do |type|
expect(push_subscription.data['alerts'][type]).to eq(alerts_payload[:data][:alerts][type.to_sym].to_s) expect(endpoint_push_subscription.data['alerts']).to include(
type.to_s => eq(alerts_payload[:data][:alerts][type.to_sym].to_s)
)
end end
end
it 'returns the expected JSON' do
expect(body_as_json.with_indifferent_access) expect(body_as_json.with_indifferent_access)
.to include( .to include(
{ endpoint: create_payload[:subscription][:endpoint], alerts: alerts_payload[:data][:alerts], policy: alerts_payload[:data][:policy] } endpoint: create_payload[:subscription][:endpoint],
alerts: alerts_payload[:data][:alerts],
policy: alerts_payload[:data][:policy]
) )
end end
end end
describe 'DELETE /api/v1/push/subscription' do describe 'DELETE /api/v1/push/subscription' do
before do subject { delete '/api/v1/push/subscription', headers: headers }
post '/api/v1/push/subscription', params: create_payload, headers: headers
delete '/api/v1/push/subscription', headers: headers before { create_subscription_with_token }
end
it 'removes the subscription' do it 'removes the subscription' do
expect(Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])).to be_nil expect { subject }
.to change { endpoint_push_subscription }.to(nil)
end end
end end
private
def endpoint_push_subscriptions
Web::PushSubscription.where(
endpoint: create_payload[:subscription][:endpoint]
)
end
def endpoint_push_subscription
endpoint_push_subscriptions.first
end
def create_subscription_with_token
Fabricate(
:web_push_subscription,
endpoint: create_payload[:subscription][:endpoint],
access_token_id: token.id
)
end
end end