diff --git a/CHANGELOG.md b/CHANGELOG.md index e51f5f40..789a87fa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## Unreleased + +* Remove router-api [PR](https://github.com/alphagov/gds-api-adapters/pull/1308) + ## 97.6.0 * Add a method to fetch a single host content item [PR](https://github.com/alphagov/gds-api-adapters/pull/1306) diff --git a/lib/gds_api.rb b/lib/gds_api.rb index 58755cb8..9852e574 100644 --- a/lib/gds_api.rb +++ b/lib/gds_api.rb @@ -14,7 +14,6 @@ require "gds_api/maslow" require "gds_api/organisations" require "gds_api/publishing_api" -require "gds_api/router" require "gds_api/search" require "gds_api/search_api_v2" require "gds_api/support" @@ -155,19 +154,6 @@ def self.publishing_api(options = {}) ) end - # Creates a GdsApi::Router adapter for communicating with Router API - # - # This will set a bearer token if a ROUTER_API_BEARER_TOKEN environment - # variable is set - # - # @return [GdsApi::Router] - def self.router(options = {}) - GdsApi::Router.new( - Plek.find("router-api"), - { bearer_token: ENV["ROUTER_API_BEARER_TOKEN"] }.merge(options), - ) - end - # Creates a GdsApi::Search adapter to access via a search.* hostname # # @return [GdsApi::Search] diff --git a/lib/gds_api/router.rb b/lib/gds_api/router.rb deleted file mode 100644 index ea1ed469..00000000 --- a/lib/gds_api/router.rb +++ /dev/null @@ -1,62 +0,0 @@ -require_relative "base" - -class GdsApi::Router < GdsApi::Base - ### Backends - - def get_backend(id) - get_json("#{endpoint}/backends/#{CGI.escape(id)}") - end - - def add_backend(id, url) - put_json("#{endpoint}/backends/#{CGI.escape(id)}", backend: { backend_url: url }) - end - - def delete_backend(id) - delete_json("#{endpoint}/backends/#{CGI.escape(id)}") - end - - ### Routes - - def get_route(path) - get_json("#{endpoint}/routes?incoming_path=#{CGI.escape(path)}") - end - - def add_route(path, type, backend_id) - put_json( - "#{endpoint}/routes", - route: { - incoming_path: path, - route_type: type, - handler: "backend", - backend_id:, - }, - ) - end - - def add_redirect_route(path, type, destination, options = {}) - put_json( - "#{endpoint}/routes", - route: { - incoming_path: path, - route_type: type, - handler: "redirect", - redirect_to: destination, - segments_mode: options[:segments_mode], - }, - ) - end - - def add_gone_route(path, type) - put_json( - "#{endpoint}/routes", - route: { incoming_path: path, route_type: type, handler: "gone" }, - ) - end - - def delete_route(path, hard_delete: false) - url = "#{endpoint}/routes?incoming_path=#{CGI.escape(path)}" - url += "&hard_delete=true" if hard_delete - - delete_json(url) - end -end diff --git a/lib/gds_api/test_helpers/router.rb b/lib/gds_api/test_helpers/router.rb deleted file mode 100644 index 2bb94e1f..00000000 --- a/lib/gds_api/test_helpers/router.rb +++ /dev/null @@ -1,94 +0,0 @@ -require "gds_api/test_helpers/json_client_helper" - -module GdsApi - module TestHelpers - module Router - ROUTER_API_ENDPOINT = Plek.find("router-api") - - def stub_router_has_route(path, route, bearer_token = ENV["ROUTER_API_BEARER_TOKEN"]) - stub_get_route(path, bearer_token).to_return( - status: 200, - body: route.to_json, - headers: { "Content-Type" => "application/json" }, - ) - end - - def stub_router_doesnt_have_route(path, bearer_token = ENV["ROUTER_API_BEARER_TOKEN"]) - stub_get_route(path, bearer_token).to_return(status: 404) - end - - def stub_router_has_backend_route(path, backend_id:, route_type: "exact", disabled: false) - stub_router_has_route(path, handler: "backend", backend_id:, disabled:, route_type:) - end - - def stub_router_has_redirect_route(path, redirect_to:, route_type: "exact", disabled: false) - stub_router_has_route(path, handler: "redirect", redirect_to:, disabled:, route_type:) - end - - def stub_router_has_gone_route(path, route_type: "exact", disabled: false) - stub_router_has_route(path, handler: "gone", route_type:, disabled:) - end - - def stub_all_router_registration - stub_request(:put, %r{\A#{ROUTER_API_ENDPOINT}/backends/[a-z0-9-]+\z}) - stub_request(:put, "#{ROUTER_API_ENDPOINT}/routes") - end - - def stub_router_backend_registration(backend_id, backend_url) - backend = { "backend" => { "backend_url" => backend_url } } - stub_http_request(:put, "#{ROUTER_API_ENDPOINT}/backends/#{backend_id}") - .with(body: backend.to_json) - .to_return(status: 201) - end - - def stub_route_registration(path, type, backend_id) - stub_route_put({ - route: { - incoming_path: path, - route_type: type, - handler: "backend", - backend_id:, - }, - }) - end - - def stub_redirect_registration(path, type, destination, segments_mode = nil) - stub_route_put({ - route: { - incoming_path: path, - route_type: type, - handler: "redirect", - redirect_to: destination, - segments_mode:, - }, - }) - end - - def stub_gone_route_registration(path, type) - stub_route_put({ - route: { - incoming_path: path, - route_type: type, - handler: "gone", - }, - }) - end - - private - - def stub_get_route(path, bearer_token) - stub_http_request(:get, "#{ROUTER_API_ENDPOINT}/routes") - .with( - query: { "incoming_path" => path }, - headers: { "Authorization" => "Bearer #{bearer_token}" }, - ) - end - - def stub_route_put(route) - stub_http_request(:put, "#{ROUTER_API_ENDPOINT}/routes") - .with(body: route.to_json) - .to_return(status: 201) - end - end - end -end diff --git a/test/router_test.rb b/test/router_test.rb deleted file mode 100644 index bdf0897d..00000000 --- a/test/router_test.rb +++ /dev/null @@ -1,376 +0,0 @@ -require "test_helper" -require "gds_api/router" -require "gds_api/test_helpers/router" - -describe GdsApi::Router do - include GdsApi::TestHelpers::Router - - before do - @base_api_url = Plek.find("router-api") - @api = GdsApi::Router.new(@base_api_url, bearer_token: "token") - end - - around do |test| - ClimateControl.modify ROUTER_API_BEARER_TOKEN: "token" do - test.call - end - end - - describe "managing backends" do - describe "fetching details about a backend" do - it "should return backend details" do - req = WebMock.stub_request(:get, "#{@base_api_url}/backends/foo") - .to_return(body: { "backend_id" => "foo", "backend_url" => "http://foo.example.com/" }.to_json, - headers: { "Content-type" => "application/json" }) - - response = @api.get_backend("foo") - assert_equal 200, response.code - assert_equal "http://foo.example.com/", response["backend_url"] - - assert_requested(req) - end - - it "raises for a non-existend backend" do - req = WebMock.stub_request(:get, "#{@base_api_url}/backends/foo") - .to_return(status: 404) - - assert_raises(GdsApi::HTTPNotFound) do - @api.get_backend("foo") - end - - assert_requested(req) - end - - it "should URI escape the given ID" do - req = WebMock.stub_request(:get, "#{@base_api_url}/backends/foo+bar") - .to_return(status: 404) - - assert_raises(GdsApi::HTTPNotFound) do - @api.get_backend("foo bar") - end - - assert_requested(req) - end - end - - describe "creating/updating a backend" do - it "should allow creating/updating a backend" do - req = WebMock.stub_request(:put, "#{@base_api_url}/backends/foo") - .with(body: { "backend" => { "backend_url" => "http://foo.example.com/" } }.to_json) - .to_return(status: 201, - body: { "backend_id" => "foo", "backend_url" => "http://foo.example.com/" }.to_json, - headers: { "Content-type" => "application/json" }) - - response = @api.add_backend("foo", "http://foo.example.com/") - assert_equal 201, response.code - assert_equal "http://foo.example.com/", response["backend_url"] - - assert_requested(req) - end - - it "should raise an error if creating/updating a backend fails" do - response_data = { - "backend_id" => "foo", - "backend_url" => "ftp://foo.example.com/", - "errors" => { "backend_url" => "is not an HTTP URL" }, - } - - req = WebMock.stub_request(:put, "#{@base_api_url}/backends/foo") - .with(body: { "backend" => { "backend_url" => "http://foo.example.com/" } }.to_json) - .to_return(status: 400, body: response_data.to_json, headers: { "Content-type" => "application/json" }) - - e = assert_raises(GdsApi::HTTPErrorResponse) do - @api.add_backend("foo", "http://foo.example.com/") - end - - assert_equal 400, e.code - assert_equal response_data, e.error_details - - assert_requested(req) - end - - it "should URI escape the passed id" do - req = WebMock.stub_request(:put, "#{@base_api_url}/backends/foo+bar") - .with(body: { "backend" => { "backend_url" => "http://foo.example.com/" } }.to_json) - .to_return(status: 404, body: "Not found") - - # We expect a GdsApi::HTTPErrorResponse, but we want to ensure nothing else is raised - begin - @api.add_backend("foo bar", "http://foo.example.com/") - rescue GdsApi::HTTPErrorResponse - nil - end - - assert_requested(req) - end - end - - describe "deleting a backend" do - it "allow deleting a backend" do - req = WebMock.stub_request(:delete, "#{@base_api_url}/backends/foo") - .to_return(status: 200, - body: { "backend_id" => "foo", "backend_url" => "http://foo.example.com/" }.to_json, - headers: { "Content-type" => "application/json" }) - - response = @api.delete_backend("foo") - assert_equal 200, response.code - assert_equal "http://foo.example.com/", response["backend_url"] - - assert_requested(req) - end - - it "should raise an error if deleting the backend fails" do - response_data = { - "backend_id" => "foo", - "backend_url" => "ftp://foo.example.com/", - "errors" => { "base" => "Backend has routes - can't delete" }, - } - - req = WebMock.stub_request(:delete, "#{@base_api_url}/backends/foo") - .to_return(status: 400, body: response_data.to_json, headers: { "Content-type" => "application/json" }) - - e = assert_raises(GdsApi::HTTPErrorResponse) do - @api.delete_backend("foo") - end - - assert_equal 400, e.code - assert_equal response_data, e.error_details - - assert_requested(req) - end - - it "should URI escape the passed id" do - req = WebMock.stub_request(:delete, "#{@base_api_url}/backends/foo+bar") - .to_return(status: 404, body: "Not found") - - assert_raises GdsApi::HTTPErrorResponse do - @api.delete_backend("foo bar") - end - - assert_requested(req) - end - end - end - - describe "managing routes" do - describe "fetching a route" do - it "should return the backend route details" do - req = stub_router_has_backend_route("/foo", backend_id: "foo") - - response = @api.get_route("/foo") - assert_equal 200, response.code - assert_equal "foo", response["backend_id"] - - assert_requested(req) - end - - it "should raise if nothing found" do - req = stub_router_doesnt_have_route("/foo") - - assert_raises(GdsApi::HTTPNotFound) do - @api.get_route("/foo") - end - - assert_requested(req) - end - - it "should return the gone route details" do - stub_router_has_gone_route("/foo") - - response = @api.get_route("/foo") - assert_equal 200, response.code - assert_equal "gone", response["handler"] - end - - it "should return the redirect route details" do - stub_router_has_redirect_route("/foo", redirect_to: "/bar") - - response = @api.get_route("/foo") - assert_equal 200, response.code - assert_equal "redirect", response["handler"] - assert_equal "/bar", response["redirect_to"] - end - - it "should escape the params" do - # The WebMock query matcher matches unescaped params. The call blows up if they're not escaped - - req = WebMock.stub_request(:get, "#{@base_api_url}/routes") - .with(query: { "incoming_path" => "/foo bar" }) - .to_return(status: 404) - - assert_raises(GdsApi::HTTPNotFound) do - @api.get_route("/foo bar") - end - - assert_requested(req) - end - end - - describe "creating/updating a route" do - it "should allow creating/updating a route" do - route_data = { "incoming_path" => "/foo", "route_type" => "exact", "handler" => "backend", "backend_id" => "foo" } - req = WebMock.stub_request(:put, "#{@base_api_url}/routes") - .with(body: { "route" => route_data }.to_json) - .to_return(status: 201, body: route_data.to_json, headers: { "Content-type" => "application/json" }) - - response = @api.add_route("/foo", "exact", "foo") - assert_equal 201, response.code - assert_equal "foo", response["backend_id"] - - assert_requested(req) - end - - it "should raise an error if creating/updating the route fails" do - route_data = { "incoming_path" => "/foo", "route_type" => "exact", "handler" => "backend", "backend_id" => "foo" } - response_data = route_data.merge("errors" => { "backend_id" => "does not exist" }) - - req = WebMock.stub_request(:put, "#{@base_api_url}/routes") - .with(body: { "route" => route_data }.to_json) - .to_return(status: 400, body: response_data.to_json, headers: { "Content-type" => "application/json" }) - - e = assert_raises(GdsApi::HTTPErrorResponse) do - @api.add_route("/foo", "exact", "foo") - end - - assert_equal 400, e.code - assert_equal response_data, e.error_details - - assert_requested(req) - end - end - - describe "creating/updating a redirect route" do - it "should allow creating/updating a redirect route" do - route_data = { "incoming_path" => "/foo", - "route_type" => "exact", - "handler" => "redirect", - "redirect_to" => "/bar", - "segments_mode" => nil } - req = WebMock.stub_request(:put, "#{@base_api_url}/routes") - .with(body: { "route" => route_data }.to_json) - .to_return(status: 201, body: route_data.to_json, headers: { "Content-type" => "application/json" }) - - response = @api.add_redirect_route("/foo", "exact", "/bar") - assert_equal 201, response.code - assert_equal "/bar", response["redirect_to"] - - assert_requested(req) - end - - it "should allow creating/updating a redirect route which preserves segments" do - route_data = { "incoming_path" => "/foo", - "route_type" => "exact", - "handler" => "redirect", - "redirect_to" => "/bar", - "segments_mode" => "preserve" } - req = WebMock.stub_request(:put, "#{@base_api_url}/routes") - .with(body: { "route" => route_data }.to_json) - .to_return(status: 201, body: route_data.to_json, headers: { "Content-type" => "application/json" }) - - response = @api.add_redirect_route("/foo", "exact", "/bar", segments_mode: "preserve") - assert_equal 201, response.code - assert_equal "/bar", response["redirect_to"] - - assert_requested(req) - end - - it "should raise an error if creating/updating the redirect route fails" do - route_data = { "incoming_path" => "/foo", - "route_type" => "exact", - "handler" => "redirect", - "redirect_to" => "bar", - "segments_mode" => nil } - response_data = route_data.merge("errors" => { "redirect_to" => "is not a valid URL path" }) - - req = WebMock.stub_request(:put, "#{@base_api_url}/routes") - .with(body: { "route" => route_data }.to_json) - .to_return(status: 400, body: response_data.to_json, headers: { "Content-type" => "application/json" }) - - e = assert_raises(GdsApi::HTTPErrorResponse) do - @api.add_redirect_route("/foo", "exact", "bar") - end - - assert_equal 400, e.code - assert_equal response_data, e.error_details - - assert_requested(req) - end - end - - describe "#add_gone_route" do - it "should allow creating/updating a gone route" do - route_data = { "incoming_path" => "/foo", "route_type" => "exact", "handler" => "gone" } - req = WebMock.stub_request(:put, "#{@base_api_url}/routes") - .with(body: { "route" => route_data }.to_json) - .to_return(status: 201, body: route_data.to_json, headers: { "Content-type" => "application/json" }) - - response = @api.add_gone_route("/foo", "exact") - assert_equal 201, response.code - assert_equal "/foo", response["incoming_path"] - - assert_requested(req) - end - - it "should raise an error if creating/updating the gone route fails" do - route_data = { "incoming_path" => "foo", "route_type" => "exact", "handler" => "gone" } - response_data = route_data.merge("errors" => { "incoming_path" => "is not a valid URL path" }) - - req = WebMock.stub_request(:put, "#{@base_api_url}/routes") - .with(body: { "route" => route_data }.to_json) - .to_return(status: 400, body: response_data.to_json, headers: { "Content-type" => "application/json" }) - - e = assert_raises(GdsApi::HTTPErrorResponse) do - @api.add_gone_route("foo", "exact") - end - - assert_equal 400, e.code - assert_equal response_data, e.error_details - - assert_requested(req) - end - end - - describe "deleting a route" do - it "should allow deleting a route" do - route_data = { "incoming_path" => "/foo", "route_type" => "exact", "handler" => "backend", "backend_id" => "foo" } - req = WebMock.stub_request(:delete, "#{@base_api_url}/routes") - .with(query: { "incoming_path" => "/foo" }) - .to_return(status: 200, body: route_data.to_json, headers: { "Content-type" => "application/json" }) - - response = @api.delete_route("/foo") - assert_equal 200, response.code - assert_equal "foo", response["backend_id"] - - assert_requested(req) - end - - it "should raise HTTPNotFound if nothing found" do - req = WebMock.stub_request(:delete, "#{@base_api_url}/routes") - .with(query: { "incoming_path" => "/foo" }) - .to_return(status: 404) - - e = assert_raises(GdsApi::HTTPNotFound) do - @api.delete_route("/foo") - end - - assert_equal 404, e.code - - assert_requested(req) - end - - it "should escape the params" do - # The WebMock query matcher matches unescaped params. The call blows up if they're not escaped - - req = WebMock.stub_request(:delete, "#{@base_api_url}/routes") - .with(query: { "incoming_path" => "/foo bar" }) - .to_return(status: 404) - - assert_raises GdsApi::HTTPNotFound do - @api.delete_route("/foo bar") - end - - assert_requested(req) - end - end - end -end