Commit f93e3885d4836b4705d9c15c6ec3a32e7ddfb9b1

Authored by Rafael Manzo
1 parent eac93023

Trying to find a unexistent resource render the 404 page

app/controllers/base_metric_configurations_controller.rb
1 include OwnershipAuthentication 1 include OwnershipAuthentication
2 include MetricConfigurationsConcern 2 include MetricConfigurationsConcern
  3 +include ResourceFinder
3 4
4 class BaseMetricConfigurationsController < ApplicationController 5 class BaseMetricConfigurationsController < ApplicationController
5 before_action :authenticate_user!, except: [:show, :index] 6 before_action :authenticate_user!, except: [:show, :index]
app/controllers/concerns/metric_configurations_concern.rb
@@ -2,6 +2,6 @@ module MetricConfigurationsConcern @@ -2,6 +2,6 @@ module MetricConfigurationsConcern
2 extend ActiveSupport::Concern 2 extend ActiveSupport::Concern
3 3
4 def set_metric_configuration 4 def set_metric_configuration
5 - @metric_configuration = MetricConfiguration.find(params[:id].to_i) 5 + @metric_configuration = find_resource(MetricConfiguration, params[:id].to_i)
6 end 6 end
7 end 7 end
app/controllers/concerns/resource_finder.rb 0 → 100644
@@ -0,0 +1,15 @@ @@ -0,0 +1,15 @@
  1 +module ResourceFinder
  2 + extend ActiveSupport::Concern
  3 +
  4 + def find_resource(klass, id)
  5 + begin
  6 + klass.find(id)
  7 + rescue KalibroGatekeeperClient::Errors::RecordNotFound
  8 + respond_to do |format|
  9 + format.html { render file: "#{Rails.root}/public/404", layout: false, status: :not_found }
  10 + end
  11 +
  12 + return
  13 + end
  14 + end
  15 +end
0 \ No newline at end of file 16 \ No newline at end of file
app/controllers/mezuro_configurations_controller.rb
1 include OwnershipAuthentication 1 include OwnershipAuthentication
  2 +include ResourceFinder
2 3
3 class MezuroConfigurationsController < ApplicationController 4 class MezuroConfigurationsController < ApplicationController
4 before_action :authenticate_user!, except: [:index, :show] 5 before_action :authenticate_user!, except: [:index, :show]
@@ -65,7 +66,7 @@ class MezuroConfigurationsController &lt; ApplicationController @@ -65,7 +66,7 @@ class MezuroConfigurationsController &lt; ApplicationController
65 private 66 private
66 # Use callbacks to share common setup or constraints between actions. 67 # Use callbacks to share common setup or constraints between actions.
67 def set_mezuro_configuration 68 def set_mezuro_configuration
68 - @mezuro_configuration = MezuroConfiguration.find(params[:id]) 69 + @mezuro_configuration = find_resource(MezuroConfiguration, params[:id].to_i)
69 end 70 end
70 71
71 # Never trust parameters from the scary internet, only allow the white list through. 72 # Never trust parameters from the scary internet, only allow the white list through.
app/controllers/mezuro_ranges_controller.rb
1 include OwnershipAuthentication 1 include OwnershipAuthentication
  2 +include ResourceFinder
