Merge pull request #423 from warpc/347-javascript_refactoring
Fixed CollaboratorsController specs
This commit is contained in:
commit
1934d5b553
|
@ -39,7 +39,6 @@ class CollaboratorsController < ApplicationController
|
|||
end
|
||||
|
||||
def create
|
||||
puts params.inspect
|
||||
@collaborator = Collaborator.new(params[:collaborator])
|
||||
@collaborator.project = @project
|
||||
if @collaborator.save
|
||||
|
|
|
@ -27,11 +27,14 @@ class Collaborator
|
|||
end
|
||||
|
||||
def create(args)
|
||||
self.new(args).save
|
||||
c = self.new(args)
|
||||
return c.save ? c : false
|
||||
end
|
||||
|
||||
def create!(args)
|
||||
self.new(args).save!
|
||||
c = self.new(args)
|
||||
c.save!
|
||||
return c
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -49,11 +52,7 @@ class Collaborator
|
|||
@actor = args[:actor_type].classify.constantize.find(args[:actor_id])
|
||||
end
|
||||
|
||||
if @relation.nil? and @actor.present? and @project.present?
|
||||
@relation = Relation.by_object(@actor).by_target(@project).limit(1).first
|
||||
@relation ||= Relation.new(:object => @actor, :target => @project)
|
||||
end
|
||||
@relation.role = args[:role] if @relation.present? and args[:role].present?
|
||||
relation.role = args[:role] if args[:role].present? #if @relation.present? and args[:role].present?
|
||||
end
|
||||
|
||||
def update_attributes(attributes, options = {})
|
||||
|
@ -70,7 +69,7 @@ class Collaborator
|
|||
end
|
||||
|
||||
def id
|
||||
@relation.try(:id)
|
||||
relation.try(:id)
|
||||
end
|
||||
|
||||
def actor_id
|
||||
|
@ -94,23 +93,23 @@ class Collaborator
|
|||
end
|
||||
|
||||
def role
|
||||
@relation.try(:role)
|
||||
relation.try(:role)
|
||||
end
|
||||
|
||||
def role=(arg)
|
||||
@relation.role = arg
|
||||
relation.role = arg
|
||||
end
|
||||
|
||||
def save
|
||||
@relation.try(:save)
|
||||
relation.try(:save)
|
||||
end
|
||||
|
||||
def save!
|
||||
@relation.try(:save!)
|
||||
relation.try(:save!)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@relation.try(:destroy)
|
||||
relation.try(:destroy)
|
||||
end
|
||||
|
||||
def attributes
|
||||
|
|
|
@ -1,19 +1,46 @@
|
|||
# -*- encoding : utf-8 -*-
|
||||
require 'spec_helper'
|
||||
|
||||
def create_params
|
||||
@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
|
||||
|
||||
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_success
|
||||
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 +48,50 @@ 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)
|
||||
create_params
|
||||
end
|
||||
|
||||
it_should_behave_like 'project admin user'
|
||||
end
|
||||
|
@ -67,57 +99,48 @@ 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')
|
||||
create_params
|
||||
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')
|
||||
|
||||
create_params
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue