Commit 7879a0bfff5b09128739e00065fb2b40e7e8e749
Exists in
colab
and in
4 other branches
Merge pull request #235 from mezuro/project_owner_simplified
RepositoriesController checks for Project ownership only if necessary
Showing
2 changed files
with
95 additions
and
48 deletions
Show diff stats
app/controllers/repositories_controller.rb
@@ -2,7 +2,7 @@ include OwnershipAuthentication | @@ -2,7 +2,7 @@ include OwnershipAuthentication | ||
2 | 2 | ||
3 | class RepositoriesController < ApplicationController | 3 | class RepositoriesController < ApplicationController |
4 | before_action :authenticate_user!, except: [:show, :state, :state_with_date] | 4 | before_action :authenticate_user!, except: [:show, :state, :state_with_date] |
5 | - before_action :project_owner?, only: [:new, :create] | 5 | + before_action :project_owner?, only: [:new, :create], unless: Proc.new { params[:project_id].nil? } |
6 | before_action :repository_owner?, only: [:edit, :update, :destroy, :process_repository] | 6 | before_action :repository_owner?, only: [:edit, :update, :destroy, :process_repository] |
7 | before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :state_with_date, :process_repository] | 7 | before_action :set_repository, only: [:show, :edit, :update, :destroy, :state, :state_with_date, :process_repository] |
8 | before_action :set_project_id_repository_types_and_configurations, only: [:new, :edit] | 8 | before_action :set_project_id_repository_types_and_configurations, only: [:new, :edit] |
spec/controllers/repositories_controller_spec.rb
1 | require 'rails_helper' | 1 | require 'rails_helper' |
2 | 2 | ||
3 | describe RepositoriesController, :type => :controller do | 3 | describe RepositoriesController, :type => :controller do |
4 | - let(:project) { FactoryGirl.build(:project_with_id) } | ||
5 | - | ||
6 | describe 'new' do | 4 | describe 'new' do |
7 | context 'with a User logged in' do | 5 | context 'with a User logged in' do |
8 | let!(:user) { FactoryGirl.create(:user) } | 6 | let!(:user) { FactoryGirl.create(:user) } |
@@ -11,30 +9,48 @@ describe RepositoriesController, :type => :controller do | @@ -11,30 +9,48 @@ describe RepositoriesController, :type => :controller do | ||
11 | sign_in user | 9 | sign_in user |
12 | end | 10 | end |
13 | 11 | ||
14 | - context 'when the current user owns the project' do | ||
15 | - before :each do | ||
16 | - KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations) | ||
17 | - Repository.expects(:repository_types).returns([]) | ||
18 | - subject.expects(:project_owner?).returns true | 12 | + context 'when a project_id is provided' do |
13 | + let(:project) { FactoryGirl.build(:project_with_id) } | ||
14 | + | ||
15 | + context 'and the current user owns the project' do | ||
16 | + before :each do | ||
17 | + KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations) | ||
18 | + Repository.expects(:repository_types).returns([]) | ||
19 | + subject.expects(:project_owner?).returns true | ||
19 | 20 | ||
20 | - get :new, project_id: project.id.to_s | 21 | + get :new, project_id: project.id.to_json |
22 | + end | ||
23 | + | ||
24 | + it { is_expected.to respond_with(:success) } | ||
25 | + it { is_expected.to render_template(:new) } | ||
21 | end | 26 | end |
22 | 27 | ||
23 | - it { is_expected.to respond_with(:success) } | ||
24 | - it { is_expected.to render_template(:new) } | 28 | + context "and the current user doesn't own the project" do |
29 | + before :each do | ||
30 | + get :new, project_id: project.id.to_s | ||
31 | + end | ||
32 | + | ||
33 | + it { is_expected.to redirect_to(projects_url) } | ||
34 | + it { is_expected.to respond_with(:redirect) } | ||
35 | + end | ||
25 | end | 36 | end |
26 | 37 | ||
27 | - context "when the current user doesn't own the project" do | 38 | + context 'when no project_id is provided' do |
28 | before :each do | 39 | before :each do |
29 | - get :new, project_id: project.id.to_s | 40 | + KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations) |
41 | + Repository.expects(:repository_types).returns([]) | ||
42 | + | ||
43 | + get :new | ||
30 | end | 44 | end |
31 | 45 | ||
32 | - it { is_expected.to redirect_to(projects_url) } | ||
33 | - it { is_expected.to respond_with(:redirect) } | 46 | + it { is_expected.to respond_with(:success) } |
47 | + it { is_expected.to render_template(:new) } | ||
34 | end | 48 | end |
35 | end | 49 | end |
36 | 50 | ||
37 | context 'with no user logged in' do | 51 | context 'with no user logged in' do |
52 | + let(:project) { FactoryGirl.build(:project_with_id) } | ||
53 | + | ||
38 | before :each do | 54 | before :each do |
39 | get :new, project_id: project.id.to_s | 55 | get :new, project_id: project.id.to_s |
40 | end | 56 | end |
@@ -46,23 +62,63 @@ describe RepositoriesController, :type => :controller do | @@ -46,23 +62,63 @@ describe RepositoriesController, :type => :controller do | ||
46 | describe 'create' do | 62 | describe 'create' do |
47 | let!(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] } | 63 | let!(:kalibro_configurations) { [FactoryGirl.build(:kalibro_configuration)] } |
48 | let!(:user) { FactoryGirl.create(:user) } | 64 | let!(:user) { FactoryGirl.create(:user) } |
49 | - let (:repository) { FactoryGirl.build(:repository, project_id: project.id) } | ||
50 | let(:repository_params) { FactoryGirl.build(:repository).to_hash } | 65 | let(:repository_params) { FactoryGirl.build(:repository).to_hash } |
51 | 66 | ||
52 | before do | 67 | before do |
53 | sign_in user | 68 | sign_in user |
54 | end | 69 | end |
55 | 70 | ||
56 | - context 'when the current user owns the project' do | ||
57 | - before :each do | ||
58 | - subject.expects(:project_owner?).returns true | 71 | + context 'when a project_id is provided' do |
72 | + let(:project) { FactoryGirl.build(:project_with_id) } | ||
73 | + let(:repository) { FactoryGirl.build(:repository, project_id: project.id) } | ||
74 | + | ||
75 | + context 'and the current user owns the project' do | ||
76 | + before :each do | ||
77 | + subject.expects(:project_owner?).returns true | ||
78 | + end | ||
79 | + | ||
80 | + context 'with valid fields' do | ||
81 | + before :each do | ||
82 | + Repository.any_instance.expects(:save).returns(true) | ||
83 | + | ||
84 | + post :create, project_id: project.id, repository: repository_params | ||
85 | + end | ||
86 | + | ||
87 | + it { is_expected.to redirect_to(repository_process_path(id: repository.id)) } | ||
88 | + it { is_expected.to respond_with(:redirect) } | ||
89 | + end | ||
90 | + | ||
91 | + context 'with an invalid field' do | ||
92 | + before :each do | ||
93 | + KalibroConfiguration.expects(:public_or_owned_by_user).with(user).returns(kalibro_configurations) | ||
94 | + Repository.any_instance.expects(:save).returns(false) | ||
95 | + Repository.expects(:repository_types).returns([]) | ||
96 | + | ||
97 | + post :create, project_id: project.id.to_s, repository: repository_params | ||
98 | + end | ||
99 | + | ||
100 | + it { is_expected.to render_template(:new) } | ||
101 | + end | ||
102 | + end | ||
103 | + | ||
104 | + context "and the current user doesn't own the project " do | ||
105 | + before :each do | ||
106 | + post :create, project_id: project.id, repository: repository_params | ||
107 | + end | ||
108 | + | ||
109 | + it { is_expected.to redirect_to(projects_url) } | ||
110 | + it { is_expected.to respond_with(:redirect) } | ||
59 | end | 111 | end |
112 | + end | ||
113 | + | ||
114 | + context 'when no project_id is provided' do | ||
115 | + let(:repository) { FactoryGirl.build(:repository) } | ||
60 | 116 | ||
61 | context 'with valid fields' do | 117 | context 'with valid fields' do |
62 | before :each do | 118 | before :each do |
63 | Repository.any_instance.expects(:save).returns(true) | 119 | Repository.any_instance.expects(:save).returns(true) |
64 | 120 | ||
65 | - post :create, project_id: project.id, repository: repository_params | 121 | + post :create, repository: repository_params |
66 | end | 122 | end |
67 | 123 | ||
68 | it { is_expected.to redirect_to(repository_process_path(id: repository.id)) } | 124 | it { is_expected.to redirect_to(repository_process_path(id: repository.id)) } |
@@ -75,21 +131,12 @@ describe RepositoriesController, :type => :controller do | @@ -75,21 +131,12 @@ describe RepositoriesController, :type => :controller do | ||
75 | Repository.any_instance.expects(:save).returns(false) | 131 | Repository.any_instance.expects(:save).returns(false) |
76 | Repository.expects(:repository_types).returns([]) | 132 | Repository.expects(:repository_types).returns([]) |
77 | 133 | ||
78 | - post :create, project_id: project.id.to_s, repository: repository_params | 134 | + post :create.to_s, repository: repository_params |
79 | end | 135 | end |
80 | 136 | ||
81 | it { is_expected.to render_template(:new) } | 137 | it { is_expected.to render_template(:new) } |
82 | end | 138 | end |
83 | end | 139 | end |
84 | - | ||
85 | - context "when the current user doesn't own the project " do | ||
86 | - before :each do | ||
87 | - post :create, project_id: project.id, repository: repository_params | ||
88 | - end | ||
89 | - | ||
90 | - it { is_expected.to redirect_to(projects_url) } | ||
91 | - it { is_expected.to respond_with(:redirect) } | ||
92 | - end | ||
93 | end | 140 | end |
94 | 141 | ||
95 | describe 'show' do | 142 | describe 'show' do |
@@ -100,7 +147,7 @@ describe RepositoriesController, :type => :controller do | @@ -100,7 +147,7 @@ describe RepositoriesController, :type => :controller do | ||
100 | KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id)) | 147 | KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id)) |
101 | Repository.expects(:find).with(repository.id).returns(repository) | 148 | Repository.expects(:find).with(repository.id).returns(repository) |
102 | 149 | ||
103 | - get :show, id: repository.id.to_s, project_id: project.id.to_s | 150 | + get :show, id: repository.id.to_s |
104 | end | 151 | end |
105 | 152 | ||
106 | it { is_expected.to render_template(:show) } | 153 | it { is_expected.to render_template(:show) } |
@@ -112,7 +159,7 @@ describe RepositoriesController, :type => :controller do | @@ -112,7 +159,7 @@ describe RepositoriesController, :type => :controller do | ||
112 | KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id)) | 159 | KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id)) |
113 | Repository.expects(:find).with(repository.id).returns(repository) | 160 | Repository.expects(:find).with(repository.id).returns(repository) |
114 | 161 | ||
115 | - get :show, id: repository.id.to_s, project_id: project.id.to_s | 162 | + get :show, id: repository.id.to_s |
116 | end | 163 | end |
117 | 164 | ||
118 | it { is_expected.to render_template(:show) } | 165 | it { is_expected.to render_template(:show) } |
@@ -133,7 +180,7 @@ describe RepositoriesController, :type => :controller do | @@ -133,7 +180,7 @@ describe RepositoriesController, :type => :controller do | ||
133 | repository.expects(:destroy) | 180 | repository.expects(:destroy) |
134 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 181 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
135 | 182 | ||
136 | - delete :destroy, id: repository.id, project_id: project.id.to_s | 183 | + delete :destroy, id: repository.id |
137 | end | 184 | end |
138 | 185 | ||
139 | it { is_expected.to redirect_to(projects_path) } | 186 | it { is_expected.to redirect_to(projects_path) } |
@@ -142,7 +189,7 @@ describe RepositoriesController, :type => :controller do | @@ -142,7 +189,7 @@ describe RepositoriesController, :type => :controller do | ||
142 | 189 | ||
143 | context "when the user doesn't own the project" do | 190 | context "when the user doesn't own the project" do |
144 | before :each do | 191 | before :each do |
145 | - delete :destroy, id: repository.id, project_id: project.id.to_s | 192 | + delete :destroy, id: repository.id |
146 | end | 193 | end |
147 | 194 | ||
148 | it { is_expected.to redirect_to(projects_url) } | 195 | it { is_expected.to redirect_to(projects_url) } |
@@ -152,7 +199,7 @@ describe RepositoriesController, :type => :controller do | @@ -152,7 +199,7 @@ describe RepositoriesController, :type => :controller do | ||
152 | 199 | ||
153 | context 'with no User logged in' do | 200 | context 'with no User logged in' do |
154 | before :each do | 201 | before :each do |
155 | - delete :destroy, id: repository.id, project_id: project.id.to_s | 202 | + delete :destroy, id: repository.id |
156 | end | 203 | end |
157 | 204 | ||
158 | it { is_expected.to redirect_to new_user_session_path } | 205 | it { is_expected.to redirect_to new_user_session_path } |
@@ -175,7 +222,7 @@ describe RepositoriesController, :type => :controller do | @@ -175,7 +222,7 @@ describe RepositoriesController, :type => :controller do | ||
175 | subject.expects(:repository_owner?).returns true | 222 | subject.expects(:repository_owner?).returns true |
176 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 223 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
177 | Repository.expects(:repository_types).returns(["SUBVERSION"]) | 224 | Repository.expects(:repository_types).returns(["SUBVERSION"]) |
178 | - get :edit, id: repository.id, project_id: project.id.to_s | 225 | + get :edit, id: repository.id |
179 | end | 226 | end |
180 | 227 | ||
181 | it { is_expected.to render_template(:edit) } | 228 | it { is_expected.to render_template(:edit) } |
@@ -183,7 +230,7 @@ describe RepositoriesController, :type => :controller do | @@ -183,7 +230,7 @@ describe RepositoriesController, :type => :controller do | ||
183 | 230 | ||
184 | context 'when the user does not own the repository' do | 231 | context 'when the user does not own the repository' do |
185 | before do | 232 | before do |
186 | - get :edit, id: repository.id, project_id: project.id.to_s | 233 | + get :edit, id: repository.id |
187 | end | 234 | end |
188 | 235 | ||
189 | it { is_expected.to redirect_to(projects_url) } | 236 | it { is_expected.to redirect_to(projects_url) } |
@@ -194,7 +241,7 @@ describe RepositoriesController, :type => :controller do | @@ -194,7 +241,7 @@ describe RepositoriesController, :type => :controller do | ||
194 | 241 | ||
195 | context 'with no user logged in' do | 242 | context 'with no user logged in' do |
196 | before :each do | 243 | before :each do |
197 | - get :edit, id: repository.id, project_id: project.id.to_s | 244 | + get :edit, id: repository.id |
198 | end | 245 | end |
199 | 246 | ||
200 | it { is_expected.to redirect_to new_user_session_path } | 247 | it { is_expected.to redirect_to new_user_session_path } |
@@ -223,7 +270,7 @@ describe RepositoriesController, :type => :controller do | @@ -223,7 +270,7 @@ describe RepositoriesController, :type => :controller do | ||
223 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 270 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
224 | Repository.any_instance.expects(:update).with(repository_params).returns(true) | 271 | Repository.any_instance.expects(:update).with(repository_params).returns(true) |
225 | 272 | ||
226 | - post :update, project_id: project.id.to_s, id: repository.id, repository: repository_params | 273 | + post :update, id: repository.id, repository: repository_params |
227 | end | 274 | end |
228 | 275 | ||
229 | it { is_expected.to redirect_to(repository_path(repository.id)) } | 276 | it { is_expected.to redirect_to(repository_path(repository.id)) } |
@@ -237,7 +284,7 @@ describe RepositoriesController, :type => :controller do | @@ -237,7 +284,7 @@ describe RepositoriesController, :type => :controller do | ||
237 | Repository.any_instance.expects(:update).with(repository_params).returns(false) | 284 | Repository.any_instance.expects(:update).with(repository_params).returns(false) |
238 | Repository.expects(:repository_types).returns([]) | 285 | Repository.expects(:repository_types).returns([]) |
239 | 286 | ||
240 | - post :update, project_id: project.id.to_s, id: repository.id, repository: repository_params | 287 | + post :update, id: repository.id, repository: repository_params |
241 | end | 288 | end |
242 | 289 | ||
243 | it { is_expected.to render_template(:edit) } | 290 | it { is_expected.to render_template(:edit) } |
@@ -246,7 +293,7 @@ describe RepositoriesController, :type => :controller do | @@ -246,7 +293,7 @@ describe RepositoriesController, :type => :controller do | ||
246 | 293 | ||
247 | context 'when the user does not own the repository' do | 294 | context 'when the user does not own the repository' do |
248 | before :each do | 295 | before :each do |
249 | - post :update, project_id: project.id.to_s, id: repository.id, repository: repository_params | 296 | + post :update, id: repository.id, repository: repository_params |
250 | end | 297 | end |
251 | 298 | ||
252 | it { is_expected.to redirect_to projects_path } | 299 | it { is_expected.to redirect_to projects_path } |
@@ -255,7 +302,7 @@ describe RepositoriesController, :type => :controller do | @@ -255,7 +302,7 @@ describe RepositoriesController, :type => :controller do | ||
255 | 302 | ||
256 | context 'with no user logged in' do | 303 | context 'with no user logged in' do |
257 | before :each do | 304 | before :each do |
258 | - post :update, project_id: project.id.to_s, id: repository.id, repository: repository_params | 305 | + post :update, id: repository.id, repository: repository_params |
259 | end | 306 | end |
260 | 307 | ||
261 | it { is_expected.to redirect_to new_user_session_path } | 308 | it { is_expected.to redirect_to new_user_session_path } |
@@ -272,7 +319,7 @@ describe RepositoriesController, :type => :controller do | @@ -272,7 +319,7 @@ describe RepositoriesController, :type => :controller do | ||
272 | repository.expects(:last_processing).returns(ready_processing) | 319 | repository.expects(:last_processing).returns(ready_processing) |
273 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 320 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
274 | 321 | ||
275 | - xhr :get, :state, {project_id: project.id.to_s, id: repository.id, last_state: 'ANALYZING'} | 322 | + xhr :get, :state, {id: repository.id, last_state: 'ANALYZING'} |
276 | end | 323 | end |
277 | 324 | ||
278 | it { is_expected.to respond_with(:success) } | 325 | it { is_expected.to respond_with(:success) } |
@@ -286,7 +333,7 @@ describe RepositoriesController, :type => :controller do | @@ -286,7 +333,7 @@ describe RepositoriesController, :type => :controller do | ||
286 | repository.expects(:last_processing).returns(processing) | 333 | repository.expects(:last_processing).returns(processing) |
287 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 334 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
288 | 335 | ||
289 | - xhr :get, :state, {project_id: project.id.to_s, id: repository.id, last_state: 'LOADING'} | 336 | + xhr :get, :state, {id: repository.id, last_state: 'LOADING'} |
290 | end | 337 | end |
291 | 338 | ||
292 | it { is_expected.to respond_with(:success) } | 339 | it { is_expected.to respond_with(:success) } |
@@ -297,7 +344,7 @@ describe RepositoriesController, :type => :controller do | @@ -297,7 +344,7 @@ describe RepositoriesController, :type => :controller do | ||
297 | before :each do | 344 | before :each do |
298 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 345 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
299 | 346 | ||
300 | - xhr :get, :state, {project_id: project.id.to_s, id: repository.id, last_state: 'READY'} | 347 | + xhr :get, :state, {id: repository.id, last_state: 'READY'} |
301 | end | 348 | end |
302 | 349 | ||
303 | it { is_expected.to respond_with(:ok) } | 350 | it { is_expected.to respond_with(:ok) } |
@@ -312,7 +359,7 @@ describe RepositoriesController, :type => :controller do | @@ -312,7 +359,7 @@ describe RepositoriesController, :type => :controller do | ||
312 | repository.expects(:last_processing).returns(errored_processing) | 359 | repository.expects(:last_processing).returns(errored_processing) |
313 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 360 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
314 | 361 | ||
315 | - xhr :get, :state, {project_id: project.id.to_s, id: repository.id, last_state: 'ANALYZING'} | 362 | + xhr :get, :state, {id: repository.id, last_state: 'ANALYZING'} |
316 | end | 363 | end |
317 | 364 | ||
318 | it { is_expected.to respond_with(:success) } | 365 | it { is_expected.to respond_with(:success) } |
@@ -328,7 +375,7 @@ describe RepositoriesController, :type => :controller do | @@ -328,7 +375,7 @@ describe RepositoriesController, :type => :controller do | ||
328 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 375 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
329 | Processing.expects(:processing_with_date_of).with(repository.id, "2013-11-11").returns(processing) | 376 | Processing.expects(:processing_with_date_of).with(repository.id, "2013-11-11").returns(processing) |
330 | 377 | ||
331 | - xhr :get, :state_with_date, {project_id: project.id.to_s, id: repository.id, day: '11', month: '11', year: '2013'} | 378 | + xhr :get, :state_with_date, {id: repository.id, day: '11', month: '11', year: '2013'} |
332 | end | 379 | end |
333 | 380 | ||
334 | it { is_expected.to respond_with(:ok) } | 381 | it { is_expected.to respond_with(:ok) } |
@@ -343,7 +390,7 @@ describe RepositoriesController, :type => :controller do | @@ -343,7 +390,7 @@ describe RepositoriesController, :type => :controller do | ||
343 | repository.expects(:process) | 390 | repository.expects(:process) |
344 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) | 391 | Repository.expects(:find).at_least_once.with(repository.id).returns(repository) |
345 | KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id)) | 392 | KalibroConfiguration.expects(:find).with(repository.id).returns(FactoryGirl.build(:kalibro_configuration, :with_id)) |
346 | - get :process_repository, project_id: project.id.to_s, id: repository.id | 393 | + get :process_repository, id: repository.id |
347 | end | 394 | end |
348 | it { is_expected.to redirect_to(repository_path(repository.id)) } | 395 | it { is_expected.to redirect_to(repository_path(repository.id)) } |
349 | end | 396 | end |