Compare commits

..

No commits in common. "2f5d989babe293c44d1baec141579fc666553085" and "9df8594415c5492d314d46fcf425006864d8aef5" have entirely different histories.

16 changed files with 1465 additions and 1360 deletions

View file

@ -111,8 +111,8 @@ group :opentelemetry do
gem 'opentelemetry-instrumentation-http_client', '~> 0.22.3', require: false
gem 'opentelemetry-instrumentation-net_http', '~> 0.22.4', require: false
gem 'opentelemetry-instrumentation-pg', '~> 0.29.0', require: false
gem 'opentelemetry-instrumentation-rack', '~> 0.25.0', require: false
gem 'opentelemetry-instrumentation-rails', '~> 0.32.0', require: false
gem 'opentelemetry-instrumentation-rack', '~> 0.24.1', require: false
gem 'opentelemetry-instrumentation-rails', '~> 0.31.0', require: false
gem 'opentelemetry-instrumentation-redis', '~> 0.25.3', require: false
gem 'opentelemetry-instrumentation-sidekiq', '~> 0.25.2', require: false
gem 'opentelemetry-sdk', '~> 1.4', require: false

View file

@ -504,7 +504,7 @@ GEM
opentelemetry-semantic_conventions
opentelemetry-helpers-sql-obfuscation (0.2.0)
opentelemetry-common (~> 0.21)
opentelemetry-instrumentation-action_mailer (0.2.0)
opentelemetry-instrumentation-action_mailer (0.1.0)
opentelemetry-api (~> 1.0)
opentelemetry-instrumentation-active_support (~> 0.1)
opentelemetry-instrumentation-base (~> 0.22.1)
@ -516,13 +516,13 @@ GEM
opentelemetry-api (~> 1.0)
opentelemetry-instrumentation-active_support (~> 0.1)
opentelemetry-instrumentation-base (~> 0.22.1)
opentelemetry-instrumentation-active_job (0.7.8)
opentelemetry-instrumentation-active_job (0.7.7)
opentelemetry-api (~> 1.0)
opentelemetry-instrumentation-base (~> 0.22.1)
opentelemetry-instrumentation-active_model_serializers (0.20.2)
opentelemetry-api (~> 1.0)
opentelemetry-instrumentation-base (~> 0.22.1)
opentelemetry-instrumentation-active_record (0.8.0)
opentelemetry-instrumentation-active_record (0.7.3)
opentelemetry-api (~> 1.0)
opentelemetry-instrumentation-base (~> 0.22.1)
opentelemetry-instrumentation-active_support (0.6.0)
@ -554,16 +554,16 @@ GEM
opentelemetry-api (~> 1.0)
opentelemetry-helpers-sql-obfuscation
opentelemetry-instrumentation-base (~> 0.22.1)
opentelemetry-instrumentation-rack (0.25.0)
opentelemetry-instrumentation-rack (0.24.6)
opentelemetry-api (~> 1.0)
opentelemetry-instrumentation-base (~> 0.22.1)
opentelemetry-instrumentation-rails (0.32.0)
opentelemetry-instrumentation-rails (0.31.2)
opentelemetry-api (~> 1.0)
opentelemetry-instrumentation-action_mailer (~> 0.2.0)
opentelemetry-instrumentation-action_mailer (~> 0.1.0)
opentelemetry-instrumentation-action_pack (~> 0.9.0)
opentelemetry-instrumentation-action_view (~> 0.7.0)
opentelemetry-instrumentation-active_job (~> 0.7.0)
opentelemetry-instrumentation-active_record (~> 0.8.0)
opentelemetry-instrumentation-active_record (~> 0.7.0)
opentelemetry-instrumentation-active_support (~> 0.6.0)
opentelemetry-instrumentation-base (~> 0.22.1)
opentelemetry-instrumentation-redis (0.25.7)
@ -591,7 +591,7 @@ GEM
parslet (2.0.0)
pastel (0.8.0)
tty-color (~> 0.5)
pg (1.5.9)
pg (1.5.8)
pghero (3.6.1)
activerecord (>= 6.1)
premailer (1.27.0)
@ -992,8 +992,8 @@ DEPENDENCIES
opentelemetry-instrumentation-http_client (~> 0.22.3)
opentelemetry-instrumentation-net_http (~> 0.22.4)
opentelemetry-instrumentation-pg (~> 0.29.0)
opentelemetry-instrumentation-rack (~> 0.25.0)
opentelemetry-instrumentation-rails (~> 0.32.0)
opentelemetry-instrumentation-rack (~> 0.24.1)
opentelemetry-instrumentation-rails (~> 0.31.0)
opentelemetry-instrumentation-redis (~> 0.25.3)
opentelemetry-instrumentation-sidekiq (~> 0.25.2)
opentelemetry-sdk (~> 1.4)

View file

@ -36,14 +36,9 @@ class IpBlock < ApplicationRecord
class << self
def blocked?(remote_ip)
blocked_ips_map = Rails.cache.fetch(CACHE_KEY) { FastIpMap.new(IpBlock.where(severity: :no_access).pluck(:ip)) }
blocked_ips_map.include?(remote_ip)
end
private
def blocked_ips_map
Rails.cache.fetch(CACHE_KEY) { FastIpMap.new(severity_no_access.pluck(:ip)) }
end
end
private

View file

