1
0
Fork 0
forked from gitea/nas

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

This commit is contained in:
KMY 2024-09-18 08:31:34 +09:00
commit 4ce35dd837
188 changed files with 1994 additions and 980 deletions

View file

@ -2,6 +2,8 @@
Fabricator(:list_account) do
list
account
before_create { |list_account, _| list_account.list.account.follow!(account) }
initialize_with do
resolved_class.new(list: list, account: list.account)
end
end

View file

@ -10,10 +10,39 @@ RSpec.describe Account do
let(:bob) { Fabricate(:account, username: 'bob') }
describe '#suspended_locally?' do
context 'when the account is not suspended' do
it 'returns false' do
expect(subject.suspended_locally?).to be false
end
end
context 'when the account is suspended locally' do
before do
subject.update!(suspended_at: 1.day.ago, suspension_origin: :local)
end
it 'returns true' do
expect(subject.suspended_locally?).to be true
end
end
context 'when the account is suspended remotely' do
before do
subject.update!(suspended_at: 1.day.ago, suspension_origin: :remote)
end
it 'returns false' do
expect(subject.suspended_locally?).to be false
end
end
end
describe '#suspend!' do
it 'marks the account as suspended and creates a deletion request' do
expect { subject.suspend! }
.to change(subject, :suspended?).from(false).to(true)
.and change(subject, :suspended_locally?).from(false).to(true)
.and(change { AccountDeletionRequest.exists?(account: subject) }.from(false).to(true))
end
@ -986,6 +1015,34 @@ RSpec.describe Account do
end
end
describe '#attribution_domains_as_text=' do
subject { Fabricate(:account) }
it 'sets attribution_domains accordingly' do
subject.attribution_domains_as_text = "hoge.com\nexample.com"
expect(subject.attribution_domains).to contain_exactly('hoge.com', 'example.com')
end
it 'strips leading "*."' do
subject.attribution_domains_as_text = "hoge.com\n*.example.com"
expect(subject.attribution_domains).to contain_exactly('hoge.com', 'example.com')
end
it 'strips the protocol if present' do
subject.attribution_domains_as_text = "http://hoge.com\nhttps://example.com"
expect(subject.attribution_domains).to contain_exactly('hoge.com', 'example.com')
end
it 'strips a combination of leading "*." and protocol' do
subject.attribution_domains_as_text = "http://*.hoge.com\nhttps://*.example.com"
expect(subject.attribution_domains).to contain_exactly('hoge.com', 'example.com')
end
end
describe 'Normalizations' do
describe 'username' do
it { is_expected.to normalize(:username).from(" \u3000bob \t \u00a0 \n ").to('bob') }

View file

@ -0,0 +1,43 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Bookmark do
describe 'Associations' do
it { is_expected.to belong_to(:account).required }
it { is_expected.to belong_to(:status).required }
end
describe 'Validations' do
subject { Fabricate.build :bookmark }
it { is_expected.to validate_uniqueness_of(:status_id).scoped_to(:account_id) }
end
describe 'Callbacks' do
describe 'reblog statuses' do
context 'when status is not a reblog' do
let(:status) { Fabricate :status }
it 'keeps status set to assigned value' do
bookmark = Fabricate.build :bookmark, status: status
expect { bookmark.valid? }
.to_not change(bookmark, :status)
end
end
context 'when status is a reblog' do
let(:original) { Fabricate :status }
let(:status) { Fabricate :status, reblog: original }
it 'keeps status set to assigned value' do
bookmark = Fabricate.build :bookmark, status: status
expect { bookmark.valid? }
.to change(bookmark, :status).to(original)
end
end
end
end
end

View file

@ -130,4 +130,13 @@ RSpec.describe CustomEmoji, :attachment_processing do
it { is_expected.to normalize(:domain).from(nil).to(nil) }
end
end
describe 'Validations' do
subject { Fabricate.build :custom_emoji }
it { is_expected.to validate_uniqueness_of(:shortcode).scoped_to(:domain) }
it { is_expected.to validate_length_of(:shortcode).is_at_least(described_class::MINIMUM_SHORTCODE_SIZE) }
it { is_expected.to allow_values('cats').for(:shortcode) }
it { is_expected.to_not allow_values('@#$@#$', 'X').for(:shortcode) }
end
end

View file

