BigW Consortium Gitlab

read_user_shared_examples.rb 2.77 KB
Newer Older
1
shared_examples_for 'allows the "read_user" scope' do
2 3 4 5 6 7 8 9 10 11 12 13 14
  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
        get api_call.call(path, user, personal_access_token: token)

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

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

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

19 20 21 22 23 24 25 26 27 28 29 30
        expect(response).to have_http_status(200)
      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) }

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

        expect(response).to have_http_status(401)
      end
31 32 33
    end
  end

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

37 38
    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" }
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
      it 'returns a "200" response' do
        get api_call.call(path, user, oauth_access_token: token)

        expect(response).to have_http_status(200)
      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)

        expect(response).to have_http_status(200)
      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)

        expect(response).to have_http_status(403)
      end
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    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) }

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

      expect(response).to have_http_status(401)
    end
  end
end