Commit aedbc3e95771dd6c8b51cf8cbebfebfc71d4d095
Committed by
Diego Camarinha
1 parent
3651e1e6
Exists in
colab
and in
4 other branches
Unit tests for Mezuro Range controller.
signed-off-by: Diego Araújo <diegoamc90@gmail.com>
Showing
2 changed files
with
97 additions
and
2 deletions
Show diff stats
app/controllers/mezuro_ranges_controller.rb
... | ... | @@ -2,7 +2,7 @@ include OwnershipAuthentication |
2 | 2 | |
3 | 3 | class MezuroRangesController < ApplicationController |
4 | 4 | before_action :authenticate_user!, except: [:show] |
5 | - before_action :metric_configuration_owner?, only: [:new, :create, :destroy, :update] | |
5 | + before_action :metric_configuration_owner?, only: [:new, :create, :destroy, :edit, :update] | |
6 | 6 | before_action :get_url_params, only: [:update, :create, :destroy] |
7 | 7 | before_action :set_mezuro_range, only: [:edit, :update] |
8 | 8 | |
... | ... | @@ -30,7 +30,6 @@ class MezuroRangesController < ApplicationController |
30 | 30 | end |
31 | 31 | |
32 | 32 | def edit |
33 | - @mezuro_range_id = params[:mezuro_range_id] | |
34 | 33 | before_form |
35 | 34 | end |
36 | 35 | ... | ... |
spec/controllers/mezuro_ranges_spec.rb
... | ... | @@ -107,4 +107,100 @@ describe MezuroRangesController do |
107 | 107 | it { should redirect_to new_user_session_path } |
108 | 108 | end |
109 | 109 | end |
110 | + | |
111 | + describe 'edit' do | |
112 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
113 | + let(:mezuro_range) { FactoryGirl.build(:mezuro_range, id: 1, metric_configuration_id: metric_configuration.id) } | |
114 | + let(:reading) { FactoryGirl.build(:reading, group_id: metric_configuration.reading_group_id) } | |
115 | + | |
116 | + context 'with an User logged in' do | |
117 | + before do | |
118 | + sign_in FactoryGirl.create(:user) | |
119 | + end | |
120 | + | |
121 | + context 'when the user owns the mezuro range' do | |
122 | + before :each do | |
123 | + subject.expects(:metric_configuration_owner?).returns true | |
124 | + MezuroRange.expects(:find).with(mezuro_range.id).returns(mezuro_range) | |
125 | + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
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 | |
128 | + end | |
129 | + | |
130 | + it { should render_template(:edit) } | |
131 | + end | |
132 | + | |
133 | + context 'when the user does not own the mezuro range' do | |
134 | + let!(:reading_group) { FactoryGirl.build(:reading_group, id: metric_configuration.reading_group_id) } | |
135 | + | |
136 | + before do | |
137 | + get :edit, id: mezuro_range.id, mezuro_configuration_id: metric_configuration.configuration_id, metric_configuration_id: metric_configuration.id | |
138 | + end | |
139 | + | |
140 | + it { should redirect_to(mezuro_configurations_url) } | |
141 | + it { should respond_with(:redirect) } | |
142 | + it { should set_the_flash[:notice].to("You're not allowed to do this operation") } | |
143 | + end | |
144 | + end | |
145 | + | |
146 | + context 'with no user logged in' do | |
147 | + before :each do | |
148 | + get :edit, id: mezuro_range.id, mezuro_configuration_id: metric_configuration.configuration_id, metric_configuration_id: metric_configuration.id | |
149 | + end | |
150 | + | |
151 | + it { should redirect_to new_user_session_path } | |
152 | + end | |
153 | + end | |
154 | + | |
155 | + describe 'update' do | |
156 | + let(:metric_configuration) { FactoryGirl.build(:metric_configuration) } | |
157 | + let(:mezuro_range) { FactoryGirl.build(:mezuro_range, id: 1, metric_configuration_id: metric_configuration.id) } | |
158 | + let(:mezuro_range_params) { Hash[FactoryGirl.attributes_for(:mezuro_range).map { |k,v| [k.to_s, v.to_s] }] } #FIXME: Mocha is creating the expectations with strings, but FactoryGirl returns everything with sybols and integers | |
159 | + let(:reading) { FactoryGirl.build(:reading, group_id: metric_configuration.reading_group_id) } | |
160 | + | |
161 | + context 'when the user is logged in' do | |
162 | + before do | |
163 | + sign_in FactoryGirl.create(:user) | |
164 | + end | |
165 | + | |
166 | + context 'when user owns the mezuro range' do | |
167 | + before :each do | |
168 | + subject.expects(:metric_configuration_owner?).returns true | |
169 | + end | |
170 | + | |
171 | + context 'with valid fields' do | |
172 | + before :each do | |
173 | + MezuroRange.expects(:find).with(mezuro_range.id).returns(mezuro_range) | |
174 | + MezuroRange.any_instance.expects(:update).with(mezuro_range_params).returns(true) | |
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 | |
177 | + end | |
178 | + | |
179 | + it { should redirect_to(mezuro_configuration_metric_configuration_path(metric_configuration.configuration_id, metric_configuration.id)) } | |
180 | + it { should respond_with(:redirect) } | |
181 | + end | |
182 | + | |
183 | + context 'with an invalid field' do | |
184 | + before :each do | |
185 | + MezuroRange.expects(:find).with(mezuro_range.id).returns(mezuro_range) | |
186 | + MezuroRange.any_instance.expects(:update).with(mezuro_range_params).returns(false) | |
187 | + MetricConfiguration.expects(:find).with(metric_configuration.id).returns(metric_configuration) | |
188 | + Reading.expects(:readings_of).with(metric_configuration.reading_group_id).returns([reading]) | |
189 | + | |
190 | + post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: mezuro_range.id, metric_configuration_id: metric_configuration.id, mezuro_range: mezuro_range_params | |
191 | + end | |
192 | + | |
193 | + it { should render_template(:edit) } | |
194 | + end | |
195 | + end | |
196 | + | |
197 | + context 'when the user does not own the mezuro range' do | |
198 | + before :each do | |
199 | + post :update, mezuro_configuration_id: metric_configuration.configuration_id, id: mezuro_range.id, metric_configuration_id: metric_configuration.id, mezuro_range: mezuro_range_params | |
200 | + end | |
201 | + | |
202 | + it { should redirect_to mezuro_configurations_path } | |
203 | + end | |
204 | + end | |
205 | + end | |
110 | 206 | end | ... | ... |