about summary refs log tree commit diff
path: root/spec/lib/webfinger_resource_spec.rb
blob: ee007da70a56808a5ef4f85d34ded989343aa58b (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
require 'rails_helper'

describe WebfingerResource do
  around do |example|
    before_local = Rails.configuration.x.local_domain
    before_web = Rails.configuration.x.web_domain
    example.run
    Rails.configuration.x.local_domain = before_local
    Rails.configuration.x.web_domain = before_web
  end

  describe '#username' do
    describe 'with a URL value' do
      it 'raises with a route whose controller is not AccountsController' do
        resource = 'https://example.com/users/alice/other'

        expect do
          WebfingerResource.new(resource).username
        end.to raise_error(ActiveRecord::RecordNotFound)
      end

      it 'raises with a route whose action is not show' do
        resource = 'https://example.com/users/alice'

        recognized = Rails.application.routes.recognize_path(resource)
        allow(recognized).to receive(:[]).with(:controller).and_return('accounts')
        allow(recognized).to receive(:[]).with(:username).and_return('alice')
        expect(recognized).to receive(:[]).with(:action).and_return('create')

        expect(Rails.application.routes).to receive(:recognize_path).with(resource).and_return(recognized).at_least(:once)

        expect do
          WebfingerResource.new(resource).username
        end.to raise_error(ActiveRecord::RecordNotFound)
      end

      it 'raises with a string that doesnt start with URL' do
        resource = 'website for http://example.com/users/alice/other'

        expect do
          WebfingerResource.new(resource).username
        end.to raise_error(WebfingerResource::InvalidRequest)
      end

      it 'finds the username in a valid https route' do
        resource = 'https://example.com/users/alice'

        result = WebfingerResource.new(resource).username
        expect(result).to eq 'alice'
      end

      it 'finds the username in a mixed case http route' do
        resource = 'HTTp://exAMPLe.com/users/alice'

        result = WebfingerResource.new(resource).username
        expect(result).to eq 'alice'
      end

      it 'finds the username in a valid http route' do
        resource = 'http://example.com/users/alice'

        result = WebfingerResource.new(resource).username
        expect(result).to eq 'alice'
      end
    end

    describe 'with a username and hostname value' do
      it 'raises on a non-local domain' do
        resource = 'user@remote-host.com'

        expect do
          WebfingerResource.new(resource).username
        end.to raise_error(ActiveRecord::RecordNotFound)
      end

      it 'finds username for a local domain' do
        Rails.configuration.x.local_domain = 'example.com'
        resource = 'alice@example.com'

        result = WebfingerResource.new(resource).username
        expect(result).to eq 'alice'
      end

      it 'finds username for a web domain' do
        Rails.configuration.x.web_domain = 'example.com'
        resource = 'alice@example.com'

        result = WebfingerResource.new(resource).username
        expect(result).to eq 'alice'
      end
    end

    describe 'with an acct value' do
      it 'raises on a non-local domain' do
        resource = 'acct:user@remote-host.com'

        expect do
          WebfingerResource.new(resource).username
        end.to raise_error(ActiveRecord::RecordNotFound)
      end

      it 'raises on a nonsense domain' do
        resource = 'acct:user@remote-host@remote-hostess.remote.local@remote'

        expect do
          WebfingerResource.new(resource).username
        end.to raise_error(ActiveRecord::RecordNotFound)
      end

      it 'finds the username for a local account if the domain is the local one' do
        Rails.configuration.x.local_domain = 'example.com'
        resource = 'acct:alice@example.com'

        result = WebfingerResource.new(resource).username
        expect(result).to eq 'alice'
      end

      it 'finds the username for a local account if the domain is the Web one' do
        Rails.configuration.x.web_domain = 'example.com'
        resource = 'acct:alice@example.com'

        result = WebfingerResource.new(resource).username
        expect(result).to eq 'alice'
      end
    end

    describe 'with a nonsense resource' do
      it 'raises InvalidRequest' do
        resource = 'df/:dfkj'

        expect do
          WebfingerResource.new(resource).username
        end.to raise_error(WebfingerResource::InvalidRequest)
      end
    end
  end
end