field_spec.rb 4.7 KB

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