BigW Consortium Gitlab

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

describe ProjectsHelper do
Valery Sizov committed
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")
Valery Sizov committed
9 10
    end
  end
11 12 13 14

  describe "can_change_visibility_level?" do
    let(:project) { create(:project) }
    let(:user) { create(:user) }
Douwe Maan committed
15
    let(:fork_project) { Projects::ForkService.new(project, user).execute }
16

17
    it "returns false if there are no appropriate permissions" do
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 49 50 51 52 53 54 55 56 57
  describe 'user_max_access_in_project' do
    let(:project) { create(:project) }
    let(:user) { create(:user) }
    before do
      project.team.add_user(user, Gitlab::Access::MASTER)
    end

    it { expect(helper.user_max_access_in_project(user.id, project)).to eq('Master') }
  end

58 59 60 61 62 63 64 65
  describe "readme_cache_key" do
    let(:project) { create(:project) }

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

    it "returns a valid cach key" do
66
      expect(helper.send(:readme_cache_key)).to eq("#{project.path_with_namespace}-#{project.commit.id}-readme")
67 68 69 70 71
    end

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

72
      expect(helper.send(:readme_cache_key)).to eq("#{project.path_with_namespace}-nil-readme")
73 74
    end
  end
75 76 77 78 79 80 81 82 83 84 85 86 87 88

  describe 'link_to_member' do
    let(:group)   { create(:group) }
    let(:project) { create(:empty_project, group: group) }
    let(:user)    { create(:user) }

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

        expect(link).to match(%r{/u/#{user.username}})
      end
    end
  end
89 90

  describe 'default_clone_protocol' do
91
    context 'when user is not logged in and gitlab protocol is HTTP' do
92
      it 'returns HTTP' do
93
        allow(helper).to receive(:current_user).and_return(nil)
94 95 96 97 98

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

99
    context 'when user is not logged in and gitlab protocol is HTTPS' do
100
      it 'returns HTTPS' do
101 102
        stub_config_setting(protocol: 'https')
        allow(helper).to receive(:current_user).and_return(nil)
103 104 105 106 107

        expect(helper.send(:default_clone_protocol)).to eq('https')
      end
    end
  end
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

  describe '#license_short_name' do
    let(:project) { create(:project) }

    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
134 135 136 137 138 139 140 141 142

  describe '#sanitized_import_error' do
    it 'removes the repo path' do
      repo = File.join(Gitlab.config.gitlab_shell.repos_path, '/namespace/test.git')
      import_error = "Could not clone #{repo}\n"

      expect(sanitize_repo_path(import_error)).to eq('Could not clone [REPOS PATH]/namespace/test.git')
    end
  end
143
end