Skip to content

Commit

Permalink
Merge pull request #5252 from parthaa/fix-protected
Browse files Browse the repository at this point in the history
Fixes #10620 - Repos no longer protected by default
  • Loading branch information
parthaa committed May 29, 2015
2 parents 7c95f60 + 70dda2a commit 3f97365
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/controllers/katello/api/v2/repositories_controller.rb
Expand Up @@ -89,9 +89,9 @@ def create
end
repo_params[:label] = labelize_params(repo_params)
repo_params[:url] = nil if repo_params[:url].blank?

unprotected = repo_params.key?(:unprotected) ? repo_params[:unprotected] : true
repository = @product.add_repo(repo_params[:label], repo_params[:name], repo_params[:url],
repo_params[:content_type], repo_params[:unprotected],
repo_params[:content_type], unprotected,
gpg_key, repository_params[:checksum_type])
repository.docker_upstream_name = repo_params[:docker_upstream_name] if repo_params[:docker_upstream_name]
sync_task(::Actions::Katello::Repository::Create, repository, false, true)
Expand Down
9 changes: 9 additions & 0 deletions app/models/katello/repository.rb
Expand Up @@ -61,6 +61,7 @@ class Repository < Katello::Model
:with => /^([a-z0-9\-_]{4,30}\/)?[a-z0-9\-_\.]{3,30}$/,
:message => (_("must be a valid docker name"))
}

#validates :content_id, :presence => true #add back after fixing add_repo orchestration
validates_with Validators::KatelloLabelFormatValidator, :attributes => :label
validates_with Validators::KatelloNameFormatValidator, :attributes => :name
Expand All @@ -75,6 +76,7 @@ class Repository < Katello::Model
:message => (_("must be one of the following: %s") % TYPES.join(', '))
}
validate :ensure_valid_docker_attributes, :if => :docker?
validate :ensure_docker_repo_unprotected, :if => :docker?

# TODO: remove this default scope
# rubocop:disable Rails/DefaultScope
Expand Down Expand Up @@ -515,5 +517,12 @@ def ensure_valid_docker_attributes
errors.add(:base, N_("Repository URL or Upstream Name is empty. Both are required for syncing from the upstream."))
end
end

def ensure_docker_repo_unprotected
unless unprotected
errors.add(:base, N_("Docker Repositories are not protected at this time. " \
"They need to be published via http to be available to containers."))
end
end
end
end
73 changes: 69 additions & 4 deletions test/controllers/api/v2/repositories_controller_test.rb
Expand Up @@ -204,7 +204,7 @@ def test_create
'Fedora Repository',
'http://www.google.com',
'yum',
nil,
true,
nil,
nil
])
Expand All @@ -213,6 +213,7 @@ def test_create
product.expect(:gpg_key, nil)
product.expect(:organization, @organization)
product.expect(:redhat?, false)
product.expect(:unprotected?, true)
assert_sync_task(::Actions::Katello::Repository::Create, @repository, false, true)

Product.stub(:find, product) do
Expand All @@ -233,7 +234,7 @@ def test_create_with_empty_string_url
'Fedora Repository',
nil,
'yum',
nil,
true,
nil,
nil
])
Expand Down Expand Up @@ -267,7 +268,7 @@ def test_create_with_gpg_key
'Fedora Repository',
'http://www.google.com',
'yum',
nil,
true,
key,
nil
])
Expand All @@ -293,7 +294,7 @@ def test_create_with_checksum
'Fedora Repository',
nil,
'yum',
nil,
true,
nil,
'sha256'
])
Expand All @@ -316,6 +317,70 @@ def test_create_with_checksum
end
end

def test_create_with_protected_true
product = MiniTest::Mock.new
product.expect(:add_repo, @repository, [
'Fedora_Repository',
'Fedora Repository',
'http://www.google.com',
'yum',
false,
nil,
nil
])

product.expect(:editable?, @product.editable?)
product.expect(:gpg_key, nil)
product.expect(:organization, @organization)
product.expect(:redhat?, false)
product.expect(:unprotected?, false)
assert_sync_task(::Actions::Katello::Repository::Create, @repository, false, true)

Product.stub(:find, product) do
post :create, :name => 'Fedora Repository',
:product_id => @product.id,
:url => 'http://www.google.com',
:content_type => 'yum',
:unprotected => false

assert_response :success
assert_template 'api/v2/repositories/show'
end
end