2 3
3 class MezuroRangesController < ApplicationController 4 class MezuroRangesController < ApplicationController
4 before_action :authenticate_user!, except: [:show] 5 before_action :authenticate_user!, except: [:show]
5 before_action :metric_configuration_owner?, only: [:new, :create, :destroy, :edit, :update] 6 before_action :metric_configuration_owner?, only: [:new, :create, :destroy, :edit, :update]
6 before_action :get_url_params, only: [:update, :create, :destroy] 7 before_action :get_url_params, only: [:update, :create, :destroy]
7 - before_action :set_mezuro_range, only: [:edit, :update] 8 + before_action :set_mezuro_range, only: [:edit, :update, :destroy]
8 9
9 def new 10 def new
10 @mezuro_range = MezuroRange.new 11 @mezuro_range = MezuroRange.new
@@ -20,7 +21,6 @@ class MezuroRangesController &lt; ApplicationController @@ -20,7 +21,6 @@ class MezuroRangesController &lt; ApplicationController
20 end 21 end
21 22
22 def destroy 23 def destroy
23 - @mezuro_range = MezuroRange.find(params[:id].to_i)  
24 @mezuro_range.destroy 24 @mezuro_range.destroy
25 respond_to do |format| 25 respond_to do |format|
26 format.html { redirect_to mezuro_configuration_metric_configuration_path( 26 format.html { redirect_to mezuro_configuration_metric_configuration_path(
@@ -85,6 +85,6 @@ class MezuroRangesController &lt; ApplicationController @@ -85,6 +85,6 @@ class MezuroRangesController &lt; ApplicationController
85 end 85 end
86 86
87 def set_mezuro_range 87 def set_mezuro_range
88 - @mezuro_range = MezuroRange.find(params[:id].to_i) 88 + @mezuro_range = find_resource(MezuroRange, params[:id].to_i)
89 end 89 end
90 end 90 end
app/controllers/modules_controller.rb
  1 +include ResourceFinder
  2 +
1 class ModulesController < ApplicationController 3 class ModulesController < ApplicationController
2 # POST /modules/1/metric_history 4 # POST /modules/1/metric_history
3 def metric_history 5 def metric_history
4 - @module_result = ModuleResult.find(params[:id].to_i) 6 + @module_result = find_resource(ModuleResult, params[:id].to_i)
5 @container = params[:container] 7 @container = params[:container]
6 @metric_name = params[:metric_name] 8 @metric_name = params[:metric_name]
7 end 9 end
8 10
9 # POST /modules/1/tree 11 # POST /modules/1/tree
10 def load_module_tree 12 def load_module_tree
11 - @root_module_result = ModuleResult.find(params[:id].to_i) 13 + @root_module_result = find_resource(ModuleResult, params[:id].to_i)
12 end 14 end
13 end 15 end
14 \ No newline at end of file 16 \ No newline at end of file
app/controllers/projects_controller.rb
1 include OwnershipAuthentication 1 include OwnershipAuthentication
  2 +include ResourceFinder
2 3
3 class ProjectsController < ApplicationController 4 class ProjectsController < ApplicationController
4 before_action :authenticate_user!, 5 before_action :authenticate_user!,
@@ -32,7 +33,7 @@ class ProjectsController &lt; ApplicationController @@ -32,7 +33,7 @@ class ProjectsController &lt; ApplicationController
32 # GET /project/1.json 33 # GET /project/1.json
33 def show 34 def show
34 set_project 35 set_project
35 - @project_repositories = @project.repositories 36 + @project_repositories = @project.repositories if @project.is_a?(Project)
36 end 37 end
37 38
38 # GET /projects/1/edit 39 # GET /projects/1/edit
@@ -66,8 +67,8 @@ class ProjectsController &lt; ApplicationController @@ -66,8 +67,8 @@ class ProjectsController &lt; ApplicationController
66 private 67 private
67 # Use callbacks to share common setup or constraints between actions. 68 # Use callbacks to share common setup or constraints between actions.
68 def set_project 69 def set_project
69 - @project = Project.find(params[:id])  
70 - @project_image = ProjectImage.find_by_project_id(@project.id) 70 + @project = find_resource(Project, params[:id].to_i)
  71 + @project_image = ProjectImage.find_by_project_id(@project.id) if @project.is_a?(Project)
71 end 72 end
72 73
73 # Never trust parameters from the scary internet, only allow the white list through. 74 # Never trust parameters from the scary internet, only allow the white list through.
app/controllers/reading_groups_controller.rb
1 include OwnershipAuthentication 1 include OwnershipAuthentication
  2 +include ResourceFinder
2 3
3 class ReadingGroupsController < ApplicationController 4 class ReadingGroupsController < ApplicationController
4 before_action :authenticate_user!, except: [:index, :show] 5 before_action :authenticate_user!, except: [:index, :show]
@@ -56,7 +57,7 @@ class ReadingGroupsController &lt; ApplicationController @@ -56,7 +57,7 @@ class ReadingGroupsController &lt; ApplicationController
56 57
57 # Use callbacks to share common setup or constraints between actions. 58 # Use callbacks to share common setup or constraints between actions.
58 def set_reading_group 59 def set_reading_group
59 - @reading_group = ReadingGroup.find(params[:id]) 60 + @reading_group = find_resource(ReadingGroup, params[:id].to_i)
60 end 61 end
61 62
62 # Never trust parameters from the scary internet, only allow the white list through. 63 # Never trust parameters from the scary internet, only allow the white list through.
app/controllers/readings_controller.rb
1 include OwnershipAuthentication 1 include OwnershipAuthentication
  2 +include ResourceFinder
2 3
3 class ReadingsController < ApplicationController 4 class ReadingsController < ApplicationController
4 before_action :authenticate_user!, except: [:index] 5 before_action :authenticate_user!, except: [:index]
@@ -73,6 +74,6 @@ class ReadingsController &lt; ApplicationController @@ -73,6 +74,6 @@ class ReadingsController &lt; ApplicationController
73 end 74 end
74 75
75 def set_reading 76 def set_reading
76 - @reading = Reading.find(params[:id].to_i) 77 + @reading = find_resource(Reading, params[:id].to_i)
77 end 78 end
78 end 79 end
app/controllers/repositories_controller.rb
@@ -105,7 +105,7 @@ private @@ -105,7 +105,7 @@ private
105 105
106 # Use callbacks to share common setup or constraints between actions. 106 # Use callbacks to share common setup or constraints between actions.
107 def set_repository 107 def set_repository
108 - @repository = Repository.find(params[:id].to_i) 108 + @repository = find_resource(Repository, params[:id].to_i)
109 end 109 end
110 110
111 def set_mezuro_configuration 111 def set_mezuro_configuration
spec/controllers/base_metric_configurations_controller_spec.rb
@@ -118,7 +118,7 @@ describe InheritsFromBaseMetricConfigurationsController, :type =&gt; :controller do @@ -118,7 +118,7 @@ describe InheritsFromBaseMetricConfigurationsController, :type =&gt; :controller do
118 context 'with a valid metric_configuration' do 118 context 'with a valid metric_configuration' do
119 before :each do 119 before :each do
120 ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) 120 ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group)
121 - MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) 121 + subject.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration)
122 MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range]) 122 MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range])
123 123
124 get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id 124 get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id
spec/controllers/compound_metric_configurations_controller_spec.rb
@@ -74,7 +74,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do @@ -74,7 +74,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do
74 74
75 before :each do 75 before :each do
76 ReadingGroup.expects(:find).with(compound_metric_configuration.reading_group_id).returns(reading_group) 76 ReadingGroup.expects(:find).with(compound_metric_configuration.reading_group_id).returns(reading_group)
77 - MetricConfiguration.expects(:find).with(compound_metric_configuration.id).returns(compound_metric_configuration) 77 + subject.expects(:find_resource).with(MetricConfiguration, compound_metric_configuration.id).returns(compound_metric_configuration)
78 MezuroRange.expects(:ranges_of).with(compound_metric_configuration.id).returns([mezuro_range]) 78 MezuroRange.expects(:ranges_of).with(compound_metric_configuration.id).returns([mezuro_range])
79 79
80 get :show, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s, id: compound_metric_configuration.id 80 get :show, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s, id: compound_metric_configuration.id
@@ -94,7 +94,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do @@ -94,7 +94,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do
94 context 'when the user owns the compound metric configuration' do 94 context 'when the user owns the compound metric configuration' do
95 before :each do 95 before :each do
96 subject.expects(:metric_configuration_owner?).returns(true) 96 subject.expects(:metric_configuration_owner?).returns(true)
97 - MetricConfiguration.expects(:find).at_least_once.with(compound_metric_configuration.id).returns(compound_metric_configuration) 97 + subject.expects(:find_resource).with(MetricConfiguration, compound_metric_configuration.id).returns(compound_metric_configuration)
98 MetricConfiguration.expects(:metric_configurations_of).with(mezuro_configuration.id).returns([compound_metric_configuration]) 98 MetricConfiguration.expects(:metric_configurations_of).with(mezuro_configuration.id).returns([compound_metric_configuration])
99 get :edit, id: compound_metric_configuration.id, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s 99 get :edit, id: compound_metric_configuration.id, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s
100 end 100 end
@@ -138,7 +138,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do @@ -138,7 +138,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do
138 138
139 context 'with valid fields' do 139 context 'with valid fields' do
140 before :each do 140 before :each do
141 - MetricConfiguration.expects(:find).at_least_once.with(compound_metric_configuration.id).returns(compound_metric_configuration) 141 + subject.expects(:find_resource).with(MetricConfiguration, compound_metric_configuration.id).returns(compound_metric_configuration)
142 MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true) 142 MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true)
143 143
144 post :update, mezuro_configuration_id: compound_metric_configuration.configuration_id, id: compound_metric_configuration.id, metric_configuration: metric_configuration_params 144 post :update, mezuro_configuration_id: compound_metric_configuration.configuration_id, id: compound_metric_configuration.id, metric_configuration: metric_configuration_params
@@ -150,7 +150,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do @@ -150,7 +150,7 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do
150 150
151 context 'with an invalid field' do 151 context 'with an invalid field' do
152 before :each do 152 before :each do
153 - MetricConfiguration.expects(:find).at_least_once.with(compound_metric_configuration.id).returns(compound_metric_configuration) 153 + subject.expects(:find_resource).with(MetricConfiguration, compound_metric_configuration.id).returns(compound_metric_configuration)
154 MetricConfiguration.expects(:metric_configurations_of).with(mezuro_configuration.id).returns([compound_metric_configuration]) 154 MetricConfiguration.expects(:metric_configurations_of).with(mezuro_configuration.id).returns([compound_metric_configuration])
155 MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false) 155 MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false)
156 156
@@ -159,7 +159,6 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do @@ -159,7 +159,6 @@ describe CompoundMetricConfigurationsController, :type =&gt; :controller do
159 159
160 it { should render_template(:edit) } 160 it { should render_template(:edit) }
161 end 161 end
162 -  
163 end 162 end
164 163
165 context 'when the user does not own the reading' do 164 context 'when the user does not own the reading' do
spec/controllers/concerns/metric_configurations_concern_spec.rb
@@ -6,7 +6,7 @@ describe MetricConfigurationsConcern, type: :controller do @@ -6,7 +6,7 @@ describe MetricConfigurationsConcern, type: :controller do
6 let! (:metric_configurations_controller) { MetricConfigurationsController.new } 6 let! (:metric_configurations_controller) { MetricConfigurationsController.new }
7 7
8 before :each do 8 before :each do
9 - MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) 9 + metric_configurations_controller.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration)
10 metric_configurations_controller.params = {id: metric_configuration.id} 10 metric_configurations_controller.params = {id: metric_configuration.id}
11 end 11 end
12 12
spec/controllers/concerns/resource_finder_spec.rb 0 → 100644
@@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
  1 +require 'rails_helper'
  2 +
  3 +describe ResourceFinder, type: :controller do
  4 + describe 'find_resource' do
  5 + let(:klass) { mock('Resource') }
  6 + let(:id) { 1 }
  7 + let!(:projects_controller) {ProjectsController.new}
  8 +
  9 + before do
  10 + projects_controller.extend(ResourceFinder)
  11 + end
  12 +
  13 + context 'when the resource exists' do
  14 + let!(:resource) { mock('resource') }
  15 +
  16 + before :each do
  17 + klass.expects(:find).with(id).returns(resource)
  18 + end
  19 +
  20 + it 'is expect to return the resource' do
  21 + expect(projects_controller.find_resource(klass, id)).to eq(resource)
  22 + end
  23 + end
  24 +
  25 + context 'when the resource does not exists' do
  26 + before :each do
  27 + klass.expects(:find).with(id).raises(KalibroGatekeeperClient::Errors::RecordNotFound)
  28 + end
  29 +
  30 + # FIXME: this is not the best test, but it it's the closest we can think of
  31 + # full coverage is achieved through projects_controller_spec.rb
  32 + it 'is expected to render the 404 page' do
  33 + projects_controller.expects(:respond_to)
  34 +
  35 + projects_controller.find_resource(klass, id)
  36 + end
  37 + end
  38 + end
  39 +end
