projects_helper_spec.rb 15.1 KB
Newer Older
1 2 3
require 'spec_helper'

describe ProjectsHelper do
V
Valery Sizov 已提交
4 5
  describe "#project_status_css_class" do
    it "returns appropriate class" do
6 7 8
      expect(project_status_css_class("started")).to eq("active")
      expect(project_status_css_class("failed")).to eq("danger")
      expect(project_status_css_class("finished")).to eq("success")
V
Valery Sizov 已提交
9 10
    end
  end
V
Valery Sizov 已提交
11 12

  describe "can_change_visibility_level?" do
13
    let(:project) { create(:project, :repository) }
14
    let(:user) { create(:project_member, :reporter, user: create(:user), project: project).user }
D
Douwe Maan 已提交
15
    let(:fork_project) { Projects::ForkService.new(project, user).execute }
V
Valery Sizov 已提交
16

17
    it "returns false if there are no appropriate permissions" do
V
Valery Sizov 已提交
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
      allow(helper).to receive(:can?) { false }

      expect(helper.can_change_visibility_level?(project, user)).to be_falsey
    end

    it "returns true if there are permissions and it is not fork" do
      allow(helper).to receive(:can?) { true }

      expect(helper.can_change_visibility_level?(project, user)).to be_truthy
    end

    context "forks" do
      it "returns false if there are permissions and origin project is PRIVATE" do
        allow(helper).to receive(:can?) { true }

        project.update visibility_level:  Gitlab::VisibilityLevel::PRIVATE

        expect(helper.can_change_visibility_level?(fork_project, user)).to be_falsey
      end

      it "returns true if there are permissions and origin project is INTERNAL" do
        allow(helper).to receive(:can?) { true }

        project.update visibility_level:  Gitlab::VisibilityLevel::INTERNAL

        expect(helper.can_change_visibility_level?(fork_project, user)).to be_truthy
      end
    end
  end
47 48

  describe "readme_cache_key" do
49
    let(:project) { create(:project, :repository) }
50 51 52 53 54 55

    before do
      helper.instance_variable_set(:@project, project)
    end

    it "returns a valid cach key" do
56
      expect(helper.send(:readme_cache_key)).to eq("#{project.full_path}-#{project.commit.id}-readme")
57 58 59 60 61
    end

    it "returns a valid cache key if HEAD does not exist" do
      allow(project).to receive(:commit) { nil }

62
      expect(helper.send(:readme_cache_key)).to eq("#{project.full_path}-nil-readme")
63 64
    end
  end
65

66
  describe "#project_list_cache_key", clean_gitlab_redis_shared_state: true do
67
    let(:project) { create(:project, :repository) }
68

69 70
    it "includes the route" do
      expect(helper.project_list_cache_key(project)).to include(project.route.cache_key)
71 72
    end

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    it "includes the project" do
      expect(helper.project_list_cache_key(project)).to include(project.cache_key)
    end

    it "includes the controller name" do
      expect(helper.controller).to receive(:controller_name).and_return("testcontroller")

      expect(helper.project_list_cache_key(project)).to include("testcontroller")
    end

    it "includes the controller action" do
      expect(helper.controller).to receive(:action_name).and_return("testaction")

      expect(helper.project_list_cache_key(project)).to include("testaction")
    end

    it "includes the application settings" do
      settings = Gitlab::CurrentSettings.current_application_settings

      expect(helper.project_list_cache_key(project)).to include(settings.cache_key)
    end

    it "includes a version" do
J
Jeff Stubler 已提交
96
      expect(helper.project_list_cache_key(project).last).to start_with('v')
97 98 99 100 101 102 103 104
    end

    it "includes the pipeline status when there is a status" do
      create(:ci_pipeline, :success, project: project, sha: project.commit.sha)

      expect(helper.project_list_cache_key(project)).to include("pipeline-status/#{project.commit.sha}-success")
    end
  end
105

106 107
  describe '#load_pipeline_status' do
    it 'loads the pipeline status in batch' do
