Commit f93e3885d4836b4705d9c15c6ec3a32e7ddfb9b1
1 parent
eac93023
Exists in
colab
and in
4 other branches
Trying to find a unexistent resource render the 404 page
Showing
22 changed files
with
138 additions
and
64 deletions
Show diff stats
app/controllers/base_metric_configurations_controller.rb
app/controllers/concerns/metric_configurations_concern.rb
| ... | ... | @@ -2,6 +2,6 @@ module MetricConfigurationsConcern |
| 2 | 2 | extend ActiveSupport::Concern |
| 3 | 3 | |
| 4 | 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 | 6 | end |
| 7 | 7 | end | ... | ... |
| ... | ... | @@ -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 | 16 | \ No newline at end of file | ... | ... |
app/controllers/mezuro_configurations_controller.rb
| 1 | 1 | include OwnershipAuthentication |
| 2 | +include ResourceFinder | |
| 2 | 3 | |
| 3 | 4 | class MezuroConfigurationsController < ApplicationController |
| 4 | 5 | before_action :authenticate_user!, except: [:index, :show] |
| ... | ... | @@ -65,7 +66,7 @@ class MezuroConfigurationsController < ApplicationController |
| 65 | 66 | private |
| 66 | 67 | # Use callbacks to share common setup or constraints between actions. |
| 67 | 68 | def set_mezuro_configuration |
| 68 | - @mezuro_configuration = MezuroConfiguration.find(params[:id]) | |
| 69 | + @mezuro_configuration = find_resource(MezuroConfiguration, params[:id].to_i) | |
| 69 | 70 | end |
| 70 | 71 | |
| 71 | 72 | # Never trust parameters from the scary internet, only allow the white list through. | ... | ... |
app/controllers/mezuro_ranges_controller.rb
| 1 | 1 | include OwnershipAuthentication |
| 2 | +include ResourceFinder | |
| 2 | 3 | |
| 3 | 4 | class MezuroRangesController < ApplicationController |
| 4 | 5 | before_action :authenticate_user!, except: [:show] |
| 5 | 6 | before_action :metric_configuration_owner?, only: [:new, :create, :destroy, :edit, :update] |
| 6 | 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 | 10 | def new |
| 10 | 11 | @mezuro_range = MezuroRange.new |
| ... | ... | @@ -20,7 +21,6 @@ class MezuroRangesController < ApplicationController |
| 20 | 21 | end |
| 21 | 22 | |
| 22 | 23 | def destroy |
| 23 | - @mezuro_range = MezuroRange.find(params[:id].to_i) | |
| 24 | 24 | @mezuro_range.destroy |
| 25 | 25 | respond_to do |format| |
| 26 | 26 | format.html { redirect_to mezuro_configuration_metric_configuration_path( |
| ... | ... | @@ -85,6 +85,6 @@ class MezuroRangesController < ApplicationController |
| 85 | 85 | end |
| 86 | 86 | |
| 87 | 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 | 89 | end |
| 90 | 90 | end | ... | ... |
app/controllers/modules_controller.rb
| 1 | +include ResourceFinder | |
| 2 | + | |
| 1 | 3 | class ModulesController < ApplicationController |
| 2 | 4 | # POST /modules/1/metric_history |
| 3 | 5 | def metric_history |
| 4 | - @module_result = ModuleResult.find(params[:id].to_i) | |
| 6 | + @module_result = find_resource(ModuleResult, params[:id].to_i) | |
| 5 | 7 | @container = params[:container] |
| 6 | 8 | @metric_name = params[:metric_name] |
| 7 | 9 | end |
| 8 | 10 | |
| 9 | 11 | # POST /modules/1/tree |
| 10 | 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 | 14 | end |
| 13 | 15 | end |
| 14 | 16 | \ No newline at end of file | ... | ... |
app/controllers/projects_controller.rb
| 1 | 1 | include OwnershipAuthentication |
| 2 | +include ResourceFinder | |
| 2 | 3 | |
| 3 | 4 | class ProjectsController < ApplicationController |
| 4 | 5 | before_action :authenticate_user!, |
| ... | ... | @@ -32,7 +33,7 @@ class ProjectsController < ApplicationController |
| 32 | 33 | # GET /project/1.json |
| 33 | 34 | def show |
| 34 | 35 | set_project |
| 35 | - @project_repositories = @project.repositories | |
| 36 | + @project_repositories = @project.repositories if @project.is_a?(Project) | |
| 36 | 37 | end |
| 37 | 38 | |
| 38 | 39 | # GET /projects/1/edit |
| ... | ... | @@ -66,8 +67,8 @@ class ProjectsController < ApplicationController |
| 66 | 67 | private |
| 67 | 68 | # Use callbacks to share common setup or constraints between actions. |
| 68 | 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 | 72 | end |
| 72 | 73 | |
| 73 | 74 | # Never trust parameters from the scary internet, only allow the white list through. | ... | ... |
app/controllers/reading_groups_controller.rb
| 1 | 1 | include OwnershipAuthentication |
| 2 | +include ResourceFinder | |
| 2 | 3 | |
| 3 | 4 | class ReadingGroupsController < ApplicationController |
| 4 | 5 | before_action :authenticate_user!, except: [:index, :show] |
| ... | ... | @@ -56,7 +57,7 @@ class ReadingGroupsController < ApplicationController |
| 56 | 57 | |
| 57 | 58 | # Use callbacks to share common setup or constraints between actions. |
| 58 | 59 | def set_reading_group |
| 59 | - @reading_group = ReadingGroup.find(params[:id]) | |
| 60 | + @reading_group = find_resource(ReadingGroup, params[:id].to_i) | |
| 60 | 61 | end |
| 61 | 62 | |
| 62 | 63 | # Never trust parameters from the scary internet, only allow the white list through. | ... | ... |
app/controllers/readings_controller.rb
| 1 | 1 | include OwnershipAuthentication |
| 2 | +include ResourceFinder | |
| 2 | 3 | |
| 3 | 4 | class ReadingsController < ApplicationController |
| 4 | 5 | before_action :authenticate_user!, except: [:index] |
| ... | ... | @@ -73,6 +74,6 @@ class ReadingsController < ApplicationController |
| 73 | 74 | end |
| 74 | 75 | |
| 75 | 76 | def set_reading |
| 76 | - @reading = Reading.find(params[:id].to_i) | |
| 77 | + @reading = find_resource(Reading, params[:id].to_i) | |
| 77 | 78 | end |
| 78 | 79 | end | ... | ... |
app/controllers/repositories_controller.rb
| ... | ... | @@ -105,7 +105,7 @@ private |
| 105 | 105 | |
| 106 | 106 | # Use callbacks to share common setup or constraints between actions. |
| 107 | 107 | def set_repository |
| 108 | - @repository = Repository.find(params[:id].to_i) | |
| 108 | + @repository = find_resource(Repository, params[:id].to_i) | |
| 109 | 109 | end |
| 110 | 110 | |
| 111 | 111 | def set_mezuro_configuration | ... | ... |
spec/controllers/base_metric_configurations_controller_spec.rb
| ... | ... | @@ -118,7 +118,7 @@ describe InheritsFromBaseMetricConfigurationsController, :type => :controller do |
| 118 | 118 | context 'with a valid metric_configuration' do |
| 119 | 119 | before :each do |
| 120 | 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 | 122 | MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range]) |
| 123 | 123 | |
| 124 | 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 => :controller do |
| 74 | 74 | |
| 75 | 75 | before :each do |
| 76 | 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 | 78 | MezuroRange.expects(:ranges_of).with(compound_metric_configuration.id).returns([mezuro_range]) |
| 79 | 79 | |
| 80 | 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 => :controller do |
| 94 | 94 | context 'when the user owns the compound metric configuration' do |
| 95 | 95 | before :each do |
| 96 | 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 | 98 | MetricConfiguration.expects(:metric_configurations_of).with(mezuro_configuration.id).returns([compound_metric_configuration]) |
| 99 | 99 | get :edit, id: compound_metric_configuration.id, mezuro_configuration_id: compound_metric_configuration.configuration_id.to_s |
| 100 | 100 | end |
| ... | ... | @@ -138,7 +138,7 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 138 | 138 | |
| 139 | 139 | context 'with valid fields' do |
| 140 | 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 | 142 | MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true) |
| 143 | 143 | |
| 144 | 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 => :controller do |
| 150 | 150 | |
| 151 | 151 | context 'with an invalid field' do |
| 152 | 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 | 154 | MetricConfiguration.expects(:metric_configurations_of).with(mezuro_configuration.id).returns([compound_metric_configuration]) |
| 155 | 155 | MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false) |
| 156 | 156 | |
| ... | ... | @@ -159,7 +159,6 @@ describe CompoundMetricConfigurationsController, :type => :controller do |
| 159 | 159 | |
| 160 | 160 | it { should render_template(:edit) } |
| 161 | 161 | end |
| 162 | - | |
| 163 | 162 | end |
| 164 | 163 | |
| 165 | 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 | 6 | let! (:metric_configurations_controller) { MetricConfigurationsController.new } |
| 7 | 7 | |
| 8 | 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 | 10 | metric_configurations_controller.params = {id: metric_configuration.id} |
| 11 | 11 | end |
| 12 | 12 | ... | ... |
| ... | ... | @@ -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 | 40 | \ No newline at end of file | ... | ... |
spec/controllers/metric_configurations_controller_spec.rb
| ... | ... | @@ -95,7 +95,7 @@ describe MetricConfigurationsController, :type => :controller do |
| 95 | 95 | |
| 96 | 96 | before :each do |
| 97 | 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 | 99 | MezuroRange.expects(:ranges_of).with(metric_configuration.id).returns([mezuro_range]) |
| 100 | 100 | |
| 101 | 101 | get :show, mezuro_configuration_id: metric_configuration.configuration_id.to_s, id: metric_configuration.id |
| ... | ... | @@ -114,8 +114,8 @@ describe MetricConfigurationsController, :type => :controller do |
| 114 | 114 | |
| 115 | 115 | context 'when the user owns the metric configuration' do |
| 116 | 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 | 119 | get :edit, id: metric_configuration.id, mezuro_configuration_id: metric_configuration.configuration_id.to_s |
| 120 | 120 | end |
| 121 | 121 | |
| ... | ... | @@ -158,7 +158,7 @@ describe MetricConfigurationsController, :type => :controller do |
| 158 | 158 | |
| 159 | 159 | context 'with valid fields' do |
| 160 | 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 | 162 | MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(true) |
| 163 | 163 | |
| 164 | 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 => :controller do |
| 170 | 170 | |
| 171 | 171 | context 'with an invalid field' do |
| 172 | 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 | 174 | MetricConfiguration.any_instance.expects(:update).with(metric_configuration_params).returns(false) |
| 175 | 175 | |
| 176 | 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 => :controller do |
| 203 | 203 | before :each do |
| 204 | 204 | subject.expects(:metric_configuration_owner?).returns true |
| 205 | 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 | 208 | delete :destroy, id: metric_configuration.id, mezuro_configuration_id: metric_configuration.configuration_id.to_s |
| 209 | 209 | end | ... | ... |
spec/controllers/mezuro_configurations_controller_spec.rb
| ... | ... | @@ -62,12 +62,14 @@ describe MezuroConfigurationsController, :type => :controller do |
| 62 | 62 | end |
| 63 | 63 | |
| 64 | 64 | describe 'show' do |
| 65 | - subject { FactoryGirl.build(:mezuro_configuration) } | |
| 65 | + let(:mezuro_configuration) { FactoryGirl.build(:mezuro_configuration) } | |
| 66 | 66 | let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } |
| 67 | + | |
| 67 | 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 | 73 | end |
| 72 | 74 | |
| 73 | 75 | it { is_expected.to render_template(:show) } |
| ... | ... | @@ -96,7 +98,7 @@ describe MezuroConfigurationsController, :type => :controller do |
| 96 | 98 | |
| 97 | 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 | 102 | delete :destroy, :id => @subject.id |
| 101 | 103 | end |
| 102 | 104 | |
| ... | ... | @@ -156,7 +158,7 @@ describe MezuroConfigurationsController, :type => :controller do |
| 156 | 158 | |
| 157 | 159 | context 'when the user owns the mezuro_configuration' do |
| 158 | 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 | 162 | @ownerships.expects(:find_by_mezuro_configuration_id).with("#{@subject.id}").returns(@ownership) |
| 161 | 163 | |
| 162 | 164 | get :edit, :id => @subject.id |
| ... | ... | @@ -213,7 +215,7 @@ describe MezuroConfigurationsController, :type => :controller do |
| 213 | 215 | |
| 214 | 216 | context 'with valid fields' do |
| 215 | 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 | 219 | MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(true) |
| 218 | 220 | end |
| 219 | 221 | |
| ... | ... | @@ -240,7 +242,7 @@ describe MezuroConfigurationsController, :type => :controller do |
| 240 | 242 | |
| 241 | 243 | context 'with an invalid field' do |
| 242 | 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 | 246 | MezuroConfiguration.any_instance.expects(:update).with(@subject_params).returns(false) |
| 245 | 247 | |
| 246 | 248 | post :update, :id => @subject.id, :mezuro_configuration => @subject_params | ... | ... |
spec/controllers/mezuro_ranges_controller_spec.rb
| ... | ... | @@ -80,7 +80,7 @@ describe MezuroRangesController, :type => :controller do |
| 80 | 80 | before :each do |
| 81 | 81 | subject.expects(:metric_configuration_owner?).returns true |
| 82 | 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 | 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 | 86 | end |
| ... | ... | @@ -121,7 +121,7 @@ describe MezuroRangesController, :type => :controller do |
| 121 | 121 | context 'when the user owns the mezuro range' do |
| 122 | 122 | before :each do |
| 123 | 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 | 125 | MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) |
| 126 | 126 | Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([reading]) |
| 127 | 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 => :controller do |
| 170 | 170 | |
| 171 | 171 | context 'with valid fields' do |
| 172 | 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 | 174 | MezuroRange.any_instance.expects(:update).with(mezuro_range_params).returns(true) |
| 175 | 175 | |
| 176 | 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 => :controller do |
| 182 | 182 | |
| 183 | 183 | context 'with an invalid field' do |
| 184 | 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 | 186 | MezuroRange.any_instance.expects(:update).with(mezuro_range_params).returns(false) |
| 187 | 187 | MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) |
| 188 | 188 | Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([reading]) | ... | ... |
spec/controllers/modules_controller_spec.rb
| ... | ... | @@ -3,7 +3,7 @@ require 'rails_helper' |
| 3 | 3 | describe ModulesController, :type => :controller do |
| 4 | 4 | describe "load_module_tree" do |
| 5 | 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 | 8 | post :load_module_tree, id: 42, format: :js |
| 9 | 9 | end |
| ... | ... | @@ -20,7 +20,7 @@ describe ModulesController, :type => :controller do |
| 20 | 20 | let! (:module_result){ FactoryGirl.build(:module_result) } |
| 21 | 21 | |
| 22 | 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 | 24 | subject.expire_fragment("#{module_result.id}_#{metric_name}") |
| 25 | 25 | |
| 26 | 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 => :controller do |
| 62 | 62 | end |
| 63 | 63 | |
| 64 | 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 | 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 | 86 | end |
| 75 | 87 | |
| 76 | 88 | describe 'destroy' do |
| ... | ... | @@ -97,7 +109,7 @@ describe ProjectsController, :type => :controller do |
| 97 | 109 | |
| 98 | 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 | 113 | delete :destroy, :id => @subject.id |
| 102 | 114 | end |
| 103 | 115 | |
| ... | ... | @@ -157,7 +169,7 @@ describe ProjectsController, :type => :controller do |
| 157 | 169 | |
| 158 | 170 | context 'when the user owns the project' do |
| 159 | 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 | 173 | @ownerships.expects(:find_by_project_id).with("#{@subject.id}").returns(@ownership) |
| 162 | 174 | ProjectImage.expects(:find_by_project_id).with(@subject.id).returns(@project_image) |
| 163 | 175 | |
| ... | ... | @@ -216,7 +228,7 @@ describe ProjectsController, :type => :controller do |
| 216 | 228 | |
| 217 | 229 | context 'with valid fields' do |
| 218 | 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 | 232 | Project.any_instance.expects(:update).with(@subject_params).returns(true) |
| 221 | 233 | end |
| 222 | 234 | |
| ... | ... | @@ -242,7 +254,7 @@ describe ProjectsController, :type => :controller do |
| 242 | 254 | |
| 243 | 255 | context 'with an invalid field' do |
| 244 | 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 | 258 | Project.any_instance.expects(:update).with(@subject_params).returns(false) |
| 247 | 259 | |
| 248 | 260 | post :update, :id => @subject.id, :project => @subject_params | ... | ... |
spec/controllers/reading_groups_controller_spec.rb
| ... | ... | @@ -61,11 +61,11 @@ describe ReadingGroupsController, :type => :controller do |
| 61 | 61 | end |
| 62 | 62 | |
| 63 | 63 | describe 'show' do |
| 64 | - subject { FactoryGirl.build(:reading_group) } | |
| 64 | + let!(:reading_group) { FactoryGirl.build(:reading_group) } | |
| 65 | 65 | let(:reading) { FactoryGirl.build(:reading) } |
| 66 | 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 | 69 | end |
| 70 | 70 | |
| 71 | 71 | it { is_expected.to render_template(:show) } |
| ... | ... | @@ -95,7 +95,7 @@ describe ReadingGroupsController, :type => :controller do |
| 95 | 95 | |
| 96 | 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 | 99 | delete :destroy, :id => @subject.id |
| 100 | 100 | end |
| 101 | 101 | |
| ... | ... | @@ -155,7 +155,7 @@ describe ReadingGroupsController, :type => :controller do |
| 155 | 155 | |
| 156 | 156 | context 'when the user owns the reading group' do |
| 157 | 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 | 159 | @ownerships.expects(:find_by_reading_group_id).with("#{@subject.id}").returns(@ownership) |
| 160 | 160 | |
| 161 | 161 | get :edit, :id => @subject.id |
| ... | ... | @@ -212,7 +212,7 @@ describe ReadingGroupsController, :type => :controller do |
| 212 | 212 | |
| 213 | 213 | context 'with valid fields' do |
| 214 | 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 | 216 | ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(true) |
| 217 | 217 | end |
| 218 | 218 | |
| ... | ... | @@ -239,7 +239,7 @@ describe ReadingGroupsController, :type => :controller do |
| 239 | 239 | |
| 240 | 240 | context 'with an invalid field' do |
| 241 | 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 | 243 | ReadingGroup.any_instance.expects(:update).with(@subject_params).returns(false) |
| 244 | 244 | |
| 245 | 245 | post :update, :id => @subject.id, :reading_group => @subject_params | ... | ... |
spec/controllers/readings_controller_spec.rb
| ... | ... | @@ -74,7 +74,7 @@ describe ReadingsController, :type => :controller do |
| 74 | 74 | context 'when the user owns the reading' do |
| 75 | 75 | before :each do |
| 76 | 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 | 78 | get :edit, id: reading.id, reading_group_id: reading_group.id.to_s |
| 79 | 79 | end |
| 80 | 80 | |
| ... | ... | @@ -117,7 +117,7 @@ describe ReadingsController, :type => :controller do |
| 117 | 117 | |
| 118 | 118 | context 'with valid fields' do |
| 119 | 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 | 121 | Reading.any_instance.expects(:update).with(reading_params).returns(true) |
| 122 | 122 | |
| 123 | 123 | post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params |
| ... | ... | @@ -129,7 +129,7 @@ describe ReadingsController, :type => :controller do |
| 129 | 129 | |
| 130 | 130 | context 'with an invalid field' do |
| 131 | 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 | 133 | Reading.any_instance.expects(:update).with(reading_params).returns(false) |
| 134 | 134 | |
| 135 | 135 | post :update, reading_group_id: reading_group.id, id: reading.id, reading: reading_params |
| ... | ... | @@ -169,7 +169,7 @@ describe ReadingsController, :type => :controller do |
| 169 | 169 | before :each do |
| 170 | 170 | subject.expects(:reading_owner?).returns true |
| 171 | 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 | 174 | delete :destroy, id: reading.id, reading_group_id: reading.group_id.to_s |
| 175 | 175 | end | ... | ... |
spec/controllers/repositories_controller_spec.rb
| ... | ... | @@ -94,7 +94,7 @@ describe RepositoriesController, :type => :controller do |
| 94 | 94 | processing = FactoryGirl.build(:processing) |
| 95 | 95 | |
| 96 | 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 | 99 | get :show, id: repository.id.to_s, project_id: project.id.to_s |
| 100 | 100 | end |
| ... | ... | @@ -108,7 +108,7 @@ describe RepositoriesController, :type => :controller do |
| 108 | 108 | processing = FactoryGirl.build(:processing) |
| 109 | 109 | |
| 110 | 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 | 113 | get :show, id: repository.id.to_s, project_id: project.id.to_s |
| 114 | 114 | end | ... | ... |