This repository has been archived by the owner on Oct 12, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
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 #6 from alphagov/add_router_to_single_registration
Add router capability to single registration.
- Loading branch information
Showing
11 changed files
with
175 additions
and
2 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
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,36 @@ | ||
require 'router' | ||
|
||
class RoutableArtefact | ||
|
||
def initialize(artefact) | ||
@artefact = artefact | ||
end | ||
|
||
def logger | ||
Rails.logger | ||
end | ||
|
||
def router | ||
@router ||= Router.new | ||
end | ||
|
||
def ensure_application_exists | ||
backend_url = Plek.current.find(@artefact.owning_app) | ||
router.update_application(@artefact.owning_app, backend_url) | ||
end | ||
|
||
def submit | ||
ensure_application_exists | ||
paths = (@artefact.paths || []) | ||
prefixes = (@artefact.prefixes || []) | ||
unless prefixes.include?(@artefact.slug) | ||
paths << @artefact.slug | ||
end | ||
paths.uniq.each do |path| | ||
@router.create_route(path, "full", @artefact.owning_app) | ||
end | ||
prefixes.each do |prefix| | ||
@router.create_route(prefix, "prefix", @artefact.owning_app) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class UpdateRouterObserver < Mongoid::Observer | ||
observe :artefact | ||
|
||
def after_save(artefact) | ||
RoutableArtefact.new(artefact).submit if artefact.live? | ||
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,18 @@ | ||
# In development environments we don't want to depend on Router unless | ||
# explicitly told to do so | ||
unless Rails.env.development? | ||
update_router = true | ||
else | ||
update_router = ENV['UPDATE_ROUTER'].present? | ||
end | ||
|
||
if update_router | ||
Rails.logger.info "Registering router observer for artefacts" | ||
# Use to_prepare so this gets reloaded with the app when in development | ||
# In production, it will only be called once | ||
ActionDispatch::Callbacks.to_prepare do | ||
Panopticon::Application.config.mongoid.observers << :update_router_observer | ||
end | ||
else | ||
Rails.logger.info "In development/test mode: not registering router observer" | ||
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
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,62 @@ | ||
require 'test_helper' | ||
|
||
class RoutableArtefactTest < ActiveSupport::TestCase | ||
setup do | ||
@artefact = FactoryGirl.create(:artefact, owning_app: "bee") | ||
@routable = RoutableArtefact.new(@artefact) | ||
end | ||
|
||
should "ensure that the application exists in the router" do | ||
Router.any_instance.expects(:update_application).with("bee", "http://bee.test.gov.uk") | ||
Router.any_instance.stubs(:create_route) | ||
@routable.submit | ||
end | ||
|
||
should "create a full route for the slug" do | ||
Router.any_instance.stubs(:update_application) | ||
Router.any_instance.expects(:create_route).with(@artefact.slug, "full", "bee") | ||
@routable.submit | ||
end | ||
|
||
should "create full routes for paths" do | ||
@artefact.paths = ["#{@artefact.slug}.json", "#{@artefact.slug}.ics"] | ||
Router.any_instance.stubs(:update_application) | ||
Router.any_instance.stubs(:create_route).with(@artefact.slug, "full", "bee") | ||
Router.any_instance.expects(:create_route).with("#{@artefact.slug}.json", "full", "bee") | ||
Router.any_instance.expects(:create_route).with("#{@artefact.slug}.ics", "full", "bee") | ||
@routable.submit | ||
end | ||
|
||
context "the slug is repeated in one of the paths" do | ||
setup do | ||
@artefact.paths = [@artefact.slug] | ||
end | ||
|
||
should "not (attempt to) register duplicate full routes" do | ||
Router.any_instance.stubs(:update_application) | ||
Router.any_instance.expects(:create_route).with(@artefact.slug, "full", "bee") | ||
@routable.submit | ||
end | ||
end | ||
|
||
should "create prefix routes for prefixes" do | ||
@artefact.prefixes = ["un", "re"] | ||
Router.any_instance.stubs(:update_application) | ||
Router.any_instance.stubs(:create_route).with(@artefact.slug, "full", "bee") | ||
Router.any_instance.expects(:create_route).with("un", "prefix", "bee") | ||
Router.any_instance.expects(:create_route).with("re", "prefix", "bee") | ||
@routable.submit | ||
end | ||
|
||
context "the slug is also a prefix" do | ||
setup do | ||
@artefact.prefixes = [@artefact.slug] | ||
end | ||
|
||
should "send it as a prefix, and not send a full route" do | ||
Router.any_instance.stubs(:update_application) | ||
Router.any_instance.expects(:create_route).with(@artefact.slug, "prefix", "bee") | ||
@routable.submit | ||
end | ||
end | ||
end |