108
      project = build(:project)
109 110 111 112 113 114 115 116 117

      helper.load_pipeline_status([project])
      # Skip lazy loading of the `pipeline_status` attribute
      pipeline_status = project.instance_variable_get('@pipeline_status')

      expect(pipeline_status).to be_a(Gitlab::Cache::Ci::ProjectPipelineStatus)
    end
  end

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
  describe '#show_no_ssh_key_message?' do
    let(:user) { create(:user) }

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'user has no keys' do
      it 'returns true' do
        expect(helper.show_no_ssh_key_message?).to be_truthy
      end
    end

    context 'user has an ssh key' do
      it 'returns false' do
        create(:personal_key, user: user)

        expect(helper.show_no_ssh_key_message?).to be_falsey
      end
    end
  end

  describe '#show_no_password_message?' do
    let(:user) { create(:user) }

    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'user has password set' do
      it 'returns false' do
        expect(helper.show_no_password_message?).to be_falsey
      end
    end

    context 'user requires a password' do
      let(:user) { create(:user, password_automatically_set: true) }

      it 'returns true' do
        expect(helper.show_no_password_message?).to be_truthy
      end
    end

    context 'user requires a personal access token' do
      it 'returns true' do
163
        stub_application_setting(password_authentication_enabled?: false)
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186

        expect(helper.show_no_password_message?).to be_truthy
      end
    end
  end

  describe '#link_to_set_password' do
    before do
      allow(helper).to receive(:current_user).and_return(user)
    end

    context 'user requires a password' do
      let(:user) { create(:user, password_automatically_set: true) }

      it 'returns link to set a password' do
        expect(helper.link_to_set_password).to match %r{<a href="#{edit_profile_password_path}">set a password</a>}
      end
    end

    context 'user requires a personal access token' do
      let(:user) { create(:user) }

      it 'returns link to create a personal access token' do
187
        stub_application_setting(password_authentication_enabled?: false)
188 189 190 191 192 193

        expect(helper.link_to_set_password).to match %r{<a href="#{profile_personal_access_tokens_path}">create a personal access token</a>}
      end
    end
  end

194
  describe '#link_to_member_avatar' do
M
Maxim Rydkin 已提交
195
    let(:user) { build_stubbed(:user) }
196
    let(:expected) { double }
197

198
    before do
199
      expect(helper).to receive(:avatar_icon).with(user, 16).and_return(expected)
200 201 202
    end

    it 'returns image tag for member avatar' do
203
      expect(helper).to receive(:image_tag).with(expected, { width: 16, class: ["avatar", "avatar-inline", "s16"], alt: "" })
M
Maxim Rydkin 已提交
204 205

      helper.link_to_member_avatar(user)
206
    end
207 208 209 210 211 212

    it 'returns image tag with avatar class' do
      expect(helper).to receive(:image_tag).with(expected, { width: 16, class: ["avatar", "avatar-inline", "s16", "any-avatar-class"], alt: "" })

      helper.link_to_member_avatar(user, avatar_class: "any-avatar-class")
    end
213 214 215
  end

  describe '#link_to_member' do
M
Maxim Rydkin 已提交
216 217 218
    let(:group)   { build_stubbed(:group) }
    let(:project) { build_stubbed(:project, group: group) }
    let(:user)    { build_stubbed(:user) }
219 220 221 222 223

    describe 'using the default options' do
      it 'returns an HTML link to the user' do
        link = helper.link_to_member(project, user)

