summaryrefslogtreecommitdiff
path: root/qa/qa/specs/features/repository/protected_branches_spec.rb
blob: fcec987ec69b62be940728d94bd870f9ae85cdaf (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
86
87
88
module QA
  feature 'branch protection support', :core do
    given(:branch_name) { 'protected-branch' }
    given(:commit_message) { 'Protected push commit message' }
    given(:project) do
      Factory::Resource::Project.fabricate! do |resource|
        resource.name = 'protected-branch-project'
      end
    end
    given(:location) do
      Page::Project::Show.act do
        choose_repository_clone_http
        repository_location
      end
    end

    before do
      Runtime::Browser.visit(:gitlab, Page::Main::Login)
      Page::Main::Login.act { sign_in_using_credentials }
    end

    after do
      # We need to clear localStorage because we're using it for the dropdown,
      # and capybara doesn't do this for us.
      # https://github.com/teamcapybara/capybara/issues/1702
      Capybara.execute_script 'localStorage.clear()'
    end

    scenario 'user is able to protect a branch' do
      protected_branch = fabricate_branch(allow_to_push: true)

      expect(protected_branch.name).to have_content(branch_name)
      expect(protected_branch.push_allowance).to have_content('Developers + Maintainers')
    end

    context 'push to protected branch' do
      scenario 'unauthorized users are blocked' do
        fabricate_branch(allow_to_push: false)

        project.visit!

        Git::Repository.perform do |repository|
          push_output = push_to_repository(repository)

          expect(push_output)
            .to match(/remote\: GitLab\: You are not allowed to push code to protected branches on this project/)
          expect(push_output)
            .to match(/\[remote rejected\] #{branch_name} -> #{branch_name} \(pre-receive hook declined\)/)
        end
      end

      scenario 'authorized users are allowed' do
        fabricate_branch(allow_to_push: true)

        project.visit!

        Git::Repository.perform do |repository|
          push_output = push_to_repository(repository)

          expect(push_output).to match(/remote: To create a merge request for protected-branch, visit/)
        end
      end
    end

    def fabricate_branch(allow_to_push:)
      Factory::Resource::Branch.fabricate! do |resource|
        resource.branch_name = branch_name
        resource.project = project
        resource.allow_to_push = allow_to_push
        resource.protected = true
      end
    end

    def push_to_repository(repository)
      repository.uri = location.uri
      repository.use_default_credentials

      repository.act do
        clone
        configure_identity('GitLab QA', 'root@gitlab.com')
        checkout('protected-branch')
        commit_file('README.md', 'readme content', 'Add a readme')
        push_changes('protected-branch')
        push_output
      end
    end
  end
end