summaryrefslogtreecommitdiff
path: root/spec/requests/oauth_tokens_spec.rb
blob: f2fb380bde0d92c78fc02245b09ad3df75b09977 (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
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe 'OAuth Tokens requests' do
  let(:user) { create :user }
  let(:application) { create :oauth_application, scopes: 'api' }
  let(:grant_type) { 'authorization_code' }
  let(:refresh_token) { nil }

  def request_access_token(user)
    post '/oauth/token',
      params: {
        grant_type: grant_type,
        code: generate_access_grant(user).token,
        redirect_uri: application.redirect_uri,
        client_id: application.uid,
        client_secret: application.secret,
        refresh_token: refresh_token

      }
  end

  def generate_access_grant(user)
    create(:oauth_access_grant, application: application, resource_owner_id: user.id)
  end

  context 'when there is already a token for the application' do
    let!(:existing_token) { create(:oauth_access_token, application: application, resource_owner_id: user.id) }

    shared_examples 'issues a new token' do
      it 'issues a new token' do
        expect do
          request_access_token(user)
        end.to change { Doorkeeper::AccessToken.count }.from(1).to(2)

        expect(json_response['access_token']).not_to eq existing_token.token
        expect(json_response['refresh_token']).not_to eq existing_token.refresh_token
      end
    end

    shared_examples 'revokes previous token' do
      it 'revokes previous token' do
        expect { request_access_token(user) }.to(
          change { existing_token.reload.revoked_at }.from(nil))
      end
    end

    context 'and the request is done by the resource owner' do
      context 'with authorization code grant type' do
        include_examples 'issues a new token'

        it 'does not revoke previous token' do
          request_access_token(user)

          expect(existing_token.reload.revoked_at).to be_nil
        end
      end

      context 'with refresh token grant type' do
        let(:grant_type) { 'refresh_token' }
        let(:refresh_token) { existing_token.refresh_token }

        include_examples 'issues a new token'
        include_examples 'revokes previous token'

        context 'expired refresh token' do
          let!(:existing_token) do
            create(:oauth_access_token, application: application,
                                        resource_owner_id: user.id,
                                        created_at: 10.minutes.ago,
                                        expires_in: 5)
          end

          include_examples 'issues a new token'
          include_examples 'revokes previous token'
        end

        context 'revoked refresh token' do
          let!(:existing_token) do
            create(:oauth_access_token,
              application: application,
              resource_owner_id: user.id,
              created_at: 2.hours.ago,
              revoked_at: 1.hour.ago,
              expires_in: 5)
          end

          it 'does not issue a new token' do
            request_access_token(user)

            expect(json_response['error']).to eq('invalid_grant')
          end
        end
      end
    end
  end

  context 'when there is no token stored for the application' do
    it 'generates and returns a new token' do
      expect do
        request_access_token(user)
      end.to change { Doorkeeper::AccessToken.count }.by(1)

      expect(json_response['access_token']).not_to be_nil
      expect(json_response['expires_in']).not_to be_nil
    end
  end
end