about summary refs log tree commit diff
path: root/spec/models/account/field_spec.rb
blob: 0ac9769bccf78a20b3d0a02dd0bbe0665ca4bc87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require 'rails_helper'

RSpec.describe Account::Field, type: :model do
  describe '#verified?' do
    let(:account) { double('Account', local?: true) }

    subject { described_class.new(account, 'name' => 'Foo', 'value' => 'Bar', 'verified_at' => verified_at) }

    context 'when verified_at is set' do
      let(:verified_at) { Time.now.utc.iso8601 }

      it 'returns true' do
        expect(subject.verified?).to be true
      end
    end

    context 'when verified_at is not set' do
      let(:verified_at) { nil }

      it 'returns false' do
        expect(subject.verified?).to be false
      end
    end
  end

  describe '#mark_verified!' do
    let(:account) { double('Account', local?: true) }
    let(:original_hash) { { 'name' => 'Foo', 'value' => 'Bar' } }

    subject { described_class.new(account, original_hash) }

    before do
      subject.mark_verified!
    end

    it 'updates verified_at' do
      expect(subject.verified_at).to_not be_nil
    end

    it 'updates original hash' do
      expect(original_hash['verified_at']).to_not be_nil
    end
  end

  describe '#verifiable?' do
    let(:account) { double('Account', local?: local) }

    subject { described_class.new(account, 'name' => 'Foo', 'value' => value) }

    context 'for local accounts' do
      let(:local) { true }

      context 'for a URL with misleading authentication' do
        let(:value) { 'https://spacex.com                                                                                            @h.43z.one' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end

      context 'for a URL' do
        let(:value) { 'https://example.com' }

        it 'returns true' do
          expect(subject.verifiable?).to be true
        end
      end

      context 'for an IDN URL' do
        let(:value) { 'https://twitter.com∕dougallj∕status∕1590357240443437057.ê.cc/twitter.html' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end

      context 'for a URL with a non-normalized path' do
        let(:value) { 'https://github.com/octocatxxxxxxxx/../mastodon' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end

      context 'for text that is not a URL' do
        let(:value) { 'Hello world' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end

      context 'for text that contains a URL' do
        let(:value) { 'Hello https://example.com world' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end
      
      context 'for text which is blank' do
        let(:value) { '' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end
    end

    context 'for remote accounts' do
      let(:local) { false }

      context 'for a link' do
        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>' }

        it 'returns true' do
          expect(subject.verifiable?).to be true
        end
      end

      context 'for a link with misleading authentication' do
        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>' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end

      context 'for HTML that has more than just a link' do
        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' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end

      context 'for a link with different visible text' do
        let(:value) { '<a href="https://google.com/bar">https://example.com/foo</a>' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end

      context 'for text that is a URL but is not linked' do
        let(:value) { 'https://example.com/foo' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end
      
      context 'for text which is blank' do
        let(:value) { '' }

        it 'returns false' do
          expect(subject.verifiable?).to be false
        end
      end
    end
  end
end