@ -3,8 +3,6 @@
require 'rails_helper'
RSpec.describe FeedManager do
subject { described_class.instance }
before do |example|
unless example.metadata[:skip_stub]
stub_const 'FeedManager::MAX_ITEMS', 10
@ -34,26 +32,26 @@ RSpec.describe FeedManager do
it 'returns false for followee\'s status' do
status = Fabricate(:status, text: 'Hello world', account: alice)
bob.follow!(alice)
expect(subject.filter?(:home, status, bob)).to be false
expect(described_class.instance.filter?(:home, status, bob)).to be false
end
it 'returns false for reblog by followee' do
status = Fabricate(:status, text: 'Hello world', account: jeff)
reblog = Fabricate(:status, reblog: status, account: alice)
bob.follow!(alice)
expect(subject.filter?(:home, reblog, bob)).to be false
expect(described_class.instance.filter?(:home, reblog, bob)).to be false
end
it 'returns true for post from account who blocked me' do
status = Fabricate(:status, text: 'Hello, World', account: alice)
alice.block!(bob)
expect(subject.filter?(:home, status, bob)).to be true
expect(described_class.instance.filter?(:home, status, bob)).to be true
end
it 'returns true for post from blocked account' do
status = Fabricate(:status, text: 'Hello, World', account: alice)
bob.block!(alice)
expect(subject.filter?(:home, status, bob)).to be true
expect(described_class.instance.filter?(:home, status, bob)).to be true
end
it 'returns true for reblog by followee of blocked account' do
@ -61,7 +59,7 @@ RSpec.describe FeedManager do
reblog = Fabricate(:status, reblog: status, account: alice)
bob.follow!(alice)
bob.block!(jeff)
expect(subject.filter?(:home, reblog, bob)).to be true
expect(described_class.instance.filter?(:home, reblog, bob)).to be true
end
it 'returns true for reblog by followee of muted account' do
@ -69,7 +67,7 @@ RSpec.describe FeedManager do
reblog = Fabricate(:status, reblog: status, account: alice)
bob.follow!(alice)
bob.mute!(jeff)
expect(subject.filter?(:home, reblog, bob)).to be true
expect(described_class.instance.filter?(:home, reblog, bob)).to be true
end
it 'returns true for reblog by followee of someone who is blocking recipient' do
@ -77,14 +75,14 @@ RSpec.describe FeedManager do
reblog = Fabricate(:status, reblog: status, account: alice)
bob.follow!(alice)
jeff.block!(bob)
expect(subject.filter?(:home, reblog, bob)).to be true
expect(described_class.instance.filter?(:home, reblog, bob)).to be true
end
it 'returns true for reblog from account with reblogs disabled' do
status = Fabricate(:status, text: 'Hello world', account: jeff)
reblog = Fabricate(:status, reblog: status, account: alice)
bob.follow!(alice, reblogs: false)
expect(subject.filter?(:home, reblog, bob)).to be true
expect(described_class.instance.filter?(:home, reblog, bob)).to be true
end
it 'returns false for reply by followee to another followee' do
@ -92,49 +90,49 @@ RSpec.describe FeedManager do
reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
bob.follow!(alice)
bob.follow!(jeff)
expect(subject.filter?(:home, reply, bob)).to be false
expect(described_class.instance.filter?(:home, reply, bob)).to be false
end
it 'returns false for reply by followee to recipient' do
status = Fabricate(:status, text: 'Hello world', account: bob)
reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
bob.follow!(alice)
expect(subject.filter?(:home, reply, bob)).to be false
expect(described_class.instance.filter?(:home, reply, bob)).to be false
end
it 'returns false for reply by followee to self' do
status = Fabricate(:status, text: 'Hello world', account: alice)
reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
bob.follow!(alice)
expect(subject.filter?(:home, reply, bob)).to be false
expect(described_class.instance.filter?(:home, reply, bob)).to be false
end
it 'returns true for reply by followee to non-followed account' do
status = Fabricate(:status, text: 'Hello world', account: jeff)
reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
bob.follow!(alice)
expect(subject.filter?(:home, reply, bob)).to be true
expect(described_class.instance.filter?(:home, reply, bob)).to be true
end
it 'returns true for the second reply by followee to a non-federated status' do
reply = Fabricate(:status, text: 'Reply 1', reply: true, account: alice)
second_reply = Fabricate(:status, text: 'Reply 2', thread: reply, account: alice)
bob.follow!(alice)
expect(subject.filter?(:home, second_reply, bob)).to be true
expect(described_class.instance.filter?(:home, second_reply, bob)).to be true
end
it 'returns false for status by followee mentioning another account' do
bob.follow!(alice)
jeff.follow!(alice)
status = PostStatusService.new.call(alice, text: 'Hey @jeff')
expect(subject.filter?(:home, status, bob)).to be false
expect(described_class.instance.filter?(:home, status, bob)).to be false
end
it 'returns true for status by followee mentioning blocked account' do
bob.block!(jeff)
bob.follow!(alice)
status = PostStatusService.new.call(alice, text: 'Hey @jeff')
expect(subject.filter?(:home, status, bob)).to be true
expect(described_class.instance.filter?(:home, status, bob)).to be true
end
it 'returns true for reblog of a personally blocked domain' do
@ -142,19 +140,19 @@ RSpec.describe FeedManager do
alice.follow!(jeff)
status = Fabricate(:status, text: 'Hello world', account: bob)
reblog = Fabricate(:status, reblog: status, account: jeff)
expect(subject.filter?(:home, reblog, alice)).to be true
expect(described_class.instance.filter?(:home, reblog, alice)).to be true
end
it 'returns true for German post when follow is set to English only' do
alice.follow!(bob, languages: %w(en))
status = Fabricate(:status, text: 'Hallo Welt', account: bob, language: 'de')
expect(subject.filter?(:home, status, alice)).to be true
expect(described_class.instance.filter?(:home, status, alice)).to be true
end
it 'returns false for German post when follow is set to German' do
alice.follow!(bob, languages: %w(de))
status = Fabricate(:status, text: 'Hallo Welt', account: bob, language: 'de')
expect(subject.filter?(:home, status, alice)).to be false
expect(described_class.instance.filter?(:home, status, alice)).to be false
end
it 'returns true for post from followee on exclusive list' do
@ -163,7 +161,7 @@ RSpec.describe FeedManager do
list.accounts << bob
allow(List).to receive(:where).and_return(list)
status = Fabricate(:status, text: 'I post a lot', account: bob)
expect(subject.filter?(:home, status, alice)).to be true
expect(described_class.instance.filter?(:home, status, alice)).to be true
end
it 'returns true for reblog from followee on exclusive list' do
@ -173,7 +171,7 @@ RSpec.describe FeedManager do
allow(List).to receive(:where).and_return(list)
status = Fabricate(:status, text: 'I post a lot', account: bob)
reblog = Fabricate(:status, reblog: status, account: jeff)
expect(subject.filter?(:home, reblog, alice)).to be true
expect(described_class.instance.filter?(:home, reblog, alice)).to be true
end
it 'returns false for post from followee on non-exclusive list' do
@ -181,7 +179,7 @@ RSpec.describe FeedManager do
alice.follow!(bob)
list.accounts << bob
status = Fabricate(:status, text: 'I post a lot', account: bob)
expect(subject.filter?(:home, status, alice)).to be false
expect(described_class.instance.filter?(:home, status, alice)).to be false
end
it 'returns false for reblog from followee on non-exclusive list' do
@ -190,7 +188,7 @@ RSpec.describe FeedManager do
list.accounts << jeff
status = Fabricate(:status, text: 'I post a lot', account: bob)
reblog = Fabricate(:status, reblog: status, account: jeff)
expect(subject.filter?(:home, reblog, alice)).to be false
expect(described_class.instance.filter?(:home, reblog, alice)).to be false
end
end
@ -198,27 +196,27 @@ RSpec.describe FeedManager do
it 'returns true for status that mentions blocked account' do
bob.block!(jeff)
status = PostStatusService.new.call(alice, text: 'Hey @jeff')
expect(subject.filter?(:mentions, status, bob)).to be true
expect(described_class.instance.filter?(:mentions, status, bob)).to be true
end
it 'returns true for status that replies to a blocked account' do
status = Fabricate(:status, text: 'Hello world', account: jeff)
reply = Fabricate(:status, text: 'Nay', thread: status, account: alice)
bob.block!(jeff)
expect(subject.filter?(:mentions, reply, bob)).to be true
expect(described_class.instance.filter?(:mentions, reply, bob)).to be true
end
it 'returns false for status by limited account who recipient is not following' do
status = Fabricate(:status, text: 'Hello world', account: alice)
alice.silence!
expect(subject.filter?(:mentions, status, bob)).to be false
expect(described_class.instance.filter?(:mentions, status, bob)).to be false
end
it 'returns false for status by followed limited account' do
status = Fabricate(:status, text: 'Hello world', account: alice)
alice.silence!
bob.follow!(alice)
expect(subject.filter?(:mentions, status, bob)).to be false
expect(described_class.instance.filter?(:mentions, status, bob)).to be false
end
end
end
@ -230,7 +228,7 @@ RSpec.describe FeedManager do
members = Array.new(described_class::MAX_ITEMS) { |count| [count, count] }
redis.zadd("feed:home:#{account.id}", members)
subject.push_to_home(account, status)
described_class.instance.push_to_home(account, status)
expect(redis.zcard("feed:home:#{account.id}")).to eq described_class::MAX_ITEMS
end
@ -241,7 +239,7 @@ RSpec.describe FeedManager do
reblogged = Fabricate(:status)
reblog = Fabricate(:status, reblog: reblogged)
expect(subject.push_to_home(account, reblog)).to be true
expect(described_class.instance.push_to_home(account, reblog)).to be true
end
it 'does not save a new reblog of a recent status' do
@ -249,9 +247,9 @@ RSpec.describe FeedManager do
reblogged = Fabricate(:status)
reblog = Fabricate(:status, reblog: reblogged)
subject.push_to_home(account, reblogged)
described_class.instance.push_to_home(account, reblogged)
expect(subject.push_to_home(account, reblog)).to be false
expect(described_class.instance.push_to_home(account, reblog)).to be false
end
it 'saves a new reblog of an old status' do
@ -259,14 +257,14 @@ RSpec.describe FeedManager do
reblogged = Fabricate(:status)
reblog = Fabricate(:status, reblog: reblogged)
subject.push_to_home(account, reblogged)
described_class.instance.push_to_home(account, reblogged)
# Fill the feed with intervening statuses
described_class::REBLOG_FALLOFF.times do
subject.push_to_home(account, Fabricate(:status))
described_class.instance.push_to_home(account, Fabricate(:status))
end
expect(subject.push_to_home(account, reblog)).to be true
expect(described_class.instance.push_to_home(account, reblog)).to be true
end
it 'does not save a new reblog of a recently-reblogged status' do
@ -275,10 +273,10 @@ RSpec.describe FeedManager do
reblogs = Array.new(2) { Fabricate(:status, reblog: reblogged) }
# The first reblog will be accepted
subject.push_to_home(account, reblogs.first)
described_class.instance.push_to_home(account, reblogs.first)
# The second reblog should be ignored
expect(subject.push_to_home(account, reblogs.last)).to be false
expect(described_class.instance.push_to_home(account, reblogs.last)).to be false
end
it 'saves a new reblog of a recently-reblogged status when previous reblog has been deleted' do
@ -287,15 +285,15 @@ RSpec.describe FeedManager do
old_reblog = Fabricate(:status, reblog: reblogged)
# The first reblog should be accepted
expect(subject.push_to_home(account, old_reblog)).to be true
expect(described_class.instance.push_to_home(account, old_reblog)).to be true
# The first reblog should be successfully removed
expect(subject.unpush_from_home(account, old_reblog)).to be true
expect(described_class.instance.unpush_from_home(account, old_reblog)).to be true
reblog = Fabricate(:status, reblog: reblogged)
# The second reblog should be accepted
expect(subject.push_to_home(account, reblog)).to be true
expect(described_class.instance.push_to_home(account, reblog)).to be true
end
it 'does not save a new reblog of a multiply-reblogged-then-unreblogged status' do
@ -304,14 +302,14 @@ RSpec.describe FeedManager do
reblogs = Array.new(3) { Fabricate(:status, reblog: reblogged) }
# Accept the reblogs
subject.push_to_home(account, reblogs[0])
subject.push_to_home(account, reblogs[1])
described_class.instance.push_to_home(account, reblogs[0])
described_class.instance.push_to_home(account, reblogs[1])
# Unreblog the first one
subject.unpush_from_home(account, reblogs[0])
described_class.instance.unpush_from_home(account, reblogs[0])
# The last reblog should still be ignored
expect(subject.push_to_home(account, reblogs.last)).to be false
expect(described_class.instance.push_to_home(account, reblogs.last)).to be false
end
it 'saves a new reblog of a long-ago-reblogged status' do
@ -320,15 +318,15 @@ RSpec.describe FeedManager do
reblogs = Array.new(2) { Fabricate(:status, reblog: reblogged) }
# The first reblog will be accepted
subject.push_to_home(account, reblogs.first)
described_class.instance.push_to_home(account, reblogs.first)
# Fill the feed with intervening statuses
described_class::REBLOG_FALLOFF.times do
subject.push_to_home(account, Fabricate(:status))
described_class.instance.push_to_home(account, Fabricate(:status))
end
# The second reblog should also be accepted
expect(subject.push_to_home(account, reblogs.last)).to be true
expect(described_class.instance.push_to_home(account, reblogs.last)).to be true
end
end
@ -336,9 +334,9 @@ RSpec.describe FeedManager do
account = Fabricate(:account)
reblog = Fabricate(:status)
status = Fabricate(:status, reblog: reblog)
subject.push_to_home(account, status)
described_class.instance.push_to_home(account, status)
expect(subject.push_to_home(account, reblog)).to be false
expect(described_class.instance.push_to_home(account, reblog)).to be false
end
end
@ -361,9 +359,9 @@ RSpec.describe FeedManager do
it "does not push when the given status's reblog is already inserted" do
reblog = Fabricate(:status)
status = Fabricate(:status, reblog: reblog)
subject.push_to_list(list, status)
described_class.instance.push_to_list(list, status)
expect(subject.push_to_list(list, reblog)).to be false
expect(described_class.instance.push_to_list(list, reblog)).to be false
end
context 'when replies policy is set to no replies' do
@ -373,19 +371,19 @@ RSpec.describe FeedManager do
it 'pushes statuses that are not replies' do
status = Fabricate(:status, text: 'Hello world', account: bob)
expect(subject.push_to_list(list, status)).to be true
expect(described_class.instance.push_to_list(list, status)).to be true
end
it 'pushes statuses that are replies to list owner' do
status = Fabricate(:status, text: 'Hello world', account: owner)
reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
expect(subject.push_to_list(list, reply)).to be true
expect(described_class.instance.push_to_list(list, reply)).to be true
end
it 'does not push replies to another member of the list' do
status = Fabricate(:status, text: 'Hello world', account: alice)
reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
expect(subject.push_to_list(list, reply)).to be false
expect(described_class.instance.push_to_list(list, reply)).to be false
end
end
@ -396,25 +394,25 @@ RSpec.describe FeedManager do
it 'pushes statuses that are not replies' do
status = Fabricate(:status, text: 'Hello world', account: bob)
expect(subject.push_to_list(list, status)).to be true
expect(described_class.instance.push_to_list(list, status)).to be true
end
it 'pushes statuses that are replies to list owner' do
status = Fabricate(:status, text: 'Hello world', account: owner)
reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
expect(subject.push_to_list(list, reply)).to be true
expect(described_class.instance.push_to_list(list, reply)).to be true
end
it 'pushes replies to another member of the list' do
status = Fabricate(:status, text: 'Hello world', account: alice)
reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
expect(subject.push_to_list(list, reply)).to be true
expect(described_class.instance.push_to_list(list, reply)).to be true
end
it 'does not push replies to someone not a member of the list' do
status = Fabricate(:status, text: 'Hello world', account: eve)
reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
expect(subject.push_to_list(list, reply)).to be false
expect(described_class.instance.push_to_list(list, reply)).to be false
end
end
@ -425,25 +423,25 @@ RSpec.describe FeedManager do
it 'pushes statuses that are not replies' do
status = Fabricate(:status, text: 'Hello world', account: bob)
expect(subject.push_to_list(list, status)).to be true
expect(described_class.instance.push_to_list(list, status)).to be true
end
it 'pushes statuses that are replies to list owner' do
status = Fabricate(:status, text: 'Hello world', account: owner)
reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
expect(subject.push_to_list(list, reply)).to be true
expect(described_class.instance.push_to_list(list, reply)).to be true
end
it 'pushes replies to another member of the list' do
status = Fabricate(:status, text: 'Hello world', account: alice)
reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
expect(subject.push_to_list(list, reply)).to be true
expect(described_class.instance.push_to_list(list, reply)).to be true
end
it 'pushes replies to someone not a member of the list' do
status = Fabricate(:status, text: 'Hello world', account: eve)
reply = Fabricate(:status, text: 'Nay', thread: status, account: bob)
expect(subject.push_to_list(list, reply)).to be true
expect(described_class.instance.push_to_list(list, reply)).to be true
end
end
end
@ -453,9 +451,9 @@ RSpec.describe FeedManager do
account = Fabricate(:account, id: 0)
reblog = Fabricate(:status)
status = Fabricate(:status, reblog: reblog)
subject.push_to_home(account, status)
described_class.instance.push_to_home(account, status)
subject.merge_into_home(account, reblog.account)
described_class.instance.merge_into_home(account, reblog.account)
expect(redis.zscore('feed:home:0', reblog.id)).to be_nil
end
@ -468,14 +466,14 @@ RSpec.describe FeedManager do
reblogged = Fabricate(:status)
status = Fabricate(:status, reblog: reblogged)
subject.push_to_home(receiver, reblogged)
described_class::REBLOG_FALLOFF.times { subject.push_to_home(receiver, Fabricate(:status)) }
subject.push_to_home(receiver, status)
described_class.instance.push_to_home(receiver, reblogged)
described_class::REBLOG_FALLOFF.times { described_class.instance.push_to_home(receiver, Fabricate(:status)) }
described_class.instance.push_to_home(receiver, status)
# The reblogging status should show up under normal conditions.
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to include(status.id.to_s)
subject.unpush_from_home(receiver, status)
described_class.instance.unpush_from_home(receiver, status)
# Restore original status
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to_not include(status.id.to_s)
@ -486,12 +484,12 @@ RSpec.describe FeedManager do
reblogged = Fabricate(:status)
status = Fabricate(:status, reblog: reblogged)
subject.push_to_home(receiver, status)
described_class.instance.push_to_home(receiver, status)
# The reblogging status should show up under normal conditions.
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [status.id.to_s]
subject.unpush_from_home(receiver, status)
described_class.instance.unpush_from_home(receiver, status)
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to be_empty
end
@ -501,14 +499,14 @@ RSpec.describe FeedManager do
reblogs = Array.new(3) { Fabricate(:status, reblog: reblogged) }
reblogs.each do |reblog|
subject.push_to_home(receiver, reblog)
described_class.instance.push_to_home(receiver, reblog)
end
# The reblogging status should show up under normal conditions.
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [reblogs.first.id.to_s]
reblogs[0...-1].each do |reblog|
subject.unpush_from_home(receiver, reblog)
described_class.instance.unpush_from_home(receiver, reblog)
end
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to eq [reblogs.last.id.to_s]
@ -517,10 +515,10 @@ RSpec.describe FeedManager do
it 'sends push updates' do
status = Fabricate(:status)
subject.push_to_home(receiver, status)
described_class.instance.push_to_home(receiver, status)
allow(redis).to receive_messages(publish: nil)
subject.unpush_from_home(receiver, status)
described_class.instance.unpush_from_home(receiver, status)
deletion = Oj.dump(event: :delete, payload: status.id.to_s)
expect(redis).to have_received(:publish).with("timeline:#{receiver.id}", deletion)
@ -534,9 +532,9 @@ RSpec.describe FeedManager do
it 'leaves a tagged status' do
status = Fabricate(:status)
status.tags << tag
subject.push_to_home(receiver, status)
described_class.instance.push_to_home(receiver, status)
subject.unmerge_tag_from_home(tag, receiver)
described_class.instance.unmerge_tag_from_home(tag, receiver)
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to_not include(status.id.to_s)
end
@ -547,9 +545,9 @@ RSpec.describe FeedManager do
status = Fabricate(:status, account: followee)
status.tags << tag
subject.push_to_home(receiver, status)
described_class.instance.push_to_home(receiver, status)
subject.unmerge_tag_from_home(tag, receiver)
described_class.instance.unmerge_tag_from_home(tag, receiver)
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to include(status.id.to_s)
end
@ -557,9 +555,9 @@ RSpec.describe FeedManager do
it 'remains a tagged status written by receiver' do
status = Fabricate(:status, account: receiver)
status.tags << tag
subject.push_to_home(receiver, status)
described_class.instance.push_to_home(receiver, status)
subject.unmerge_tag_from_home(tag, receiver)
described_class.instance.unmerge_tag_from_home(tag, receiver)
expect(redis.zrange("feed:home:#{receiver.id}", 0, -1)).to include(status.id.to_s)
end
@ -590,7 +588,7 @@ RSpec.describe FeedManager do
end
it 'correctly cleans the home timeline' do
subject.clear_from_home(account, target_account)
described_class.instance.clear_from_home(account, target_account)
expect(redis.zrange("feed:home:#{account.id}", 0, -1)).to eq [status_from_followed_account_first.id.to_s, status_from_followed_account_next.id.to_s]
end

