Commit bd7af6a9 authored by Michael Kozono's avatar Michael Kozono

Merge branch 'rp/use-after-build-in-project-factory' into 'master'

Remove `allows_nil: false` setting in default_value_for container_registry_access_level

See merge request gitlab-org/gitlab!67321
parents df335a1b 99b5f0ff
......@@ -79,7 +79,7 @@ class ProjectFeature < ApplicationRecord
end
end
default_value_for(:container_registry_access_level, allows_nil: false) do |feature|
default_value_for(:container_registry_access_level) do |feature|
if gitlab_config_features.container_registry
ENABLED
else
......
......@@ -51,7 +51,7 @@ FactoryBot.define do
ci_job_token_scope_enabled { nil }
end
before(:create) do |project, evaluator|
after(:build) 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
......@@ -67,21 +67,11 @@ FactoryBot.define do
pages_access_level: evaluator.pages_access_level,
metrics_dashboard_access_level: evaluator.metrics_dashboard_access_level,
operations_access_level: evaluator.operations_access_level,
analytics_access_level: evaluator.analytics_access_level
analytics_access_level: evaluator.analytics_access_level,
container_registry_access_level: evaluator.container_registry_access_level
}
project.build_project_feature(hash)
# This is not included in the `hash` above because the default_value_for in
# the ProjectFeature model overrides the value set by `build_project_feature` when
# evaluator.container_registry_access_level == ProjectFeature::DISABLED.
#
# This is because the default_value_for gem uses the <column>_changed? method
# to determine if the default value should be applied. For new records,
# <column>_changed? returns false if the value of the column is the same as
# the database default.
# See https://github.com/FooBarWidget/default_value_for/blob/release-3.4.0/lib/default_value_for.rb#L158.
project.project_feature.container_registry_access_level = evaluator.container_registry_access_level
end
after(:create) do |project, evaluator|
......
......@@ -218,5 +218,29 @@ RSpec.describe ProjectFeature do
end
end
end
context 'test build factory' do
let(:project) { build(:project, container_registry_access_level: level) }
subject { project.container_registry_access_level }
context 'private' do
let(:level) { ProjectFeature::PRIVATE }
it { is_expected.to eq(level) }
end
context 'enabled' do
let(:level) { ProjectFeature::ENABLED }
it { is_expected.to eq(level) }
end
context 'disabled' do
let(:level) { ProjectFeature::DISABLED }
it { is_expected.to eq(level) }
end
end
end
end
......@@ -318,7 +318,8 @@ RSpec.describe Project, factory_default: :keep do
end
it 'validates presence of project_feature' do
project = build(:project, project_feature: nil)
project = build(:project)
project.project_feature = nil
expect(project).not_to be_valid
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment