summaryrefslogtreecommitdiff
path: root/spec/features/projects/branches_spec.rb
blob: 4fae324d8d5edf39ef9af399a6bf0c6d42e5deea (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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
require 'spec_helper'

describe 'Branches', feature: true do
  let(:user) { create(:user) }
  let(:project) { create(:project, :public) }
  let(:repository) { project.repository }

  def set_protected_branch_name(branch_name)
    find(".js-protected-branch-select").click
    find(".dropdown-input-field").set(branch_name)
    click_on("Create wildcard #{branch_name}")
  end

  context 'logged in as developer' do
    before do
      sign_in(user)
      project.team << [user, :developer]
    end

    describe 'Initial branches page' do
      it 'shows all the branches' do
        visit project_branches_path(project)

        repository.branches_sorted_by(:name).first(20).each do |branch|
          expect(page).to have_content("#{branch.name}")
        end
        expect(page).to have_content("Protected branches can be managed in project settings")
      end

      it 'sorts the branches by name' do
        visit project_branches_path(project)

        click_button "Name" # Open sorting dropdown
        click_link "Name"

        sorted = repository.branches_sorted_by(:name).first(20).map do |branch|
          Regexp.escape(branch.name)
        end
        expect(page).to have_content(/#{sorted.join(".*")}/)
      end

      it 'sorts the branches by last updated' do
        visit project_branches_path(project)

        click_button "Name" # Open sorting dropdown
        click_link "Last updated"

        sorted = repository.branches_sorted_by(:updated_desc).first(20).map do |branch|
          Regexp.escape(branch.name)
        end
        expect(page).to have_content(/#{sorted.join(".*")}/)
      end

      it 'sorts the branches by oldest updated' do
        visit project_branches_path(project)

        click_button "Name" # Open sorting dropdown
        click_link "Oldest updated"

        sorted = repository.branches_sorted_by(:updated_asc).first(20).map do |branch|
          Regexp.escape(branch.name)
        end
        expect(page).to have_content(/#{sorted.join(".*")}/)
      end

      it 'avoids a N+1 query in branches index' do
        control_count = ActiveRecord::QueryRecorder.new { visit project_branches_path(project) }.count

        %w(one two three four five).each { |ref| repository.add_branch(user, ref, 'master') }

        expect { visit project_branches_path(project) }.not_to exceed_query_limit(control_count)
      end
    end

    describe 'Find branches' do
      it 'shows filtered branches', js: true do
        visit project_branches_path(project)

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

        expect(page).to have_content('fix')
        expect(find('.all-branches')).to have_selector('li', count: 1)
      end
    end

    describe 'Delete unprotected branch' do
      it 'removes branch after confirmation', js: true do
        visit project_branches_path(project)

        fill_in 'branch-search', with: 'fix'

        find('#branch-search').native.send_keys(:enter)

        expect(page).to have_content('fix')
        expect(find('.all-branches')).to have_selector('li', count: 1)
        find('.js-branch-fix .btn-remove').trigger(:click)

        expect(page).not_to have_content('fix')
        expect(find('.all-branches')).to have_selector('li', count: 0)
      end
    end

    describe 'Delete protected branch' do
      before do
        project.add_user(user, :master)
        visit project_protected_branches_path(project)
        set_protected_branch_name('fix')
        click_on "Protect"

        within(".protected-branches-list") { expect(page).to have_content('fix') }
        expect(ProtectedBranch.count).to eq(1)
        project.add_user(user, :developer)
      end

      it 'does not allow devleoper to removes protected branch', js: true do
        visit project_branches_path(project)

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

        expect(page).to have_css('.btn-remove.disabled')
      end
    end
  end

  context 'logged in as master' do
    before do
      sign_in(user)
      project.team << [user, :master]
    end

    describe 'Delete protected branch' do
      before do
        visit project_protected_branches_path(project)
        set_protected_branch_name('fix')
        click_on "Protect"

        within(".protected-branches-list") { expect(page).to have_content('fix') }
        expect(ProtectedBranch.count).to eq(1)
      end

      it 'removes branch after modal confirmation', js: true do
        visit project_branches_path(project)

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

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

        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
    end
  end

  context 'logged out' do
    before do
      visit project_branches_path(project)
    end

    it 'does not show merge request button' do
      page.within first('.all-branches li') do
        expect(page).not_to have_content 'Merge Request'
      end
    end
  end
end