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

This commit is contained in:
KMY 2024-03-12 09:27:01 +09:00
commit 8e94ed2cec
204 changed files with 5112 additions and 1998 deletions

View file

@ -13,7 +13,7 @@ describe Rack::Attack, type: :request do
# to avoid crossing period boundaries.
# The code Rack::Attack uses to set periods is the following:
# https://github.com/rack/rack-attack/blob/v6.6.1/lib/rack/attack/cache.rb#L64-L66
# https://github.com/rack/rack-attack/blob/v6.7.0/lib/rack/attack/cache.rb#L70-L72
# So we want to minimize `Time.now.to_i % period`
travel_to Time.zone.at(counter_prefix * period.seconds)

View file

@ -1,6 +1,6 @@
inherit_from: ../../.rubocop.yml
# Anonymous controllers in specs cannot access described_class
# https://github.com/rubocop/rubocop-rspec/blob/master/lib/rubocop/cop/rspec/described_class.rb#L36-L39
# Anonymous controllers in specs cannot access `described_class`, explanation:
# https://github.com/rubocop/rubocop-rspec/blob/v2.26.1/lib/rubocop/cop/rspec/described_class.rb#L36-L56
RSpec/DescribedClass:
SkipBlocks: true

View file

@ -6,9 +6,9 @@ Fabrication.manager.load_definitions if Fabrication.manager.empty?
Fabrication.manager.schematics.map(&:first).each do |factory_name|
describe "The #{factory_name} factory" do
it 'is valid' do
factory = Fabricate(factory_name)
expect(factory).to be_valid
it 'is able to create valid records' do
records = Fabricate.times(2, factory_name) # Create multiple of each to uncover uniqueness issues
expect(records).to all(be_valid)
end
end
end

View file

@ -3,5 +3,5 @@
Fabricator(:identity) do
user { Fabricate.build(:user) }
provider 'MyString'
uid 'MyString'
uid { sequence(:uid) { |i| "uid_string_#{i}" } }
end

View file

@ -1,6 +1,6 @@
# frozen_string_literal: true
Fabricator(:relay) do
inbox_url 'https://example.com/inbox'
inbox_url { sequence(:inbox_url) { |i| "https://example.com/inboxes/#{i}" } }
state :idle
end

View file

@ -2,5 +2,5 @@
Fabricator(:site_upload) do
file { Rails.root.join('spec', 'fabricators', 'assets', 'utah_teapot.png').open }
var 'thumbnail'
var { sequence(:var) { |i| "thumbnail_#{i}" } }
end

View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::Metrics::Dimension do
describe '.retrieve' do
subject { described_class.retrieve(reports, start_at, end_at, 5, params) }
let(:start_at) { 2.days.ago }
let(:end_at) { Time.now.utc }
let(:params) { ActionController::Parameters.new({ instance_accounts: [123], instance_languages: ['en'] }) }
let(:reports) { [:instance_accounts, :instance_languages] }
it 'returns instances of provided classes' do
expect(subject)
.to contain_exactly(
be_a(Admin::Metrics::Dimension::InstanceAccountsDimension),
be_a(Admin::Metrics::Dimension::InstanceLanguagesDimension)
)
end
end
end

View file

@ -0,0 +1,22 @@
# frozen_string_literal: true
require 'rails_helper'
describe Admin::Metrics::Measure do
describe '.retrieve' do
subject { described_class.retrieve(reports, start_at, end_at, params) }
let(:start_at) { 2.days.ago }
let(:end_at) { Time.now.utc }
let(:params) { ActionController::Parameters.new({ instance_accounts: [123], instance_followers: [123] }) }
let(:reports) { [:instance_accounts, :instance_followers] }
it 'returns instances of provided classes' do
expect(subject)
.to contain_exactly(
be_a(Admin::Metrics::Measure::InstanceAccountsMeasure),
be_a(Admin::Metrics::Measure::InstanceFollowersMeasure)
)
end
end
end

View file

