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

describe ProjectsHelper do
Valery Sizov's avatar
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's avatar
Valery Sizov committed
9 10
    end
  end
Valery Sizov's avatar
Valery Sizov committed
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 }
Douwe Maan's avatar
Douwe Maan committed
15
    let(:fork_project) { Projects::ForkService.new(project, user).execute }
Valery Sizov's avatar
Valery Sizov committed
16

17
    it "returns false if there are no appropriate permissions" do
Valery Sizov's avatar
Valery Sizov committed
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

  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
56
      expect(helper.send(:readme_cache_key)).to eq("#{project.path_with_namespace}-#{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.path_with_namespace}-nil-readme")
63 64
    end
  end
65 66 67 68 69 70 71 72 73 74

  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)

75
        expect(link).to match(%r{/#{user.username}})
76 77 78
      end
    end
  end
79 80

  describe 'default_clone_protocol' do
81
    context 'when user is not logged in and gitlab protocol is HTTP' do
82
      it 'returns HTTP' do
83
        allow(helper).to receive(:current_user).and_return(nil)
84 85 86 87 88

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

89
    context 'when user is not logged in and gitlab protocol is HTTPS' do
90
      it 'returns HTTPS' do
91 92
        stub_config_setting(protocol: 'https')
        allow(helper).to receive(:current_user).and_return(nil)
93 94 95 96

        expect(helper.send(:default_clone_protocol)).to eq('https')
      end
    end
97 98 99 100 101 102 103 104 105

    context 'when gitlab.config.kerberos is enabled and user is logged in' do
      it 'returns krb5 as default protocol' do
        allow(Gitlab.config.kerberos).to receive(:enabled).and_return(true)
        allow(helper).to receive(:current_user).and_return(double)

        expect(helper.send(:default_clone_protocol)).to eq('krb5')
      end
    end
106
  end
107 108

  describe '#license_short_name' do
109
    let(:project) { create(:empty_project) }
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    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
133 134

  describe '#sanitized_import_error' do
135 136 137 138 139 140
    let(:project) { create(:project) }

    before do
      allow(project).to receive(:repository_storage_path).and_return('/base/repo/path')
    end

141
    it 'removes the repo path' do
142
      repo = '/base/repo/path/namespace/test.git'
143 144
      import_error = "Could not clone #{repo}\n"

145
      expect(sanitize_repo_path(project, import_error)).to eq('Could not clone [REPOS PATH]/namespace/test.git')
146 147
    end
  end
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 177 178 179 180 181 182 183 184 185

  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
Felipe Artur's avatar
Felipe Artur committed
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229

  describe "#project_feature_access_select" do
    let(:project) { create(:empty_project, :public) }
    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
      before { project.update_attributes(visibility_level: Gitlab::VisibilityLevel::PRIVATE) }

      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")
        expect(result).not_to include("Everyone with access")
      end
    end

    context "when project moves from public to private" do
      before do
        project.project_feature.update_attributes(issues_access_level: ProjectFeature::ENABLED)
        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")
        expect(result).not_to include("Everyone with access")
        expect(result).to have_selector('option[selected]', text: "Only team members")
      end
    end
  end
230
end