summaryrefslogtreecommitdiff
path: root/spec/requests/api/doorkeeper_access_spec.rb
blob: b2864f448a89d1c6324e5ea7c7c00e451fa2848d (plain)
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
require 'spec_helper'

shared_examples 'user login request with unique ip limit' do
  include_context 'limit login to only one ip' do
    it 'allows user authenticating from the same ip' do
      change_ip('ip')
      request
      expect(response).to have_http_status(200)

      request
      expect(response).to have_http_status(200)
    end

    it 'blocks user authenticating from two distinct ips' do
      change_ip('ip')
      request
      expect(response).to have_http_status(200)

      change_ip('ip2')
      request
      expect(response).to have_http_status(403)
    end
  end
end

describe API::API, api: true do
  include ApiHelpers

  let!(:user) { create(:user) }
  let!(:application) { Doorkeeper::Application.create!(name: 'MyApp', redirect_uri: 'https://app.com', owner: user) }
  let!(:token) { Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id, scopes: 'api' }

  describe 'when unauthenticated' do
    it 'returns authentication success' do
      get api('/user'), access_token: token.token
      expect(response).to have_http_status(200)
    end

    include_examples 'user login request with unique ip limit' do
      def request
        get api('/user'), access_token: token.token
      end
    end
  end

  describe 'when token invalid' do
    it 'returns authentication error' do
      get api('/user'), access_token: '123a'
      expect(response).to have_http_status(401)
    end
  end

  describe 'authorization by private token' do
    it 'returns authentication success' do
      get api('/user', user)
      expect(response).to have_http_status(200)
    end

    include_examples 'user login request with unique ip limit' do
      def request
        get api('/user', user)
      end
    end
  end
end