Merge remote-tracking branch 'parent/main' into upstream-20240604

This commit is contained in:
KMY 2024-06-04 08:02:05 +09:00
commit 8927b1444b
89 changed files with 1132 additions and 458 deletions

View file

@ -1,7 +1,12 @@
# frozen_string_literal: true
Fabricator(:user) do
account { Fabricate.build(:account, user: nil) }
account do |attrs|
Fabricate.build(
:account,
attrs.fetch(:account_attributes, {}).merge(user: nil)
)
end
email { sequence(:email) { |i| "#{i}#{Faker::Internet.email}" } }
password '123456789'
confirmed_at { Time.zone.now }

View file

@ -151,6 +151,66 @@ RSpec.describe Notification do
end
end
describe '.paginate_groups_by_max_id' do
let(:account) { Fabricate(:account) }
let!(:notifications) do
['group-1', 'group-1', nil, 'group-2', nil, 'group-1', 'group-2', 'group-1']
.map { |group_key| Fabricate(:notification, account: account, group_key: group_key) }
end
context 'without since_id or max_id' do
it 'returns the most recent notifications, only keeping one notification per group' do
expect(described_class.without_suspended.paginate_groups_by_max_id(4).pluck(:id))
.to eq [notifications[7], notifications[6], notifications[4], notifications[2]].pluck(:id)
end
end
context 'with since_id' do
it 'returns the most recent notifications, only keeping one notification per group' do
expect(described_class.without_suspended.paginate_groups_by_max_id(4, since_id: notifications[4].id).pluck(:id))
.to eq [notifications[7], notifications[6]].pluck(:id)
end
end
context 'with max_id' do
it 'returns the most recent notifications after max_id, only keeping one notification per group' do
expect(described_class.without_suspended.paginate_groups_by_max_id(4, max_id: notifications[7].id).pluck(:id))
.to eq [notifications[6], notifications[5], notifications[4], notifications[2]].pluck(:id)
end
end
end
describe '.paginate_groups_by_min_id' do
let(:account) { Fabricate(:account) }
let!(:notifications) do
['group-1', 'group-1', nil, 'group-2', nil, 'group-1', 'group-2', 'group-1']
.map { |group_key| Fabricate(:notification, account: account, group_key: group_key) }
end
context 'without min_id or max_id' do
it 'returns the oldest notifications, only keeping one notification per group' do
expect(described_class.without_suspended.paginate_groups_by_min_id(4).pluck(:id))
.to eq [notifications[0], notifications[2], notifications[3], notifications[4]].pluck(:id)
end
end
context 'with max_id' do
it 'returns the oldest notifications, stopping at max_id, only keeping one notification per group' do
expect(described_class.without_suspended.paginate_groups_by_min_id(4, max_id: notifications[4].id).pluck(:id))
.to eq [notifications[0], notifications[2], notifications[3]].pluck(:id)
end
end
context 'with min_id' do
it 'returns the most oldest notifications after min_id, only keeping one notification per group' do
expect(described_class.without_suspended.paginate_groups_by_min_id(4, min_id: notifications[0].id).pluck(:id))
.to eq [notifications[1], notifications[2], notifications[3], notifications[4]].pluck(:id)
end
end
end
describe '.preload_cache_collection_target_statuses' do
subject do
described_class.preload_cache_collection_target_statuses(notifications) do |target_statuses|

View file

@ -0,0 +1,161 @@
# 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}" } }
describe 'GET /api/v2_alpha/notifications', :sidekiq_inline do
subject do
get '/api/v2_alpha/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)
mentioning_status = PostStatusService.new.call(bob.account, text: 'Hello @alice')
mentioning_status.mentions.first
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)
expect(body_json_types).to include('reblog', 'mention', 'favourite', 'follow')
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)
expect(body_as_json.size).to_not eq 0
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
notifications = user.account.notifications
expect(body_as_json.size)
.to eq(params[:limit])
expect(response)
.to include_pagination_headers(
prev: api_v2_alpha_notifications_url(limit: params[:limit], min_id: notifications.last.id),
# TODO: one downside of the current approach is that we return the first ID matching the group,
# not the last that has been skipped, so pagination is very likely to give overlap
next: api_v2_alpha_notifications_url(limit: params[:limit], max_id: notifications[1].id)
)
end
end
def body_json_types
body_as_json.pluck(:type)
end
end
describe 'GET /api/v2_alpha/notifications/:id' do
subject do
get "/api/v2_alpha/notifications/#{notification.group_key}", headers: headers
end
let(:notification) { Fabricate(:notification, account: user.account, group_key: 'foobar') }
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, group_key: 'foobar') }
it 'returns http not found' do
subject
expect(response).to have_http_status(404)
end
end
end
describe 'POST /api/v2_alpha/notifications/:id/dismiss' do
subject do
post "/api/v2_alpha/notifications/#{notification.group_key}/dismiss", headers: headers
end
let!(:notification) { Fabricate(:notification, account: user.account, group_key: 'foobar') }
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/v2_alpha/notifications/clear' do
subject do
post '/api/v2_alpha/notifications/clear', headers: headers
end
before do
Fabricate(:notification, account: user.account)
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

View file

@ -6,7 +6,7 @@ describe 'The /.well-known/oauth-authorization-server request' do
let(:protocol) { ENV.fetch('LOCAL_HTTPS', true) ? :https : :http }
before do
host! ENV.fetch('LOCAL_DOMAIN')
host! Rails.configuration.x.local_domain
end
it 'returns http success with valid JSON response' do

View file

@ -6,7 +6,7 @@ module SignedRequestHelpers
headers ||= {}
headers['Date'] = Time.now.utc.httpdate
headers['Host'] = ENV.fetch('LOCAL_DOMAIN')
headers['Host'] = Rails.configuration.x.local_domain
signed_headers = headers.merge('(request-target)' => "get #{path}").slice('(request-target)', 'Host', 'Date')
key_id = ActivityPub::TagManager.instance.key_uri_for(sign_with)

View file

@ -7,7 +7,7 @@ describe 'Profile' do
subject { page }
let(:local_domain) { ENV['LOCAL_DOMAIN'] }
let(:local_domain) { Rails.configuration.x.local_domain }
before do
as_a_logged_in_user