summaryrefslogtreecommitdiff
path: root/spec/helpers/import_helper_spec.rb
blob: af4931e3370da1177b92f1695b595e5db21fe794 (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
require 'rails_helper'

describe ImportHelper do
  describe '#sanitize_project_name' do
    it 'removes leading tildes' do
      expect(helper.sanitize_project_name('~~root')).to eq('root')
    end

    it 'removes whitespace' do
      expect(helper.sanitize_project_name('my test repo')).to eq('my-test-repo')
    end

    it 'removes disallowed characters' do
      expect(helper.sanitize_project_name('Test&me$over*h_ere')).to eq('Test-me-over-h_ere')
    end
  end

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

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

    context 'when current user can create namespaces' do
      it 'returns project namespace' do
        user.update_attribute(:can_create_group, true)

        expect(helper.import_project_target('asd', 'vim')).to eq 'asd/vim'
      end
    end

    context 'when current user can not create namespaces' do
      it "takes the current user's namespace" do
        user.update_attribute(:can_create_group, false)

        expect(helper.import_project_target('asd', 'vim')).to eq "#{user.namespace_path}/vim"
      end
    end
  end

  describe '#provider_project_link' do
    context 'when provider is "github"' do
      let(:github_server_url) { nil }
      let(:provider) { OpenStruct.new(name: 'github', url: github_server_url) }

      before do
        stub_omniauth_setting(providers: [provider])
      end

      context 'when provider does not specify a custom URL' do
        it 'uses default GitHub URL' do
          expect(helper.provider_project_link('github', 'octocat/Hello-World'))
          .to include('href="https://github.com/octocat/Hello-World"')
        end
      end

      context 'when provider specify a custom URL' do
        let(:github_server_url) { 'https://github.company.com' }

        it 'uses custom URL' do
          expect(helper.provider_project_link('github', 'octocat/Hello-World'))
          .to include('href="https://github.company.com/octocat/Hello-World"')
        end
      end

      context "when custom URL contains a '/' char at the end" do
        let(:github_server_url) { 'https://github.company.com/' }

        it "doesn't render double slash" do
          expect(helper.provider_project_link('github', 'octocat/Hello-World'))
          .to include('href="https://github.company.com/octocat/Hello-World"')
        end
      end

      context 'when provider is missing' do
        it 'uses the default URL' do
          allow(Gitlab.config.omniauth).to receive(:providers).and_return([])

          expect(helper.provider_project_link('github', 'octocat/Hello-World'))
          .to include('href="https://github.com/octocat/Hello-World"')
        end
      end
    end

    context 'when provider is "gitea"' do
      before do
        assign(:gitea_host_url, 'https://try.gitea.io/')
      end

      it 'uses given host' do
        expect(helper.provider_project_link('gitea', 'octocat/Hello-World'))
        .to include('href="https://try.gitea.io/octocat/Hello-World"')
      end
    end
  end
end