2019-10-06 23:07:19 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2012-08-28 01:42:28 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2022-10-21 08:11:29 -04:00
|
|
|
# `:saas` is used to test `gitlab_subscription` factory.
|
|
|
|
# It's not available on FOSS but also this very factory is not.
|
|
|
|
RSpec.describe 'factories', :saas do
|
2020-12-18 13:10:03 -05:00
|
|
|
include Database::DatabaseHelpers
|
2020-12-10 07:09:43 -05:00
|
|
|
|
2022-10-21 08:11:29 -04:00
|
|
|
# Used in `skipped` and indicates whether to skip any traits including the
|
|
|
|
# plain factory.
|
|
|
|
any = Object.new
|
|
|
|
|
2021-03-03 16:11:13 -05:00
|
|
|
# https://gitlab.com/groups/gitlab-org/-/epics/5464 tracks the remaining
|
2022-10-21 08:11:29 -04:00
|
|
|
# skipped factories or traits.
|
2021-03-03 16:11:13 -05:00
|
|
|
#
|
|
|
|
# Consider adding a code comment if a trait cannot produce a valid object.
|
2022-10-21 08:11:29 -04:00
|
|
|
skipped = [
|
|
|
|
[:audit_event, :unauthenticated],
|
|
|
|
[:ci_build_trace_chunk, :fog_with_data],
|
|
|
|
[:ci_job_artifact, :remote_store],
|
|
|
|
[:ci_job_artifact, :raw],
|
|
|
|
[:ci_job_artifact, :gzip],
|
|
|
|
[:ci_job_artifact, :correct_checksum],
|
|
|
|
[:environment, :non_playable],
|
|
|
|
[:composer_cache_file, :object_storage],
|
|
|
|
[:debian_project_component_file, :object_storage],
|
|
|
|
[:debian_project_distribution, :object_storage],
|
|
|
|
[:debian_file_metadatum, :unknown],
|
|
|
|
[:issue_customer_relations_contact, :for_contact],
|
|
|
|
[:issue_customer_relations_contact, :for_issue],
|
|
|
|
[:package_file, :object_storage],
|
|
|
|
[:rpm_repository_file, :object_storage],
|
|
|
|
[:pages_domain, :without_certificate],
|
|
|
|
[:pages_domain, :without_key],
|
|
|
|
[:pages_domain, :with_missing_chain],
|
|
|
|
[:pages_domain, :with_trusted_chain],
|
|
|
|
[:pages_domain, :with_trusted_expired_chain],
|
|
|
|
[:pages_domain, :explicit_ecdsa],
|
|
|
|
[:project_member, :blocked],
|
|
|
|
[:remote_mirror, :ssh],
|
|
|
|
[:user_preference, :only_comments],
|
|
|
|
[:ci_pipeline_artifact, :remote_store],
|
|
|
|
# EE
|
|
|
|
[:dast_profile, :with_dast_site_validation],
|
|
|
|
[:ee_ci_build, :dependency_scanning_report],
|
|
|
|
[:ee_ci_build, :license_scan_v1],
|
|
|
|
[:ee_ci_job_artifact, :v1],
|
|
|
|
[:ee_ci_job_artifact, :v1_1],
|
|
|
|
[:ee_ci_job_artifact, :v2],
|
|
|
|
[:ee_ci_job_artifact, :v2_1],
|
|
|
|
[:geo_ci_secure_file_state, any],
|
|
|
|
[:geo_event_log, :geo_event],
|
|
|
|
[:geo_job_artifact_state, any],
|
|
|
|
[:geo_lfs_object_state, any],
|
|
|
|
[:geo_pages_deployment_state, any],
|
|
|
|
[:geo_upload_state, any],
|
|
|
|
[:geo_ci_secure_file_state, any],
|
|
|
|
[:lfs_object, :checksum_failure],
|
|
|
|
[:lfs_object, :checksummed],
|
|
|
|
[:merge_request, :blocked],
|
|
|
|
[:merge_request_diff, :verification_failed],
|
|
|
|
[:merge_request_diff, :verification_succeeded],
|
|
|
|
[:package_file, :verification_failed],
|
|
|
|
[:package_file, :verification_succeeded],
|
|
|
|
[:project, :with_vulnerabilities],
|
|
|
|
[:scan_execution_policy, :with_schedule_and_agent],
|
|
|
|
[:vulnerability, :with_cluster_image_scanning_finding],
|
|
|
|
[:vulnerability, :with_findings],
|
|
|
|
[:vulnerability_export, :finished]
|
|
|
|
].freeze
|
2021-02-22 07:10:38 -05:00
|
|
|
|
2020-10-09 23:08:52 -04:00
|
|
|
shared_examples 'factory' do |factory|
|
2022-10-21 08:11:29 -04:00
|
|
|
skip_any = skipped.include?([factory.name, any])
|
|
|
|
|
2016-03-29 07:39:27 -04:00
|
|
|
describe "#{factory.name} factory" do
|
2017-05-31 09:43:19 -04:00
|
|
|
it 'does not raise error when built' do
|
2022-10-21 08:11:29 -04:00
|
|
|
# We use `skip` here because using `build` mostly work even if
|
|
|
|
# factories break when creating them.
|
|
|
|
skip 'Factory skipped linting due to legacy error' if skip_any
|
|
|
|
|
2017-05-31 09:43:19 -04:00
|
|
|
expect { build(factory.name) }.not_to raise_error
|
|
|
|
end
|
2016-03-29 07:39:27 -04:00
|
|
|
|
2016-05-23 14:16:35 -04:00
|
|
|
it 'does not raise error when created' do
|
2022-10-21 08:11:29 -04:00
|
|
|
pending 'Factory skipped linting due to legacy error' if skip_any
|
|
|
|
|
2021-04-13 23:09:18 -04:00
|
|
|
expect { create(factory.name) }.not_to raise_error # rubocop:disable Rails/SaveBang
|
2016-03-29 07:39:27 -04:00
|
|
|
end
|
|
|
|
|
2017-05-31 09:43:19 -04:00
|
|
|
factory.definition.defined_traits.map(&:name).each do |trait_name|
|
2022-10-21 08:11:29 -04:00
|
|
|
skip_trait = skip_any || skipped.include?([factory.name, trait_name.to_sym])
|
|
|
|
|
2021-02-22 07:10:38 -05:00
|
|
|
describe "linting :#{trait_name} trait" do
|
|
|
|
it 'does not raise error when created' do
|
2022-10-21 08:11:29 -04:00
|
|
|
pending 'Trait skipped linting due to legacy error' if skip_trait
|
2021-02-22 07:10:38 -05:00
|
|
|
|
2017-05-31 09:43:19 -04:00
|
|
|
expect { create(factory.name, trait_name) }.not_to raise_error
|
|
|
|
end
|
|
|
|
end
|
2016-03-29 07:39:27 -04:00
|
|
|
end
|
2012-08-28 01:42:28 -04:00
|
|
|
end
|
|
|
|
end
|
2020-10-09 23:08:52 -04:00
|
|
|
|
|
|
|
# FactoryDefault speed up specs by creating associations only once
|
|
|
|
# and reuse them in other factories.
|
|
|
|
#
|
|
|
|
# However, for some factories we cannot use FactoryDefault because the
|
2021-02-18 01:09:43 -05:00
|
|
|
# associations must be unique and cannot be reused, or the factory default
|
|
|
|
# is being mutated.
|
2020-10-09 23:08:52 -04:00
|
|
|
skip_factory_defaults = %i[
|
2021-06-10 17:10:02 -04:00
|
|
|
ci_job_token_project_scope_link
|
2022-10-21 08:11:29 -04:00
|
|
|
ci_subscriptions_project
|
2021-03-17 11:09:03 -04:00
|
|
|
evidence
|
|
|
|
exported_protected_branch
|
2020-10-09 23:08:52 -04:00
|
|
|
fork_network_member
|
2021-02-18 01:09:43 -05:00
|
|
|
group_member
|
|
|
|
import_state
|
2021-10-19 11:12:08 -04:00
|
|
|
issue_customer_relations_contact
|
2021-10-25 08:10:19 -04:00
|
|
|
member_task
|
2022-10-21 08:11:29 -04:00
|
|
|
merge_request_block
|
2021-03-17 11:09:03 -04:00
|
|
|
milestone_release
|
2021-02-18 01:09:43 -05:00
|
|
|
namespace
|
2021-09-15 11:10:13 -04:00
|
|
|
project_namespace
|
2021-05-19 05:10:19 -04:00
|
|
|
project_repository
|
2022-10-21 08:11:29 -04:00
|
|
|
project_security_setting
|
2021-02-22 07:10:38 -05:00
|
|
|
prometheus_alert
|
|
|
|
prometheus_alert_event
|
|
|
|
prometheus_metric
|
2021-03-17 11:09:03 -04:00
|
|
|
protected_branch
|
|
|
|
protected_branch_merge_access_level
|
|
|
|
protected_branch_push_access_level
|
2022-10-21 08:11:29 -04:00
|
|
|
protected_branch_unprotect_access_level
|
2021-03-17 11:09:03 -04:00
|
|
|
protected_tag
|
2022-10-21 08:11:29 -04:00
|
|
|
protected_tag_create_access_level
|
2021-03-17 11:09:03 -04:00
|
|
|
release
|
|
|
|
release_link
|
2021-02-22 07:10:38 -05:00
|
|
|
self_managed_prometheus_alert_event
|
2021-05-19 05:10:19 -04:00
|
|
|
shard
|
2021-02-18 01:09:43 -05:00
|
|
|
users_star_project
|
2022-10-21 08:11:29 -04:00
|
|
|
vulnerabilities_finding_identifier
|
2021-02-18 01:09:43 -05:00
|
|
|
wiki_page
|
2021-02-22 07:10:38 -05:00
|
|
|
wiki_page_meta
|
2020-10-09 23:08:52 -04:00
|
|
|
].to_set.freeze
|
|
|
|
|
2020-12-10 07:09:43 -05:00
|
|
|
# Some factories and their corresponding models are based on
|
|
|
|
# database views. In order to use those, we have to swap the
|
|
|
|
# view out with a table of the same structure.
|
|
|
|
factories_based_on_view = %i[
|
|
|
|
postgres_index
|
|
|
|
postgres_index_bloat_estimate
|
2022-07-07 08:09:35 -04:00
|
|
|
postgres_autovacuum_activity
|
2020-12-10 07:09:43 -05:00
|
|
|
].to_set.freeze
|
|
|
|
|
2020-10-09 23:08:52 -04:00
|
|
|
without_fd, with_fd = FactoryBot.factories
|
|
|
|
.partition { |factory| skip_factory_defaults.include?(factory.name) }
|
|
|
|
|
2022-10-21 08:11:29 -04:00
|
|
|
# Some EE models check licensed features so stub them.
|
|
|
|
shared_context 'with licensed features' do
|
|
|
|
licensed_features = %i[
|
|
|
|
board_milestone_lists
|
|
|
|
board_assignee_lists
|
|
|
|
].index_with(true)
|
|
|
|
|
|
|
|
before do
|
|
|
|
stub_licensed_features(licensed_features)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
include_context 'with licensed features' if Gitlab.ee?
|
|
|
|
|
2020-10-09 23:08:52 -04:00
|
|
|
context 'with factory defaults', factory_default: :keep do
|
2021-02-18 01:09:43 -05:00
|
|
|
let_it_be(:namespace) { create_default(:namespace).freeze }
|
|
|
|
let_it_be(:project) { create_default(:project, :repository).freeze }
|
|
|
|
let_it_be(:user) { create_default(:user).freeze }
|
2020-10-09 23:08:52 -04:00
|
|
|
|
2020-12-10 07:09:43 -05:00
|
|
|
before do
|
|
|
|
factories_based_on_view.each do |factory|
|
|
|
|
view = build(factory).class.table_name
|
|
|
|
swapout_view_for_table(view)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-09 23:08:52 -04:00
|
|
|
with_fd.each do |factory|
|
|
|
|
it_behaves_like 'factory', factory
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'without factory defaults' do
|
|
|
|
without_fd.each do |factory|
|
|
|
|
it_behaves_like 'factory', factory
|
|
|
|
end
|
|
|
|
end
|
2012-08-28 01:42:28 -04:00
|
|
|
end
|