@ -0,0 +1,46 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe ListAccount do
describe 'Callbacks to set follows' do
context 'when list owner follows account' do
let!(:follow) { Fabricate :follow }
let(:list) { Fabricate :list, account: follow.account }
it 'finds and sets the follow with the list account' do
list_account = Fabricate :list_account, list: list, account: follow.target_account
expect(list_account)
.to have_attributes(
follow: eq(follow),
follow_request: be_nil
)
end
end
context 'when list owner has a follow request for account' do
let!(:follow_request) { Fabricate :follow_request }
let(:list) { Fabricate :list, account: follow_request.account }
it 'finds and sets the follow request with the list account' do
list_account = Fabricate :list_account, list: list, account: follow_request.target_account
expect(list_account)
.to have_attributes(
follow: be_nil,
follow_request: eq(follow_request)
)
end
end
context 'when list owner is the account' do
it 'does not set follow or follow request' do
list_account = Fabricate :list_account
expect(list_account)
.to have_attributes(
follow: be_nil,
follow_request: be_nil
)
end
end
end
end

View file

@ -3,6 +3,12 @@
require 'rails_helper'
RSpec.describe PreviewCard do
describe 'file size limit', :attachment_processing do
it 'is set differently whether vips is enabled or not' do
expect(described_class::LIMIT).to eq(Rails.configuration.x.use_vips ? 8.megabytes : 2.megabytes)
end
end
describe 'validations' do
describe 'urls' do
it 'allows http schemes' do

View file

@ -8,9 +8,9 @@ RSpec.describe 'Accounts in grouped notifications' do
let(:scopes) { 'read:notifications write:notifications' }
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'GET /api/v2_alpha/notifications/:group_key/accounts', :inline_jobs do
describe 'GET /api/v2/notifications/:group_key/accounts', :inline_jobs do
subject do
get "/api/v2_alpha/notifications/#{user.account.notifications.first.group_key}/accounts", headers: headers, params: params
get "/api/v2/notifications/#{user.account.notifications.first.group_key}/accounts", headers: headers, params: params
end
let(:params) { {} }
@ -71,8 +71,8 @@ RSpec.describe 'Accounts in grouped notifications' do
expect(response)
.to include_pagination_headers(
prev: api_v2_alpha_notification_accounts_url(limit: params[:limit], min_id: notifications.first.id),
next: api_v2_alpha_notification_accounts_url(limit: params[:limit], max_id: notifications.second.id)
prev: api_v2_notification_accounts_url(limit: params[:limit], min_id: notifications.first.id),
next: api_v2_notification_accounts_url(limit: params[:limit], max_id: notifications.second.id)
)
end
end

View file

