Read about our upcoming Code of Conduct on this issue

projects.rb 9.7 KB
Newer Older
1
require_relative '../support/helpers/test_env'
2

3
FactoryBot.define do
4
5
6
  # Project without repository
  #
  # Project does not have bare repository.
7
  # Use this factory if you don't need repository in tests
8
  factory :project, class: 'Project' do
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
9
10
    sequence(:name) { |n| "project#{n}" }
    path { name.downcase.gsub(/\s/, '_') }
11
12
13
    # Behaves differently to nil due to cache_has_external_issue_tracker
    has_external_issue_tracker false

14
15
16
17
18
    # Associations
    namespace
    creator { group ? create(:user) : namespace&.owner }

    transient do
19
      # Nest Project Feature attributes
20
21
22
23
24
25
      wiki_access_level ProjectFeature::ENABLED
      builds_access_level ProjectFeature::ENABLED
      snippets_access_level ProjectFeature::ENABLED
      issues_access_level ProjectFeature::ENABLED
      merge_requests_access_level ProjectFeature::ENABLED
      repository_access_level ProjectFeature::ENABLED
26
27
28
29

      # we can't assign the delegated `#ci_cd_settings` attributes directly, as the
      # `#ci_cd_settings` relation needs to be created first
      group_runners_enabled nil
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
    end

    after(:create) do |project, evaluator|
      # Builds and MRs can't have higher visibility level than repository access level.
      builds_access_level = [evaluator.builds_access_level, evaluator.repository_access_level].min
      merge_requests_access_level = [evaluator.merge_requests_access_level, evaluator.repository_access_level].min

      project.project_feature.update_columns(
        wiki_access_level: evaluator.wiki_access_level,
        builds_access_level: builds_access_level,
        snippets_access_level: evaluator.snippets_access_level,
        issues_access_level: evaluator.issues_access_level,
        merge_requests_access_level: merge_requests_access_level,
        repository_access_level: evaluator.repository_access_level)

      # Normally the class Projects::CreateService is used for creating
      # projects, and this class takes care of making sure the owner and current
      # user have access to the project. Our specs don't use said service class,
      # thus we must manually refresh things here.
      unless project.group || project.pending_delete
        project.add_master(project.owner)
      end

      project.group&.refresh_members_authorized_projects
54
55
56

      # assign the delegated `#ci_cd_settings` attributes after create
      project.reload.group_runners_enabled = evaluator.group_runners_enabled unless evaluator.group_runners_enabled.nil?
57
58
    end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
59
60
61
62
63
64
65
66
67
68
69
    trait :public do
      visibility_level Gitlab::VisibilityLevel::PUBLIC
    end

    trait :internal do
      visibility_level Gitlab::VisibilityLevel::INTERNAL
    end

    trait :private do
      visibility_level Gitlab::VisibilityLevel::PRIVATE
    end
70

71
    trait :import_scheduled do
72
      import_status :scheduled
73
74
75
    end

    trait :import_started do
76
      import_status :started
77
78
79
    end

    trait :import_finished do
80
      import_status :finished
81
82
83
    end

    trait :import_failed do
84
      import_status :failed
85
86
    end

87
88
89
90
    trait :archived do
      archived true
    end

Nick Thomas's avatar
Nick Thomas committed
91
92
93
94
    storage_version Project::LATEST_STORAGE_VERSION

    trait :legacy_storage do
      storage_version nil
95
96
    end

97
98
99
100
    trait :access_requestable do
      request_access_enabled true
    end

101
    trait :with_avatar do
102
      avatar { fixture_file_upload('spec/fixtures/dk.png') }
103
104
    end

105
    trait :with_export do
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
      before(:create) do |_project, _evaluator|
        allow(Feature).to receive(:enabled?).with(:import_export_object_storage) { false }
        allow(Feature).to receive(:enabled?).with('import_export_object_storage') { false }
      end

      after(:create) do |project, _evaluator|
        ProjectExportWorker.new.perform(project.creator.id, project.id)
      end
    end

    trait :with_object_export do
      before(:create) do |_project, _evaluator|
        allow(Feature).to receive(:enabled?).with(:import_export_object_storage) { true }
        allow(Feature).to receive(:enabled?).with('import_export_object_storage') { true }
      end

122
123
124
      after(:create) do |project, evaluator|
        ProjectExportWorker.new.perform(project.creator.id, project.id)
      end
125
126
    end

127
128
129
130
131
132
    trait :broken_storage do
      after(:create) do |project|
        project.update_column(:repository_storage, 'broken')
      end
    end

133
    # Test repository - https://gitlab.com/gitlab-org/gitlab-test
134
    trait :repository do
135
136
137
      test_repo

      transient do
138
        create_templates nil
139
140
141
      end

      after :create do |project, evaluator|
142
143
        if evaluator.create_templates
          templates_path = "#{evaluator.create_templates}_templates"
144
145

          project.repository.create_file(
146
147
            project.creator,
            ".gitlab/#{templates_path}/bug.md",
148
149
150
151
            'something valid',
            message: 'test 3',
            branch_name: 'master')
          project.repository.create_file(
152
153
            project.creator,
            ".gitlab/#{templates_path}/template_test.md",
154
155
156
157
            'template_test',
            message: 'test 1',
            branch_name: 'master')
          project.repository.create_file(
158
159
            project.creator,
            ".gitlab/#{templates_path}/feature_proposal.md",
160
161
162
163
164
            'feature_proposal',
            message: 'test 2',
            branch_name: 'master')
        end
      end
