File: read_user_shared_examples.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 (113 lines) | stat: -rw-r--r-- 4,170 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
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
# frozen_string_literal: true

RSpec.shared_examples 'allows the "read_user" scope' do |api_version|
  let(:version) { api_version || 'v4' }

  context 'for personal access tokens' do
    context 'when the requesting token has the "api" scope' do
      let(:token) { create(:personal_access_token, scopes: ['api'], user: user) }

      it 'returns a "200" response on get request' do
        get api_call.call(path, user, personal_access_token: token, version: version)

        expect(response).to have_gitlab_http_status(:ok)
      end

      it 'returns a "200" response on head request' do
        head api_call.call(path, user, personal_access_token: token, version: version)

        expect(response).to have_gitlab_http_status(:ok)
      end
    end

    context 'when the requesting token has the "read_user" scope' do
      let(:token) { create(:personal_access_token, scopes: ['read_user'], user: user) }

      it 'returns a "200" response on get request' do
        get api_call.call(path, user, personal_access_token: token, version: version)

        expect(response).to have_gitlab_http_status(:ok)
      end

      it 'returns a "200" response on head request' do
        head api_call.call(path, user, personal_access_token: token, version: version)

        expect(response).to have_gitlab_http_status(:ok)
      end
    end

    context 'when the requesting token does not have any required scope' do
      let(:token) { create(:personal_access_token, scopes: ['read_registry'], user: user) }

      before do
        stub_container_registry_config(enabled: true)
      end

      it 'returns a "403" response' do
        get api_call.call(path, user, personal_access_token: token, version: version)

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end
  end

  context 'for doorkeeper (OAuth) tokens' do
    let!(:application) { Doorkeeper::Application.create!(name: "MyApp", redirect_uri: "https://app.com", owner: user) }

    context 'when the requesting token has the "api" scope' do
      let!(:token) do
        Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id, scopes: "api", organization_id: user.namespace.organization.id
      end

      it 'returns a "200" response on get request' do
        get api_call.call(path, user, oauth_access_token: token)

        expect(response).to have_gitlab_http_status(:ok)
      end

      it 'returns a "200" response on head request' do
        head api_call.call(path, user, oauth_access_token: token)

        expect(response).to have_gitlab_http_status(:ok)
      end
    end

    context 'when the requesting token has the "read_user" scope' do
      let!(:token) { Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id, scopes: "read_user", organization_id: user.namespace.organization.id }

      it 'returns a "200" response on get request' do
        get api_call.call(path, user, oauth_access_token: token)

        expect(response).to have_gitlab_http_status(:ok)
      end

      it 'returns a "200" response on head request' do
        head api_call.call(path, user, oauth_access_token: token)

        expect(response).to have_gitlab_http_status(:ok)
      end
    end

    context 'when the requesting token does not have any required scope' do
      let!(:token) { Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id, scopes: "invalid", organization_id: user.namespace.organization.id }

      it 'returns a "403" response' do
        get api_call.call(path, user, oauth_access_token: token)

        expect(response).to have_gitlab_http_status(:forbidden)
      end
    end
  end
end

RSpec.shared_examples 'does not allow the "read_user" scope' do
  context 'when the requesting token has the "read_user" scope' do
    let(:token) { create(:personal_access_token, scopes: ['read_user'], user: user) }

    it 'returns a "403" response' do
      post api_call.call(path, user, personal_access_token: token), params: attributes_for(:user, projects_limit: 3)

      expect(response).to have_gitlab_http_status(:forbidden)
    end
  end
end