projects.rb 6.75 KB
Newer Older
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
1
FactoryGirl.define do
2 3 4
  # Project without repository
  #
  # Project does not have bare repository.
5
  # Use this factory if you don't need repository in tests
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
6 7 8 9 10 11
  factory :empty_project, class: 'Project' do
    sequence(:name) { |n| "project#{n}" }
    path { name.downcase.gsub(/\s/, '_') }
    namespace
    creator

12 13 14
    # Behaves differently to nil due to cache_has_external_issue_tracker
    has_external_issue_tracker false

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
15 16 17 18 19 20 21 22 23 24 25
    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
26

27 28 29 30
    trait :archived do
      archived true
    end

31 32 33 34
    trait :access_requestable do
      request_access_enabled true
    end

35 36 37 38
    trait :repository do
      # no-op... for now!
    end

39 40 41
    trait :empty_repo do
      after(:create) do |project|
        project.create_repository
42 43 44 45

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

49 50 51 52 53 54 55 56
    trait :broken_repo do
      after(:create) do |project|
        project.create_repository

        FileUtils.rm_r(File.join(project.repository_storage_path, "#{project.path_with_namespace}.git", 'refs'))
      end
    end

57 58 59 60 61 62
    trait :test_repo do
      after :create do |project|
        TestEnv.copy_repo(project)
      end
    end

63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    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 }

82 83 84 85 86 87 88
    # 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
89
      repository_access_level ProjectFeature::ENABLED
90 91 92
    end

    after(:create) do |project, evaluator|
93 94 95 96
      # 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

97
      project.project_feature.
98
        update_attributes!(
99
          wiki_access_level: evaluator.wiki_access_level,
100
          builds_access_level: builds_access_level,
101 102
          snippets_access_level: evaluator.snippets_access_level,
          issues_access_level: evaluator.issues_access_level,
103
          merge_requests_access_level: merge_requests_access_level,
104
          repository_access_level: evaluator.repository_access_level
105 106
        )
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
107 108
  end

109 110 111 112 113
  # Project with empty repository
  #
  # This is a case when you just created a project
  # but not pushed any code there yet
  factory :project_empty_repo, parent: :empty_project do
114
    empty_repo
115 116
  end

117 118 119 120 121 122 123
  # Project with broken repository
  #
  # Project with an invalid repository state
  factory :project_broken_repo, parent: :empty_project do
    broken_repo
  end

124 125 126 127
  # Project with test repository
  #
  # Test repository source can be found at
  # https://gitlab.com/gitlab-org/gitlab-test
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
128 129 130
  factory :project, parent: :empty_project do
    path { 'gitlabhq' }

131
    test_repo
132

133 134 135 136 137
    transient do
      create_template nil
    end

    after :create do |project, evaluator|
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
138
      TestEnv.copy_repo(project)
139 140 141 142 143 144

      if evaluator.create_template
        args = evaluator.create_template

        project.add_user(args[:user], args[:access])

145
        project.repository.create_file(
146 147 148 149
          args[:user],
          ".gitlab/#{args[:path]}/bug.md",
          'something valid',
          message: 'test 3',
150 151
          branch_name: 'master')
        project.repository.create_file(
152 153 154 155
          args[:user],
          ".gitlab/#{args[:path]}/template_test.md",
          'template_test',
          message: 'test 1',
156 157
          branch_name: 'master')
        project.repository.create_file(
158 159 160 161
          args[:user],
          ".gitlab/#{args[:path]}/feature_proposal.md",
          'feature_proposal',
          message: 'test 2',
162
          branch_name: 'master')
163
      end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
164 165 166
    end
  end

167 168 169 170 171 172 173 174
  factory :forked_project_with_submodules, parent: :empty_project do
    path { 'forked-gitlabhq' }

    after :create do |project|
      TestEnv.copy_forked_repo_with_submodules(project)
    end
  end

Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
175
  factory :redmine_project, parent: :project do
176 177
    has_external_issue_tracker true

178 179 180 181
    after :create do |project|
      project.create_redmine_service(
        active: true,
        properties: {
182 183 184
          'project_url' => 'http://redmine/projects/project_name_in_redmine',
          'issues_url' => "http://redmine/#{project.id}/project_name_in_redmine/:id",
          'new_issue_url' => 'http://redmine/projects/project_name_in_redmine/issues/new'
185 186 187
        }
      )
    end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
188
  end
189 190

  factory :jira_project, parent: :project do
191
    has_external_issue_tracker true
192
    jira_service
193
  end
194 195

  factory :kubernetes_project, parent: :empty_project do
196
    kubernetes_service
197
  end
198 199 200 201 202 203 204 205 206 207 208

  factory :prometheus_project, parent: :empty_project do
    after :create do |project|
      project.create_prometheus_service(
        active: true,
        properties: {
          api_url: 'https://prometheus.example.com'
        }
      )
    end
  end
Dmitriy Zaporozhets's avatar
Dmitriy Zaporozhets committed
209
end