Skip to content

Commit

Permalink
[api] Raise exception for invalid DownloadRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
hennevogel committed Apr 13, 2017
1 parent 21f3387 commit b1cef3f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 35 deletions.
6 changes: 4 additions & 2 deletions src/api/app/models/project/update_from_xml_command.rb
Expand Up @@ -46,7 +46,6 @@ def run(xmlhash, force = nil)
project.update_relationships_from_xml(xmlhash)

#--- update flag group ---#
# TODO: Where does update_all_flags method exist??!?
project.update_all_flags(xmlhash)
if ::Configuration.default_access_disabled == true && new_record
# write a default access disable flag by default in this mode for projects if not defined
Expand Down Expand Up @@ -299,6 +298,7 @@ def update_repository_architectures(current_repo, xml_hash)
def update_download_repositories(current_repo, xml_hash)
dod_repositories = xml_hash.elements("download").map do |dod|
dod_attributes = {
repository: current_repo,
arch: dod["arch"],
url: dod["url"],
repotype: dod["repotype"],
Expand All @@ -310,7 +310,9 @@ def update_download_repositories(current_repo, xml_hash)
dod_attributes[:mastersslfingerprint] = dod["master"]["sslfingerprint"]
end

DownloadRepository.new(dod_attributes)
repository = DownloadRepository.new(dod_attributes)
raise SaveError, repository.errors.full_messages.to_sentence unless repository.valid?
repository
end
current_repo.download_repositories.replace(dod_repositories)
end
Expand Down
92 changes: 59 additions & 33 deletions src/api/spec/models/project/update_from_xml_command_spec.rb
Expand Up @@ -2,7 +2,6 @@

RSpec.describe Project::UpdateFromXmlCommand do
let!(:project) { create(:project) }
let(:leap_project) { create(:project, name: 'openSUSE_Leap') }
let(:attribute_type) { AttribType.find_by_namespace_and_name!('OBS', 'ImageTemplates') }

describe "#update_repositories" do
Expand Down Expand Up @@ -200,42 +199,69 @@
end

describe "download repositories" do
before do
xml_hash = Xmlhash.parse(
<<-EOF
<project name="#{project.name}">
<repository name="repo_1" />
<repository name="dod_repo">
<download arch='i586' url='http://opensuse.org' repotype='rpmmd'>
<archfilter>i586, noarch</archfilter>
<master url='http://master.opensuse.org' sslfingerprint='my_fingerprint'/>
<pubkey>my_pubkey</pubkey>
</download>
<arch>i586</arch>
</repository>
</project>
EOF
)
Project::UpdateFromXmlCommand.new(project).send(:update_repositories, xml_hash, false)
end
context "valid usecase" do
before do
xml_hash = Xmlhash.parse(
<<-EOF
<project name="#{project.name}">
<repository name="repo_1" />
<repository name="dod_repo">
<download arch='i586' url='http://opensuse.org' repotype='rpmmd'>
<archfilter>i586, noarch</archfilter>
<master url='http://master.opensuse.org' sslfingerprint='my_fingerprint'/>
<pubkey>my_pubkey</pubkey>
</download>
<arch>i586</arch>
</repository>
</project>
EOF
)
Project::UpdateFromXmlCommand.new(project).send(:update_repositories, xml_hash, false)
end

it "updates download repositories of a repository" do
expect(repository_1.download_repositories).to be_empty
it "updates download repositories of a repository" do
expect(repository_1.download_repositories).to be_empty

dod_repo = project.repositories.find_by(name: "dod_repo")
expect(dod_repo).not_to be_nil
expect(dod_repo.download_repositories.count).to eq 1
dod_repo = project.repositories.find_by(name: "dod_repo")
expect(dod_repo).not_to be_nil
expect(dod_repo.download_repositories.count).to eq 1
end

it "updates download_repository attributes" do
download_repository = project.repositories.find_by(name: "dod_repo").download_repositories.first
expect(download_repository.arch).to eq "i586"
expect(download_repository.repotype).to eq "rpmmd"
expect(download_repository.url).to eq "http://opensuse.org"
expect(download_repository.archfilter).to eq "i586, noarch"
expect(download_repository.masterurl).to eq "http://master.opensuse.org"
expect(download_repository.mastersslfingerprint).to eq "my_fingerprint"
expect(download_repository.pubkey).to eq "my_pubkey"
end
end
context "invalid usecase" do
subject { Project::UpdateFromXmlCommand.new(project).send(:update_repositories, @xml_hash, false) }

before do
@xml_hash = Xmlhash.parse(
<<-EOF
<project name="#{project.name}">
<repository name="repo_1" />
<repository name="dod_repo">
<download arch='i586' url='http://opensuse.org' repotype='INVALID'>
<archfilter>i586, noarch</archfilter>
<master url='http://master.opensuse.org' sslfingerprint='my_fingerprint'/>
<pubkey>my_pubkey</pubkey>
</download>
<arch>i586</arch>
</repository>
</project>
EOF
)
end

it "updates download_repository attributes" do
download_repository = project.repositories.find_by(name: "dod_repo").download_repositories.first
expect(download_repository.arch).to eq "i586"
expect(download_repository.repotype).to eq "rpmmd"
expect(download_repository.url).to eq "http://opensuse.org"
expect(download_repository.archfilter).to eq "i586, noarch"
expect(download_repository.masterurl).to eq "http://master.opensuse.org"
expect(download_repository.mastersslfingerprint).to eq "my_fingerprint"
expect(download_repository.pubkey).to eq "my_pubkey"
it "raises an exception for a wrong repotype" do
expect { subject }.to raise_error(Project::SaveError, "Repotype is not included in the list")
end
end
end

Expand Down

0 comments on commit b1cef3f

Please sign in to comment.