update_service_spec.rb 4.69 KB
Newer Older
1
2
require 'spec_helper'

3
describe Projects::UpdateService, '#execute', :services do
4
5
  let(:user) { create(:user) }
  let(:admin) { create(:admin) }
6

7
8
9
10
11
  let(:project) do
    create(:empty_project, creator: user, namespace: user.namespace)
  end

  context 'when changing visibility level' do
12
13
14
    context 'when visibility_level is INTERNAL' do
      it 'updates the project to internal' do
        result = update_project(project, user, visibility_level: Gitlab::VisibilityLevel::INTERNAL)
15

16
17
        expect(result).to eq({ status: :success })
        expect(project).to be_internal
18
19
20
      end
    end

21
22
23
24
25
    context 'when visibility_level is PUBLIC' do
      it 'updates the project to public' do
        result = update_project(project, user, visibility_level: Gitlab::VisibilityLevel::PUBLIC)
        expect(result).to eq({ status: :success })
        expect(project).to be_public
26
27
28
      end
    end

29
    context 'when visibility levels are restricted to PUBLIC only' do
30
      before do
31
        stub_application_setting(restricted_visibility_levels: [Gitlab::VisibilityLevel::PUBLIC])
32
33
      end

34
35
36
37
38
      context 'when visibility_level is INTERNAL' do
        it 'updates the project to internal' do
          result = update_project(project, user, visibility_level: Gitlab::VisibilityLevel::INTERNAL)
          expect(result).to eq({ status: :success })
          expect(project).to be_internal
39
40
41
        end
      end

42
43
44
      context 'when visibility_level is PUBLIC' do
        it 'does not update the project to public' do
          result = update_project(project, user, visibility_level: Gitlab::VisibilityLevel::PUBLIC)
45

46
          expect(result).to eq({ status: :error, message: 'New visibility level not allowed!' })
47
          expect(project).to be_private
48
49
        end

50
51
52
53
54
55
        context 'when updated by an admin' do
          it 'updates the project to public' do
            result = update_project(project, admin, visibility_level: Gitlab::VisibilityLevel::PUBLIC)
            expect(result).to eq({ status: :success })
            expect(project).to be_public
          end
56
57
58
59
60
        end
      end
    end
  end

61
  describe 'when updating project that has forks' do
62
    let(:project) { create(:empty_project, :internal) }
63
    let(:forked_project) { create(:forked_project_with_submodules, :internal) }
64
65

    before do
66
67
      forked_project.build_forked_project_link(forked_to_project_id: forked_project.id,
                                               forked_from_project_id: project.id)
68
69
70
      forked_project.save
    end

71
72
73
74
75
    it 'updates forks visibility level when parent set to more restrictive' do
      opts = { visibility_level: Gitlab::VisibilityLevel::PRIVATE }

      expect(project).to be_internal
      expect(forked_project).to be_internal
76

77
78
79
80
      expect(update_project(project, admin, opts)).to eq({ status: :success })

      expect(project).to be_private
      expect(forked_project.reload).to be_private
81
82
    end

83
84
85
86
87
    it 'does not update forks visibility level when parent set to less restrictive' do
      opts = { visibility_level: Gitlab::VisibilityLevel::PUBLIC }

      expect(project).to be_internal
      expect(forked_project).to be_internal
88

89
90
91
92
      expect(update_project(project, admin, opts)).to eq({ status: :success })

      expect(project).to be_public
      expect(forked_project.reload).to be_internal
93
94
95
    end
  end

96
97
98
99
100
101
102
103
104
105
  context 'when updating a default branch' do
    let(:project) { create(:project, :repository) }

    it 'changes a default branch' do
      update_project(project, admin, default_branch: 'feature')

      expect(Project.find(project.id).default_branch).to eq 'feature'
    end
  end

106
  context 'when updating a project that contains container images' do
107
108
109
110
111
112
113
114
115
116
117
118
    before do
      stub_container_registry_config(enabled: true)
      stub_container_registry_tags(repository: /image/, tags: %w[rc1])
      create(:container_repository, project: project, name: :image)
    end

    it 'does not allow to rename the project' do
      result = update_project(project, admin, path: 'renamed')

      expect(result).to include(status: :error)
      expect(result[:message]).to match(/contains container registry tags/)
    end
119
120
121
122
123
124
125

    it 'allows to update other settings' do
      result = update_project(project, admin, public_builds: true)

      expect(result[:status]).to eq :success
      expect(project.reload.public_builds).to be true
    end
126
127
  end

128
129
130
  context 'when passing invalid parameters' do
    it 'returns an error result when record cannot be updated' do
      result = update_project(project, admin, { name: 'foo&bar' })
131

132
133
134
      expect(result).to eq({ status: :error,
                             message: 'Project could not be updated!' })
    end
135
136
  end

137
  def update_project(project, user, opts)
138
    described_class.new(project, user, opts).execute
139
  end
140
end