0 \ No newline at end of file 40 \ No newline at end of file
spec/controllers/metric_configurations_controller_spec.rb
@@ -95,7 +95,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do @@ -95,7 +95,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do
95 95
96 before :each do 96 before :each do
97 ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group) 97 ReadingGroup.expects(:find).with(metric_configuration.reading_group_id).returns(reading_group)
98 - MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) 98 + subject.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration)
99 MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range]) 99 MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range])
100 100
101 get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id 101 get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id
@@ -114,8 +114,8 @@ describe MetricConfigurationsController, :type =&gt; :controller do @@ -114,8 +114,8 @@ describe MetricConfigurationsController, :type =&gt; :controller do
114 114
115 context 'when the user owns the metric configuration' do 115 context 'when the user owns the metric configuration' do
116 before :each do 116 before :each do
117 - subject.expects(:metric_configuration_owner?).returns true  
118 - MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) 117 + subject.expects(:metric_configuration_owner?).returns(true)
  118 + subject.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration)
119 get :edit, id: metric_configuration.id, mezuro_configuration_id: metric_configuration.configuration_id.to_s 119 get :edit, id: metric_configuration.id, mezuro_configuration_id: metric_configuration.configuration_id.to_s
120 end 120 end
121 121
@@ -158,7 +158,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do @@ -158,7 +158,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do
158 158
159 context 'with valid fields' do 159 context 'with valid fields' do
160 before :each do 160 before :each do
161 - MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) 161 + subject.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration)
162 MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true) 162 MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true)
163 163
164 post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params 164 post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params
@@ -170,7 +170,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do @@ -170,7 +170,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do
170 170
171 context 'with an invalid field' do 171 context 'with an invalid field' do
172 before :each do 172 before :each do
173 - MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) 173 + subject.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration)
174 MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false) 174 MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false)
175 175
176 post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params 176 post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: metric_configuration.id, metric_configuration: metric_configuration_params
@@ -203,7 +203,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do @@ -203,7 +203,7 @@ describe MetricConfigurationsController, :type =&gt; :controller do
203 before :each do 203 before :each do
204 subject.expects(:metric_configuration_owner?).returns true 204 subject.expects(:metric_configuration_owner?).returns true
205 metric_configuration.expects(:destroy) 205 metric_configuration.expects(:destroy)
206 - MetricConfiguration.expects(:find).at_least_once.with(metric_configuration.id).returns(metric_configuration) 206 + subject.expects(:find_resource).with(MetricConfiguration, metric_configuration.id).returns(metric_configuration)
207 207
208 delete :destroy, id: metric_configuration.id, mezuro_configuration_id: metric_configuration.configuration_id.to_s 208 delete :destroy, id: metric_configuration.id, mezuro_configuration_id: metric_configuration.configuration_id.to_s
209 end 209 end
spec/controllers/mezuro_configurations_controller_spec.rb
@@ -62,12 +62,14 @@ describe MezuroConfigurationsController, :type =&gt; :controller do @@ -62,12 +62,14 @@ describe MezuroConfigurationsController, :type =&gt; :controller do
62 end 62 end
63 63
64 describe 'show' do 64 describe 'show' do
65 - subject { FactoryGirl.build(:mezuro_configuration) } 65 + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) }
66 let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } 66 let(:metric_configuration) { FactoryGirl.build(:metric_configuration) }
  67 +
