read_user_shared_examples.rb 3.01 KB
Newer Older
1 2 3
shared_examples_for 'allows the "read_user" scope' do |api_version|
  let(:version) { api_version || 'v4' }

4 5 6 7 8
  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' do
9
        get api_call.call(path, user, personal_access_token: token, version: version)
10

11
        expect(response).to have_gitlab_http_status(200)
12 13 14 15 16
      end
    end

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

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

21
        expect(response).to have_gitlab_http_status(200)
22 23 24 25 26 27
      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) }

28 29 30 31
      before do
        stub_container_registry_config(enabled: true)
      end

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

35
        expect(response).to have_gitlab_http_status(403)
36
      end
37 38 39
    end
  end

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

43 44
    context 'when the requesting token has the "api" scope' do
      let!(:token) { Doorkeeper::AccessToken.create! application_id: application.id, resource_owner_id: user.id, scopes: "api" }
45

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

49
        expect(response).to have_gitlab_http_status(200)
50 51 52 53 54 55 56 57 58
      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" }

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

59
        expect(response).to have_gitlab_http_status(200)
60 61 62 63 64 65 66 67 68
      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" }

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

69
        expect(response).to have_gitlab_http_status(403)
70
      end
71 72 73 74 75 76 77 78
    end
  end
end

shared_examples_for '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) }

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

82
      expect(response).to have_gitlab_http_status(403)
83 84 85
    end
  end
end