def test_create_with_protected_docker
docker_upstream_name = "busybox"
product = MiniTest::Mock.new
product.expect(:add_repo, @repository, [
'Fedora_Repository',
'Fedora Repository',
'http://hub.registry.com',
'docker',
true,
nil,
nil
])

product.expect(:editable?, @product.editable?)
product.expect(:gpg_key, nil)
product.expect(:organization, @organization)
product.expect(:redhat?, false)
product.expect(:unprotected?, true)
product.expect(:docker_upstream_name, docker_upstream_name)
assert_sync_task(::Actions::Katello::Repository::Create, @repository, false, true)

Product.stub(:find, product) do
post :create, :name => 'Fedora Repository',
:product_id => @product.id,
:url => 'http://hub.registry.com',
:content_type => 'docker',
:docker_upstream_name => "busybox"

assert_response :success
assert_template 'api/v2/repositories/show'
end
end

def test_create_without_label_or_name
post :create, :product_id => @product.id
# TODO: fix this test, returns 500 because of undefined method add_repo for Katello::Product
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/models/katello_repositories.yml
Expand Up @@ -220,6 +220,7 @@ redis:
product_id: <%= ActiveRecord::Fixtures.identify(:puppet_product) %>
gpg_key_id: <%= ActiveRecord::Fixtures.identify(:fedora_gpg_key) %>
content_view_version_id: <%= ActiveRecord::Fixtures.identify(:library_default_version) %>
unprotected: <%= true %>

busybox:
name: busybox
Expand All @@ -232,6 +233,7 @@ busybox:
product_id: <%= ActiveRecord::Fixtures.identify(:puppet_product) %>
gpg_key_id: <%= ActiveRecord::Fixtures.identify(:fedora_gpg_key) %>
content_view_version_id: <%= ActiveRecord::Fixtures.identify(:library_default_version) %>
unprotected: <%= true %>

busybox_view1:
name: busybox
Expand All @@ -245,6 +247,7 @@ busybox_view1:
product_id: <%= ActiveRecord::Fixtures.identify(:puppet_product) %>
gpg_key_id: <%= ActiveRecord::Fixtures.identify(:fedora_gpg_key) %>
content_view_version_id: <%= ActiveRecord::Fixtures.identify(:library_view_version_1) %>
unprotected: <%= true %>

busybox_view2:
name: busybox
Expand All @@ -258,6 +261,7 @@ busybox_view2:
product_id: <%= ActiveRecord::Fixtures.identify(:puppet_product) %>
gpg_key_id: <%= ActiveRecord::Fixtures.identify(:fedora_gpg_key) %>
content_view_version_id: <%= ActiveRecord::Fixtures.identify(:library_view_version_2) %>
unprotected: <%= true %>

rhel_6_x86_64_composite_view_version_1:
name: RHEL 6 x86_64
Expand Down
14 changes: 14 additions & 0 deletions test/models/repository_test.rb
Expand Up @@ -35,6 +35,7 @@ def test_unique_repository_name_per_product_and_environment
end

def test_docker_repository_docker_upstream_name_url
@repo.unprotected = true
@repo.content_type = 'docker'
@repo.docker_upstream_name = ""
@repo.url = "http://registry.com"
Expand All @@ -52,6 +53,7 @@ def test_docker_repository_docker_upstream_name_url
end

def test_docker_repository_docker_upstream_name_format
@repo.unprotected = true
@repo.content_type = 'docker'
@repo.docker_upstream_name = 'valid'
assert @repo.valid?
Expand Down Expand Up @@ -109,10 +111,22 @@ def test_docker_pulp_id
@repo.pulp_id = 'PULP-ID'
@repo.content_type = Repository::DOCKER_TYPE
@repo.docker_upstream_name = "haha"
@repo.unprotected = true
assert @repo.save
assert @repo.pulp_id.ends_with?('pulp-id')
end

def test_docker_repo_unprotected
@repo.name = 'docker_repo'
@repo.pulp_id = 'PULP-ID'
@repo.content_type = Repository::DOCKER_TYPE
@repo.docker_upstream_name = "haha"
@repo.unprotected = true
assert @repo.save
@repo.unprotected = false
refute @repo.save
end

def test_yum_type_pulp_id
@repo.pulp_id = 'PULP-ID'
@repo.content_type = Repository::YUM_TYPE
Expand Down

0 comments on commit 3f97365

Please sign in to comment.