From f4e23bca144710fe406e65b0ea1c0396534d4186 Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Tue, 24 Apr 2012 19:02:51 +0400 Subject: [PATCH] [issue #347] Fixed CollaboratorsController spec. --- .../collaborators_controller_spec.rb | 143 ++++++++++++------ 1 file changed, 94 insertions(+), 49 deletions(-) diff --git a/spec/controllers/collaborators_controller_spec.rb b/spec/controllers/collaborators_controller_spec.rb index 2ce30fbf6..02994d082 100644 --- a/spec/controllers/collaborators_controller_spec.rb +++ b/spec/controllers/collaborators_controller_spec.rb @@ -4,16 +4,26 @@ require 'spec_helper' shared_examples_for 'project admin user' do it 'should be able to view collaborators list' do get :index, :project_id => @project.id - response.should redirect_to(edit_project_collaborators_path(@project)) + response.should be_succes end it 'should be able to perform update action' do - post :update, {:project_id => @project.id}.merge(@update_params) - response.should redirect_to(edit_project_collaborators_path(@project)) + put :update, {:project_id => @project.id, :id => @collaborator.id}.merge(@update_params) + response.should be_success + end + + it 'should add new collaborator with reader role' do + post :create, @create_params.merge(:collaborator => @user_params) + @project.relations.exists?(:object_type => 'User', :object_id => @another_user.id, :role => 'reader').should be_true + end + + it 'should add new group with reader role' do + post :create, @create_params.merge(:collaborator => @group_params) + @project.relations.exists?(:object_type => 'Group', :object_id => @group.id, :role => 'reader').should be_true end it 'should be able to set reader role for any user' do - post :update, {:project_id => @project.id}.merge(@update_params) + put :update, {:project_id => @project.id, :id => @collaborator.id}.merge(@update_params) @another_user.relations.exists? :target_id => @project.id, :target_type => 'Project', :role => 'read' end end @@ -21,45 +31,63 @@ end shared_examples_for 'user with no rights for this project' do it 'should not be able to view collaborators list' do get :index, :project_id => @project.id - response.should redirect_to(edit_project_collaborators_path(@project)) + response.should redirect_to(forbidden_path) end it 'should not be able to perform update action' do - post :update, {:project_id => @project.id}.merge(@update_params) - response.should redirect_to(edit_project_collaborators_path(@project)) + put :update, {:project_id => @project.id, :id => @collaborator.id}.merge(@update_params) + response.should redirect_to(forbidden_path) end it 'should not be able to set reader role for any user' do - post :update, {:project_id => @project.id}.merge(@update_params) + put :update, {:project_id => @project.id, :id => @collaborator.id}.merge(@update_params) !@another_user.relations.exists? :target_id => @project.id, :target_type => 'Project', :role => 'read' end end describe CollaboratorsController do - before(:each) do + before(:each) do stub_rsync_methods @project = FactoryGirl.create(:project) @another_user = FactoryGirl.create(:user) - @update_params = {:user => {@another_user.id => :reader}} - end + @member_user = FactoryGirl.create(:user) + @update_params = {:collaborator => {:role => :reader}, :format => :json} + # Create relation with 'writer' rights + @collaborator = Collaborator.create(:actor => @member_user, :project => @project, :role => 'writer') + end - context 'for guest' do + context 'for guest' do it 'should not be able to perform index action' do get :index, :project_id => @project.id response.should redirect_to(new_user_session_path) end it 'should not be able to perform update action' do - post :update, {:project_id => @project.id}.merge(@update_params) - response.should redirect_to(new_user_session_path) + put :update, {:project_id => @project.id, :id => @collaborator.id}.merge(@update_params) + response.code.should == '401' end end context 'for global admin' do - before(:each) do - @admin = FactoryGirl.create(:admin) - set_session_for(@admin) - end + before(:each) do + @admin = FactoryGirl.create(:admin) + set_session_for(@admin) + @group = FactoryGirl.create(:group) + @user_params = { + :actor_id => @another_user.id.to_s, + :actor_type => 'user', + :role => 'reader' + } + @group_params = { + :actor_id => @group.id.to_s, + :actor_type => 'group', + :role => 'reader' + } + @create_params = { + :project_id => @project.id.to_s, + :format => :json + } + end it_should_behave_like 'project admin user' end @@ -67,57 +95,74 @@ describe CollaboratorsController do context 'for admin user' do before(:each) do @user = FactoryGirl.create(:user) - @user.relations +# @user.relations set_session_for(@user) @group = FactoryGirl.create(:group) @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') + @user_params = { + :actor_id => @another_user.id.to_s, + :actor_type => 'user', + :role => 'reader' + } + @group_params = { + :actor_id => @group.id.to_s, + :actor_type => 'group', + :role => 'reader' + } + @create_params = { + :project_id => @project.id.to_s, + :format => :json + } end it_should_behave_like 'project admin user' - it 'should add new collaborator with reader role' do - @params = {:member_id => @another_user.id.to_s, :project_id => @project.id.to_s} - post :add, @params - @project.relations.exists?(:object_type => 'User', :object_id => @another_user.id, :role => 'reader').should be_true - end - - it 'should add new group with reader role' do - @params = {:group_id => @group.id.to_s, :project_id => @project.id.to_s} - post :add, @params - @project.relations.exists?(:object_type => 'Group', :object_id => @group.id, :role => 'reader').should be_true - end - end context 'for owner user' do - before(:each) do - @user = FactoryGirl.create(:user) - set_session_for(@user) - @project.update_attribute(:owner, @user) - @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') - end + before(:each) do + @user = FactoryGirl.create(:user) + set_session_for(@user) + @group = FactoryGirl.create(:group) + + @project.update_attribute(:owner, @user) + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'admin') + + @user_params = { + :actor_id => @another_user.id.to_s, + :actor_type => 'user', + :role => 'reader' + } + @group_params = { + :actor_id => @group.id.to_s, + :actor_type => 'group', + :role => 'reader' + } + @create_params = { + :project_id => @project.id.to_s, + :format => :json + } + end it_should_behave_like 'project admin user' end context 'for reader user' do - before(:each) do - @user = FactoryGirl.create(:user) - set_session_for(@user) - @project.update_attribute(:owner, @user) - @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader') - end + before(:each) do + @user = FactoryGirl.create(:user) + set_session_for(@user) + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'reader') + end it_should_behave_like 'user with no rights for this project' end context 'for writer user' do - before(:each) do - @user = FactoryGirl.create(:user) - set_session_for(@user) - @project.update_attribute(:owner, @user) - @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'writer') - end + before(:each) do + @user = FactoryGirl.create(:user) + set_session_for(@user) + @project.relations.create!(:object_type => 'User', :object_id => @user.id, :role => 'writer') + end it_should_behave_like 'user with no rights for this project' end