165
166
    end

167
168
    trait :empty_repo do
      after(:create) do |project|
169
        raise "Failed to create repository!" unless project.create_repository
170
171
      end
    end
Felipe Artur's avatar
Felipe Artur committed
172

173
174
175
176
177
178
179
180
181
182
183
    trait :remote_mirror do
      transient do
        remote_name "remote_mirror_#{SecureRandom.hex}"
        url "http://foo.com"
        enabled true
      end
      after(:create) do |project, evaluator|
        project.remote_mirrors.create!(url: evaluator.url, enabled: evaluator.enabled)
      end
    end

184
185
186
187
    trait :stubbed_repository do
      after(:build) do |project|
        allow(project).to receive(:empty_repo?).and_return(false)
        allow(project.repository).to receive(:empty?).and_return(false)
188
189
      end
    end
Felipe Artur's avatar
Felipe Artur committed
190

191
192
193
194
195
196
    trait :wiki_repo do
      after(:create) do |project|
        raise 'Failed to create wiki repository!' unless project.create_wiki
      end
    end

Toon Claes's avatar
Toon Claes committed
197
    trait :read_only do
198
199
200
      repository_read_only true
    end

201
202
    trait :broken_repo do
      after(:create) do |project|
203
        raise "Failed to create repository!" unless project.create_repository
204

205
206
        project.gitlab_shell.rm_directory(project.repository_storage,
                                          File.join("#{project.disk_path}.git", 'refs'))
207
208
209
      end
    end

210
211
    trait :test_repo do
      after :create do |project|
212
213
214
        TestEnv.copy_repo(project,
          bare_repo: TestEnv.factory_repo_path_bare,
          refs: TestEnv::BRANCH_SHA)
215
216
217
      end
    end

218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
    trait(:wiki_enabled)            { wiki_access_level ProjectFeature::ENABLED }
    trait(:wiki_disabled)           { wiki_access_level ProjectFeature::DISABLED }
    trait(:wiki_private)            { wiki_access_level ProjectFeature::PRIVATE }
    trait(:builds_enabled)          { builds_access_level ProjectFeature::ENABLED }
    trait(:builds_disabled)         { builds_access_level ProjectFeature::DISABLED }
    trait(:builds_private)          { builds_access_level ProjectFeature::PRIVATE }
    trait(:snippets_enabled)        { snippets_access_level ProjectFeature::ENABLED }
    trait(:snippets_disabled)       { snippets_access_level ProjectFeature::DISABLED }
    trait(:snippets_private)        { snippets_access_level ProjectFeature::PRIVATE }
    trait(:issues_disabled)         { issues_access_level ProjectFeature::DISABLED }
    trait(:issues_enabled)          { issues_access_level ProjectFeature::ENABLED }
    trait(:issues_private)          { issues_access_level ProjectFeature::PRIVATE }
    trait(:merge_requests_enabled)  { merge_requests_access_level ProjectFeature::ENABLED }
    trait(:merge_requests_disabled) { merge_requests_access_level ProjectFeature::DISABLED }
    trait(:merge_requests_private)  { merge_requests_access_level ProjectFeature::PRIVATE }
    trait(:repository_enabled)      { repository_access_level ProjectFeature::ENABLED }
    trait(:repository_disabled)     { repository_access_level ProjectFeature::DISABLED }
    trait(:repository_private)      { repository_access_level ProjectFeature::PRIVATE }
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
236
237
  end

238
239
240
241
  # Project with empty repository
  #
  # This is a case when you just created a project
  # but not pushed any code there yet
242
  factory :project_empty_repo, parent: :project do
243
    empty_repo
244
245
  end

246
247
248
  # Project with broken repository
  #
  # Project with an invalid repository state
249
  factory :project_broken_repo, parent: :project do
250
251
252
    broken_repo
  end

253
  factory :forked_project_with_submodules, parent: :project do
254
255
256
    path { 'forked-gitlabhq' }

    after :create do |project|
257
258
259
      TestEnv.copy_repo(project,
        bare_repo: TestEnv.forked_repo_path_bare,
        refs: TestEnv::FORKED_BRANCH_SHA)
260
261
262
    end
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
263
  factory :redmine_project, parent: :project do
264
265
    has_external_issue_tracker true

266
267
268
269
    after :create do |project|
      project.create_redmine_service(
        active: true,
        properties: {
270
          'project_url' => 'http://redmine/projects/project_name_in_redmine',
271
          'issues_url' => 'http://redmine/projects/project_name_in_redmine/issues/:id',
272
          'new_issue_url' => 'http://redmine/projects/project_name_in_redmine/issues/new'
273
274
275
        }
      )
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
276
  end
277
278

  factory :jira_project, parent: :project do
279
    has_external_issue_tracker true
280
    jira_service
281
  end
282

283
  factory :kubernetes_project, parent: :project do
284
    kubernetes_service
285
  end
286

287
  factory :prometheus_project, parent: :project do
288
289
290
291
    after :create do |project|
      project.create_prometheus_service(
        active: true,
        properties: {
292
          api_url: 'https://prometheus.example.com/',
293
          manual_configuration: true
294
295
296
297
        }
      )
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
298
end