process_mentions_service_spec.rb 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe ProcessMentionsService do
  4. subject { described_class.new }
  5. let(:account) { Fabricate(:account, username: 'alice') }
  6. context 'when mentions contain blocked accounts' do
  7. let(:non_blocked_account) { Fabricate(:account) }
  8. let(:individually_blocked_account) { Fabricate(:account) }
  9. let(:domain_blocked_account) { Fabricate(:account, domain: 'evil.com') }
  10. let(:status) { Fabricate(:status, account: account, text: "Hello @#{non_blocked_account.acct} @#{individually_blocked_account.acct} @#{domain_blocked_account.acct}", visibility: :public) }
  11. before do
  12. account.block!(individually_blocked_account)
  13. account.domain_blocks.create!(domain: domain_blocked_account.domain)
  14. end
  15. it 'creates a mention to the non-blocked account but not the individually or domain blocked accounts' do
  16. expect { subject.call(status) }
  17. .to create_mention_for_non_blocked
  18. .and skip_mention_for_individual
  19. .and skip_mention_for_domain_blocked
  20. end
  21. def create_mention_for_non_blocked
  22. change { non_blocked_account.mentions.where(status: status).count }.to(1)
  23. end
  24. def skip_mention_for_individual
  25. not_change { individually_blocked_account.mentions.where(status: status).count }.from(0)
  26. end
  27. def skip_mention_for_domain_blocked
  28. not_change { domain_blocked_account.mentions.where(status: status).count }.from(0)
  29. end
  30. end
  31. context 'with resolving a mention to a remote account' do
  32. let(:status) { Fabricate(:status, account: account, text: "Hello @#{remote_user.acct}", visibility: :public) }
  33. context 'with ActivityPub' do
  34. context 'with a valid remote user' do
  35. let!(:remote_user) { Fabricate(:account, username: 'remote_user', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
  36. it 'creates a mention' do
  37. subject.call(status)
  38. expect(remote_user.mentions.where(status: status).count).to eq 1
  39. end
  40. end
  41. context 'when mentioning a user several times when not saving records' do
  42. let!(:remote_user) { Fabricate(:account, username: 'remote_user', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox') }
  43. let(:status) { Fabricate(:status, account: account, text: "Hello @#{remote_user.acct} @#{remote_user.acct} @#{remote_user.acct}", visibility: :public) }
  44. it 'creates exactly one mention' do
  45. subject.call(status, save_records: false)
  46. expect(status.mentions.size).to eq 1
  47. end
  48. end
  49. context 'with an IDN domain' do
  50. let!(:remote_user) { Fabricate(:account, username: 'sneak', protocol: :activitypub, domain: 'xn--hresiar-mxa.ch', inbox_url: 'http://example.com/inbox') }
  51. let!(:status) { Fabricate(:status, account: account, text: 'Hello @sneak@hæresiar.ch') }
  52. it 'creates a mention' do
  53. subject.call(status)
  54. expect(remote_user.mentions.where(status: status).count).to eq 1
  55. end
  56. end
  57. context 'with an IDN TLD' do
  58. let!(:remote_user) { Fabricate(:account, username: 'foo', protocol: :activitypub, domain: 'xn--y9a3aq.xn--y9a3aq', inbox_url: 'http://example.com/inbox') }
  59. let!(:status) { Fabricate(:status, account: account, text: 'Hello @foo@հայ.հայ') }
  60. it 'creates a mention' do
  61. subject.call(status)
  62. expect(remote_user.mentions.where(status: status).count).to eq 1
  63. end
  64. end
  65. end
  66. context 'with a Temporarily-unreachable ActivityPub user' do
  67. let!(:remote_user) { Fabricate(:account, username: 'remote_user', protocol: :activitypub, domain: 'example.com', inbox_url: 'http://example.com/inbox', last_webfingered_at: nil) }
  68. before do
  69. stub_request(:get, 'https://example.com/.well-known/host-meta').to_return(status: 404)
  70. stub_request(:get, 'https://example.com/.well-known/webfinger?resource=acct:remote_user@example.com').to_return(status: 500)
  71. end
  72. it 'creates a mention' do
  73. subject.call(status)
  74. expect(remote_user.mentions.where(status: status).count).to eq 1
  75. end
  76. end
  77. end
  78. end