2024-03-07 15:53:37 +01:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Requests' do
|
|
|
|
let(:user) { Fabricate(:user, account_attributes: { username: 'alice' }) }
|
|
|
|
let(:token) { Fabricate(:accessible_access_token, resource_owner_id: user.id, scopes: scopes) }
|
|
|
|
let(:scopes) { 'read:notifications write:notifications' }
|
|
|
|
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
|
|
|
|
|
2024-07-08 18:01:08 +02:00
|
|
|
describe 'GET /api/v1/notifications/requests', :inline_jobs do
|
2024-03-07 15:53:37 +01:00
|
|
|
subject do
|
|
|
|
get '/api/v1/notifications/requests', headers: headers, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
before do
|
|
|
|
Fabricate(:notification_request, account: user.account)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
|
|
|
|
|
|
|
|
context 'with no options' do
|
|
|
|
it 'returns http success', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-03-07 15:53:37 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/notifications/requests/:id/accept' do
|
|
|
|
subject do
|
|
|
|
post "/api/v1/notifications/requests/#{notification_request.id}/accept", headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:notification_request) { Fabricate(:notification_request, account: user.account) }
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
|
|
|
|
|
2024-09-19 12:15:21 +02:00
|
|
|
it 'returns http success and creates notification permission' do
|
2024-03-07 15:53:37 +01:00
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-03-07 15:53:37 +01:00
|
|
|
expect(NotificationPermission.find_by(account: notification_request.account, from_account: notification_request.from_account)).to_not be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when notification request belongs to someone else' do
|
|
|
|
let(:notification_request) { Fabricate(:notification_request) }
|
|
|
|
|
|
|
|
it 'returns http not found' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-03-07 15:53:37 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/notifications/requests/:id/dismiss' do
|
|
|
|
subject do
|
|
|
|
post "/api/v1/notifications/requests/#{notification_request.id}/dismiss", headers: headers
|
|
|
|
end
|
|
|
|
|
2024-07-12 14:09:52 +02:00
|
|
|
let!(:notification_request) { Fabricate(:notification_request, account: user.account) }
|
2024-03-07 15:53:37 +01:00
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
|
|
|
|
|
2024-07-12 14:09:52 +02:00
|
|
|
it 'returns http success and destroys the notification request', :aggregate_failures do
|
|
|
|
expect { subject }.to change(NotificationRequest, :count).by(-1)
|
2024-03-07 15:53:37 +01:00
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-03-07 15:53:37 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'when notification request belongs to someone else' do
|
|
|
|
let(:notification_request) { Fabricate(:notification_request) }
|
|
|
|
|
|
|
|
it 'returns http not found' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-03-07 15:53:37 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-08-01 14:36:54 +02:00
|
|
|
|
|
|
|
describe 'POST /api/v1/notifications/requests/accept' do
|
|
|
|
subject do
|
|
|
|
post '/api/v1/notifications/requests/accept', params: { id: [notification_request.id] }, headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:notification_request) { Fabricate(:notification_request, account: user.account) }
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
|
|
|
|
|
|
|
|
it 'returns http success and creates notification permission', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(NotificationPermission.find_by(account: notification_request.account, from_account: notification_request.from_account)).to_not be_nil
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-08-01 14:36:54 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/notifications/requests/dismiss' do
|
|
|
|
subject do
|
|
|
|
post '/api/v1/notifications/requests/dismiss', params: { id: [notification_request.id] }, headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:notification_request) { Fabricate(:notification_request, account: user.account) }
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
|
|
|
|
|
|
|
|
it 'returns http success and destroys the notification request', :aggregate_failures do
|
|
|
|
expect { subject }.to change(NotificationRequest, :count).by(-1)
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-08-01 14:36:54 +02:00
|
|
|
end
|
|
|
|
end
|
2024-08-19 17:59:06 +02:00
|
|
|
|
|
|
|
describe 'GET /api/v1/notifications/requests/merged' do
|
|
|
|
subject do
|
|
|
|
get '/api/v1/notifications/requests/merged', headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
|
|
|
|
|
|
|
|
context 'when the user has no accepted request pending merge' do
|
|
|
|
it 'returns http success and returns merged: true' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-09-06 11:58:46 +02:00
|
|
|
expect(response.parsed_body).to match({ merged: true })
|
2024-08-19 17:59:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when the user has an accepted request pending merge' do
|
|
|
|
before do
|
|
|
|
redis.set("notification_unfilter_jobs:#{user.account_id}", 1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns http success and returns merged: false' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-20 15:13:04 +02:00
|
|
|
expect(response.content_type)
|
|
|
|
.to start_with('application/json')
|
2024-09-06 11:58:46 +02:00
|
|
|
expect(response.parsed_body).to match({ merged: false })
|
2024-08-19 17:59:06 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2024-03-07 15:53:37 +01:00
|
|
|
end
|