View file

@ -9,8 +9,8 @@ RSpec.describe AccountMigration do
end
end
describe 'Validations' do
subject { Fabricate.build :account_migration, account: source_account }
describe 'validations' do
subject { described_class.new(account: source_account, acct: target_acct) }
let(:source_account) { Fabricate(:account) }
let(:target_acct) { target_account.acct }
@ -26,7 +26,9 @@ RSpec.describe AccountMigration do
allow(service_double).to receive(:call).with(target_acct, anything).and_return(target_account)
end
it { is_expected.to allow_value(target_account.acct).for(:acct) }
it 'passes validations' do
expect(subject).to be_valid
end
end
context 'with unresolvable account' do
@ -38,13 +40,17 @@ RSpec.describe AccountMigration do
allow(service_double).to receive(:call).with(target_acct, anything).and_return(nil)
end
it { is_expected.to_not allow_value(target_acct).for(:acct) }
it 'has errors on acct field' do
expect(subject).to model_have_error_on_field(:acct)
end
end
context 'with a space in the domain part' do
let(:target_acct) { 'target@remote. org' }
it { is_expected.to_not allow_value(target_acct).for(:acct) }
it 'has errors on acct field' do
expect(subject).to model_have_error_on_field(:acct)
end
end
end
end

View file

@ -3,24 +3,29 @@
require 'rails_helper'
RSpec.describe AccountModerationNote do
describe 'Scopes' do
describe '.chronological' do
it 'returns account moderation notes oldest to newest' do
account = Fabricate(:account)
note1 = Fabricate(:account_moderation_note, target_account: account)
note2 = Fabricate(:account_moderation_note, target_account: account)
describe 'chronological scope' do
it 'returns account moderation notes oldest to newest' do
account = Fabricate(:account)
note1 = Fabricate(:account_moderation_note, target_account: account)
note2 = Fabricate(:account_moderation_note, target_account: account)
expect(account.targeted_moderation_notes.chronological).to eq [note1, note2]
end
expect(account.targeted_moderation_notes.chronological).to eq [note1, note2]
end
end
describe 'Validations' do
subject { Fabricate.build :account_moderation_note }
describe 'validations' do
it 'is invalid if the content is empty' do
report = Fabricate.build(:account_moderation_note, content: '')
expect(report.valid?).to be false
end
describe 'content' do
it { is_expected.to_not allow_value('').for(:content) }
it { is_expected.to validate_length_of(:content).is_at_most(described_class::CONTENT_SIZE_LIMIT) }
it 'is invalid if content is longer than character limit' do
report = Fabricate.build(:account_moderation_note, content: comment_over_limit)
expect(report.valid?).to be false
end
def comment_over_limit
Faker::Lorem.paragraph_by_chars(number: described_class::CONTENT_SIZE_LIMIT * 2)
end
end
end

