Skip to content

Commit

Permalink
[webui][ci] CRUD actions for Download on Demand repos and tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
mdeniz committed Jan 20, 2016
1 parent ee7c5ed commit 6fbbc0b
Show file tree
Hide file tree
Showing 15 changed files with 337 additions and 44 deletions.
42 changes: 42 additions & 0 deletions 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
10 changes: 5 additions & 5 deletions 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
13 changes: 13 additions & 0 deletions 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
6 changes: 6 additions & 0 deletions src/api/app/views/webui/download_on_demand/_add.html.erb
@@ -0,0 +1,6 @@
<div class="box" style="margin: 1em">
<h2 class="box-header">
Add Download on Demand for <%= repository.name %>
</h2>
<%= render partial: 'webui/download_on_demand/form', locals: { project: project, repository: repository, download_on_demand: download_on_demand } %>
</div>
6 changes: 6 additions & 0 deletions src/api/app/views/webui/download_on_demand/_edit.html.erb
@@ -0,0 +1,6 @@
<div class="box" style="margin: 1em">
<h2 class="box-header">
Edit Download on Demand for <%= repository.name %> / <%= download_on_demand.arch %>
</h2>
<%= render partial: 'webui/download_on_demand/form', locals: { project: project, repository: repository, download_on_demand: download_on_demand } %>
</div>
33 changes: 33 additions & 0 deletions src/api/app/views/webui/download_on_demand/_form.html.erb
@@ -0,0 +1,33 @@
<%= form_for(download_on_demand) do |form| %>
<div style="margin-bottom: 1.2em; margin-left: 2em" >
<%= form.label :arch, 'Architecture', style: "display:inline-block;width:100px;" %>
<%= form.select :arch, options_for_select(repository.architectures, download_on_demand.try(:arch)) %>
<br/><br/>
<%= form.label :repotype, 'Type', style: "display:inline-block;width:100px;" %>
<%= form.select :repotype, options_for_select(DownloadRepository::REPOTYPES, download_on_demand.try(:repotype)) %>
<br/><br/>
<%= form.label :url, style: "display:inline-block;width:100px;" %>
<%= form.text_field :url %>
<br/><br/>
<%= form.label :archfilter, 'Arch. Filter' , style: "display:inline-block;width:100px;"%>
<%= form.text_field :archfilter %>
<br/><br/>
<%= form.label :masterurl, 'Master Url', style: "display:inline-block;width:100px;" %>
<%= form.text_field :masterurl %>
<br/><br/>
<%= form.label :mastersslfingerprint, 'SSL Fingerprint', style: "display:inline-block;width:100px;" %>
<%= form.text_field :mastersslfingerprint %>
<br/><br/>
<%= 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? %>
<p><%= form.submit "Add Download on Demand", id: "add_dod_button" %></p>
<% else %>
<p><%= form.submit "Update Download on Demand", id: "update_dod_button-#{download_on_demand}"%></p>
<% end %>
</div>
<% end %>
44 changes: 44 additions & 0 deletions src/api/app/views/webui/download_on_demand/_index.html.erb
@@ -0,0 +1,44 @@
<div style="margin-top: 1em"><%= repository.download_repositories.empty? ? "No d" : "D" %>ownload on demand repositories
<% if User.current.is_admin? && repository.download_repositories.count != repository.architectures.count%>
&nbsp;
<span id="<%= "add_dod_repository_link_container_#{repository_id}" %>">
<%= sprite_tag('drive_add') %>
<%= link_to('Add', '#', id: "add_dod_repository_link_#{repository_id}") %>
</span>
<div class="hidden" id="<%= "add_dod_repository_form_#{repository_id}" %>">
<%= render partial: 'webui/download_on_demand/add', locals: { project: project, repository: repository, download_on_demand: new_download_on_demand } %>
</div>
<% 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 -%>
<ul style="list-style-type: none;">
<% repository.download_repositories.each do |dod_element| %>
<li>
<b><%= dod_element.arch %></b>:&nbsp;<a href="<%= dod_element.url %>" target="_blank" title="Go to the repository"><%= dod_element.url %></a>&nbsp;(<%= dod_element.repotype %>)
<% if User.current.is_admin? %>
&nbsp;
<span id="<%= "edit_dod_repository_link_container_#{dod_element}" %>" class="edit_dod_repository_link_container">
<%= sprite_tag('drive_edit') %>
<%= link_to('Edit', '#', id: "edit_dod_repository_link_#{dod_element}") %>
<%= sprite_tag('drive_delete', title: 'Delete repository') %>
<%= link_to "Delete", download_repository_path(dod_element, project: project),
{data: {confirm: "Really remove Download on Demand for '#{repository.name} / #{dod_element.arch}'?"}, class: 'x', method: :delete} %>
</span>
<div class="hidden" id="<%= "edit_dod_repository_form_#{dod_element}" %>">
<%= render partial: 'webui/download_on_demand/edit', locals: { project: project, repository: repository, download_on_demand: dod_element } %>
</div>
<% content_for :ready_function do %>
$("#<%= "edit_dod_repository_link_#{dod_element}" %>").click(function() {
$("#<%= "edit_dod_repository_form_#{dod_element}" %>").show();
$("#<%= "edit_dod_repository_link_container_#{dod_element}" %>").hide();
});
<% end -%>
<% end -%>
</li>
<% end -%>
</ul>
</div>
7 changes: 0 additions & 7 deletions src/api/app/views/webui/project/_download_on_demand.html.erb

This file was deleted.

6 changes: 3 additions & 3 deletions src/api/app/views/webui/project/repositories.html.erb
Expand Up @@ -10,7 +10,7 @@
<!-- renders each line in flag table, excepting the 'all' row -->
<% @project.repositories.each do |repository| %>
<% repository_id = valid_xml_id(repository.name) %>
<div style="margin-bottom: 1.2em" id="<%= valid_xml_id(repository.name) %>">
<div style="margin-bottom: 1.5em" id="<%= valid_xml_id(repository.name) %>">
<div style="margin-left: 15px">
<strong><%= link_to(repository.name, :action => 'repository_state', :project => @project, :repository => repository.name) %></strong>
<i>(<%= repository.architectures.pluck(:name).join(', ') %>)</i>
Expand Down Expand Up @@ -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') %>
Expand All @@ -55,7 +55,7 @@
<% end %>
</div>

<%= 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)}%>
</div>
</div>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions src/api/config/routes.rb
Expand Up @@ -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
Expand Down
@@ -0,0 +1,14 @@
<project name='home:user5'>
<title>User5 Home Project</title>
<description/>
<person userid='user5' role='maintainer'/>
<repository name='standard'>
<download arch='i586' url='http://mola.org2' repotype='rpmmd'>
<archfilter>i586, noarch</archfilter>
<master url='http://opensuse.org' sslfingerprint='asdfasd'/>
<pubkey>3jnlkdsjfoisdjf0932juro2ikjfdslñkfj</pubkey>
</download>
<arch>i586</arch>
<arch>x86_64</arch>
</repository>
</project>
@@ -0,0 +1,9 @@
<project name='home:user5'>
<title>User5 Home Project</title>
<description/>
<person userid='user5' role='maintainer'/>
<repository name='standard'>
<arch>i586</arch>
<arch>x86_64</arch>
</repository>
</project>
152 changes: 152 additions & 0 deletions 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

0 comments on commit 6fbbc0b

Please sign in to comment.