summaryrefslogtreecommitdiff
path: root/spec/lib/gitlab/legacy_github_import/project_creator_spec.rb
blob: 02cc2eba4da0b0166159867c67f8b1925b047703 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# frozen_string_literal: true

require 'spec_helper'

RSpec.describe Gitlab::LegacyGithubImport::ProjectCreator do
  let(:user) { create(:user) }
  let(:namespace) { create(:group) }

  let(:repo) do
    OpenStruct.new(
      login: 'vim',
      name: 'vim',
      full_name: 'asd/vim',
      clone_url: 'https://gitlab.com/asd/vim.git'
    )
  end

  subject(:service) { described_class.new(repo, repo.name, namespace, user, github_access_token: 'asdffg') }

  before do
    namespace.add_owner(user)

    expect_next_instance_of(Project) do |project|
      expect(project).to receive(:add_import_job)
    end
  end

  describe '#execute' do
    it 'creates a project' do
      expect { service.execute }.to change(Project, :count).by(1)
    end

    it 'handle GitHub credentials' do
      project = service.execute

      expect(project.import_url).to eq('https://asdffg@gitlab.com/asd/vim.git')
      expect(project.safe_import_url).to eq('https://*****@gitlab.com/asd/vim.git')
      expect(project.import_data.credentials).to eq(user: 'asdffg', password: nil)
    end

    context 'when GitHub project is private' do
      it 'sets project visibility to private' do
        repo.private = true

        project = service.execute

        expect(project.visibility_level).to eq(Gitlab::VisibilityLevel::PRIVATE)
      end
    end

    context 'when GitHub project is public' do
      it 'sets project visibility to namespace visibility level' do
        repo.private = false
        project = service.execute

        expect(project.visibility_level).to eq(namespace.visibility_level)
      end

      context 'when importing into a user namespace' do
        subject(:service) { described_class.new(repo, repo.name, user.namespace, user, github_access_token: 'asdffg') }

        it 'sets project visibility to user namespace visibility level' do
          repo.private = false
          project = service.execute

          expect(project.visibility_level).to eq(user.namespace.visibility_level)
        end
      end
    end

    context 'when visibility level is restricted' do
      context 'when GitHub project is private' do
        before do
          stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PRIVATE])
          allow_any_instance_of(ApplicationSetting).to receive(:default_project_visibility).and_return(Gitlab::VisibilityLevel::INTERNAL)
        end

        it 'sets project visibility to the default project visibility' do
          repo.private = true

          project = service.execute

          expect(project.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
        end
      end

      context 'when GitHub project is public' do
        before do
          stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
          allow_any_instance_of(ApplicationSetting).to receive(:default_project_visibility).and_return(Gitlab::VisibilityLevel::INTERNAL)
        end

        it 'sets project visibility to the default project visibility' do
          repo.private = false

          project = service.execute

          expect(project.visibility_level).to eq(Gitlab::VisibilityLevel::INTERNAL)
        end
      end
    end

    context 'when GitHub project has wiki' do
      it 'does not create the wiki repository' do
        allow(repo).to receive(:has_wiki?).and_return(true)

        project = service.execute

        expect(project.wiki.repository_exists?).to eq false
      end
    end

    context 'when GitHub project does not have wiki' do
      it 'creates the wiki repository' do
        allow(repo).to receive(:has_wiki?).and_return(false)

        project = service.execute

        expect(project.wiki.repository_exists?).to eq true
      end
    end
  end
end