field_spec.rb 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. # frozen_string_literal: true
  2. require 'rails_helper'
  3. RSpec.describe Account::Field do
  4. describe '#verified?' do
  5. subject { described_class.new(account, 'name' => 'Foo', 'value' => 'Bar', 'verified_at' => verified_at) }
  6. let(:account) { instance_double(Account, local?: true) }
  7. context 'when verified_at is set' do
  8. let(:verified_at) { Time.now.utc.iso8601 }
  9. it 'returns true' do
  10. expect(subject.verified?).to be true
  11. end
  12. end
  13. context 'when verified_at is not set' do
  14. let(:verified_at) { nil }
  15. it 'returns false' do
  16. expect(subject.verified?).to be false
  17. end
  18. end
  19. end
  20. describe '#mark_verified!' do
  21. subject { described_class.new(account, original_hash) }
  22. let(:account) { instance_double(Account, local?: true) }
  23. let(:original_hash) { { 'name' => 'Foo', 'value' => 'Bar' } }
  24. before do
  25. subject.mark_verified!
  26. end
  27. it 'updates verified_at' do
  28. expect(subject.verified_at).to_not be_nil
  29. end
  30. it 'updates original hash' do
  31. expect(original_hash['verified_at']).to_not be_nil
  32. end
  33. end
  34. describe '#verifiable?' do
  35. subject { described_class.new(account, 'name' => 'Foo', 'value' => value) }
  36. let(:account) { instance_double(Account, local?: local) }
  37. context 'with local accounts' do
  38. let(:local) { true }
  39. context 'with a URL with misleading authentication' do
  40. let(:value) { 'https://spacex.com @h.43z.one' }
  41. it 'returns false' do
  42. expect(subject.verifiable?).to be false
  43. end
  44. end
  45. context 'with a URL' do
  46. let(:value) { 'https://example.com' }
  47. it 'returns true' do
  48. expect(subject.verifiable?).to be true
  49. end
  50. end
  51. context 'with an IDN URL' do
  52. let(:value) { 'https://twitter.com∕dougallj∕status∕1590357240443437057.ê.cc/twitter.html' }
  53. it 'returns false' do
  54. expect(subject.verifiable?).to be false
  55. end
  56. end
  57. context 'with a URL with a non-normalized path' do
  58. let(:value) { 'https://github.com/octocatxxxxxxxx/../mastodon' }
  59. it 'returns false' do
  60. expect(subject.verifiable?).to be false
  61. end
  62. end
  63. context 'with text that is not a URL' do
  64. let(:value) { 'Hello world' }
  65. it 'returns false' do
  66. expect(subject.verifiable?).to be false
  67. end
  68. end
  69. context 'with text that contains a URL' do
  70. let(:value) { 'Hello https://example.com world' }
  71. it 'returns false' do
  72. expect(subject.verifiable?).to be false
  73. end
  74. end
  75. context 'with text which is blank' do
  76. let(:value) { '' }
  77. it 'returns false' do
  78. expect(subject.verifiable?).to be false
  79. end
  80. end
  81. end
  82. context 'with remote accounts' do
  83. let(:local) { false }
  84. context 'with a link' do
  85. let(:value) { '<a href="https://www.patreon.com/mastodon" target="_blank" rel="nofollow noopener noreferrer me"><span class="invisible">https://www.</span><span class="">patreon.com/mastodon</span><span class="invisible"></span></a>' }
  86. it 'returns true' do
  87. expect(subject.verifiable?).to be true
  88. end
  89. end
  90. context 'with a link with misleading authentication' do
  91. let(:value) { '<a href="https://google.com @h.43z.one" target="_blank" rel="nofollow noopener noreferrer me"><span class="invisible">https://</span><span class="">google.com</span><span class="invisible"> @h.43z.one</span></a>' }
  92. it 'returns false' do
  93. expect(subject.verifiable?).to be false
  94. end
  95. end
  96. context 'with HTML that has more than just a link' do
  97. let(:value) { '<a href="https://google.com" target="_blank" rel="nofollow noopener noreferrer me"><span class="invisible">https://</span><span class="">google.com</span><span class="invisible"></span></a> @h.43z.one' }
  98. it 'returns false' do
  99. expect(subject.verifiable?).to be false
  100. end
  101. end
  102. context 'with a link with different visible text' do
  103. let(:value) { '<a href="https://google.com/bar">https://example.com/foo</a>' }
  104. it 'returns false' do
  105. expect(subject.verifiable?).to be false
  106. end
  107. end
  108. context 'with text that is a URL but is not linked' do
  109. let(:value) { 'https://example.com/foo' }
  110. it 'returns false' do
  111. expect(subject.verifiable?).to be false
  112. end
  113. end
  114. context 'with text which is blank' do
  115. let(:value) { '' }
  116. it 'returns false' do
  117. expect(subject.verifiable?).to be false
  118. end
  119. end
  120. end
  121. end
  122. end