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

This commit is contained in:
KMY 2024-11-26 12:56:31 +09:00
commit 8a075ba4c6
303 changed files with 7495 additions and 4498 deletions

View file

@ -21,18 +21,27 @@ RSpec.describe AnnualReport::CommonlyInteractedWithAccounts do
let(:account) { Fabricate :account }
let(:other_account) { Fabricate :account }
let(:most_other_account) { Fabricate :account }
before do
_other = Fabricate :status
Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: other_account).id
Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: other_account).id
Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: most_other_account).id
Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: most_other_account).id
Fabricate :status, account: account, reply: true, in_reply_to_id: Fabricate(:status, account: most_other_account).id
end
it 'builds a report for an account' do
expect(subject.generate)
.to include(
commonly_interacted_with_accounts: contain_exactly(
include(account_id: other_account.id.to_s, count: 2)
commonly_interacted_with_accounts: eq(
[
{ account_id: most_other_account.id.to_s, count: 3 },
{ account_id: other_account.id.to_s, count: 2 },
]
)
)
end

View file

@ -21,18 +21,26 @@ RSpec.describe AnnualReport::MostRebloggedAccounts do
let(:account) { Fabricate :account }
let(:other_account) { Fabricate :account }
let(:most_other_account) { Fabricate :account }
before do
_other = Fabricate :status
Fabricate :status, account: account, reblog: Fabricate(:status, account: other_account)
Fabricate :status, account: account, reblog: Fabricate(:status, account: other_account)
Fabricate :status, account: account, reblog: Fabricate(:status, account: most_other_account)
Fabricate :status, account: account, reblog: Fabricate(:status, account: most_other_account)
Fabricate :status, account: account, reblog: Fabricate(:status, account: most_other_account)
end
it 'builds a report for an account' do
expect(subject.generate)
.to include(
most_reblogged_accounts: contain_exactly(
include(account_id: other_account.id.to_s, count: 2)
most_reblogged_accounts: eq(
[
{ account_id: most_other_account.id.to_s, count: 3 },
{ account_id: other_account.id.to_s, count: 2 },
]
)
)
end

View file

@ -20,18 +20,23 @@ RSpec.describe AnnualReport::MostUsedApps do
context 'with an active account' do
let(:account) { Fabricate :account }
let(:application) { Fabricate :application }
let(:application) { Fabricate :application, name: 'App' }
let(:most_application) { Fabricate :application, name: 'Most App' }
before do
_other = Fabricate :status
Fabricate.times 2, :status, account: account, application: application
Fabricate.times 3, :status, account: account, application: most_application
end
it 'builds a report for an account' do
expect(subject.generate)
.to include(
most_used_apps: contain_exactly(
include(name: application.name, count: 2)
most_used_apps: eq(
[
{ name: most_application.name, count: 3 },
{ name: application.name, count: 2 },
]
)
)
end

View file

@ -21,20 +21,31 @@ RSpec.describe AnnualReport::TopHashtags do
let(:account) { Fabricate :account }
let(:tag) { Fabricate :tag }
let(:most_tag) { Fabricate :tag }
before do
_other = Fabricate :status
first = Fabricate :status, account: account
first.tags << tag
first.tags << most_tag
last = Fabricate :status, account: account
last.tags << tag
last.tags << most_tag
middle = Fabricate :status, account: account
middle.tags << most_tag
end
it 'builds a report for an account' do
expect(subject.generate)
.to include(
top_hashtags: contain_exactly(
include(name: tag.name, count: 2)
top_hashtags: eq(
[
{ name: most_tag.name, count: 3 },
{ name: tag.name, count: 2 },
]
)
)
end

View file

@ -49,7 +49,8 @@ RSpec.describe LinkDetailsExtractor do
<html lang="en">
<head>
<title>Man bites dog</title>
<meta name="description" content="A dog&#39;s tale">
<meta name="descripTION" content="A dog&#39;s tale">
<link rel="pretty IcoN" href="/favicon.ico">
</head>
</html>
HTML
@ -59,7 +60,8 @@ RSpec.describe LinkDetailsExtractor do
.to have_attributes(
title: eq('Man bites dog'),
description: eq("A dog's tale"),
language: eq('en')
language: eq('en'),
icon: eq('https://example.com/favicon.ico')
)
end
end
@ -256,7 +258,7 @@ RSpec.describe LinkDetailsExtractor do
<head>
<meta property="og:url" content="https://example.com/dog.html">
<meta property="og:title" content="Man bites dog">
<meta property="og:description" content="A dog's tale">
<meta property="OG:description" content="A dog's tale">
<meta property="article:published_time" content="2022-01-31T19:53:00+00:00">
<meta property="og:author" content="Charlie Brown">
<meta property="og:locale" content="en">

View file

@ -30,7 +30,7 @@ RSpec.describe FollowRequest do
follow_request.authorize!
expect(account).to have_received(:follow!).with(target_account, reblogs: true, notify: false, uri: follow_request.uri, languages: nil, bypass_limit: true)
expect(MergeWorker).to have_received(:perform_async).with(target_account.id, account.id)
expect(MergeWorker).to have_received(:perform_async).with(target_account.id, account.id, 'home')
expect(follow_request).to have_received(:destroy!)
end

View file

@ -0,0 +1,17 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe LoginActivity do
include_examples 'BrowserDetection'
describe 'Associations' do
it { is_expected.to belong_to(:user).required }
end
describe 'Validations' do
subject { Fabricate.build :login_activity }
it { is_expected.to define_enum_for(:authentication_method).backed_by_column_of_type(:string) }
end
end

View file

@ -108,15 +108,22 @@ RSpec.describe Report do
let(:report) { Fabricate(:report, target_account_id: target_account.id, status_ids: [status.id], created_at: 3.days.ago, updated_at: 1.day.ago) }
let(:target_account) { Fabricate(:account) }
let(:status) { Fabricate(:status) }
let(:account_warning) { Fabricate(:account_warning, report_id: report.id) }
before do
Fabricate(:action_log, target_type: 'Report', account_id: target_account.id, target_id: report.id, created_at: 2.days.ago)
Fabricate(:action_log, target_type: 'Account', account_id: target_account.id, target_id: report.target_account_id, created_at: 2.days.ago)
Fabricate(:action_log, target_type: 'Status', account_id: target_account.id, target_id: status.id, created_at: 2.days.ago)
Fabricate(:action_log, target_type: 'AccountWarning', account_id: target_account.id, target_id: account_warning.id, created_at: 2.days.ago)
end
it 'returns right logs' do
expect(action_logs.count).to eq 3
it 'returns expected logs' do
expect(action_logs)
.to have_attributes(count: 4)
.and include(have_attributes(target_type: 'Account'))
.and include(have_attributes(target_type: 'AccountWarning'))
.and include(have_attributes(target_type: 'Report'))
.and include(have_attributes(target_type: 'Status'))
end
end

View file

@ -3,39 +3,7 @@
require 'rails_helper'
RSpec.describe SessionActivation do
describe '#detection' do
let(:session_activation) { Fabricate(:session_activation, user_agent: 'Chrome/62.0.3202.89') }
it 'sets a Browser instance as detection' do
expect(session_activation.detection).to be_a Browser::Chrome
end
end
describe '#browser' do
before do
allow(session_activation).to receive(:detection).and_return(detection)
end
let(:detection) { instance_double(Browser::Chrome, id: 1) }
let(:session_activation) { Fabricate(:session_activation) }
it 'returns detection.id' do
expect(session_activation.browser).to be 1
end
end
describe '#platform' do
before do
allow(session_activation).to receive(:detection).and_return(detection)
end
let(:session_activation) { Fabricate(:session_activation) }
let(:detection) { instance_double(Browser::Chrome, platform: instance_double(Browser::Platform, id: 1)) }
it 'returns detection.platform.id' do
expect(session_activation.platform).to be 1
end
end
include_examples 'BrowserDetection'
describe '.active?' do
subject { described_class.active?(id) }

View file

@ -3,9 +3,69 @@
require 'rails_helper'
RSpec.describe UserRole do
subject { described_class.create(name: 'Foo', position: 1) }
describe 'Validations' do
describe 'name' do
context 'when everyone' do
subject { described_class.everyone }
it { is_expected.to_not validate_presence_of(:name) }
end
context 'when not everyone' do
subject { Fabricate.build :user_role }
it { is_expected.to validate_presence_of(:name) }
end
end
describe 'color' do
it { is_expected.to allow_values('#112233', '#aabbcc', '').for(:color) }
it { is_expected.to_not allow_values('x', '112233445566', '#xxyyzz').for(:color) }
end
context 'when current_account is set' do
subject { Fabricate :user_role }
let(:account) { Fabricate :account }
before { subject.current_account = account }
it { is_expected.to_not allow_value(999_999).for(:position).with_message(:elevated) }
it { is_expected.to_not allow_value(999_999).for(:permissions).against(:permissions_as_keys).with_message(:elevated) }
context 'when current_account is changing their own role' do
let(:account) { Fabricate :account, user: Fabricate(:user, role: subject) }
it { is_expected.to_not allow_value(100).for(:permissions).against(:permissions_as_keys).with_message(:own_role) }
it { is_expected.to_not allow_value(100).for(:position).with_message(:own_role) }
end
end
end
describe 'Callback for position' do
context 'when everyone' do
subject { Fabricate.build :user_role, id: described_class::EVERYONE_ROLE_ID }
it 'sets the position to nobody position' do
expect { subject.valid? }
.to change(subject, :position).to(described_class::NOBODY_POSITION)
end
end
context 'when not everyone' do
subject { Fabricate.build :user_role }
it 'does not change the position' do
expect { subject.valid? }
.to_not change(subject, :position)
end
end
end
describe '#can?' do
subject { Fabricate :user_role }
context 'with a single flag' do
it 'returns true if any of them are present' do
subject.permissions = described_class::FLAGS[:manage_reports]
@ -92,6 +152,8 @@ RSpec.describe UserRole do
end
describe '#computed_permissions' do
subject { Fabricate :user_role }
context 'when the role is nobody' do
subject { described_class.nobody }

View file

@ -33,14 +33,12 @@ RSpec.describe User do
end
end
describe 'validations' do
describe 'Associations' do
it { is_expected.to belong_to(:account).required }
end
it 'is invalid without a valid email' do
user = Fabricate.build(:user, email: 'john@')
user.valid?
expect(user).to model_have_error_on_field(:email)
end
describe 'Validations' do
it { is_expected.to_not allow_value('john@').for(:email) }
it 'is valid with an invalid e-mail that has already been saved' do
user = Fabricate.build(:user, email: 'invalid-email')
@ -48,11 +46,7 @@ RSpec.describe User do
expect(user.valid?).to be true
end
it 'is valid with a localhost e-mail address' do
user = Fabricate.build(:user, email: 'admin@localhost')
user.valid?
expect(user.valid?).to be true
end
it { is_expected.to allow_value('admin@localhost').for(:email) }
end
describe 'Normalizations' do
@ -183,6 +177,39 @@ RSpec.describe User do
end
end
describe '#update_sign_in!' do
context 'with an existing user' do
let!(:user) { Fabricate :user, last_sign_in_at: 10.days.ago, current_sign_in_at: 1.hour.ago, sign_in_count: 123 }
context 'with new sign in false' do
it 'updates timestamps but not counts' do
expect { user.update_sign_in!(new_sign_in: false) }
.to change(user, :last_sign_in_at)
.and change(user, :current_sign_in_at)
.and not_change(user, :sign_in_count)
end
end
context 'with new sign in true' do
it 'updates timestamps and counts' do
expect { user.update_sign_in!(new_sign_in: true) }
.to change(user, :last_sign_in_at)
.and change(user, :current_sign_in_at)
.and change(user, :sign_in_count).by(1)
end
end
end
context 'with a new user' do
let(:user) { Fabricate.build :user }
it 'does not persist the user' do
expect { user.update_sign_in! }
.to_not change(user, :persisted?).from(false)
end
end
end
describe '#confirmed?' do
it 'returns true when a confirmed_at is set' do
user = Fabricate.build(:user, confirmed_at: Time.now.utc)

View file

@ -6,20 +6,28 @@ RSpec.describe Webhook do
let(:webhook) { Fabricate(:webhook) }
describe 'Validations' do
subject { Fabricate.build :webhook }
it { is_expected.to validate_presence_of(:events) }
it 'requires non-empty events value' do
record = described_class.new(events: [])
record.valid?
it { is_expected.to_not allow_values([], %w(account.invalid)).for(:events) }
expect(record).to model_have_error_on_field(:events)
end
it { is_expected.to_not allow_values('{{account }').for(:template) }
it 'requires valid events value from EVENTS' do
record = described_class.new(events: ['account.invalid'])
record.valid?
context 'when current_account is assigned' do
subject { Fabricate.build :webhook, current_account: account }
expect(record).to model_have_error_on_field(:events)
context 'with account that has permissions' do
let(:account) { Fabricate(:user, role: UserRole.find_by(name: 'Admin')).account }
it { is_expected.to allow_values(%w(account.created)).for(:events) }
end
context 'with account lacking permissions' do
let(:account) { Fabricate :account }
it { is_expected.to_not allow_values(%w(account.created)).for(:events) }
end
end
end
@ -29,6 +37,96 @@ RSpec.describe Webhook do
end
end
describe 'Callbacks' do
describe 'Generating a secret' do
context 'when secret exists already' do
subject { described_class.new(secret: 'secret') }
it 'does not override' do
expect { subject.valid? }
.to_not change(subject, :secret)
end
end
context 'when secret does not exist' do
subject { described_class.new(secret: nil) }
it 'does not override' do
expect { subject.valid? }
.to change(subject, :secret)
end
end
end
end
describe '.permission_for_event' do
subject { described_class.permission_for_event(event) }
context 'with a nil value' do
let(:event) { nil }
it { is_expected.to be_nil }
end
context 'with an account approved event' do
let(:event) { 'account.approved' }
it { is_expected.to eq(:manage_users) }
end
context 'with an account created event' do
let(:event) { 'account.created' }
it { is_expected.to eq(:manage_users) }
end
context 'with an account updated event' do
let(:event) { 'account.updated' }
it { is_expected.to eq(:manage_users) }
end
context 'with an report created event' do
let(:event) { 'report.created' }
it { is_expected.to eq(:manage_reports) }
end
context 'with an report updated event' do
let(:event) { 'report.updated' }
it { is_expected.to eq(:manage_reports) }
end
context 'with an status created event' do
let(:event) { 'status.created' }
it { is_expected.to eq(:view_devops) }
end
context 'with an status updated event' do
let(:event) { 'status.updated' }
it { is_expected.to eq(:view_devops) }
end
end
describe '#required_permissions' do
subject { described_class.new(events:).required_permissions }
context 'with empty events' do
let(:events) { [] }
it { is_expected.to eq([]) }
end
context 'with multiple event types' do
let(:events) { %w(account.created account.updated status.created) }
it { is_expected.to eq %i(manage_users view_devops) }
end
end
describe '#rotate_secret!' do
it 'changes the secret' do
expect { webhook.rotate_secret! }

View file

@ -27,7 +27,7 @@ RSpec.describe 'The /.well-known/oauth-authorization-server request' do
response_modes_supported: Doorkeeper.configuration.authorization_response_flows.flat_map(&:response_mode_matches).uniq,
token_endpoint_auth_methods_supported: %w(client_secret_basic client_secret_post),
grant_types_supported: grant_types_supported,
code_challenge_methods_supported: ['S256'],
code_challenge_methods_supported: Doorkeeper.configuration.pkce_code_challenge_methods_supported,
# non-standard extension:
app_registration_endpoint: api_v1_apps_url
)

View file

@ -16,7 +16,7 @@ RSpec.describe UnmuteService do
it 'removes the account mute and sets up a merge' do
expect { subject.call(account, target_account) }
.to remove_account_mute
expect(MergeWorker).to have_enqueued_sidekiq_job(target_account.id, account.id)
expect(MergeWorker).to have_enqueued_sidekiq_job(target_account.id, account.id, 'home')
end
end

View file

@ -46,6 +46,21 @@ RSpec.describe VerifyLinkService do
end
end
context 'when a link contains an <a rel=ME> back' do
let(:html) do
<<~HTML
<!doctype html>
<body>
<a href="#{ActivityPub::TagManager.instance.url_for(account)}" rel=ME>Follow me on Mastodon</a>
</body>
HTML
end
it 'marks the field as verified' do
expect(field.verified?).to be true
end
end
context 'when a link contains a <link> back' do
let(:html) do
<<~HTML

View file

@ -0,0 +1,37 @@
# frozen_string_literal: true
RSpec.shared_examples 'BrowserDetection' do
subject { described_class.new(user_agent: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.1 Safari/605.1.15') }
describe '#detection' do
it 'sets a Browser instance as detection' do
expect(subject.detection)
.to be_a(Browser::Safari)
end
end
describe '#browser' do
it 'returns browser name from id' do
expect(subject.browser)
.to eq(:safari)
end
end
describe '#platform' do
it 'returns detected platform' do
expect(subject.platform)
.to eq(:mac)
end
end
describe 'Callbacks' do
describe 'populating the user_agent value' do
subject { Fabricate.build described_class.name.underscore.to_sym, user_agent: nil }
it 'changes nil to empty string' do
expect { subject.save }
.to change(subject, :user_agent).from(nil).to('')
end
end
end
end

View file

@ -115,6 +115,8 @@ RSpec.describe 'Using OAuth from an external app' do
subject
within '.form-container .flash-message' do
# FIXME: Replace with doorkeeper.errors.messages.invalid_code_challenge_method.one for Doorkeeper > 5.8.0
# see: https://github.com/doorkeeper-gem/doorkeeper/pull/1747
expect(page).to have_content(I18n.t('doorkeeper.errors.messages.invalid_code_challenge_method'))
end
end

View file

@ -12,12 +12,26 @@ RSpec.describe PublishScheduledStatusWorker do
subject.perform(scheduled_status.id)
end
it 'creates a status' do
expect(scheduled_status.account.statuses.first.text).to eq 'Hello world, future!'
context 'when the account is not disabled' do
it 'creates a status' do
expect(scheduled_status.account.statuses.first.text).to eq 'Hello world, future!'
end
it 'removes the scheduled status' do
expect(ScheduledStatus.find_by(id: scheduled_status.id)).to be_nil
end
end
it 'removes the scheduled status' do
expect(ScheduledStatus.find_by(id: scheduled_status.id)).to be_nil
context 'when the account is disabled' do
let(:scheduled_status) { Fabricate(:scheduled_status, account: Fabricate(:account, user: Fabricate(:user, disabled: true))) }
it 'does not create a status' do
expect(Status.count).to eq 0
end
it 'removes the scheduled status' do
expect(ScheduledStatus.find_by(id: scheduled_status.id)).to be_nil
end
end
end
end