BigW Consortium Gitlab

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

Dmitriy Zaporozhets committed
3
FactoryGirl.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 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 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
    # Associations
    namespace
    creator { group ? create(:user) : namespace&.owner }

    # Nest Project Feature attributes
    transient do
      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
    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
    end

Dmitriy Zaporozhets committed
52 53 54 55 56 57 58 59 60 61 62
    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
63

64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
    trait :import_scheduled do
      import_status :scheduled
    end

    trait :import_started do
      import_status :started
    end

    trait :import_finished do
      import_status :finished
    end

    trait :import_failed do
      import_status :failed
    end

80 81 82 83
    trait :archived do
      archived true
    end

84
    trait :hashed do
85
      storage_version Project::LATEST_STORAGE_VERSION
86 87
    end

88 89 90 91
    trait :access_requestable do
      request_access_enabled true
    end

92 93 94 95
    trait :with_avatar do
      avatar { File.open(Rails.root.join('spec/fixtures/dk.png')) }
    end

96 97 98 99 100 101
    trait :broken_storage do
      after(:create) do |project|
        project.update_column(:repository_storage, 'broken')
      end
    end

102
    # Test repository - https://gitlab.com/gitlab-org/gitlab-test
103
    trait :repository do
104 105 106
      test_repo

      transient do
107
        create_templates nil
108 109 110
      end

      after :create do |project, evaluator|
111 112
        if evaluator.create_templates
          templates_path = "#{evaluator.create_templates}_templates"
113 114

          project.repository.create_file(
115 116
            project.creator,
            ".gitlab/#{templates_path}/bug.md",
117 118 119 120
            'something valid',
            message: 'test 3',
            branch_name: 'master')
          project.repository.create_file(
121 122
            project.creator,
            ".gitlab/#{templates_path}/template_test.md",
123 124 125 126
            'template_test',
            message: 'test 1',
            branch_name: 'master')
          project.repository.create_file(
127 128
            project.creator,
            ".gitlab/#{templates_path}/feature_proposal.md",
129 130 131 132 133
            'feature_proposal',
            message: 'test 2',
            branch_name: 'master')
        end
      end
134 135
    end

136 137
    trait :empty_repo do
      after(:create) do |project|
138
        raise "Failed to create repository!" unless project.create_repository
139 140 141

        # We delete hooks so that gitlab-shell will not try to authenticate with
        # an API that isn't running
142
        FileUtils.rm_r(File.join(project.repository_storage_path, "#{project.disk_path}.git", 'hooks'))
143 144
      end
    end
145

146 147 148 149 150 151
    trait :wiki_repo do
      after(:create) do |project|
        raise 'Failed to create wiki repository!' unless project.create_wiki
      end
    end

152
    trait :read_only do
153 154 155
      repository_read_only true
    end

156 157
    trait :broken_repo do
      after(:create) do |project|
158
        raise "Failed to create repository!" unless project.create_repository
159

160
        FileUtils.rm_r(File.join(project.repository_storage_path, "#{project.disk_path}.git", 'refs'))
161 162 163
      end
    end

164 165
    trait :test_repo do
      after :create do |project|
166 167 168
        TestEnv.copy_repo(project,
          bare_repo: TestEnv.factory_repo_path_bare,
          refs: TestEnv::BRANCH_SHA)
169 170 171
      end
    end

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    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 committed
190 191
  end

192 193 194 195
  # Project with empty repository
  #
  # This is a case when you just created a project
  # but not pushed any code there yet
196
  factory :project_empty_repo, parent: :project do
197
    empty_repo
198 199
  end

200 201 202
  # Project with broken repository
  #
  # Project with an invalid repository state
203
  factory :project_broken_repo, parent: :project do
204 205 206
    broken_repo
  end

207
  factory :forked_project_with_submodules, parent: :project do
208 209 210
    path { 'forked-gitlabhq' }

    after :create do |project|
211 212 213
      TestEnv.copy_repo(project,
        bare_repo: TestEnv.forked_repo_path_bare,
        refs: TestEnv::FORKED_BRANCH_SHA)
214 215 216
    end
  end

Dmitriy Zaporozhets committed
217
  factory :redmine_project, parent: :project do
218 219
    has_external_issue_tracker true

220 221 222 223
    after :create do |project|
      project.create_redmine_service(
        active: true,
        properties: {
224
          'project_url' => 'http://redmine/projects/project_name_in_redmine',
225
          'issues_url' => 'http://redmine/projects/project_name_in_redmine/issues/:id',
226
          'new_issue_url' => 'http://redmine/projects/project_name_in_redmine/issues/new'
227 228 229
        }
      )
    end
Dmitriy Zaporozhets committed
230
  end
231 232

  factory :jira_project, parent: :project do
233
    has_external_issue_tracker true
234
    jira_service
235
  end
236

237
  factory :kubernetes_project, parent: :project do
238
    kubernetes_service
239
  end
240

241
  factory :prometheus_project, parent: :project do
242 243 244 245 246 247 248 249 250
    after :create do |project|
      project.create_prometheus_service(
        active: true,
        properties: {
          api_url: 'https://prometheus.example.com'
        }
      )
    end
  end
Dmitriy Zaporozhets committed
251
end