-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a new endpoint to unreserve a path
This adds a new command to unreserve (destroy) a PathReservation, provided the reservation exists and matches the given publishing app. As we migrate formats from Whitehall to Content Publisher, users sometimes need to revert to Whitehall if certain features aren't available. Because Whitehall assumes it is authoritative for all paths in a format, we need to be able to cleanup the Publishing API in order for the user to re-create their content in Whitehall and re-use a path.
- Loading branch information
Ben Thorner
committed
Mar 18, 2019
1 parent
0b1cdab
commit 8014783
Showing
7 changed files
with
159 additions
and
0 deletions.
There are no files selected for viewing
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,30 @@ | ||
module Commands | ||
class UnreservePath < BaseCommand | ||
def call | ||
reservation = lookup_reservation | ||
check_is_owned_by_app(reservation) | ||
reservation.destroy | ||
Success.new(payload) | ||
end | ||
|
||
private | ||
|
||
def lookup_reservation | ||
PathReservation.find_by!( | ||
base_path: payload[:base_path] | ||
) | ||
rescue ActiveRecord::RecordNotFound | ||
msg = "#{payload[:base_path]} is not reserved" | ||
raise CommandError.new(code: 404, message: msg) | ||
end | ||
|
||
def check_is_owned_by_app(reservation) | ||
publishing_app = payload[:publishing_app] | ||
base_path = payload[:base_path] | ||
return if reservation.publishing_app == publishing_app | ||
|
||
msg = "#{base_path} is reserved by #{reservation.publishing_app}" | ||
raise CommandError.new(code: 422, message: msg) | ||
end | ||
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
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
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
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,40 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe Commands::UnreservePath do | ||
describe "#call" do | ||
context "when the path is owned by the app" do | ||
it "successfully removes the reservation" do | ||
payload = { publishing_app: "foo", base_path: "/bar" } | ||
create(:path_reservation, payload) | ||
described_class.call(payload) | ||
expect(PathReservation.count).to be_zero | ||
end | ||
end | ||
|
||
context "when the path is not owned by the app" do | ||
it "returns an error" do | ||
payload = { base_path: "/bar", publishing_app: "foo" } | ||
|
||
create(:path_reservation, | ||
base_path: "/bar", | ||
publishing_app: "bar") | ||
|
||
expect { described_class.call(payload) } | ||
.to raise_error( | ||
CommandError, /is reserved/ | ||
) | ||
end | ||
end | ||
|
||
context "when the path has not been reserved" do | ||
it "returns an error" do | ||
payload = { base_path: "/bar" } | ||
|
||
expect { described_class.call(payload) } | ||
.to raise_error( | ||
CommandError, /is not reserved/ | ||
) | ||
end | ||
end | ||
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
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,32 @@ | ||
require "rails_helper" | ||
|
||
RSpec.describe "DELETE /paths", type: :request do | ||
let(:request_body) { payload.to_json } | ||
|
||
def do_request(body: request_body, headers: {}) | ||
delete request_path, params: body, headers: headers | ||
end | ||
|
||
context "with path /vat-rates" do | ||
let(:request_path) { "/paths#{base_path}" } | ||
let(:payload) { { publishing_app: "publisher" } } | ||
|
||
before do | ||
create(:path_reservation, | ||
base_path: base_path, | ||
publishing_app: "publisher") | ||
end | ||
|
||
it "responds successfully" do | ||
do_request | ||
|
||
expect(response.status).to eq(200) | ||
end | ||
|
||
it "unreserves the path" do | ||
expect { | ||
do_request | ||
}.to change(PathReservation, :count).by(-1) | ||
end | ||
end | ||
end |