features_visibility_spec.rb 8.76 KB
Newer Older
1 2
require 'spec_helper'

3
describe 'Edit Project Settings' do
4
  let(:member) { create(:user) }
5
  let!(:project) { create(:project, :public, :repository) }
6
  let!(:issue) { create(:issue, project: project) }
7 8
  let(:non_member) { create(:user) }

9
  describe 'project features visibility selectors', :js do
10
    before do
11
      project.add_maintainer(member)
12
      sign_in(member)
13 14 15 16 17 18 19
    end

    tools = { builds: "pipelines", issues: "issues", wiki: "wiki", snippets: "snippets", merge_requests: "merge_requests" }

    tools.each do |tool_name, shortcut_name|
      describe "feature #{tool_name}" do
        it 'toggles visibility' do
20
          visit edit_project_path(project)
21

22 23
          # disable by clicking toggle
          toggle_feature_off("project[project_feature_attributes][#{tool_name}_access_level]")
tauriedavis's avatar
tauriedavis committed
24
          page.within('.sharing-permissions') do
25
            find('input[value="Save changes"]').click
tauriedavis's avatar
tauriedavis committed
26
          end
27
          wait_for_requests
28 29
          expect(page).not_to have_selector(".shortcuts-#{shortcut_name}")

30 31
          # re-enable by clicking toggle again
          toggle_feature_on("project[project_feature_attributes][#{tool_name}_access_level]")
tauriedavis's avatar
tauriedavis committed
32
          page.within('.sharing-permissions') do
33
            find('input[value="Save changes"]').click
tauriedavis's avatar
tauriedavis committed
34
          end
35
          wait_for_requests
36 37 38 39
          expect(page).to have_selector(".shortcuts-#{shortcut_name}")
        end
      end
    end
Felipe Artur's avatar
Felipe Artur committed
40

41 42
    context 'When external issue tracker is enabled and issues enabled on project settings' do
      it 'does not hide issues tab' do
43 44
        allow_any_instance_of(Project).to receive(:external_issue_tracker).and_return(JiraService.new)

45
        visit project_path(project)
46

47 48 49 50 51 52 53 54 55 56
        expect(page).to have_selector('.shortcuts-issues')
      end
    end

    context 'When external issue tracker is enabled and issues disabled on project settings' do
      it 'hides issues tab' do
        project.issues_enabled = false
        project.save!
        allow_any_instance_of(Project).to receive(:external_issue_tracker).and_return(JiraService.new)

57
        visit project_path(project)
58 59

        expect(page).not_to have_selector('.shortcuts-issues')
60 61 62
      end
    end

Felipe Artur's avatar
Felipe Artur committed
63 64
    context "pipelines subtabs" do
      it "shows builds when enabled" do
65
        visit project_pipelines_path(project)
Felipe Artur's avatar
Felipe Artur committed
66 67 68 69 70 71 72

        expect(page).to have_selector(".shortcuts-builds")
      end

      it "hides builds when disabled" do
        allow(Ability).to receive(:allowed?).with(member, :read_builds, project).and_return(false)

73
        visit project_pipelines_path(project)
Felipe Artur's avatar
Felipe Artur committed
74 75 76 77

        expect(page).not_to have_selector(".shortcuts-builds")
      end
    end
78 79 80
  end

  describe 'project features visibility pages' do
81 82 83
    let(:pipeline) { create(:ci_empty_pipeline, project: project) }
    let(:job) { create(:ci_build, pipeline: pipeline) }

Michael Kozono's avatar
Michael Kozono committed
84
    let(:tools) do
85
      {
86 87 88 89 90
        builds: project_job_path(project, job),
        issues: project_issues_path(project),
        wiki: project_wiki_path(project, :home),
        snippets: project_snippets_path(project),
        merge_requests: project_merge_requests_path(project)
91
      }
Michael Kozono's avatar
Michael Kozono committed
92
    end
93 94

    context 'normal user' do
95
      before do
96
        sign_in(member)
97 98
      end

99
      it 'renders 200 if tool is enabled' do
100
        tools.each do |method_name, url|
101 102 103 104 105 106 107
          project.project_feature.update_attribute("#{method_name}_access_level", ProjectFeature::ENABLED)
          visit url
          expect(page.status_code).to eq(200)
        end
      end

      it 'renders 404 if feature is disabled' do
108
        tools.each do |method_name, url|
109 110 111 112 113 114 115 116 117
          project.project_feature.update_attribute("#{method_name}_access_level", ProjectFeature::DISABLED)
          visit url
          expect(page.status_code).to eq(404)
        end
      end

      it 'renders 404 if feature is enabled only for team members' do
        project.team.truncate

118
        tools.each do |method_name, url|
119 120 121 122 123 124
          project.project_feature.update_attribute("#{method_name}_access_level", ProjectFeature::PRIVATE)
          visit url
          expect(page.status_code).to eq(404)
        end
      end

125
      it 'renders 200 if user is member of group' do
126 127 128 129 130 131
        group = create(:group)
        project.group = group
        project.save

        group.add_owner(member)

132
        tools.each do |method_name, url|