67 before :each do 68 before :each do
68 - MezuroConfiguration.expects(:find).with(subject.id.to_s).returns(subject)  
69 - subject.expects(:metric_configurations).returns(metric_configuration)  
70 - get :show, :id => subject.id 69 + mezuro_configuration.expects(:metric_configurations).returns(metric_configuration)
  70 + subject.expects(:find_resource).with(MezuroConfiguration, mezuro_configuration.id).returns(mezuro_configuration)
  71 +
  72 + get :show, :id => mezuro_configuration.id
71 end 73 end
72 74
73 it { is_expected.to render_template(:show) } 75 it { is_expected.to render_template(:show) }
@@ -96,7 +98,7 @@ describe MezuroConfigurationsController, :type =&gt; :controller do @@ -96,7 +98,7 @@ describe MezuroConfigurationsController, :type =&gt; :controller do
96 98
97 User.any_instance.expects(:mezuro_configuration_ownerships).at_least_once.returns(@ownerships) 99 User.any_instance.expects(:mezuro_configuration_ownerships).at_least_once.returns(@ownerships)
98 100
99 - MezuroConfiguration.expects(:find).with(@subject.id.to_s).returns(@subject) 101 + subject.expects(:find_resource).with(MezuroConfiguration, @subject.id).returns(@subject)
100 delete :destroy, :id => @subject.id 102 delete :destroy, :id => @subject.id
101 end 103 end
102 104
@@ -156,7 +158,7 @@ describe MezuroConfigurationsController, :type =&gt; :controller do @@ -156,7 +158,7 @@ describe MezuroConfigurationsController, :type =&gt; :controller do
156 158
157 context 'when the user owns the mezuro_configuration' do 159 context 'when the user owns the mezuro_configuration' do
158 before :each do 160 before :each do
159 - MezuroConfiguration.expects(:find).with(@subject.id.to_s).returns(@subject) 161 + subject.expects(:find_resource).with(MezuroConfiguration, @subject.id).returns(@subject)
160 @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(@ownership) 162 @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(@ownership)
161 163
162 get :edit, :id => @subject.id 164 get :edit, :id => @subject.id
@@ -213,7 +215,7 @@ describe MezuroConfigurationsController, :type =&gt; :controller do @@ -213,7 +215,7 @@ describe MezuroConfigurationsController, :type =&gt; :controller do
213 215
214 context 'with valid fields' do 216 context 'with valid fields' do
215 before :each do 217 before :each do
216 - MezuroConfiguration.expects(:find).with(@subject.id.to_s).returns(@subject) 218 + subject.expects(:find_resource).with(MezuroConfiguration, @subject.id).returns(@subject)
217 MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(true) 219 MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(true)
218 end 220 end
219 221
@@ -240,7 +242,7 @@ describe MezuroConfigurationsController, :type =&gt; :controller do @@ -240,7 +242,7 @@ describe MezuroConfigurationsController, :type =&gt; :controller do
240 242
241 context 'with an invalid field' do 243 context 'with an invalid field' do
242 before :each do 244 before :each do
243 - MezuroConfiguration.expects(:find).with(@subject.id.to_s).returns(@subject) 245 + subject.expects(:find_resource).with(MezuroConfiguration, @subject.id).returns(@subject)
244 MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(false) 246 MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(false)
245 247
246 post :update, :id => @subject.id, :mezuro_configuration => @subject_params 248 post :update, :id => @subject.id, :mezuro_configuration => @subject_params
spec/controllers/mezuro_ranges_controller_spec.rb
@@ -80,7 +80,7 @@ describe MezuroRangesController, :type =&gt; :controller do @@ -80,7 +80,7 @@ describe MezuroRangesController, :type =&gt; :controller do
80 before :each do 80 before :each do
81 subject.expects(:metric_configuration_owner?).returns true 81 subject.expects(:metric_configuration_owner?).returns true
82 mezuro_range.expects(:destroy) 82 mezuro_range.expects(:destroy)
83 - MezuroRange.expects(:find).at_least_once.with(mezuro_range.id).returns(mezuro_range) 83 + subject.expects(:find_resource).with(MezuroRange, mezuro_range.id).returns(mezuro_range)
84 84
85 delete :destroy, id: mezuro_range.id.to_s, metric_configuration_id: metric_configuration.id.to_s, mezuro_configuration_id: metric_configuration.configuration_id.to_s 85 delete :destroy, id: mezuro_range.id.to_s, metric_configuration_id: metric_configuration.id.to_s, mezuro_configuration_id: metric_configuration.configuration_id.to_s
86 end 86 end
@@ -121,7 +121,7 @@ describe MezuroRangesController, :type =&gt; :controller do @@ -121,7 +121,7 @@ describe MezuroRangesController, :type =&gt; :controller do
121 context 'when the user owns the mezuro range' do 121 context 'when the user owns the mezuro range' do
122 before :each do 122 before :each do
123 subject.expects(:metric_configuration_owner?).returns true 123 subject.expects(:metric_configuration_owner?).returns true
124 - MezuroRange.expects(:find).with(mezuro_range.id).returns(mezuro_range) 124 + subject.expects(:find_resource).with(MezuroRange, mezuro_range.id).returns(mezuro_range)
125 MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) 125 MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration)
126 Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([reading]) 126 Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([reading])
127 get :edit, id: mezuro_range.id, mezuro_configuration_id: metric_configuration.configuration_id, metric_configuration_id: metric_configuration.id 127 get :edit, id: mezuro_range.id, mezuro_configuration_id: metric_configuration.configuration_id, metric_configuration_id: metric_configuration.id
@@ -170,7 +170,7 @@ describe MezuroRangesController, :type =&gt; :controller do @@ -170,7 +170,7 @@ describe MezuroRangesController, :type =&gt; :controller do
170 170
171 context 'with valid fields' do 171 context 'with valid fields' do
172 before :each do 172 before :each do
173 - MezuroRange.expects(:find).with(mezuro_range.id).returns(mezuro_range) 173 + subject.expects(:find_resource).with(MezuroRange, mezuro_range.id).returns(mezuro_range)
174 MezuroRange.any_instance.expects(:update).with(mezuro_range_params).returns(true) 174 MezuroRange.any_instance.expects(:update).with(mezuro_range_params).returns(true)
175 175
176 post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: mezuro_range.id, metric_configuration_id: metric_configuration.id, mezuro_range: mezuro_range_params 176 post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: mezuro_range.id, metric_configuration_id: metric_configuration.id, mezuro_range: mezuro_range_params
@@ -182,7 +182,7 @@ describe MezuroRangesController, :type =&gt; :controller do @@ -182,7 +182,7 @@ describe MezuroRangesController, :type =&gt; :controller do
182 182
183 context 'with an invalid field' do 183 context 'with an invalid field' do
184 before :each do 184 before :each do
185 - MezuroRange.expects(:find).with(mezuro_range.id).returns(mezuro_range) 185 + subject.expects(:find_resource).with(MezuroRange, mezuro_range.id).returns(mezuro_range)
186 MezuroRange.any_instance.expects(:update).with(mezuro_range_params).returns(false) 186 MezuroRange.any_instance.expects(:update).with(mezuro_range_params).returns(false)
187 MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) 187 MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration)
188 Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([reading]) 188 Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([reading])
spec/controllers/modules_controller_spec.rb
@@ -3,7 +3,7 @@ require &#39;rails_helper&#39; @@ -3,7 +3,7 @@ require &#39;rails_helper&#39;
3 describe ModulesController, :type => :controller do 3 describe ModulesController, :type => :controller do
4 describe "load_module_tree" do 4 describe "load_module_tree" do
5 before :each do 5 before :each do
6 - ModuleResult.expects(:find).with(42).returns(FactoryGirl.build(:module_result)) 6 + subject.expects(:find_resource).with(ModuleResult, 42).returns(FactoryGirl.build(:module_result))
7 7
8 post :load_module_tree, id: 42, format: :js 8 post :load_module_tree, id: 42, format: :js
9 end 9 end
@@ -20,7 +20,7 @@ describe ModulesController, :type =&gt; :controller do @@ -20,7 +20,7 @@ describe ModulesController, :type =&gt; :controller do
20 let! (:module_result){ FactoryGirl.build(:module_result) } 20 let! (:module_result){ FactoryGirl.build(:module_result) }
21 21
22 before :each do 22 before :each do
23 - ModuleResult.expects(:find).at_least_once.with(module_result.id).returns(module_result) 23 + subject.expects(:find_resource).with(ModuleResult, module_result.id).returns(module_result)
24 subject.expire_fragment("#{module_result.id}_#{metric_name}") 24 subject.expire_fragment("#{module_result.id}_#{metric_name}")
25 25
26 xhr :get, :metric_history, {id: module_result.id, metric_name: metric_name, module_id: module_id} 26 xhr :get, :metric_history, {id: module_result.id, metric_name: metric_name, module_id: module_id}
spec/controllers/projects_controller_spec.rb
@@ -62,15 +62,27 @@ describe ProjectsController, :type =&gt; :controller do @@ -62,15 +62,27 @@ describe ProjectsController, :type =&gt; :controller do
62 end 62 end
63 63
64 describe 'show' do 64 describe 'show' do
65 - subject { FactoryGirl.build(:project) }  
66 - let(:repository) { FactoryGirl.build(:repository) }  
67 - before :each do  
68 - Project.expects(:find).with(subject.id.to_s).returns(subject)  
69 - subject.expects(:repositories).returns(repository)  
70 - get :show, :id => subject.id 65 + let(:project) { FactoryGirl.build(:project) }
  66 +
  67 + context 'when the project exists' do
  68 + let(:repository) { FactoryGirl.build(:repository) }
  69 + before :each do
  70 + subject.expects(:find_resource).with(Project, project.id).returns(project)
  71 + project.expects(:repositories).returns(repository)
  72 + get :show, :id => project.id
  73 + end
  74 +
  75 + it { is_expected.to render_template(:show) }
