protected_branches_spec.rb 5.5 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Protected Branches', :js do
4 5
  let(:user) { create(:user) }
  let(:admin) { create(:admin) }
6
  let(:project) { create(:project, :repository) }
7

8 9 10 11 12
  context 'logged in as developer' do
    before do
      project.add_developer(user)
      sign_in(user)
    end
13

14 15 16 17 18 19 20 21 22 23 24
    describe 'Delete protected branch' do
      before do
        create(:protected_branch, project: project, name: 'fix')
        expect(ProtectedBranch.count).to eq(1)
      end

      it 'does not allow developer to removes protected branch' do
        visit project_branches_path(project)

        fill_in 'branch-search', with: 'fix'
        find('#branch-search').native.send_keys(:enter)
25

26 27 28 29
        expect(page).to have_css('.btn-remove.disabled')
      end
    end
  end
30

31
  context 'logged in as maintainer' do
32
    before do
33
      project.add_maintainer(user)
34
      sign_in(user)
35 36
    end

37 38 39 40 41
    describe 'Delete protected branch' do
      before do
        create(:protected_branch, project: project, name: 'fix')
        expect(ProtectedBranch.count).to eq(1)
      end
42

43 44
      it 'removes branch after modal confirmation' do
        visit project_branches_path(project)
45

46 47
        fill_in 'branch-search', with: 'fix'
        find('#branch-search').native.send_keys(:enter)
48

49 50
        expect(page).to have_content('fix')
        expect(find('.all-branches')).to have_selector('li', count: 1)
51
        page.find('[data-target="#modal-delete-branch"]').click
52

53 54 55 56 57 58 59 60 61
        expect(page).to have_css('.js-delete-branch[disabled]')
        fill_in 'delete_branch_input', with: 'fix'
        click_link 'Delete protected branch'

        fill_in 'branch-search', with: 'fix'
        find('#branch-search').native.send_keys(:enter)

        expect(page).to have_content('No branches to show')
      end
62 63 64
    end
  end

65 66 67
  context 'logged in as admin' do
    before do
      sign_in(admin)
68 69
    end

70 71 72
    describe "explicit protected branches" do
      it "allows creating explicit protected branches" do
        visit project_protected_branches_path(project)
Luke Bennett's avatar
Luke Bennett committed
73
        set_defaults
74 75
        set_protected_branch_name('some-branch')
        click_on "Protect"
76

77 78 79 80
        within(".protected-branches-list") { expect(page).to have_content('some-branch') }
        expect(ProtectedBranch.count).to eq(1)
        expect(ProtectedBranch.last.name).to eq('some-branch')
      end
81

82 83 84 85 86
      it "displays the last commit on the matching branch if it exists" do
        commit = create(:commit, project: project)
        project.repository.add_branch(admin, 'some-branch', commit.id)

        visit project_protected_branches_path(project)
Luke Bennett's avatar
Luke Bennett committed
87
        set_defaults
88 89 90 91 92 93 94 95
        set_protected_branch_name('some-branch')
        click_on "Protect"

        within(".protected-branches-list") { expect(page).to have_content(commit.id[0..7]) }
      end

      it "displays an error message if the named branch does not exist" do
        visit project_protected_branches_path(project)
Luke Bennett's avatar
Luke Bennett committed
96
        set_defaults
97 98 99
        set_protected_branch_name('some-branch')
        click_on "Protect"

100
        within(".protected-branches-list") { expect(page).to have_content('branch was deleted') }
101
      end
102 103
    end

104 105 106
    describe "wildcard protected branches" do
      it "allows creating protected branches with a wildcard" do
        visit project_protected_branches_path(project)
Luke Bennett's avatar
Luke Bennett committed
107
        set_defaults
108 109 110 111 112 113 114
        set_protected_branch_name('*-stable')
        click_on "Protect"

        within(".protected-branches-list") { expect(page).to have_content('*-stable') }
        expect(ProtectedBranch.count).to eq(1)
        expect(ProtectedBranch.last.name).to eq('*-stable')
      end
115

116 117 118
      it "displays the number of matching branches" do
        project.repository.add_branch(admin, 'production-stable', 'master')
        project.repository.add_branch(admin, 'staging-stable', 'master')
119

120
        visit project_protected_branches_path(project)
Luke Bennett's avatar
Luke Bennett committed
121
        set_defaults
122 123
        set_protected_branch_name('*-stable')
        click_on "Protect"
124

125 126 127 128
        within(".protected-branches-list") do
          expect(page).to have_content("Protected branch (2)")
          expect(page).to have_content("2 matching branches")
        end
129
      end
130 131 132 133 134 135 136 137

      it "displays all the branches matching the wildcard" do
        project.repository.add_branch(admin, 'production-stable', 'master')
        project.repository.add_branch(admin, 'staging-stable', 'master')
        project.repository.add_branch(admin, 'development', 'master')

        visit project_protected_branches_path(project)
        set_protected_branch_name('*-stable')
Luke Bennett's avatar
Luke Bennett committed
138
        set_defaults
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
        click_on "Protect"

        visit project_protected_branches_path(project)
        click_on "2 matching branches"

        within(".protected-branches-list") do
          expect(page).to have_content("production-stable")
          expect(page).to have_content("staging-stable")
          expect(page).not_to have_content("development")
        end
      end
    end

    describe "access control" do
      include_examples "protected branches > access control > CE"
154 155
    end
  end
156

157
  def set_protected_branch_name(branch_name)
158
    find(".js-protected-branch-select").click
159 160
    find(".dropdown-input-field").set(branch_name)
    click_on("Create wildcard #{branch_name}")
161
  end
Luke Bennett's avatar
Luke Bennett committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175

  def set_defaults
    find(".js-allowed-to-merge").click
    within('.qa-allowed-to-merge-dropdown') do
      expect(first("li")).to have_content("Roles")
      find(:link, 'No one').click
    end

    find(".js-allowed-to-push").click
    within('.qa-allowed-to-push-dropdown') do
      expect(first("li")).to have_content("Roles")
      find(:link, 'No one').click
    end
  end
176
end