diff --git a/src/api/app/controllers/webui/download_on_demand_controller.rb b/src/api/app/controllers/webui/download_on_demand_controller.rb new file mode 100644 index 00000000000..037ab38ad77 --- /dev/null +++ b/src/api/app/controllers/webui/download_on_demand_controller.rb @@ -0,0 +1,42 @@ +class Webui::DownloadOnDemandController < Webui::WebuiController + before_filter :set_project + + def create + @download_on_demand = DownloadRepository.new(permitted_params) + authorize @download_on_demand + if @download_on_demand.save + @project.store + redirect_to project_repositories_path(@project), notice: "Successfully created Download on Demand" + else + redirect_to :back, error: "Download on Demand can't be created: #{@download_on_demand.errors.full_messages.to_sentence}" + end + end + + def update + @download_on_demand = DownloadRepository.find(params[:id]) + authorize @download_on_demand + if @download_on_demand.update_attributes(permitted_params) + @project.store + redirect_to project_repositories_path(@project), notice: "Successfully updated Download on Demand" + else + redirect_to :back, error: "Download on Demand can't be created: #{@download_on_demand.errors.full_messages.to_sentence}" + end + end + + def destroy + @download_on_demand = DownloadRepository.find(params[:id]) + authorize @download_on_demand + if @download_on_demand.destroy + @project.store + redirect_to project_repositories_path(@project), notice: "Successfully removed Download on Demand" + else + redirect_to :back, error: "Download on Demand can't be removed: #{@download_on_demand.errors.full_messages.to_sentence}" + end + end + + private + + def permitted_params + params.require(:download_repository).permit(:arch, :repotype, :url, :repository_id, :archfilter, :masterurl, :mastersslfingerprint, :pubkey) + end +end diff --git a/src/api/app/models/download_repository.rb b/src/api/app/models/download_repository.rb index 843e08950a0..85a730705cc 100644 --- a/src/api/app/models/download_repository.rb +++ b/src/api/app/models/download_repository.rb @@ -1,12 +1,12 @@ class DownloadRepository < ActiveRecord::Base belongs_to :repository - validates :repository, presence: true - validates :arch, presence: true + validates :repository_id, presence: true + validates :arch, uniqueness: { scope: :repository_id}, presence: true validates :url, presence: true validates :repotype, presence: true -# def self._sync_keys -# [ :arch, :url ] -# end + REPOTYPES = ["rpmmd", "susetags", "deb", "arch", "mdk"] + + delegate :to_s, to: :id end diff --git a/src/api/app/policies/download_repository_policy.rb b/src/api/app/policies/download_repository_policy.rb new file mode 100644 index 00000000000..27905e1adc3 --- /dev/null +++ b/src/api/app/policies/download_repository_policy.rb @@ -0,0 +1,13 @@ +class DownloadRepositoryPolicy < ApplicationPolicy + def create? + @user.is_admin? + end + + def update? + @user.is_admin? + end + + def destroy? + @user.is_admin? + end +end diff --git a/src/api/app/views/webui/download_on_demand/_add.html.erb b/src/api/app/views/webui/download_on_demand/_add.html.erb new file mode 100644 index 00000000000..f389f5a98c1 --- /dev/null +++ b/src/api/app/views/webui/download_on_demand/_add.html.erb @@ -0,0 +1,6 @@ +
+

+ Add Download on Demand for <%= repository.name %> +

+ <%= render partial: 'webui/download_on_demand/form', locals: { project: project, repository: repository, download_on_demand: download_on_demand } %> +
diff --git a/src/api/app/views/webui/download_on_demand/_edit.html.erb b/src/api/app/views/webui/download_on_demand/_edit.html.erb new file mode 100644 index 00000000000..ba3394465b3 --- /dev/null +++ b/src/api/app/views/webui/download_on_demand/_edit.html.erb @@ -0,0 +1,6 @@ +
+

+ Edit Download on Demand for <%= repository.name %> / <%= download_on_demand.arch %> +

+ <%= render partial: 'webui/download_on_demand/form', locals: { project: project, repository: repository, download_on_demand: download_on_demand } %> +
diff --git a/src/api/app/views/webui/download_on_demand/_form.html.erb b/src/api/app/views/webui/download_on_demand/_form.html.erb new file mode 100644 index 00000000000..b51112e83da --- /dev/null +++ b/src/api/app/views/webui/download_on_demand/_form.html.erb @@ -0,0 +1,33 @@ +<%= form_for(download_on_demand) do |form| %> +
+ <%= form.label :arch, 'Architecture', style: "display:inline-block;width:100px;" %> + <%= form.select :arch, options_for_select(repository.architectures, download_on_demand.try(:arch)) %> +