View file

@ -10,6 +10,64 @@ 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
context 'when the account is of a local user' do
subject { local_user_account }
let!(:local_user_account) { Fabricate(:user, email: 'foo+bar@domain.org').account }
it 'creates a canonical domain block' do
subject.suspend!
expect(CanonicalEmailBlock.block?(subject.user_email)).to be true
end
context 'when a canonical domain block already exists for that email' do
before do
Fabricate(:canonical_email_block, email: subject.user_email)
end
it 'does not raise an error' do
expect { subject.suspend! }.to_not raise_error
end
end
end
end
describe '#follow!' do
it 'creates a follow' do
follow = subject.follow!(bob)
@ -694,42 +752,26 @@ RSpec.describe Account do
end
end
describe 'Callbacks' do
describe 'Stripping content when required' do
context 'with a remote account' do
subject { Fabricate.build :account, domain: 'host.example', note: ' note ', display_name: ' display name ' }
describe '#prepare_contents' do
subject { Fabricate.build :account, domain: domain, note: ' padded note ', display_name: ' padded name ' }
it 'preserves content' do
expect { subject.valid? }
.to not_change(subject, :note)
.and not_change(subject, :display_name)
end
context 'with local account' do
let(:domain) { nil }
it 'strips values' do
expect { subject.valid? }
.to change(subject, :note).to('padded note')
.and(change(subject, :display_name).to('padded name'))
end
end
context 'with a local account' do
subject { Fabricate.build :account, domain: nil, note:, display_name: }
context 'with remote account' do
let(:domain) { 'host.example' }
context 'with populated fields' do
let(:note) { ' note ' }
let(:display_name) { ' display name ' }
it 'strips content' do
expect { subject.valid? }
.to change(subject, :note).to('note')
.and change(subject, :display_name).to('display name')
end
end
context 'with empty fields' do
let(:note) { nil }
let(:display_name) { nil }
it 'preserves content' do
expect { subject.valid? }
.to not_change(subject, :note)
.and not_change(subject, :display_name)
end
end
it 'preserves values' do
expect { subject.valid? }
.to not_change(subject, :note)
.and(not_change(subject, :display_name))
end
end
end
@ -784,19 +826,22 @@ RSpec.describe Account do
end
end
describe 'Validations' do
describe 'validations' do
it { is_expected.to validate_presence_of(:username) }
context 'when account is local' do
subject { Fabricate.build :account, domain: nil }
context 'with an existing differently-cased username account' do
before { Fabricate :account, username: 'the_doctor' }
it { is_expected.to_not allow_value('the_Doctor').for(:username) }
context 'when is local' do
it 'is invalid if the username is not unique in case-insensitive comparison among local accounts' do
_account = Fabricate(:account, username: 'the_doctor')
non_unique_account = Fabricate.build(:account, username: 'the_Doctor')
non_unique_account.valid?
expect(non_unique_account).to model_have_error_on_field(:username)
end
it { is_expected.to_not allow_value('support').for(:username) }
it 'is invalid if the username is reserved' do
account = Fabricate.build(:account, username: 'support')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is valid when username is reserved but record has already been created' do
account = Fabricate.build(:account, username: 'support')
@ -804,10 +849,9 @@ RSpec.describe Account do
expect(account.valid?).to be true
end
context 'with the instance actor' do
subject { Fabricate.build :account, id: described_class::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true }
it { is_expected.to allow_value('example.com').for(:username) }
it 'is valid if we are creating an instance actor account with a period' do
account = Fabricate.build(:account, id: described_class::INSTANCE_ACTOR_ID, actor_type: 'Application', locked: true, username: 'example.com')
expect(account.valid?).to be true
end
it 'is valid if we are creating a possibly-conflicting instance actor account' do
@ -816,31 +860,81 @@ RSpec.describe Account do
expect(instance_account.valid?).to be true
end
it { is_expected.to_not allow_values('the-doctor', 'the.doctor').for(:username) }
it { is_expected.to validate_length_of(:username).is_at_most(described_class::USERNAME_LENGTH_LIMIT) }
it { is_expected.to validate_length_of(:display_name).is_at_most(described_class::DISPLAY_NAME_LENGTH_LIMIT) }
it { is_expected.to_not allow_values(account_note_over_limit).for(:note) }
end
context 'when account is remote' do
subject { Fabricate.build :account, domain: 'host.example' }
context 'when a normalized domain account exists' do
subject { Fabricate.build :account, domain: 'xn--r9j5b5b' }
before { Fabricate(:account, domain: 'にゃん', username: 'username') }
it { is_expected.to_not allow_values('username', 'Username').for(:username) }
it 'is invalid if the username doesn\'t only contains letters, numbers and underscores' do
account = Fabricate.build(:account, username: 'the-doctor')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it { is_expected.to allow_values('the-doctor', username_over_limit).for(:username) }
it { is_expected.to_not allow_values('the doctor').for(:username) }
it 'is invalid if the username contains a period' do
account = Fabricate.build(:account, username: 'the.doctor')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it { is_expected.to allow_values(display_name_over_limit).for(:display_name) }
it 'is invalid if the username is longer than the character limit' do
account = Fabricate.build(:account, username: username_over_limit)
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it { is_expected.to allow_values(account_note_over_limit).for(:note) }
it 'is invalid if the display name is longer than the character limit' do
account = Fabricate.build(:account, display_name: display_name_over_limit)
account.valid?
expect(account).to model_have_error_on_field(:display_name)
end
it 'is invalid if the note is longer than the character limit' do
account = Fabricate.build(:account, note: account_note_over_limit)
account.valid?
expect(account).to model_have_error_on_field(:note)
end
end
context 'when is remote' do
it 'is invalid if the username is same among accounts in the same normalized domain' do
Fabricate(:account, domain: 'にゃん', username: 'username')
account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'username')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is invalid if the username is not unique in case-insensitive comparison among accounts in the same normalized domain' do
Fabricate(:account, domain: 'にゃん', username: 'username')
account = Fabricate.build(:account, domain: 'xn--r9j5b5b', username: 'Username')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is valid even if the username contains hyphens' do
account = Fabricate.build(:account, domain: 'domain', username: 'the-doctor')
account.valid?
expect(account).to_not model_have_error_on_field(:username)
end
it 'is invalid if the username doesn\'t only contains letters, numbers, underscores and hyphens' do
account = Fabricate.build(:account, domain: 'domain', username: 'the doctor')
account.valid?
expect(account).to model_have_error_on_field(:username)
end
it 'is valid even if the username is longer than the character limit' do
account = Fabricate.build(:account, domain: 'domain', username: username_over_limit)
account.valid?
expect(account).to_not model_have_error_on_field(:username)
end
it 'is valid even if the display name is longer than the character limit' do
account = Fabricate.build(:account, domain: 'domain', display_name: display_name_over_limit)
account.valid?
expect(account).to_not model_have_error_on_field(:display_name)
end
it 'is valid even if the note is longer than the character limit' do
account = Fabricate.build(:account, domain: 'domain', note: account_note_over_limit)
account.valid?
expect(account).to_not model_have_error_on_field(:note)
end
end
def username_over_limit
@ -991,6 +1085,14 @@ RSpec.describe Account do
end
end
describe 'suspended' do
it 'returns an array of accounts who are suspended' do
suspended_account = Fabricate(:account, suspended: true)
_account = Fabricate(:account, suspended: false)
expect(described_class.suspended).to contain_exactly(suspended_account)
end
end
describe 'searchable' do
let!(:suspended_local) { Fabricate(:account, suspended: true, username: 'suspended_local') }
let!(:suspended_remote) { Fabricate(:account, suspended: true, domain: 'example.org', username: 'suspended_remote') }

