File: token_info_controller_spec.rb

package info (click to toggle)
gitlab 17.6.5-19
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 629,368 kB
  • sloc: ruby: 1,915,304; javascript: 557,307; sql: 60,639; xml: 6,509; sh: 4,567; makefile: 1,239; python: 406
file content (75 lines) | stat: -rw-r--r-- 2,573 bytes parent folder | download
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
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Oauth::TokenInfoController do
  describe '#show' do
    context 'when the user is not authenticated' do
      it 'responds with a 401' do
        get :show

        expect(response).to have_gitlab_http_status(:unauthorized)
        expect(Gitlab::Json.parse(response.body)).to include('error' => 'invalid_token')
      end
    end

    context 'when the request is valid' do
      let(:application) { create(:oauth_application, scopes: 'api') }
      let(:access_token) do
        create(:oauth_access_token, expires_in: 5.minutes, application: application)
      end

      it 'responds with the token info' do
        get :show, params: { access_token: access_token.token }

        expect(response).to have_gitlab_http_status(:ok)
        expect(Gitlab::Json.parse(response.body)).to eq(
          'scope' => %w[api],
          'scopes' => %w[api],
          'created_at' => access_token.created_at.to_i,
          'expires_in' => access_token.expires_in,
          'application' => { 'uid' => application.uid },
          'resource_owner_id' => access_token.resource_owner_id,
          'expires_in_seconds' => access_token.expires_in
        )
      end
    end

    context 'when the doorkeeper_token is not recognised' do
      it 'responds with a 401' do
        get :show, params: { access_token: 'unknown_token' }

        expect(response).to have_gitlab_http_status(:unauthorized)
        expect(Gitlab::Json.parse(response.body)).to include('error' => 'invalid_token')
      end
    end

    context 'when the token is expired' do
      let(:access_token) do
        create(:oauth_access_token, created_at: 2.days.ago, expires_in: 10.minutes)
      end

      it 'responds with a 401' do
        get :show, params: { access_token: access_token.token }

        expect(response).to have_gitlab_http_status(:unauthorized)
        expect(Gitlab::Json.parse(response.body)).to include('error' => 'invalid_token')
      end
    end

    context 'when the token is revoked' do
      let(:access_token) { create(:oauth_access_token, revoked_at: 2.days.ago) }

      it 'responds with a 401' do
        get :show, params: { access_token: access_token.token }

        expect(response).to have_gitlab_http_status(:unauthorized)
        expect(Gitlab::Json.parse(response.body)).to include('error' => 'invalid_token')
      end
    end
  end

  it 'includes Two-factor enforcement concern' do
    expect(described_class.included_modules.include?(EnforcesTwoFactorAuthentication)).to eq(true)
  end
end