+ <%= form.label :repotype, 'Type', style: "display:inline-block;width:100px;" %> + <%= form.select :repotype, options_for_select(DownloadRepository::REPOTYPES, download_on_demand.try(:repotype)) %> +

+ <%= form.label :url, style: "display:inline-block;width:100px;" %> + <%= form.text_field :url %> +

+ <%= form.label :archfilter, 'Arch. Filter' , style: "display:inline-block;width:100px;"%> + <%= form.text_field :archfilter %> +

+ <%= form.label :masterurl, 'Master Url', style: "display:inline-block;width:100px;" %> + <%= form.text_field :masterurl %> +

+ <%= form.label :mastersslfingerprint, 'SSL Fingerprint', style: "display:inline-block;width:100px;" %> + <%= form.text_field :mastersslfingerprint %> +

+ <%= form.label :pubkey, 'Public Key', style: "display:inline-block;width:100px;" %> + <%= form.text_area :pubkey %> + + <%= form.hidden_field :repository_id %> + <%= hidden_field_tag :project, project %> + + <% if form.object.new_record? %> +

<%= form.submit "Add Download on Demand", id: "add_dod_button" %>

+ <% else %> +

<%= form.submit "Update Download on Demand", id: "update_dod_button-#{download_on_demand}"%>

+ <% end %> +
+<% end %> diff --git a/src/api/app/views/webui/download_on_demand/_index.html.erb b/src/api/app/views/webui/download_on_demand/_index.html.erb new file mode 100644 index 00000000000..9dbd48c8a15 --- /dev/null +++ b/src/api/app/views/webui/download_on_demand/_index.html.erb @@ -0,0 +1,44 @@ +
<%= repository.download_repositories.empty? ? "No d" : "D" %>ownload on demand repositories + <% if User.current.is_admin? && repository.download_repositories.count != repository.architectures.count%> +   + "> + <%= sprite_tag('drive_add') %> + <%= link_to('Add', '#', id: "add_dod_repository_link_#{repository_id}") %> + + + <% content_for :ready_function do %> + $("#<%= "add_dod_repository_link_#{repository_id}" %>").click(function() { + $("#<%= "add_dod_repository_form_#{repository_id}" %>").show(); + $("#<%= "add_dod_repository_link_container_#{repository_id}" %>").hide(); + }); + <% end -%> + <% end -%> + +
diff --git a/src/api/app/views/webui/project/_download_on_demand.html.erb b/src/api/app/views/webui/project/_download_on_demand.html.erb deleted file mode 100644 index a95c093b2d7..00000000000 --- a/src/api/app/views/webui/project/_download_on_demand.html.erb +++ /dev/null @@ -1,7 +0,0 @@ -
Download on demand repositories - -
diff --git a/src/api/app/views/webui/project/repositories.html.erb b/src/api/app/views/webui/project/repositories.html.erb index c166b60d4b4..d4a2ed225e3 100644 --- a/src/api/app/views/webui/project/repositories.html.erb +++ b/src/api/app/views/webui/project/repositories.html.erb @@ -10,7 +10,7 @@ <% @project.repositories.each do |repository| %> <% repository_id = valid_xml_id(repository.name) %> -
+
<%= link_to(repository.name, :action => 'repository_state', :project => @project, :repository => repository.name) %> (<%= repository.architectures.pluck(:name).join(', ') %>) @@ -38,7 +38,7 @@ <% content_for :ready_function do %> $("#<%= "edit_repository_link_#{repository_id}" %>").click(function() { $("#<%= "edit_repository_form_#{repository_id}" %>").show(); - $("#<%= "edit_repository_link_container_#{repository_id}" %>").hide(); + $("#<%= "edit_repository_link_container_#{repository_id}" %>").hide(); }); <% end %> <%= sprite_tag('drive_delete', title: 'Delete repository') %> @@ -55,7 +55,7 @@ <% end %>
- <%= render :partial => 'download_on_demand', :locals => {repository: repository, repository_id: repository_id} unless repository.download_repositories.empty?%> + <%= render :partial => 'webui/download_on_demand/index', :locals => {project: @project, repository: repository, repository_id: repository_id, new_download_on_demand: DownloadRepository.new(repository_id: repository.id)}%>
<% end %> diff --git a/src/api/config/routes.rb b/src/api/config/routes.rb index e566a1ca7e4..2e6775c7898 100644 --- a/src/api/config/routes.rb +++ b/src/api/config/routes.rb @@ -61,6 +61,8 @@ def self.matches?(request) end end + resources :download_repositories, constraints: cons, only: [:create, :update, :destroy], controller: 'webui/download_on_demand' + controller 'webui/configuration' do get 'configuration' => :index patch 'configuration' => :update diff --git a/src/api/test/fixtures/backend/download_on_demand/project_with_dod.xml b/src/api/test/fixtures/backend/download_on_demand/project_with_dod.xml new file mode 100644 index 00000000000..83a5dc88987 --- /dev/null +++ b/src/api/test/fixtures/backend/download_on_demand/project_with_dod.xml @@ -0,0 +1,14 @@ + +User5 Home Project + + + + + i586, noarch + + 3jnlkdsjfoisdjf0932juro2ikjfdslñkfj + + i586 + x86_64 + + diff --git a/src/api/test/fixtures/backend/download_on_demand/project_without_dod.xml b/src/api/test/fixtures/backend/download_on_demand/project_without_dod.xml new file mode 100644 index 00000000000..92a88565513 --- /dev/null +++ b/src/api/test/fixtures/backend/download_on_demand/project_without_dod.xml @@ -0,0 +1,9 @@ + +User5 Home Project + + + + i586 + x86_64 + + diff --git a/src/api/test/functional/webui/download_on_demand_controller_test.rb b/src/api/test/functional/webui/download_on_demand_controller_test.rb new file mode 100644 index 00000000000..6788cd20ff7 --- /dev/null +++ b/src/api/test/functional/webui/download_on_demand_controller_test.rb @@ -0,0 +1,152 @@ +# encoding: utf-8 +require_relative '../../test_helper' + +class Webui::DownloadOnDemandControllerTest < Webui::IntegrationTest + PROJECT_WITH_DOWNLOAD_ON_DEMAND = load_backend_file("download_on_demand/project_with_dod.xml") + PROJECT_WITHOUT_DOWNLOAD_ON_DEMAND = load_backend_file("download_on_demand/project_without_dod.xml") + + def test_listing_download_on_demand + use_js + + # Login as admin + login_king + visit(project_show_path(project: "home:user5")) + + # Updating via meta + click_link("Advanced") + click_link("Meta") + page.evaluate_script("editors[0].setValue(\"#{PROJECT_WITH_DOWNLOAD_ON_DEMAND.gsub("\n", '\n')}\");") + click_button("Save") + find(:id, 'flash-messages').must_have_text('Config successfully saved!') + + click_link("Repositories") + page.must_have_link 'http://mola.org2' + page.must_have_text 'rpmmd' + + find(:xpath, "//span[@class='edit_dod_repository_link_container']").must_have_link('Edit') + find(:xpath, "//span[@class='edit_dod_repository_link_container']").must_have_link('Delete') + + # Login as normal user, can't change DoDs + login_tom + visit(project_show_path(project: "home:user5")) + + click_link("Repositories") + page.must_have_text 'Download on demand repositories' + page.must_have_link 'http://mola.org2' + page.must_have_text 'rpmmd' + end + + def test_adding_download_on_demand + use_js + + # Login as admin + login_king + visit(project_show_path(project: "home:user5")) + + # Updating via meta + click_link("Advanced") + click_link("Meta") + page.evaluate_script("editors[0].setValue(\"#{PROJECT_WITHOUT_DOWNLOAD_ON_DEMAND.gsub("\n", '\n')}\");") + click_button("Save") + find(:id, 'flash-messages').must_have_text('Config successfully saved!') + + click_link("Repositories") + click_link("add_dod_repository_link_standard") + + # Fill in the form and send a working dod data + select('i586', from: 'Architecture') + select('rpmmd', from: 'Type') + fill_in('Url', with: 'http://somerandomurl.es') + fill_in('Arch. Filter', with: 'i586, noarch') + fill_in('Master Url', with: 'http://somerandomurl2.es') + fill_in('SSL Fingerprint', with: '293470239742093') + fill_in('Public Key', with: 'JLKSDJFSJ83U4902RKLJSDFLJF2J9IJ23OJFKJFSDF') + click_button('Add Download on Demand') + find(:id, 'flash-messages').must_have_text('Successfully created Download on Demand') + find(:id, "add_dod_repository_link_container_standard").must_have_link('Add') + find(:xpath, "//span[@class='edit_dod_repository_link_container']").must_have_link('Edit') + find(:xpath, "//span[@class='edit_dod_repository_link_container']").must_have_link('Delete') + page.must_have_text 'Download on demand repositories' + page.must_have_link 'http://somerandomurl.es' + page.must_have_text 'rpmmd' + + click_link("Repositories") + click_link("add_dod_repository_link_standard") + + # Fill in the form and send a not working dod data + select('x86_64', from: 'Architecture') + select('rpmmd', from: 'Type') + fill_in('Url', with: '') + click_button('Add Download on Demand') + find(:id, 'flash-messages').must_have_text("Download on Demand can't be created: Url can't be blank") + end + + def test_editing_download_on_demand + use_js + + # Login as admin + login_king + visit(project_show_path(project: "home:user5")) + + # Updating via meta + click_link("Advanced") + click_link("Meta") + page.evaluate_script("editors[0].setValue(\"#{PROJECT_WITH_DOWNLOAD_ON_DEMAND.gsub("\n", '\n')}\");") + click_button("Save") + find(:id, 'flash-messages').must_have_text('Config successfully saved!') + + click_link("Repositories") + within(:css, "span.edit_dod_repository_link_container") do + click_link("Edit") + end + + # Fill in the form and send a working dod data + select('i586', from: 'Architecture') + select('deb', from: 'Type') + fill_in('Url', with: 'http://somerandomurl_2.es') + fill_in('Arch. Filter', with: 'i586, noarch') + fill_in('Master Url', with: 'http://somerandomurl__2.es') + fill_in('SSL Fingerprint', with: '33333333444444') + fill_in('Public Key', with: '902RKLJSDFLJF902RKLJSDFLJF902RKLJSDFLJF') + click_button('Update Download on Demand') + find(:id, 'flash-messages').must_have_text('Successfully updated Download on Demand') + page.must_have_text 'Download on demand repositories' + page.must_have_link 'http://somerandomurl_2.es' + page.must_have_text 'deb' + + click_link("Repositories") + within(:css, "span.edit_dod_repository_link_container") do + click_link("Edit") + end + + # Fill in the form and send a not working dod data + fill_in('Url', with: '') + click_button('Update Download on Demand') + find(:id, 'flash-messages').must_have_text("Download on Demand can't be created: Url can't be blank") + page.must_have_link 'http://somerandomurl_2.es' + end + + def test_destroying_download_on_demand + use_js + + # Login as admin + login_king + visit(project_show_path(project: "home:user5")) + + # Updating via meta + click_link("Advanced") + click_link("Meta") + page.evaluate_script("editors[0].setValue(\"#{PROJECT_WITH_DOWNLOAD_ON_DEMAND.gsub("\n", '\n')}\");") + click_button("Save") + find(:id, 'flash-messages').must_have_text('Config successfully saved!') + + click_link("Repositories") + within(:css, "span.edit_dod_repository_link_container") do + click_link("Delete") + end + + page.wont_have_text 'Download on demand repositories' + page.wont_have_link 'http://mola.org2' + page.wont_have_text 'rpmmd' + end +end diff --git a/src/api/test/functional/webui/project_controller_test.rb b/src/api/test/functional/webui/project_controller_test.rb index cdbef40f3b1..550c62a7ece 100644 --- a/src/api/test/functional/webui/project_controller_test.rb +++ b/src/api/test/functional/webui/project_controller_test.rb @@ -16,16 +16,6 @@ class Webui::ProjectControllerTest < Webui::IntegrationTest Ignore: package:bash Ignore: package:cups' - DOWNLOAD_ON_DEMAND_PROJECT_FOR_ADRIAN = " - My Home Project - - - - - x86_64 - -" - def test_save_distributions login_tom visit "/project/add_repository_from_default_list/home:tom" @@ -443,25 +433,6 @@ def test_save_meta_permission_check assert_nil Project.find_by_name("home:adrian").remoteurl end - def test_download_on_demand_list - use_js - - login_king - visit(project_show_path(project: "home:adrian")) - - # Test reading meta data - click_link("Advanced") - click_link("Meta") - - page.evaluate_script("editors[0].setValue(\"#{DOWNLOAD_ON_DEMAND_PROJECT_FOR_ADRIAN.gsub("\n", '\n')}\");") - click_button("Save") - - click_link("Repositories") - page.must_have_text 'http://somewhere/' - page.must_have_text 'rpmmd' - assert_equal Project.find_by_name("home:adrian").repositories.first.download_repositories.count, 1 - end - def test_list_all use_js diff --git a/src/api/test/test_helper.rb b/src/api/test/test_helper.rb index 3ebca38f983..49a0866bceb 100644 --- a/src/api/test/test_helper.rb +++ b/src/api/test/test_helper.rb @@ -251,6 +251,14 @@ def current_user @current_user end + def self.load_fixture(path) + File.open(File.join(ActionController::TestCase.fixture_path, path)).read() + end + + def self.load_backend_file(path) + load_fixture("backend/#{path}") + end + self.use_transactional_fixtures = true fixtures :all