View file

@ -5,12 +5,13 @@ require 'rails_helper'
RSpec.describe AccountStatusesCleanupPolicy do
let(:account) { Fabricate(:account, username: 'alice', domain: nil) }
describe 'Validations' do
subject { Fabricate.build :account_statuses_cleanup_policy }
let(:remote_account) { Fabricate(:account, domain: 'example.com') }
it { is_expected.to_not allow_value(remote_account).for(:account) }
describe 'validation' do
it 'disallow remote accounts' do
account.update(domain: 'example.com')
account_statuses_cleanup_policy = Fabricate.build(:account_statuses_cleanup_policy, account: account)
account_statuses_cleanup_policy.valid?
expect(account_statuses_cleanup_policy).to model_have_error_on_field(:account)
end
end
describe 'save hooks' do
@ -338,7 +339,14 @@ RSpec.describe AccountStatusesCleanupPolicy do
end
context 'when policy is set to keep DMs and reject everything else' do
before { establish_policy(keep_direct: true) }
before do
account_statuses_cleanup_policy.keep_direct = true
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'returns every old status except does not return the old direct message for deletion' do
expect(subject.pluck(:id))
@ -348,7 +356,14 @@ RSpec.describe AccountStatusesCleanupPolicy do
end
context 'when policy is set to keep self-bookmarked toots and reject everything else' do
before { establish_policy(keep_self_bookmark: true) }
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = true
end
it 'returns every old status but does not return the old self-bookmarked message for deletion' do
expect(subject.pluck(:id))
@ -358,7 +373,14 @@ RSpec.describe AccountStatusesCleanupPolicy do
end
context 'when policy is set to keep self-faved toots and reject everything else' do
before { establish_policy(keep_self_fav: true) }
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = true
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'returns every old status but does not return the old self-faved message for deletion' do
expect(subject.pluck(:id))
@ -368,7 +390,14 @@ RSpec.describe AccountStatusesCleanupPolicy do
end
context 'when policy is set to keep toots with media and reject everything else' do
before { establish_policy(keep_media: true) }
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = true
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'returns every old status but does not return the old message with media for deletion' do
expect(subject.pluck(:id))
@ -378,7 +407,14 @@ RSpec.describe AccountStatusesCleanupPolicy do
end
context 'when policy is set to keep toots with polls and reject everything else' do
before { establish_policy(keep_polls: true) }
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = true
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'returns every old status but does not return the old poll message for deletion' do
expect(subject.pluck(:id))
@ -388,7 +424,14 @@ RSpec.describe AccountStatusesCleanupPolicy do
end
context 'when policy is set to keep pinned toots and reject everything else' do
before { establish_policy(keep_pinned: true) }
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = true
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'returns every old status but does not return the old pinned message for deletion' do
expect(subject.pluck(:id))
@ -398,7 +441,14 @@ RSpec.describe AccountStatusesCleanupPolicy do
end
context 'when policy is to not keep any special messages' do
before { establish_policy }
before do
account_statuses_cleanup_policy.keep_direct = false
account_statuses_cleanup_policy.keep_pinned = false
account_statuses_cleanup_policy.keep_polls = false
account_statuses_cleanup_policy.keep_media = false
account_statuses_cleanup_policy.keep_self_fav = false
account_statuses_cleanup_policy.keep_self_bookmark = false
end
it 'returns every old status but does not return the recent or unrelated statuses' do
expect(subject.pluck(:id))
@ -409,7 +459,14 @@ RSpec.describe AccountStatusesCleanupPolicy do
end
context 'when policy is set to keep every category of toots' do
before { establish_policy(keep_direct: true, keep_pinned: true, keep_polls: true, keep_media: true, keep_self_fav: true, keep_self_bookmark: true) }
before do
account_statuses_cleanup_policy.keep_direct = true
account_statuses_cleanup_policy.keep_pinned = true
account_statuses_cleanup_policy.keep_polls = true
account_statuses_cleanup_policy.keep_media = true
account_statuses_cleanup_policy.keep_self_fav = true
account_statuses_cleanup_policy.keep_self_bookmark = true
end
it 'returns normal statuses and does not return unrelated old status' do
expect(subject.pluck(:id))
@ -445,24 +502,5 @@ RSpec.describe AccountStatusesCleanupPolicy do
.and include(very_old_status.id, faved_primary.id, reblogged_primary.id, reblogged_secondary.id)
end
end
private
def establish_policy(options = {})
default_policy_options.merge(options).each do |attribute, value|
account_statuses_cleanup_policy.send :"#{attribute}=", value
end
end
def default_policy_options
{
keep_direct: false,
keep_media: false,
keep_pinned: false,
keep_polls: false,
keep_self_bookmark: false,
keep_self_fav: false,
}
end
end
end