71 end 76 end
72 77
73 - it { is_expected.to render_template(:show) } 78 + context 'when the project does not exists' do
  79 + before :each do
  80 + Project.expects(:find).with(project.id).raises(KalibroGatekeeperClient::Errors::RecordNotFound)
  81 + get :show, :id => project.id
  82 + end
  83 +
  84 + it { is_expected.to respond_with(:not_found) }
  85 + end
74 end 86 end
75 87
76 describe 'destroy' do 88 describe 'destroy' do
@@ -97,7 +109,7 @@ describe ProjectsController, :type =&gt; :controller do @@ -97,7 +109,7 @@ describe ProjectsController, :type =&gt; :controller do
97 109
98 User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships) 110 User.any_instance.expects(:project_ownerships).at_least_once.returns(@ownerships)
99 111
100 - Project.expects(:find).with(@subject.id.to_s).returns(@subject) 112 + subject.expects(:find_resource).with(Project, @subject.id).returns(@subject)
101 delete :destroy, :id => @subject.id 113 delete :destroy, :id => @subject.id
102 end 114 end
103 115
@@ -157,7 +169,7 @@ describe ProjectsController, :type =&gt; :controller do @@ -157,7 +169,7 @@ describe ProjectsController, :type =&gt; :controller do
157 169
158 context 'when the user owns the project' do 170 context 'when the user owns the project' do
159 before :each do 171 before :each do
160 - Project.expects(:find).with(@subject.id.to_s).returns(@subject) 172 + subject.expects(:find_resource).with(Project, @subject.id).returns(@subject)
161 @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) 173 @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership)
162 ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image) 174 ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image)
163 175
@@ -216,7 +228,7 @@ describe ProjectsController, :type =&gt; :controller do @@ -216,7 +228,7 @@ describe ProjectsController, :type =&gt; :controller do
216 228
217 context 'with valid fields' do 229 context 'with valid fields' do
218 before :each do 230 before :each do
219 - Project.expects(:find).with(@subject.id.to_s).returns(@subject) 231 + subject.expects(:find_resource).with(Project, @subject.id).returns(@subject)
220 Project.any_instance.expects(:update).with(@subject_params).returns(true) 232 Project.any_instance.expects(:update).with(@subject_params).returns(true)
221 end 233 end
222 234
@@ -242,7 +254,7 @@ describe ProjectsController, :type =&gt; :controller do @@ -242,7 +254,7 @@ describe ProjectsController, :type =&gt; :controller do
242 254
243 context 'with an invalid field' do 255 context 'with an invalid field' do
244 before :each do 256 before :each do
245 - Project.expects(:find).with(@subject.id.to_s).returns(@subject) 257 + subject.expects(:find_resource).with(Project, @subject.id).returns(@subject)
246 Project.any_instance.expects(:update).with(@subject_params).returns(false) 258 Project.any_instance.expects(:update).with(@subject_params).returns(false)
247 259
248 post :update, :id => @subject.id, :project => @subject_params 260 post :update, :id => @subject.id, :project => @subject_params
spec/controllers/reading_groups_controller_spec.rb
@@ -61,11 +61,11 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -61,11 +61,11 @@ describe ReadingGroupsController, :type =&gt; :controller do
61 end 61 end
62 62
63 describe 'show' do 63 describe 'show' do
64 - subject { FactoryGirl.build(:reading_group) } 64 + let!(:reading_group) { FactoryGirl.build(:reading_group) }
65 let(:reading) { FactoryGirl.build(:reading) } 65 let(:reading) { FactoryGirl.build(:reading) }
66 before :each do 66 before :each do
67 - ReadingGroup.expects(:find).with(subject.id.to_s).returns(subject)  
68 - get :show, :id => subject.id 67 + subject.expects(:find_resource).with(ReadingGroup, reading_group.id).returns(reading_group)
  68 + get :show, :id => reading_group.id
