rosa-build/spec/controllers/projects/projects_controller_spec.rb

342 lines
12 KiB
Ruby
Raw Normal View History

2012-01-30 20:39:34 +00:00
# -*- encoding : utf-8 -*-
2011-03-10 11:35:46 +00:00
require 'spec_helper'
2012-09-06 11:53:03 +01:00
shared_examples_for 'projects user with reader rights' do
it 'should be able to fork project' do
post :fork, :owner_name => @project.owner.uname, :project_name => @project.name
response.should redirect_to(project_path(Project.last))
end
it 'should be able to fork project to their group' do
group = FactoryGirl.create(:group)
group.actors.create(:actor_type => 'User', :actor_id => @user.id, :role => 'admin')
lambda {post :fork, :owner_name => @project.owner.uname, :project_name => @project.name,
:group => group.id}.should change{ Project.count }.by(1)
end
it 'should be able to fork project to own group' do
group = FactoryGirl.create(:group, :owner => @user)
lambda {post :fork, :owner_name => @project.owner.uname, :project_name => @project.name,
:group => group.id}.should change{ Project.count }.by(1)
end
# it 'should be able to view project' do
# get :show, :owner_name => @project.owner.uname, :project_name => @project.name
# assigns(:project).should eq @project
# end
end
shared_examples_for 'projects user with project admin rights' do
it 'should be able to perform update action' do
put :update, {:owner_name => @project.owner.uname, :project_name => @project.name}.merge(@update_params)
response.should redirect_to(project_path(@project))
end
end
shared_examples_for 'user with destroy rights' do
it 'should be able to perform destroy action' do
delete :destroy, {:owner_name => @project.owner.uname, :project_name => @project.name}
response.should redirect_to(@project.owner)
end
it 'should change objects count on destroy' do
lambda { delete :destroy, :owner_name => @project.owner.uname, :project_name => @project.name }.should change{ Project.count }.by(-1)
end
end
shared_examples_for 'projects user without project admin rights' do
it 'should not be able to edit project' do
description = @project.description
put :update, :project=>{:description =>"hack"}, :owner_name => @project.owner.uname, :project_name => @project.name
@project.reload.description.should == description
response.should redirect_to(forbidden_path)
end
it 'should not be able to edit project sections' do
has_wiki, has_issues = @project.has_wiki, @project.has_issues
post :sections, :project =>{:has_wiki => !has_wiki, :has_issues => !has_issues}, :owner_name => @project.owner.uname, :project_name => @project.name
@project.reload.has_wiki.should == has_wiki
@project.reload.has_issues.should == has_issues
response.should redirect_to(forbidden_path)
end
it 'writer group should be able to fork project to their group' do
group = FactoryGirl.create(:group)
group.actors.create(:actor_type => 'User', :actor_id => @user.id, :role => 'writer')
lambda {post :fork, :owner_name => @project.owner.uname, :project_name => @project.name,
:group => group.id}.should change{ Project.count }.by(1)
end
it 'reader group should not be able to fork project to their group' do
group = FactoryGirl.create(:group)
group.actors.create(:actor_type => 'User', :actor_id => @user.id, :role => 'reader')
lambda {post :fork, :owner_name => @project.owner.uname, :project_name => @project.name,
:group => group.id}.should change{ Project.count }.by(0)
end
it 'writer group should be able to create project to their group' do
group = FactoryGirl.create(:group)
group.actors.create(:actor_type => 'User', :actor_id => @user.id, :role => 'writer')
lambda {post :create, @create_params.merge(:who_owns => 'group', :owner_id => group.id)}.should change{ Project.count }.by(1)
end
it 'reader group should not be able to create project to their group' do
group = FactoryGirl.create(:group)
group.actors.create(:actor_type => 'User', :actor_id => @user.id, :role => 'reader')
lambda {post :create, @create_params.merge(:who_owns => 'group', :owner_id => group.id)}.should change{ Project.count }.by(0)
end
2012-09-06 11:53:03 +01:00
end
describe Projects::ProjectsController do
before(:each) do
stub_symlink_methods
@project = FactoryGirl.create(:project)
2012-09-06 11:53:03 +01:00
@create_params = {:project => {:name => 'pro'}}
@update_params = {:project => {:description => 'pro2'}}
2012-09-06 11:53:03 +01:00
@user = FactoryGirl.create(:user)
set_session_for(@user)
end
2011-03-10 11:35:46 +00:00
context 'for users' do
2012-09-06 11:53:03 +01:00
context 'guest' do
2012-09-06 11:53:03 +01:00
before(:each) do
set_session_for(User.new)
end
2012-09-06 11:53:03 +01:00
it 'should not be able to perform index action' do
get :index
response.should redirect_to(new_user_session_path)
end
2012-09-06 11:53:03 +01:00
it 'should not be able to perform update action' do
put :update, {:owner_name => @project.owner.uname, :project_name => @project.name}.merge(@update_params)
response.should redirect_to(new_user_session_path)
end
it 'should not be able to perform create action' do
post :create, @create_params
response.should redirect_to(new_user_session_path)
end
end
2012-09-06 11:53:03 +01:00
context 'registered user' do
2012-09-06 11:53:03 +01:00
it 'should be able to perform index action' do
get :index
response.should render_template(:index)
end
context 'create project for myself' do
it 'should be able to perform create action' do
post :create, @create_params
response.should redirect_to(project_path( Project.last ))
end
2012-09-06 11:53:03 +01:00
it 'should create project in the database' do
lambda { post :create, @create_params }.should change{ Project.count }.by(1)
end
end
2012-09-06 11:53:03 +01:00
context 'create project for group' do
2012-09-06 11:53:03 +01:00
it 'should not be able to create project for alien group' do
group = FactoryGirl.create(:group)
post :create, @create_params.merge({:who_owns => 'group', :owner_id => group.id})
response.should redirect_to(forbidden_path)
end
2012-09-06 11:53:03 +01:00
it 'should be able to create project for their group' do
group = FactoryGirl.create(:group)
group.actors.create(:actor_type => 'User', :actor_id => @user.id, :role => 'admin')
lambda { post :create, @create_params.merge({:who_owns => 'group', :owner_id => group.id})}.should change{ Project.count }.by(1)
end
2012-09-06 11:53:03 +01:00
it 'should be able to create project for own group' do
group = FactoryGirl.create(:group, :owner => @user)
lambda { post :create, @create_params.merge({:who_owns => 'group', :owner_id => group.id})}.should change{ Project.count }.by(1)
end
end
2012-09-06 11:53:03 +01:00
end # context 'registered user'
end # context 'for users'
2012-09-06 11:53:03 +01:00
context 'for project members' do
2012-09-06 11:53:03 +01:00
context 'for global admin' do
before(:each) do
@user.role = "admin"
@user.save
set_session_for(@user)
end
2012-04-09 18:56:03 +01:00
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user with project admin rights'
it_should_behave_like 'projects user with reader rights'
it_should_behave_like 'user with destroy rights'
2012-04-09 18:56:03 +01:00
end
2012-09-06 11:53:03 +01:00
context 'for owner user' do
before(:each) do
@user = @project.owner
set_session_for(@user) # owner should be user
end
it_should_behave_like 'projects user with project admin rights'
it_should_behave_like 'projects user with reader rights'
it_should_behave_like 'user with destroy rights'
it 'should not be able to fork own project' do
post :fork, :owner_name => @project.owner.uname, :project_name => @project.name
response.should redirect_to(@project)
end
2011-12-16 00:33:44 +00:00
end
2012-09-06 11:53:03 +01:00
context 'for reader user' do
before(:each) do
@project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'reader')
end
it_should_behave_like 'projects user with reader rights'
it_should_behave_like 'projects user without project admin rights'
2011-12-16 00:33:44 +00:00
end
2012-09-06 11:53:03 +01:00
context 'for writer user' do
before(:each) do
@project.relations.create!(:actor_type => 'User', :actor_id => @user.id, :role => 'writer')
end
it_should_behave_like 'projects user with reader rights'
it_should_behave_like 'projects user without project admin rights'
2012-05-15 11:36:36 +01:00
end
2012-09-06 11:53:03 +01:00
context 'for other user' do
it 'should not be able to fork hidden project' do
@project.update_attributes(:visibility => 'hidden')
post :fork, :owner_name => @project.owner.uname, :project_name => @project.name
response.should redirect_to(forbidden_path)
end
it_should_behave_like 'projects user without project admin rights'
end
2012-05-15 11:36:36 +01:00
2012-09-06 11:53:03 +01:00
end # context 'for project members'
2012-05-15 11:36:36 +01:00
2012-06-19 19:24:35 +01:00
context 'for group' do
before(:each) do
@group = FactoryGirl.create(:group)
end
2012-09-06 11:53:03 +01:00
context 'group is owner of the project' do
2012-06-19 19:24:35 +01:00
before(:each) do
2012-09-06 11:53:03 +01:00
@project = FactoryGirl.create(:project, :owner => @group)
2012-06-19 19:24:35 +01:00
end
2012-09-06 11:53:03 +01:00
context 'group member user with reader role' do
2012-06-19 19:24:35 +01:00
before(:each) do
2012-09-06 11:53:03 +01:00
@group.actors.create(:actor_id => @user.id, :actor_type => 'User', :role => 'reader')
2012-06-19 19:24:35 +01:00
end
it_should_behave_like 'projects user with reader rights'
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user without project admin rights'
2012-06-19 19:24:35 +01:00
it 'should has reader role to group project' do
2012-09-06 11:53:03 +01:00
@user.best_role(@project).should eql('reader')
2012-06-19 19:24:35 +01:00
end
context 'user should has best role' do
before(:each) do
2012-09-06 11:53:03 +01:00
@project.relations.create :actor_id => @user.id, :actor_type => @user.class.to_s, :role => 'admin'
2012-06-19 19:24:35 +01:00
end
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user with project admin rights'
2012-06-19 19:24:35 +01:00
end
end
2012-09-06 11:53:03 +01:00
context 'group member user with admin role' do
2012-06-19 19:24:35 +01:00
before(:each) do
2012-09-06 11:53:03 +01:00
@group.actors.create(:actor_id => @user.id, :actor_type => 'User', :role => 'admin')
2012-06-19 19:24:35 +01:00
end
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user with project admin rights'
2012-06-19 19:24:35 +01:00
it_should_behave_like 'projects user with reader rights'
end
end
2012-09-06 11:53:03 +01:00
context 'group is member of the project' do
2012-06-19 19:24:35 +01:00
context 'with admin rights' do
before(:each) do
@project.relations.create :actor_id => @group.id, :actor_type => @group.class.to_s, :role => 'admin'
end
2012-09-06 11:53:03 +01:00
context 'group member user with reader role' do
2012-06-19 19:24:35 +01:00
before(:each) do
2012-09-06 11:53:03 +01:00
@group.actors.create(:actor_id => @user.id, :actor_type => 'User', :role => 'reader')
2012-06-19 19:24:35 +01:00
end
it_should_behave_like 'projects user with reader rights'
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user with project admin rights'
2012-06-19 19:24:35 +01:00
context 'user should has best role' do
before(:each) do
2012-09-06 11:53:03 +01:00
@project.relations.create :actor_id => @user.id, :actor_type => @user.class.to_s, :role => 'reader'
2012-06-19 19:24:35 +01:00
end
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user with project admin rights'
2012-06-19 19:24:35 +01:00
end
end
2012-09-06 11:53:03 +01:00
context 'group member user with admin role' do
2012-06-19 19:24:35 +01:00
before(:each) do
2012-09-06 11:53:03 +01:00
@group.actors.create(:actor_id => @user.id, :actor_type => 'User', :role => 'admin')
2012-06-19 19:24:35 +01:00
end
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user with project admin rights'
2012-06-19 19:24:35 +01:00
it_should_behave_like 'projects user with reader rights'
end
end
context 'with reader rights' do
before(:each) do
@project.relations.create :actor_id => @group.id, :actor_type => @group.class.to_s, :role => 'reader'
end
2012-09-06 11:53:03 +01:00
context 'group member user with reader role' do
2012-06-19 19:24:35 +01:00
before(:each) do
2012-09-06 11:53:03 +01:00
@group.actors.create(:actor_id => @user.id, :actor_type => 'User', :role => 'reader')
2012-06-19 19:24:35 +01:00
end
it_should_behave_like 'projects user with reader rights'
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user without project admin rights'
2012-06-19 19:24:35 +01:00
context 'user should has best role' do
before(:each) do
2012-09-06 11:53:03 +01:00
@project.relations.create :actor_id => @user.id, :actor_type => @user.class.to_s, :role => 'admin'
2012-06-19 19:24:35 +01:00
end
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user with project admin rights'
2012-06-19 19:24:35 +01:00
end
end
2012-09-06 11:53:03 +01:00
context 'group member user with admin role' do
2012-06-19 19:24:35 +01:00
before(:each) do
2012-09-06 11:53:03 +01:00
@group.actors.create(:actor_id => @user.id, :actor_type => 'User', :role => 'admin')
2012-06-19 19:24:35 +01:00
end
it_should_behave_like 'projects user with reader rights'
2012-09-06 11:53:03 +01:00
it_should_behave_like 'projects user without project admin rights'
2012-06-19 19:24:35 +01:00
end
end
2012-05-15 11:36:36 +01:00
end
end
2011-03-10 11:35:46 +00:00
end