-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6336 from Ana06/ignored_requests_api
Create excluded request API
- Loading branch information
Showing
4 changed files
with
207 additions
and
0 deletions.
There are no files selected for viewing
54 changes: 54 additions & 0 deletions
54
src/api/app/controllers/staging/excluded_requests_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class Staging::ExcludedRequestsController < ApplicationController | ||
before_action :require_login | ||
before_action :set_staging_workflow, only: :create | ||
before_action :set_request_exclusion, only: :destroy | ||
|
||
def create | ||
request = @staging_workflow.target_of_bs_requests.find_by!(number: params[:number]) | ||
request_exclusion = @staging_workflow.request_exclusions.build(bs_request: request, description: params[:description]) | ||
|
||
authorize request_exclusion | ||
|
||
if request_exclusion.save | ||
render_ok | ||
else | ||
render_error( | ||
status: 400, | ||
errorcode: 'invalid_request', | ||
message: request_exclusion.errors.full_messages.to_sentence | ||
) | ||
end | ||
end | ||
|
||
def destroy | ||
authorize @request_exclusion | ||
|
||
if @request_exclusion.destroy | ||
render_ok | ||
else | ||
render_error( | ||
status: 400, | ||
errorcode: 'invalid_request', | ||
message: "Request #{@request_exclusion.number} couldn't be unexcluded" | ||
) | ||
end | ||
end | ||
|
||
private | ||
|
||
def set_staging_workflow | ||
project = Project.get_by_name(params[:project_name]) | ||
@staging_workflow = project.staging | ||
return if @staging_workflow | ||
|
||
raise InvalidParameterError, "Project #{params[:project_name]} doesn't have an asociated Staging Workflow" | ||
end | ||
|
||
def set_request_exclusion | ||
request = BsRequest.find_by!(number: params[:number]) | ||
@request_exclusion = request.request_exclusion | ||
return if @request_exclusion | ||
|
||
raise InvalidParameterError, "Request #{params[:number]} is not excluded" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
class Staging::RequestExclusionPolicy < ApplicationPolicy | ||
def create? | ||
group = record.staging_workflow.managers_group | ||
user.groups_users.where(group: group).exists? || | ||
ProjectPolicy.new(user, record.staging_workflow.project).update? | ||
end | ||
|
||
def destroy? | ||
create? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
src/api/spec/controllers/staging/excluded_requests_controller_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
require 'rails_helper' | ||
|
||
RSpec.describe Staging::ExcludedRequestsController, type: :controller, vcr: true do | ||
let(:user) { create(:confirmed_user, login: 'user') } | ||
let(:other_user) { create(:confirmed_user, login: 'other_user') } | ||
let(:project) { user.home_project } | ||
let(:staging_workflow) { create(:staging_workflow_with_staging_projects, project: project) } | ||
let(:group) { staging_workflow.managers_group } | ||
let(:manager) { create(:confirmed_user, login: 'manager', groups: [group]) } | ||
let(:source_project) { create(:project, name: 'source_project') } | ||
let(:target_package) { create(:package, name: 'target_package', project: project) } | ||
let(:source_package) { create(:package, name: 'source_package', project: source_project) } | ||
let(:review) { create(:review, by_group: group.title) } | ||
let(:bs_request) do | ||
create(:bs_request_with_submit_action, | ||
creator: other_user, | ||
target_project: project.name, | ||
target_package: target_package.name, | ||
source_project: source_project.name, | ||
source_package: source_package.name, | ||
reviews: [review]) | ||
end | ||
|
||
before { login(manager) } | ||
|
||
describe 'POST #create' do | ||
context 'succeeds' do | ||
subject { post :create, params: { number: bs_request.number, project_name: project, description: "I don't want to see you any more" }, format: :xml } | ||
|
||
context 'response' do | ||
before { subject } | ||
|
||
it { expect(response).to have_http_status(:success) } | ||
end | ||
|
||
it { expect { subject }.to(change { staging_workflow.request_exclusions.count }.by(1)) } | ||
end | ||
|
||
context 'fails: project does not exist' do | ||
subject { post :create, params: { number: bs_request.number, project_name: 'i_do_not_exist', description: "I don't want to see you any more" }, format: :xml } | ||
|
||
context 'response' do | ||
before { subject } | ||
|
||
it { expect(response).to have_http_status(404) } | ||
end | ||
|
||
it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) } | ||
end | ||
|
||
context 'fails: project without staging_workflow' do | ||
let(:project_without_staging) { create(:project, name: 'no_staging') } | ||
subject { post :create, params: { number: bs_request.number, project_name: project_without_staging, description: "I don't want to see you any more" }, format: :xml } | ||
|
||
context 'response' do | ||
before { subject } | ||
|
||
it { expect(response).to have_http_status(400) } | ||
end | ||
|
||
it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) } | ||
end | ||
|
||
context 'fails: invalid request exclusion' do | ||
let(:project_without_staging) { create(:project, name: 'without_staging_wokflow') } | ||
subject { post :create, params: { number: bs_request.number, project_name: project_without_staging }, format: :xml } | ||
|
||
context 'response' do | ||
before { subject } | ||
|
||
it { expect(response).to have_http_status(400) } | ||
end | ||
|
||
it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) } | ||
end | ||
end | ||
|
||
describe 'DELETE #destroy' do | ||
let(:request_exclusion) { create(:request_exclusion, bs_request: bs_request, staging_workflow: staging_workflow) } | ||
|
||
context 'succeeds' do | ||
subject { delete :destroy, params: { number: bs_request.number }, format: :xml } | ||
|
||
before { request_exclusion } | ||
|
||
context 'response' do | ||
before { subject } | ||
|
||
it { expect(response).to have_http_status(:success) } | ||
end | ||
|
||
it { expect { subject }.to(change { staging_workflow.request_exclusions.count }.by(-1)) } | ||
end | ||
|
||
context 'fails: request does not exist' do | ||
subject { delete :destroy, params: { number: 43543 }, format: :xml } | ||
|
||
context 'response' do | ||
before { subject } | ||
|
||
it { expect(response).to have_http_status(404) } | ||
end | ||
|
||
it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) } | ||
end | ||
|
||
context 'fails: request not excluded' do | ||
subject { delete :destroy, params: { number: bs_request.number }, format: :xml } | ||
|
||
context 'response' do | ||
before { subject } | ||
|
||
it { expect(response).to have_http_status(400) } | ||
end | ||
|
||
it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) } | ||
end | ||
|
||
context 'fails: unable to destroy' do | ||
before do | ||
request_exclusion | ||
allow_any_instance_of(Staging::RequestExclusion).to receive(:destroy).and_return(false) | ||
end | ||
|
||
subject { delete :destroy, params: { number: bs_request.number }, format: :xml } | ||
|
||
context 'response' do | ||
before { subject } | ||
|
||
it { expect(response).to have_http_status(400) } | ||
end | ||
|
||
it { expect { subject }.not_to(change { staging_workflow.request_exclusions.count }) } | ||
end | ||
end | ||
end |