View file

@ -1,65 +0,0 @@
# frozen_string_literal: true
require 'rails_helper'
RSpec.describe Account::Suspensions do
subject { Fabricate(:account) }
describe '.suspended' do
let!(:suspended_account) { Fabricate :account, suspended: true }
before { Fabricate :account, suspended: false }
it 'returns accounts that are suspended' do
expect(Account.suspended)
.to contain_exactly(suspended_account)
end
end
describe '#suspended_locally?' do
context 'when the account is not suspended' do
it { is_expected.to_not be_suspended_locally }
end
context 'when the account is suspended locally' do
before { subject.update!(suspended_at: 1.day.ago, suspension_origin: :local) }
it { is_expected.to be_suspended_locally }
end
context 'when the account is suspended remotely' do
before { subject.update!(suspended_at: 1.day.ago, suspension_origin: :remote) }
it { is_expected.to_not be_suspended_locally }
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
context 'when the account is of a local user' do
subject { local_user_account }
let!(:local_user_account) { Fabricate(:user, email: 'foo+bar@domain.org').account }
it 'creates a canonical domain block' do
expect { subject.suspend! }
.to change { CanonicalEmailBlock.block?(subject.user_email) }.from(false).to(true)
end
context 'when a canonical domain block already exists for that email' do
before { Fabricate(:canonical_email_block, email: subject.user_email) }
it 'does not raise an error' do
expect { subject.suspend! }
.to_not raise_error
end
end
end
end
end

View file

@ -6,10 +6,11 @@ RSpec.describe DomainAllow do
describe 'Validations' do
it { is_expected.to validate_presence_of(:domain) }
context 'when a normalized domain exists' do
before { Fabricate(:domain_allow, domain: 'にゃん') }
it { is_expected.to_not allow_value('xn--r9j5b5b').for(:domain) }
it 'is invalid if the same normalized domain already exists' do
_domain_allow = Fabricate(:domain_allow, domain: 'にゃん')
domain_allow_with_normalized_value = Fabricate.build(:domain_allow, domain: 'xn--r9j5b5b')
domain_allow_with_normalized_value.valid?
expect(domain_allow_with_normalized_value).to model_have_error_on_field(:domain)
end
end
end

View file

@ -3,17 +3,33 @@
require 'rails_helper'
RSpec.describe Form::AdminSettings do
describe 'Validations' do
describe 'validations' do
describe 'site_contact_username' do
context 'with no accounts' do
it { is_expected.to_not allow_value('Test').for(:site_contact_username) }
it 'is not valid' do
setting = described_class.new(site_contact_username: 'Test')
setting.valid?
expect(setting).to model_have_error_on_field(:site_contact_username)
end
end
context 'with an account' do
before { Fabricate(:account, username: 'Glorp') }
it { is_expected.to_not allow_value('Test').for(:site_contact_username) }
it { is_expected.to allow_value('Glorp').for(:site_contact_username) }
it 'is not valid when account doesnt match' do
setting = described_class.new(site_contact_username: 'Test')
setting.valid?
expect(setting).to model_have_error_on_field(:site_contact_username)
end
it 'is valid when account matches' do
setting = described_class.new(site_contact_username: 'Glorp')
setting.valid?
expect(setting).to_not model_have_error_on_field(:site_contact_username)
end
end
end
end