69 end 69 end
70 70
71 it { is_expected.to render_template(:show) } 71 it { is_expected.to render_template(:show) }
@@ -95,7 +95,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -95,7 +95,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
95 95
96 User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships) 96 User.any_instance.expects(:reading_group_ownerships).at_least_once.returns(@ownerships)
97 97
98 - ReadingGroup.expects(:find).with(@subject.id.to_s).returns(@subject) 98 + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
99 delete :destroy, :id => @subject.id 99 delete :destroy, :id => @subject.id
100 end 100 end
101 101
@@ -155,7 +155,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -155,7 +155,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
155 155
156 context 'when the user owns the reading group' do 156 context 'when the user owns the reading group' do
157 before :each do 157 before :each do
158 - ReadingGroup.expects(:find).with(@subject.id.to_s).returns(@subject) 158 + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
159 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) 159 @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership)
160 160
161 get :edit, :id => @subject.id 161 get :edit, :id => @subject.id
@@ -212,7 +212,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -212,7 +212,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
212 212
213 context 'with valid fields' do 213 context 'with valid fields' do
214 before :each do 214 before :each do
215 - ReadingGroup.expects(:find).with(@subject.id.to_s).returns(@subject) 215 + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
216 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(true) 216 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(true)
217 end 217 end
218 218
@@ -239,7 +239,7 @@ describe ReadingGroupsController, :type =&gt; :controller do @@ -239,7 +239,7 @@ describe ReadingGroupsController, :type =&gt; :controller do
239 239
240 context 'with an invalid field' do 240 context 'with an invalid field' do
241 before :each do 241 before :each do
242 - ReadingGroup.expects(:find).with(@subject.id.to_s).returns(@subject) 242 + subject.expects(:find_resource).with(ReadingGroup, @subject.id).returns(@subject)
243 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false) 243 ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false)
244 244
245 post :update, :id => @subject.id, :reading_group => @subject_params 245 post :update, :id => @subject.id, :reading_group => @subject_params
spec/controllers/readings_controller_spec.rb
@@ -74,7 +74,7 @@ describe ReadingsController, :type =&gt; :controller do @@ -74,7 +74,7 @@ describe ReadingsController, :type =&gt; :controller do
74 context 'when the user owns the reading' do 74 context 'when the user owns the reading' do
75 before :each do 75 before :each do
76 subject.expects(:reading_owner?).returns true 76 subject.expects(:reading_owner?).returns true
77 - Reading.expects(:find).at_least_once.with(reading.id).returns(reading) 77 + subject.expects(:find_resource).with(Reading, reading.id).returns(reading)
78 get :edit, id: reading.id, reading_group_id: reading_group.id.to_s 78 get :edit, id: reading.id, reading_group_id: reading_group.id.to_s
79 end 79 end
80 80
@@ -117,7 +117,7 @@ describe ReadingsController, :type =&gt; :controller do @@ -117,7 +117,7 @@ describe ReadingsController, :type =&gt; :controller do
117 117
118 context 'with valid fields' do 118 context 'with valid fields' do
119 before :each do 119 before :each do
120 - Reading.expects(:find).at_least_once.with(reading.id).returns(reading) 120 + subject.expects(:find_resource).with(Reading, reading.id).returns(reading)
121 Reading.any_instance.expects(:update).with(reading_params).returns(true) 121 Reading.any_instance.expects(:update).with(reading_params).returns(true)
122 122
123 post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params 123 post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params
@@ -129,7 +129,7 @@ describe ReadingsController, :type =&gt; :controller do @@ -129,7 +129,7 @@ describe ReadingsController, :type =&gt; :controller do
129 129
130 context 'with an invalid field' do 130 context 'with an invalid field' do
131 before :each do 131 before :each do
132 - Reading.expects(:find).at_least_once.with(reading.id).returns(reading) 132 + subject.expects(:find_resource).with(Reading, reading.id).returns(reading)
133 Reading.any_instance.expects(:update).with(reading_params).returns(false) 133 Reading.any_instance.expects(:update).with(reading_params).returns(false)
134 134
135 post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params 135 post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params
@@ -169,7 +169,7 @@ describe ReadingsController, :type =&gt; :controller do @@ -169,7 +169,7 @@ describe ReadingsController, :type =&gt; :controller do
169 before :each do 169 before :each do
170 subject.expects(:reading_owner?).returns true 170 subject.expects(:reading_owner?).returns true
171 reading.expects(:destroy) 171 reading.expects(:destroy)
172 - Reading.expects(:find).at_least_once.with(reading.id).returns(reading) 172 + subject.expects(:find_resource).with(Reading, reading.id).returns(reading)
173 173
174 delete :destroy, id: reading.id, reading_group_id: reading.group_id.to_s 174 delete :destroy, id: reading.id, reading_group_id: reading.group_id.to_s
175 end 175 end
spec/controllers/repositories_controller_spec.rb
@@ -94,7 +94,7 @@ describe RepositoriesController, :type =&gt; :controller do @@ -94,7 +94,7 @@ describe RepositoriesController, :type =&gt; :controller do
94 processing = FactoryGirl.build(:processing) 94 processing = FactoryGirl.build(:processing)
95 95
96 MezuroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:mezuro_configuration)) 96 MezuroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:mezuro_configuration))
97 - Repository.expects(:find).with(repository.id).returns(repository) 97 + subject.expects(:find_resource).with(Repository, repository.id).returns(repository)
98 98
99 get :show, id: repository.id.to_s, project_id: project.id.to_s 99 get :show, id: repository.id.to_s, project_id: project.id.to_s
100 end 100 end
@@ -108,7 +108,7 @@ describe RepositoriesController, :type =&gt; :controller do @@ -108,7 +108,7 @@ describe RepositoriesController, :type =&gt; :controller do
108 processing = FactoryGirl.build(:processing) 108 processing = FactoryGirl.build(:processing)
109 109
110 MezuroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:mezuro_configuration)) 110 MezuroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:mezuro_configuration))
111 - Repository.expects(:find).with(repository.id).returns(repository) 111 + subject.expects(:find_resource).with(Repository, repository.id).returns(repository)
112 112
113 get :show, id: repository.id.to_s, project_id: project.id.to_s 113 get :show, id: repository.id.to_s, project_id: project.id.to_s
114 end 114 end