summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/api/1_manage/project_access_token_spec.rb
blob: 6024c8658d501e83030c6c2c015ad498ebd59cc6 (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
# frozen_string_literal: true

module QA
  RSpec.describe 'Manage' do
    describe 'Project access token' do
      before(:all) do
        @project_access_token = QA::Resource::ProjectAccessToken.fabricate_via_api!
        @user_api_client = Runtime::API::Client.new(:gitlab, personal_access_token: @project_access_token.token)
      end

      context 'for the same project' do
        it 'can be used to create a file via the project API', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/1734' do
          expect do
            Resource::File.fabricate_via_api! do |file|
              file.api_client = @user_api_client
              file.project = @project_access_token.project
              file.branch = 'new_branch'
              file.commit_message = 'Add new file'
              file.name = "text-#{SecureRandom.hex(8)}.txt"
              file.content = 'New file'
            end
          end.not_to raise_error
        end

        it 'can be used to commit via the API', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/1735' do
          expect do
            Resource::Repository::Commit.fabricate_via_api! do |commit|
              commit.api_client = @user_api_client
              commit.project = @project_access_token.project
              commit.branch = 'new_branch'
              commit.start_branch = @project_access_token.project.default_branch
              commit.commit_message = 'Add new file'
              commit.add_files([
                { file_path: "text-#{SecureRandom.hex(8)}.txt", content: 'new file' }
              ])
            end
          end.not_to raise_error
        end
      end

      context 'for a different project' do
        before(:all) do
          @different_project = Resource::Project.fabricate!
        end

        it 'cannot be used to create a file via the project API', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/1736' do
          expect do
            Resource::File.fabricate_via_api! do |file|
              file.api_client = @user_api_client
              file.project = @different_project
              file.branch = 'new_branch'
              file.commit_message = 'Add new file'
              file.name = "text-#{SecureRandom.hex(8)}.txt"
              file.content = 'New file'
            end
          end.to raise_error(Resource::ApiFabricator::ResourceFabricationFailedError, /403 Forbidden/)
        end

        it 'cannot be used to commit via the API', testcase: 'https://gitlab.com/gitlab-org/quality/testcases/-/issues/1737' do
          expect do
            Resource::Repository::Commit.fabricate_via_api! do |commit|
              commit.api_client = @user_api_client
              commit.project = @different_project
              commit.branch = 'new_branch'
              commit.start_branch = @different_project.default_branch
              commit.commit_message = 'Add new file'
              commit.add_files([
                { file_path: "text-#{SecureRandom.hex(8)}.txt", content: 'new file' }
              ])
            end
          end.to raise_error(Resource::ApiFabricator::ResourceFabricationFailedError, /403 Forbidden - You are not allowed to push into this branch/)
        end

        after(:all) do
          @different_project.remove_via_api!
        end
      end

      after(:all) do
        @project_access_token.remove_via_api!
        @project_access_token.project.remove_via_api!
      end
    end
  end
end