@ -0,0 +1,343 @@
# 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/notifications/unread_count', :inline_jobs do
subject do
get '/api/v2/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)
expect(response.parsed_body[:count]).to eq 4
end
end
context 'with grouped_types parameter' do
let(:params) { { grouped_types: %w(reblog) } }
it 'returns expected notifications count' do
subject
expect(response).to have_http_status(200)
expect(response.parsed_body[:count]).to eq 5
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)
expect(response.parsed_body[:count]).to eq 2
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)
expect(response.parsed_body[:count]).to eq 3
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)
expect(response.parsed_body[:count]).to eq 2
end
end
context 'when there are more notifications than the limit' do
before do
stub_const('Api::V2::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT', 2)
end
it 'returns a capped value' do
subject
expect(response).to have_http_status(200)
expect(response.parsed_body[:count]).to eq Api::V2::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT
end
end
end
describe 'GET /api/v2/notifications', :inline_jobs do
subject do
get '/api/v2/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)
PostStatusService.new.call(bob.account, text: 'Hello @alice')
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 'when there are no notifications' do
before do
user.account.notifications.destroy_all
end
it 'returns 0 notifications' do
subject
expect(response).to have_http_status(200)
expect(response.parsed_body[:notification_groups]).to eq []
end
end
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 grouped_types param' do
let(:params) { { grouped_types: %w(reblog) } }
it 'returns everything, but does not group favourites' do
subject
expect(response).to have_http_status(200)
expect(response.parsed_body[:notification_groups]).to contain_exactly(
a_hash_including(
type: 'reblog',
sample_account_ids: [bob.account_id.to_s]
),
a_hash_including(
type: 'mention',
sample_account_ids: [bob.account_id.to_s]
),
a_hash_including(
type: 'favourite',
sample_account_ids: [bob.account_id.to_s]
),
a_hash_including(
type: 'favourite',
sample_account_ids: [tom.account_id.to_s]
),
a_hash_including(
type: 'follow',
sample_account_ids: [bob.account_id.to_s]
)
)
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(response.parsed_body.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']
expect(response.parsed_body.dig(:notification_groups, 0, :page_min_id)).to_not be_nil
end
end
context 'with limit param' do
let(:params) { { limit: 3 } }
let(:notifications) { user.account.notifications.reorder(id: :desc) }
it 'returns the requested number of notifications paginated', :aggregate_failures do
subject
expect(response.parsed_body[:notification_groups].size)
.to eq(params[:limit])
expect(response)
.to include_pagination_headers(
prev: api_v2_notifications_url(limit: params[:limit], min_id: notifications.first.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_notifications_url(limit: params[:limit], max_id: notifications[3].id)
)
end
end
context 'with since_id param' do
let(:params) { { since_id: notifications[2].id } }
let(:notifications) { user.account.notifications.reorder(id: :desc) }
it 'returns the requested number of notifications paginated', :aggregate_failures do
subject
expect(response.parsed_body[:notification_groups].size)
.to eq(2)
expect(response)
.to include_pagination_headers(
prev: api_v2_notifications_url(limit: params[:limit], min_id: notifications.first.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_notifications_url(limit: params[:limit], max_id: notifications[1].id)
)
end
end
context 'when requesting stripped-down accounts' do
let(:params) { { expand_accounts: 'partial_avatars' } }
let(:recent_account) { Fabricate(:account) }
before do
FavouriteService.new.call(recent_account, user.account.statuses.first)
end
it 'returns an account in "partial_accounts", with the expected keys', :aggregate_failures do
subject
expect(response).to have_http_status(200)
expect(response.parsed_body[:partial_accounts].size).to be > 0
expect(response.parsed_body[:partial_accounts][0].keys.map(&:to_sym)).to contain_exactly(:acct, :avatar, :avatar_static, :bot, :id, :locked, :url)
expect(response.parsed_body[:partial_accounts].pluck(:id)).to_not include(recent_account.id.to_s)
expect(response.parsed_body[:accounts].pluck(:id)).to include(recent_account.id.to_s)
end
end
context 'when passing an invalid value for "expand_accounts"' do
let(:params) { { expand_accounts: 'unknown_foobar' } }
it 'returns http bad request' do
subject
expect(response).to have_http_status(400)
end
end
def body_json_types
response.parsed_body[:notification_groups].pluck(:type)
end
end
describe 'GET /api/v2/notifications/:id' do
subject do
get "/api/v2/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/notifications/:id/dismiss' do
subject do
post "/api/v2/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/notifications/clear' do
subject do
post '/api/v2/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

@ -1,5 +1,7 @@
# frozen_string_literal: true
# TODO: remove this before 4.3.0-rc1
require 'rails_helper'
RSpec.describe 'Notifications' do
@ -84,14 +86,14 @@ RSpec.describe 'Notifications' do
context 'when there are more notifications than the limit' do
before do
stub_const('Api::V2Alpha::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT', 2)
stub_const('Api::V2::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT', 2)
end
it 'returns a capped value' do
subject
expect(response).to have_http_status(200)
expect(response.parsed_body[:count]).to eq Api::V2Alpha::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT
expect(response.parsed_body[:count]).to eq Api::V2::NotificationsController::DEFAULT_NOTIFICATIONS_COUNT_LIMIT
end
end
end
@ -206,10 +208,10 @@ RSpec.describe 'Notifications' do
expect(response)
.to include_pagination_headers(
prev: api_v2_alpha_notifications_url(limit: params[:limit], min_id: notifications.first.id),
prev: api_v2_notifications_url(limit: params[:limit], min_id: notifications.first.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[3].id)
next: api_v2_notifications_url(limit: params[:limit], max_id: notifications[3].id)
)
end
end
@ -226,10 +228,10 @@ RSpec.describe 'Notifications' do
expect(response)
.to include_pagination_headers(
prev: api_v2_alpha_notifications_url(limit: params[:limit], min_id: notifications.first.id),
prev: api_v2_notifications_url(limit: params[:limit], min_id: notifications.first.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)
next: api_v2_notifications_url(limit: params[:limit], max_id: notifications[1].id)
)
end
end