BigW Consortium Gitlab

Commit e7061396 by James Edwards-Jones

DestroyService for protected tags/branches used from controller

parent 1f7328f8
......@@ -5,12 +5,8 @@ class Projects::ProtectedBranchesController < Projects::ProtectedRefsController
@project.repository.branches
end
def create_service_class
::ProtectedBranches::CreateService
end
def update_service_class
::ProtectedBranches::UpdateService
def service_namespace
::ProtectedBranches
end
def load_protected_ref
......
......@@ -37,7 +37,7 @@ class Projects::ProtectedRefsController < Projects::ApplicationController
end
def destroy
@protected_ref.destroy
destroy_service_class.new(@project, current_user).execute(@protected_ref)
respond_to do |format|
format.html { redirect_to_repository_settings(@project) }
......@@ -47,6 +47,18 @@ class Projects::ProtectedRefsController < Projects::ApplicationController
protected
def create_service_class
service_namespace::CreateService
end
def update_service_class
service_namespace::UpdateService
end
def destroy_service_class
service_namespace::DestroyService
end
def access_level_attributes
%i(access_level id)
end
......
......@@ -5,12 +5,8 @@ class Projects::ProtectedTagsController < Projects::ProtectedRefsController
@project.repository.tags
end
def create_service_class
::ProtectedTags::CreateService
end
def update_service_class
::ProtectedTags::UpdateService
def service_namespace
::ProtectedTags
end
def load_protected_ref
......
module ProtectedBranches
class DestroyService < BaseService
def execute(protected_branch)
protected_branch.destroy
end
end
end
module ProtectedTags
class DestroyService < BaseService
def execute(protected_tag)
protected_tag.destroy
end
end
end
require('spec_helper')
describe Projects::ProtectedBranchesController do
let(:project) { create(:project, :repository) }
let(:protected_branch) { create(:protected_branch, project: project) }
let(:project_params) { { namespace_id: project.namespace.to_param, project_id: project } }
let(:base_params) { project_params.merge(id: protected_branch.id) }
let(:user) { create(:user) }
before do
project.add_master(user)
end
describe "GET #index" do
let(:project) { create(:project_empty_repo, :public) }
......@@ -8,4 +18,50 @@ describe Projects::ProtectedBranchesController do
get(:index, namespace_id: project.namespace.to_param, project_id: project)
end
end
describe "POST #create" do
let(:master_access_level) { [{ access_level: Gitlab::Access::MASTER }] }
let(:access_level_params) do
{ merge_access_levels_attributes: master_access_level,
push_access_levels_attributes: master_access_level }
end
let(:create_params) { attributes_for(:protected_branch).merge(access_level_params) }
before do
sign_in(user)
end
it 'creates the protected branch rule' do
expect do
post(:create, project_params.merge(protected_branch: create_params))
end.to change(ProtectedBranch, :count).by(1)
end
end
describe "PUT #update" do
let(:update_params) { { name: 'new_name' } }
before do
sign_in(user)
end
it 'updates the protected branch rule' do
put(:update, base_params.merge(protected_branch: update_params))
expect(protected_branch.reload.name).to eq('new_name')
expect(json_response["name"]).to eq('new_name')
end
end
describe "DELETE #destroy" do
before do
sign_in(user)
end
it "deletes the protected branch rule" do
delete(:destroy, base_params)
expect { ProtectedBranch.find(protected_branch.id) }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
......@@ -29,7 +29,7 @@ describe ProtectedBranchPolicy do
context 'and unprotection is limited to admins' do #TODO: remove this is temporary exploration
before do
stub_ee_application_setting(only_admins_can_unprotect_master_branch: true)
stub_application_setting(only_admins_can_unprotect_master_branch: true)
end
context 'and the protection is for master' do
......
require 'spec_helper'
describe ProtectedBranches::DestroyService do
let(:protected_branch) { create(:protected_branch) }
let(:project) { protected_branch.project }
let(:user) { project.owner }
describe '#execute' do
subject(:service) { described_class.new(project, user) }
it 'destroys a protected branch' do
service.execute(protected_branch)
expect(protected_branch).to be_destroyed
end
end
end
require 'spec_helper'
describe ProtectedTags::DestroyService do
let(:protected_tag) { create(:protected_tag) }
let(:project) { protected_tag.project }
let(:user) { project.owner }
describe '#execute' do
subject(:service) { described_class.new(project, user) }
it 'destroy a protected tag' do
service.execute(protected_tag)
expect(protected_tag).to be_destroyed
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment