2023-10-17 02:15:24 +11:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require 'rails_helper'
|
|
|
|
|
|
|
|
RSpec.describe 'Notifications' 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-30 18:39:11 +10:00
|
|
|
describe 'GET /api/v1/notifications/unread_count', :inline_jobs do
|
|
|
|
subject do
|
|
|
|
get '/api/v1/notifications/unread_count', headers: headers, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
before do
|
|
|
|
first_status = PostStatusService.new.call(user.account, text: 'Test')
|
|
|
|
ReblogService.new.call(Fabricate(:account), first_status)
|
|
|
|
PostStatusService.new.call(Fabricate(:account), text: 'Hello @alice')
|
|
|
|
FavouriteService.new.call(Fabricate(:account), first_status)
|
|
|
|
FavouriteService.new.call(Fabricate(:account), first_status)
|
|
|
|
FollowService.new.call(Fabricate(:account), user.account)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
|
|
|
|
|
|
|
|
context 'with no options' do
|
|
|
|
it 'returns expected notifications count' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body[:count]).to eq 5
|
2024-07-30 18:39:11 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a read marker' do
|
|
|
|
before do
|
|
|
|
id = user.account.notifications.browserable.order(id: :desc).offset(2).first.id
|
|
|
|
user.markers.create!(timeline: 'notifications', last_read_id: id)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns expected notifications count' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body[:count]).to eq 2
|
2024-07-30 18:39:11 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with exclude_types param' do
|
|
|
|
let(:params) { { exclude_types: %w(mention) } }
|
|
|
|
|
|
|
|
it 'returns expected notifications count' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body[:count]).to eq 4
|
2024-07-30 18:39:11 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with a user-provided limit' do
|
|
|
|
let(:params) { { limit: 2 } }
|
|
|
|
|
|
|
|
it 'returns a capped value' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body[:count]).to eq 2
|
2024-07-30 18:39:11 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when there are more notifications than the limit' do
|
|
|
|
before do
|
|
|
|
stub_const('Api::V1::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT', 2)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns a capped value' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body[:count]).to eq Api::V1::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT
|
2024-07-30 18:39:11 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-07-09 02:01:08 +10:00
|
|
|
describe 'GET /api/v1/notifications', :inline_jobs do
|
2023-10-17 02:15:24 +11:00
|
|
|
subject do
|
|
|
|
get '/api/v1/notifications', headers: headers, params: params
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:bob) { Fabricate(:user) }
|
|
|
|
let(:tom) { Fabricate(:user) }
|
|
|
|
let(:params) { {} }
|
|
|
|
|
|
|
|
before do
|
|
|
|
first_status = PostStatusService.new.call(user.account, text: 'Test')
|
|
|
|
ReblogService.new.call(bob.account, first_status)
|
2024-07-15 19:29:57 +10:00
|
|
|
PostStatusService.new.call(bob.account, text: 'Hello @alice')
|
|
|
|
PostStatusService.new.call(tom.account, text: 'Hello @alice', visibility: :direct) # Filtered by default
|
2023-10-17 02:15:24 +11:00
|
|
|
FavouriteService.new.call(bob.account, first_status)
|
|
|
|
FavouriteService.new.call(tom.account, first_status)
|
|
|
|
FollowService.new.call(bob.account, user.account)
|
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
|
|
|
|
|
|
|
|
context 'with no options' do
|
|
|
|
it 'returns expected notification types', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body.size).to eq 5
|
2024-07-15 19:29:57 +10:00
|
|
|
expect(body_json_types).to include('reblog', 'mention', 'favourite', 'follow')
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body.any? { |x| x[:filtered] }).to be false
|
2024-07-15 19:29:57 +10:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with include_filtered' do
|
|
|
|
let(:params) { { include_filtered: true } }
|
|
|
|
|
|
|
|
it 'returns expected notification types, including filtered notifications' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body.size).to eq 6
|
2024-07-15 19:29:57 +10:00
|
|
|
expect(body_json_types).to include('reblog', 'mention', 'favourite', 'follow')
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body.any? { |x| x[:filtered] }).to be true
|
2023-10-17 02:15:24 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with account_id param' do
|
|
|
|
let(:params) { { account_id: tom.account.id } }
|
|
|
|
|
|
|
|
it 'returns only notifications from specified user', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(body_json_account_ids.uniq).to eq [tom.account.id.to_s]
|
|
|
|
end
|
|
|
|
|
|
|
|
def body_json_account_ids
|
2024-09-06 19:58:46 +10:00
|
|
|
response.parsed_body.map { |x| x[:account][:id] }
|
2023-10-17 02:15:24 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with invalid account_id param' do
|
|
|
|
let(:params) { { account_id: 'foo' } }
|
|
|
|
|
|
|
|
it 'returns nothing', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body.size).to eq 0
|
2023-10-17 02:15:24 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with exclude_types param' do
|
|
|
|
let(:params) { { exclude_types: %w(mention) } }
|
|
|
|
|
|
|
|
it 'returns everything but excluded type', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body.size).to_not eq 0
|
2023-10-17 02:15:24 +11:00
|
|
|
expect(body_json_types.uniq).to_not include 'mention'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with types param' do
|
|
|
|
let(:params) { { types: %w(mention) } }
|
|
|
|
|
|
|
|
it 'returns only requested type', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect(body_json_types.uniq).to eq ['mention']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'with limit param' do
|
|
|
|
let(:params) { { limit: 3 } }
|
|
|
|
|
|
|
|
it 'returns the requested number of notifications paginated', :aggregate_failures do
|
|
|
|
subject
|
|
|
|
|
2024-08-20 00:53:54 +10:00
|
|
|
notifications = user.account.notifications.browserable.order(id: :asc)
|
2023-10-17 02:15:24 +11:00
|
|
|
|
2024-09-06 19:58:46 +10:00
|
|
|
expect(response.parsed_body.size)
|
2024-03-15 21:17:45 +11:00
|
|
|
.to eq(params[:limit])
|
|
|
|
|
|
|
|
expect(response)
|
|
|
|
.to include_pagination_headers(
|
|
|
|
prev: api_v1_notifications_url(limit: params[:limit], min_id: notifications.last.id),
|
|
|
|
next: api_v1_notifications_url(limit: params[:limit], max_id: notifications[2].id)
|
|
|
|
)
|
2023-10-17 02:15:24 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def body_json_types
|
2024-09-06 19:58:46 +10:00
|
|
|
response.parsed_body.pluck(:type)
|
2023-10-17 02:15:24 +11:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'GET /api/v1/notifications/:id' do
|
|
|
|
subject do
|
|
|
|
get "/api/v1/notifications/#{notification.id}", headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
let(:notification) { Fabricate(:notification, account: user.account) }
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'write write:notifications'
|
|
|
|
|
|
|
|
it 'returns http success' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when notification belongs to someone else' do
|
|
|
|
let(:notification) { Fabricate(:notification) }
|
|
|
|
|
|
|
|
it 'returns http not found' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/notifications/:id/dismiss' do
|
|
|
|
subject do
|
|
|
|
post "/api/v1/notifications/#{notification.id}/dismiss", headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
let!(:notification) { Fabricate(:notification, account: user.account) }
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
|
|
|
|
|
|
|
|
it 'destroys the notification' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
expect { notification.reload }.to raise_error(ActiveRecord::RecordNotFound)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'when notification belongs to someone else' do
|
|
|
|
let(:notification) { Fabricate(:notification) }
|
|
|
|
|
|
|
|
it 'returns http not found' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(response).to have_http_status(404)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
describe 'POST /api/v1/notifications/clear' do
|
|
|
|
subject do
|
|
|
|
post '/api/v1/notifications/clear', headers: headers
|
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2023-11-22 00:05:59 +11:00
|
|
|
Fabricate(:notification, account: user.account)
|
2023-10-17 02:15:24 +11:00
|
|
|
end
|
|
|
|
|
|
|
|
it_behaves_like 'forbidden for wrong scope', 'read read:notifications'
|
|
|
|
|
|
|
|
it 'clears notifications for the account' do
|
|
|
|
subject
|
|
|
|
|
|
|
|
expect(user.account.reload.notifications).to be_empty
|
|
|
|
expect(response).to have_http_status(200)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|