Skip to content

Commit

Permalink
Create destroy method, authorization and tests for staging workflow d…
Browse files Browse the repository at this point in the history
…eletion
  • Loading branch information
krauselukas committed Nov 9, 2018
1 parent dc4e57c commit 77fe170
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
19 changes: 18 additions & 1 deletion src/api/app/controllers/webui/staging_workflows_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ def new

def create
staging_workflow = @project.build_staging

authorize staging_workflow

if staging_workflow.save
Expand Down Expand Up @@ -47,6 +46,24 @@ def edit
@project = @staging_workflow.project
end

def destroy
@staging_workflow = StagingWorkflow.find_by(id: params[:id])
authorize @staging_workflow
@project = @staging_workflow.project

# attached staging projects get nullified by default but we want to
# allow to destroy manually by setting a checkbox
@staging_workflow.staging_projects.where(id: params[:staging_project_ids]).destroy_all

if @staging_workflow.destroy
flash[:success] = "Staging Workflow for #{@project.name} was successfully deleted."
render js: "window.location='#{project_show_path(@project)}'"
else
flash[:error] = "Staging Workflow for #{@project.name} couldn't be deleted: #{@staging_workflow.errors.full_messages.to_sentence}."
render js: "window.location='#{staging_workflow_path(@staging_workflow)}'"
end
end

private

def set_bootstrap_views
Expand Down
2 changes: 1 addition & 1 deletion src/api/app/policies/staging_workflow_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ def edit?
end

def destroy?
update?
ProjectPolicy.new(@user, @record.project).destroy?
end
end
2 changes: 1 addition & 1 deletion src/api/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def self.public_or_about_path?(request)
get 'published/:project(/:repository(/:arch(/:binary)))' => 'published#index', constraints: cons
get 'published/' => 'source#index', via: :get

resources :staging_workflows, except: [:index, :update, :destroy], controller: 'webui/staging_workflows', constraints: cons do
resources :staging_workflows, except: [:index, :update], controller: 'webui/staging_workflows', constraints: cons do
resources :staging_projects, only: [:create, :destroy], controller: 'webui/staging_workflows/staging_projects', param: :project_name, constraints: cons
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,50 @@
it { expect(response).to render_template(:edit) }
end
end

describe 'DELETE #destroy' do
context 'a staging workflow and staging projects' do
before do
project.create_staging
params = { id: project.staging, staging_project_ids: project.staging.staging_projects.ids, format: :js }
delete :destroy, params: params
end

subject { project.staging }

it { expect(StagingWorkflow.count).to eq(0) }
it { expect(subject.staging_projects.count).to eq(0) }
it { expect(flash[:success]).not_to be_nil }
it { expect(response.body).to eq("window.location='#{project_show_path(project)}'") }
it { expect(project.subprojects.count).to eq(0) }
end

context 'a staging workflow and one staging project' do
before do
project.create_staging
params = { id: project.staging, staging_project_ids: project.staging.staging_projects.ids.first, format: :js }
delete :destroy, params: params
end

subject { project.staging }

it { expect(StagingWorkflow.count).to eq(0) }
it { expect(subject.staging_projects.count).to eq(0) }
it { expect(project.subprojects.count).to eq(1) }
it { expect(flash[:success]).not_to be_nil }
it { expect(response.body).to eq("window.location='#{project_show_path(project)}'") }
end

context 'a staging workflow unsuccessful' do
before do
project.create_staging
allow_any_instance_of(StagingWorkflow).to receive(:destroy).and_return(false)
params = { id: project.staging, staging_project_ids: project.staging.staging_projects.ids, format: :js }
delete :destroy, params: params
end

it { expect(flash[:error]).not_to be_nil }
it { expect(response.body).to eq("window.location='#{staging_workflow_path(project.staging)}'") }
end
end
end

0 comments on commit 77fe170

Please sign in to comment.