@ -252,6 +252,24 @@ describe Account::Interactions do
end
end
describe '#block_idna_domain!' do
subject do
[
account.block_domain!(idna_domain),
account.block_domain!(punycode_domain),
]
end
let(:idna_domain) { '대한민국.한국' }
let(:punycode_domain) { 'xn--3e0bs9hfvinn1a.xn--3e0b707e' }
it 'creates single AccountDomainBlock' do
expect do
expect(subject).to all(be_a AccountDomainBlock)
end.to change { account.domain_blocks.count }.by 1
end
end
describe '#unfollow!' do
subject { account.unfollow!(target_account) }
@ -347,6 +365,28 @@ describe Account::Interactions do
end
end
describe '#unblock_idna_domain!' do
subject { account.unblock_domain!(punycode_domain) }
let(:idna_domain) { '대한민국.한국' }
let(:punycode_domain) { 'xn--3e0bs9hfvinn1a.xn--3e0b707e' }
context 'when blocking the domain' do
it 'returns destroyed AccountDomainBlock' do
account_domain_block = Fabricate(:account_domain_block, domain: idna_domain)
account.domain_blocks << account_domain_block
expect(subject).to be_a AccountDomainBlock
expect(subject).to be_destroyed
end
end
context 'when unblocking idna domain' do
it 'returns nil' do
expect(subject).to be_nil
end
end
end
describe '#following?' do
subject { account.following?(target_account) }

View file

@ -9,7 +9,7 @@ RSpec.describe NotificationPolicy do
let(:sender) { Fabricate(:account) }
before do
Fabricate.times(2, :notification, account: subject.account, activity: Fabricate(:status, account: sender))
Fabricate.times(2, :notification, account: subject.account, activity: Fabricate(:status, account: sender), filtered: true)
Fabricate(:notification_request, account: subject.account, from_account: sender)
subject.summarize!
end

View file

@ -10,7 +10,7 @@ RSpec.describe NotificationRequest do
context 'when there are remaining notifications' do
before do
Fabricate(:notification, account: subject.account, activity: Fabricate(:status, account: subject.from_account))
Fabricate(:notification, account: subject.account, activity: Fabricate(:status, account: subject.from_account), filtered: true)
subject.reconsider_existence!
end

View file

@ -37,66 +37,88 @@ describe 'API V1 Push Subscriptions' do
let(:headers) { { 'Authorization' => "Bearer #{token.token}" } }
describe 'POST /api/v1/push/subscription' do
before do
post '/api/v1/push/subscription', params: create_payload, headers: headers
end
subject { post '/api/v1/push/subscription', params: create_payload, headers: headers }
it 'saves push subscriptions' do
push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
it 'saves push subscriptions and returns expected JSON' do
subject
expect(push_subscription.endpoint).to eq(create_payload[:subscription][:endpoint])
expect(push_subscription.key_p256dh).to eq(create_payload[:subscription][:keys][:p256dh])
expect(push_subscription.key_auth).to eq(create_payload[:subscription][:keys][:auth])
expect(push_subscription.user_id).to eq user.id
expect(push_subscription.access_token_id).to eq token.id
end
expect(endpoint_push_subscription)
.to have_attributes(
endpoint: eq(create_payload[:subscription][:endpoint]),
key_p256dh: eq(create_payload[:subscription][:keys][:p256dh]),
key_auth: eq(create_payload[:subscription][:keys][:auth]),
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)
.to include(
{ endpoint: create_payload[:subscription][:endpoint], alerts: {}, policy: 'all' }
)
end
it 'replaces old subscription on repeat calls' do
2.times { subject }
expect(endpoint_push_subscriptions.count)
.to eq(1)
end
end
describe 'PUT /api/v1/push/subscription' do
before do
post '/api/v1/push/subscription', params: create_payload, headers: headers
put '/api/v1/push/subscription', params: alerts_payload, headers: headers
end
subject { put '/api/v1/push/subscription', params: alerts_payload, headers: headers }
it 'changes alert settings' do
push_subscription = Web::PushSubscription.find_by(endpoint: create_payload[:subscription][:endpoint])
before { create_subscription_with_token }
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|
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
it 'returns the expected JSON' do
expect(body_as_json.with_indifferent_access)
.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
describe 'DELETE /api/v1/push/subscription' do
before do
post '/api/v1/push/subscription', params: create_payload, headers: headers
delete '/api/v1/push/subscription', headers: headers
end
subject { delete '/api/v1/push/subscription', headers: headers }
before { create_subscription_with_token }
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
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