From 3e653227bd9d2bbb59023963453c97825c26863f Mon Sep 17 00:00:00 2001 From: Samir Noir Date: Tue, 7 Jul 2020 18:04:21 +0200 Subject: [PATCH] Return 404 when an ui template cannot be found --- app/controllers/ui_controller.rb | 14 ++++++++------ app/views/ui/404.html.haml | 8 ++++++++ spec/controllers/ui_controller_spec.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 42 insertions(+), 6 deletions(-) create mode 100644 app/views/ui/404.html.haml create mode 100644 spec/controllers/ui_controller_spec.rb diff --git a/app/controllers/ui_controller.rb b/app/controllers/ui_controller.rb index cdcd1e25..735599b6 100644 --- a/app/controllers/ui_controller.rb +++ b/app/controllers/ui_controller.rb @@ -13,28 +13,30 @@ # limitations under the License. class UiController < ApplicationController - + rescue_from ActionView::MissingTemplate do + render :template => "ui/404.html.haml", :status => 404 + end + def show params[:page] ||= "dashboard" @id = params[:page].downcase.gsub(/[^a-z]/,'_').squeeze('_') @title = params[:page] - + respond_to do |format| format.html { render params[:page].to_sym } end end - - def visualization + def visualization @id = params[:page].downcase.gsub(/[^a-z]/,'_').squeeze('_') @title = params[:page] - + respond_to do |format| format.html { render "ui/visualizations/#{params[:page]}.html.haml" } end end -end \ No newline at end of file +end diff --git a/app/views/ui/404.html.haml b/app/views/ui/404.html.haml new file mode 100644 index 00000000..512cb109 --- /dev/null +++ b/app/views/ui/404.html.haml @@ -0,0 +1,8 @@ +- content_for :before_js do + %script{:src => relative_path_to("/javascripts/vendor/exhibitv2/exhibit/api/exhibit-api.js")+"?autoCreate=false", + :type => "text/javascript"} + %script{:src => relative_path_to("/javascripts/lib/exhibit.js"), + :type => "text/javascript"} + +%h2 + Page not found diff --git a/spec/controllers/ui_controller_spec.rb b/spec/controllers/ui_controller_spec.rb new file mode 100644 index 00000000..ec499658 --- /dev/null +++ b/spec/controllers/ui_controller_spec.rb @@ -0,0 +1,26 @@ +# Copyright (c) 2009-2011 Cyril Rohr, INRIA Rennes - Bretagne Atlantique +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +require 'spec_helper' + +describe UiController do + render_views + + describe "GET /ui/doesnotexist" do + it "should return 404 if the page does not exist" do + get :visualization, format: :html, params: { :page => 'doesnotexist' } + expect(response.status).to eq(404) + end + end +end -- GitLab