View file

@ -3,13 +3,18 @@
require 'rails_helper'
RSpec.describe IpBlock do
describe 'Validations' do
subject { Fabricate.build :ip_block }
describe 'validations' do
it { is_expected.to validate_presence_of(:ip) }
it { is_expected.to validate_presence_of(:severity) }
it { is_expected.to validate_uniqueness_of(:ip) }
it 'validates ip uniqueness', :aggregate_failures do
described_class.create!(ip: '127.0.0.1', severity: :no_access)
ip_block = described_class.new(ip: '127.0.0.1', severity: :no_access)
expect(ip_block).to_not be_valid
expect(ip_block).to model_have_error_on_field(:ip)
end
end
describe '#to_log_human_identifier' do

View file

@ -9,10 +9,26 @@ RSpec.describe PreviewCard do
end
end
describe 'Validations' do
describe 'url' do
it { is_expected.to allow_values('http://example.host/path', 'https://example.host/path').for(:url) }
it { is_expected.to_not allow_value('javascript:alert()').for(:url) }
describe 'validations' do
describe 'urls' do
it 'allows http schemes' do
record = described_class.new(url: 'http://example.host/path')
expect(record).to be_valid
end
it 'allows https schemes' do
record = described_class.new(url: 'https://example.host/path')
expect(record).to be_valid
end
it 'does not allow javascript: schemes' do
record = described_class.new(url: 'javascript:alert()')
expect(record).to_not be_valid
expect(record).to model_have_error_on_field(:url)
end
end
end
end

View file

@ -3,24 +3,29 @@
require 'rails_helper'
RSpec.describe ReportNote do
describe 'Scopes' do
describe '.chronological' do
it 'returns report notes oldest to newest' do
report = Fabricate(:report)
note1 = Fabricate(:report_note, report: report)
note2 = Fabricate(:report_note, report: report)
describe 'chronological scope' do
it 'returns report notes oldest to newest' do
report = Fabricate(:report)
note1 = Fabricate(:report_note, report: report)
note2 = Fabricate(:report_note, report: report)
expect(report.notes.chronological).to eq [note1, note2]
end
expect(report.notes.chronological).to eq [note1, note2]
end
end
describe 'Validations' do
subject { Fabricate.build :report_note }
describe 'validations' do
it 'is invalid if the content is empty' do
report = Fabricate.build(:report_note, content: '')
expect(report.valid?).to be false
end
describe 'content' do
it { is_expected.to_not allow_value('').for(:content) }
it { is_expected.to validate_length_of(:content).is_at_most(described_class::CONTENT_SIZE_LIMIT) }
it 'is invalid if content is longer than character limit' do
report = Fabricate.build(:report_note, content: comment_over_limit)
expect(report.valid?).to be false
end
def comment_over_limit
Faker::Lorem.paragraph_by_chars(number: described_class::CONTENT_SIZE_LIMIT * 2)
end
end
end

View file

@ -3,17 +3,53 @@
require 'rails_helper'
RSpec.describe WebauthnCredential do
describe 'Validations' do
subject { Fabricate.build :webauthn_credential }
describe 'validations' do
it { is_expected.to validate_presence_of(:external_id) }
it { is_expected.to validate_presence_of(:public_key) }
it { is_expected.to validate_presence_of(:nickname) }
it { is_expected.to validate_presence_of(:sign_count) }
it { is_expected.to validate_uniqueness_of(:external_id) }
it { is_expected.to validate_uniqueness_of(:nickname).scoped_to(:user_id) }
it 'is invalid if already exist a webauthn credential with the same external id' do
Fabricate(:webauthn_credential, external_id: '_Typ0ygudDnk9YUVWLQayw')
new_webauthn_credential = Fabricate.build(:webauthn_credential, external_id: '_Typ0ygudDnk9YUVWLQayw')
it { is_expected.to validate_numericality_of(:sign_count).only_integer.is_greater_than_or_equal_to(0).is_less_than_or_equal_to(described_class::SIGN_COUNT_LIMIT - 1) }
new_webauthn_credential.valid?
expect(new_webauthn_credential).to model_have_error_on_field(:external_id)
end
it 'is invalid if user already registered a webauthn credential with the same nickname' do
user = Fabricate(:user)
Fabricate(:webauthn_credential, user_id: user.id, nickname: 'USB Key')
new_webauthn_credential = Fabricate.build(:webauthn_credential, user_id: user.id, nickname: 'USB Key')
new_webauthn_credential.valid?
expect(new_webauthn_credential).to model_have_error_on_field(:nickname)
end
it 'is invalid if sign_count is not a number' do
webauthn_credential = Fabricate.build(:webauthn_credential, sign_count: 'invalid sign_count')
webauthn_credential.valid?
expect(webauthn_credential).to model_have_error_on_field(:sign_count)
end
it 'is invalid if sign_count is negative number' do
webauthn_credential = Fabricate.build(:webauthn_credential, sign_count: -1)
webauthn_credential.valid?
expect(webauthn_credential).to model_have_error_on_field(:sign_count)
end
it 'is invalid if sign_count is greater than the limit' do
webauthn_credential = Fabricate.build(:webauthn_credential, sign_count: (described_class::SIGN_COUNT_LIMIT * 2))
webauthn_credential.valid?
expect(webauthn_credential).to model_have_error_on_field(:sign_count)
end
end
end

2035
yarn.lock

File diff suppressed because it is too large Load diff