133 134 135 136 137 138 139 140 141 142
          project.project_feature.update_attribute("#{method_name}_access_level", ProjectFeature::PRIVATE)
          visit url
          expect(page.status_code).to eq(200)
        end
      end
    end

    context 'admin user' do
      before do
        non_member.update_attribute(:admin, true)
143
        sign_in(non_member)
144 145 146
      end

      it 'renders 404 if feature is disabled' do
147
        tools.each do |method_name, url|
148 149 150 151 152 153 154 155 156
          project.project_feature.update_attribute("#{method_name}_access_level", ProjectFeature::DISABLED)
          visit url
          expect(page.status_code).to eq(404)
        end
      end

      it 'renders 200 if feature is enabled only for team members' do
        project.team.truncate

157
        tools.each do |method_name, url|
158 159 160 161 162 163 164
          project.project_feature.update_attribute("#{method_name}_access_level", ProjectFeature::PRIVATE)
          visit url
          expect(page.status_code).to eq(200)
        end
      end
    end
  end
165

166
  describe 'repository visibility', :js do
167
    before do
168
      project.add_maintainer(member)
169
      sign_in(member)
170
      visit edit_project_path(project)
171 172 173
    end

    it "disables repository related features" do
174
      toggle_feature_off('project[project_feature_attributes][repository_access_level]')
175

Phil Hughes's avatar
Phil Hughes committed
176 177 178 179
      page.within('.sharing-permissions') do
        click_button "Save changes"
      end

Filipa Lacerda's avatar
Filipa Lacerda committed
180
      expect(find(".sharing-permissions")).to have_selector(".project-feature-toggle.is-disabled", count: 2)
181 182 183
    end

    it "shows empty features project homepage" do
184 185 186
      toggle_feature_off('project[project_feature_attributes][repository_access_level]')
      toggle_feature_off('project[project_feature_attributes][issues_access_level]')
      toggle_feature_off('project[project_feature_attributes][wiki_access_level]')
187

Phil Hughes's avatar
Phil Hughes committed
188 189 190
      page.within('.sharing-permissions') do
        click_button "Save changes"
      end
191
      wait_for_requests
192

193
      visit project_path(project)
194 195 196

      expect(page).to have_content "Customize your workflow!"
    end
197 198

    it "hides project activity tabs" do
199 200 201
      toggle_feature_off('project[project_feature_attributes][repository_access_level]')
      toggle_feature_off('project[project_feature_attributes][issues_access_level]')
      toggle_feature_off('project[project_feature_attributes][wiki_access_level]')
202

Phil Hughes's avatar
Phil Hughes committed
203 204 205
      page.within('.sharing-permissions') do
        click_button "Save changes"
      end
206
      wait_for_requests
207

208
      visit activity_project_path(project)
209 210 211 212 213 214 215 216

      page.within(".event-filter") do
        expect(page).to have_selector("a", count: 2)
        expect(page).not_to have_content("Push events")
        expect(page).not_to have_content("Merge events")
        expect(page).not_to have_content("Comments")
      end
    end
217 218 219

    # Regression spec for https://gitlab.com/gitlab-org/gitlab-ce/issues/25272
    it "hides comments activity tab only on disabled issues, merge requests and repository" do
220
      toggle_feature_off('project[project_feature_attributes][issues_access_level]')
221 222 223 224 225

      save_changes_and_check_activity_tab do
        expect(page).to have_content("Comments")
      end

226
      visit edit_project_path(project)
227

228
      toggle_feature_off('project[project_feature_attributes][merge_requests_access_level]')
229 230 231 232 233

      save_changes_and_check_activity_tab do
        expect(page).to have_content("Comments")
      end

234
      visit edit_project_path(project)
235

236
      toggle_feature_off('project[project_feature_attributes][repository_access_level]')
237 238 239 240 241

      save_changes_and_check_activity_tab do
        expect(page).not_to have_content("Comments")
      end

242
      visit edit_project_path(project)
243 244 245
    end

    def save_changes_and_check_activity_tab
Phil Hughes's avatar
Phil Hughes committed
246 247 248
      page.within('.sharing-permissions') do
        click_button "Save changes"
      end
249
      wait_for_requests
250

251
      visit activity_project_path(project)
252 253 254 255 256

      page.within(".event-filter") do
        yield
      end
    end
257
  end
258 259 260

  # Regression spec for https://gitlab.com/gitlab-org/gitlab-ce/issues/24056
  describe 'project statistic visibility' do
261
    let!(:project) { create(:project, :private) }
262 263

    before do
264
      project.add_guest(member)
265
      sign_in(member)
266
      visit project_path(project)
267 268 269 270 271 272
    end

    it "does not show project statistic for guest" do
      expect(page).not_to have_selector('.project-stats')
    end
  end
273 274

  def toggle_feature_off(feature_name)
Filipa Lacerda's avatar
Filipa Lacerda committed
275
    find(".project-feature-controls[data-for=\"#{feature_name}\"] .project-feature-toggle.is-checked").click
276 277 278
  end

  def toggle_feature_on(feature_name)
Filipa Lacerda's avatar
Filipa Lacerda committed
279
    find(".project-feature-controls[data-for=\"#{feature_name}\"] .project-feature-toggle:not(.is-checked)").click
280
  end
281
end