224
        expect(link).to match(%r{/#{user.username}})
225 226 227
      end
    end
  end
228 229

  describe 'default_clone_protocol' do
230
    context 'when user is not logged in and gitlab protocol is HTTP' do
231
      it 'returns HTTP' do
232
        allow(helper).to receive(:current_user).and_return(nil)
233 234 235 236 237

        expect(helper.send(:default_clone_protocol)).to eq('http')
      end
    end

238
    context 'when user is not logged in and gitlab protocol is HTTPS' do
239
      it 'returns HTTPS' do
240 241
        stub_config_setting(protocol: 'https')
        allow(helper).to receive(:current_user).and_return(nil)
242 243 244 245 246

        expect(helper.send(:default_clone_protocol)).to eq('https')
      end
    end
  end
247 248

  describe '#license_short_name' do
249
    let(:project) { create(:project) }
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272

    context 'when project.repository has a license_key' do
      it 'returns the nickname of the license if present' do
        allow(project.repository).to receive(:license_key).and_return('agpl-3.0')

        expect(helper.license_short_name(project)).to eq('GNU AGPLv3')
      end

      it 'returns the name of the license if nickname is not present' do
        allow(project.repository).to receive(:license_key).and_return('mit')

        expect(helper.license_short_name(project)).to eq('MIT License')
      end
    end

    context 'when project.repository has no license_key but a license_blob' do
      it 'returns LICENSE' do
        allow(project.repository).to receive(:license_key).and_return(nil)

        expect(helper.license_short_name(project)).to eq('LICENSE')
      end
    end
  end
273 274

  describe '#sanitized_import_error' do
275
    let(:project) { create(:project, :repository) }
276 277 278

    before do
      allow(project).to receive(:repository_storage_path).and_return('/base/repo/path')
279
      allow(Settings.shared).to receive(:[]).with('path').and_return('/base/repo/export/path')
280 281
    end

282
    it 'removes the repo path' do
283
      repo = '/base/repo/path/namespace/test.git'
284 285
      import_error = "Could not clone #{repo}\n"

286
      expect(sanitize_repo_path(project, import_error)).to eq('Could not clone [REPOS PATH]/namespace/test.git')
287
    end
288 289 290 291 292 293 294

    it 'removes the temporary repo path used for uploads/exports' do
      repo = '/base/repo/export/path/tmp/project_exports/uploads/test.tar.gz'
      import_error = "Unable to decompress #{repo}\n"

      expect(sanitize_repo_path(project, import_error)).to eq('Unable to decompress [REPO EXPORT PATH]/uploads/test.tar.gz')
    end
295
  end
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

  describe '#last_push_event' do
    let(:user) { double(:user, fork_of: nil) }
    let(:project) { double(:project, id: 1) }

    before do
      allow(helper).to receive(:current_user).and_return(user)
      helper.instance_variable_set(:@project, project)
    end

    context 'when there is no current_user' do
      let(:user) { nil }

      it 'returns nil' do
        expect(helper.last_push_event).to eq(nil)
      end
    end

    it 'returns recent push on the current project' do
      event = double(:event)
      expect(user).to receive(:recent_push).with([project.id]).and_return(event)

      expect(helper.last_push_event).to eq(event)
    end

    context 'when current user has a fork of the current project' do
      let(:fork) { double(:fork, id: 2) }

      it 'returns recent push considering fork events' do
        expect(user).to receive(:fork_of).with(project).and_return(fork)

        event_on_fork = double(:event)
        expect(user).to receive(:recent_push).with([project.id, fork.id]).and_return(event_on_fork)

        expect(helper.last_push_event).to eq(event_on_fork)
      end
    end
  end
F
Felipe Artur 已提交
334 335

  describe "#project_feature_access_select" do
336
    let(:project) { create(:project, :public) }
F
Felipe Artur 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349
    let(:user)    { create(:user) }

    context "when project is internal or public" do
      it "shows all options" do
        helper.instance_variable_set(:@project, project)
        result = helper.project_feature_access_select(:issues_access_level)
        expect(result).to include("Disabled")
        expect(result).to include("Only team members")
        expect(result).to include("Everyone with access")
      end
    end

    context "when project is private" do
350 351 352
      before do
        project.update_attributes(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
      end
F
Felipe Artur 已提交
353 354 355 356 357 358

      it "shows only allowed options" do
        helper.instance_variable_set(:@project, project)
        result = helper.project_feature_access_select(:issues_access_level)
        expect(result).to include("Disabled")
        expect(result).to include("Only team members")
359
        expect(result).to have_selector('option[disabled]', text: "Everyone with access")
F
Felipe Artur 已提交
360 361 362 363 364 365 366 367 368 369 370 371 372 373
      end
    end

    context "when project moves from public to private" do
      before do
        project.update_attributes(visibility_level: Gitlab::VisibilityLevel::PRIVATE)
      end

      it "shows the highest allowed level selected" do
        helper.instance_variable_set(:@project, project)
        result = helper.project_feature_access_select(:issues_access_level)

        expect(result).to include("Disabled")
        expect(result).to include("Only team members")
374
        expect(result).to have_selector('option[disabled]', text: "Everyone with access")
F
Felipe Artur 已提交
375 376 377 378
        expect(result).to have_selector('option[selected]', text: "Only team members")
      end
    end
  end
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

  describe "#visibility_select_options" do
    let(:project) { create(:project, :repository) }
    let(:user)    { create(:user) }

    before do
      allow(helper).to receive(:current_user).and_return(user)

      stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
    end

    it "does not include the Public restricted level" do
      expect(helper.send(:visibility_select_options, project, Gitlab::VisibilityLevel::PRIVATE)).not_to include('Public')
    end

    it "includes the Internal level" do
      expect(helper.send(:visibility_select_options, project, Gitlab::VisibilityLevel::PRIVATE)).to include('Internal')
    end

    it "includes the Private level" do
      expect(helper.send(:visibility_select_options, project, Gitlab::VisibilityLevel::PRIVATE)).to include('Private')
    end
  end
K
Kamil Trzcinski 已提交
402 403

  describe '#get_project_nav_tabs' do
404
    let(:project) { create(:project) }
K
Kamil Trzcinski 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
    let(:user)    { create(:user) }

    before do
      allow(helper).to receive(:can?) { true }
    end

    subject do
      helper.send(:get_project_nav_tabs, project, user)
    end

    context 'when builds feature is enabled' do
      before do
        allow(project).to receive(:builds_enabled?).and_return(true)
      end

      it "does include pipelines tab" do
        is_expected.to include(:pipelines)
      end
    end

    context 'when builds feature is disabled' do
      before do
        allow(project).to receive(:builds_enabled?).and_return(false)
      end

      it "do not include pipelines tab" do
        is_expected.not_to include(:pipelines)
      end
    end
  end
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455

  describe '#has_projects_or_name?' do
    let(:projects) do
      create(:project)
      Project.all
    end

    it 'returns true when there are projects' do
      expect(helper.has_projects_or_name?(projects, {})).to eq(true)
    end

    it 'returns true when there are no projects but a name is given' do
      expect(helper.has_projects_or_name?(Project.none, name: 'foo')).to eq(true)
    end

    it 'returns false when there are no projects and there is no name' do
      expect(helper.has_projects_or_name?(Project.none, {})).to eq(false)
    end
  end

  describe '#any_projects?' do
456
    let!(:project) { create(:project) }
457 458 459 460 461 462 463 464 465

    it 'returns true when projects will be returned' do
      expect(helper.any_projects?(Project.all)).to eq(true)
    end

    it 'returns false when no projects will be returned' do
      expect(helper.any_projects?(Project.none)).to eq(false)
    end

466 467 468 469 470 471 472 473
    it 'returns true when using a non-empty Array' do
      expect(helper.any_projects?([project])).to eq(true)
    end

    it 'returns false when using an empty Array' do
      expect(helper.any_projects?([])).to eq(false)
    end

474 475 476 477 478 479 480 481 482 483 484
    it 'only executes a single query when a LIMIT is applied' do
      relation = Project.limit(1)
      recorder = ActiveRecord::QueryRecorder.new do
        2.times do
          helper.any_projects?(relation)
        end
      end

      expect(recorder.count).to eq(1)
    end
  end
485
end