create_service_spec.rb 3.29 KB
Newer Older
1
2
require 'spec_helper'

3
describe Projects::CreateService do
4
5
6
7
  describe :create_by_user do
    before do
      @user = create :user
      @opts = {
8
9
        name: "GitLab",
        namespace: @user.namespace
10
11
12
      }
    end

13
14
15
16
17
18
19
    it 'creates services on Project creation' do
      project = create_project(@user, @opts)
      project.reload

      expect(project.services).not_to be_empty
    end

Valery Sizov's avatar
Valery Sizov committed
20
21
22
23
24
25
26
27
    it 'creates labels on Project creation if there are templates' do
      Label.create(title: "bug", template: true)
      project = create_project(@user, @opts)
      project.reload

      expect(project.labels).not_to be_empty
    end

28
29
30
31
32
    context 'user namespace' do
      before do
        @project = create_project(@user, @opts)
      end

33
34
35
      it { expect(@project).to be_valid }
      it { expect(@project.owner).to eq(@user) }
      it { expect(@project.namespace).to eq(@user.namespace) }
36
37
38
39
    end

    context 'group namespace' do
      before do
40
41
42
        @group = create :group
        @group.add_owner(@user)

43
44
45
46
        @opts.merge!(namespace_id: @group.id)
        @project = create_project(@user, @opts)
      end

47
48
49
      it { expect(@project).to be_valid }
      it { expect(@project.owner).to eq(@group) }
      it { expect(@project.namespace).to eq(@group) }
50
    end
51

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
52
53
54
55
    context 'wiki_enabled creates repository directory' do
      context 'wiki_enabled true creates wiki repository directory' do
        before do
          @project = create_project(@user, @opts)
56
          @path = ProjectWiki.new(@project, @user).send(:path_to_repo)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
57
58
        end

59
        it { expect(File.exists?(@path)).to be_truthy }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
60
61
62
63
64
65
      end

      context 'wiki_enabled false does not create wiki repository directory' do
        before do
          @opts.merge!(wiki_enabled: false)
          @project = create_project(@user, @opts)
66
          @path = ProjectWiki.new(@project, @user).send(:path_to_repo)
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
67
68
        end

69
        it { expect(File.exists?(@path)).to be_falsey }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
70
71
      end
    end
72
73
74

    context 'restricted visibility level' do
      before do
75
        stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91

        @opts.merge!(
          visibility_level: Gitlab::VisibilityLevel.options['Public']
        )
      end

      it 'should not allow a restricted visibility level for non-admins' do
        project = create_project(@user, @opts)
        expect(project).to respond_to(:errors)
        expect(project.errors.messages).to have_key(:visibility_level)
        expect(project.errors.messages[:visibility_level].first).to(
          match('restricted by your GitLab administrator')
        )
      end

      it 'should allow a restricted visibility level for admins' do
92
93
94
        admin = create(:admin)
        project = create_project(admin, @opts)

95
96
97
98
        expect(project.errors.any?).to be(false)
        expect(project.saved?).to be(true)
      end
    end
99
100
101
102
103
104
105
106
107
108
109

    context 'repository creation' do
      it 'should synchronously create the repository' do
        expect_any_instance_of(Project).to receive(:create_repository)

        project = create_project(@user, @opts)
        expect(project).to be_valid
        expect(project.owner).to eq(@user)
        expect(project.namespace).to eq(@user.namespace)
      end
    end
110
111
112
  end

  def create_project(user, opts)
113
    Projects::CreateService.new(user, opts).execute
114
115
  end
end