[refs #2249] CanCan tests

This commit is contained in:
konstantin.grabar 2011-11-22 23:21:09 +04:00
parent ba77244b02
commit fc3c6a05c9
13 changed files with 178 additions and 3 deletions

View File

@ -14,7 +14,7 @@ class Ability
can :forbidden, Platform can :forbidden, Platform
#cannot :read, Platform, :visibility => 'hidden' #cannot :read, Platform, :visibility => 'hidden'
can :read, [Project, Platform], :visibility => 'open' can :read, [Repository, Platform], :visibility => 'open'
can :auto_build, Project # TODO: This needs to be checked! can :auto_build, Project # TODO: This needs to be checked!
can [:status_build, :pre_build, :post_build, :circle_build, :new_bbdt], BuildList can [:status_build, :pre_build, :post_build, :circle_build, :new_bbdt], BuildList

View File

@ -11,7 +11,7 @@ class Repository < ActiveRecord::Base
has_many :groups, :through => :objects, :source => :object, :source_type => 'Group' has_many :groups, :through => :objects, :source => :object, :source_type => 'Group'
validates :name, :uniqueness => {:scope => :platform_id}, :presence => true validates :name, :uniqueness => {:scope => :platform_id}, :presence => true
validates :unixname, :uniqueness => {:scope => :platform_id}, :presence => true, :format => { :with => /^[a-z0-9\-.]+$/ } validates :unixname, :uniqueness => {:scope => :platform_id}, :presence => true, :format => { :with => /^[a-z0-9_]+$/ }
# validates :platform_id, :presence => true # if you uncomment this platform clone will not work # validates :platform_id, :presence => true # if you uncomment this platform clone will not work
scope :recent, order("name ASC") scope :recent, order("name ASC")

View File

@ -34,3 +34,18 @@ Rosa::Application.configure do
# Print deprecation notices to the stderr # Print deprecation notices to the stderr
config.active_support.deprecation = :stderr config.active_support.deprecation = :stderr
end end
# Uncomment this to stub XML RPC calls
require 'xmlrpc/client'
module XMLRPC
class Client
def call(*args)
# raise args.inspect
case
when args.first == 'get_status'
{:client_count => '1', :count_new_task => '2', :count_build_task => 3}
else; 0
end
end
end
end

View File

@ -1,4 +1,5 @@
require 'xmlrpc/client' require 'xmlrpc/client'
class BuildServer class BuildServer
SUCCESS = 0 SUCCESS = 0

View File

@ -1,4 +1,5 @@
require 'xmlrpc/client' require 'xmlrpc/client'
class ProductBuilder class ProductBuilder
SUCCESS = 0 SUCCESS = 0

View File

@ -6,6 +6,14 @@ Factory.sequence :string do |n|
"Lorem ipsum #{n}" "Lorem ipsum #{n}"
end end
Factory.sequence :uname do |n|
"test#{n}"
end
Factory.sequence :unixname do |n|
"test_unixname#{n}"
end
Factory.sequence :email do |n| Factory.sequence :email do |n|
"email#{n}@example.com" "email#{n}@example.com"
end end

View File

@ -0,0 +1,3 @@
Factory.define(:arch) do |p|
p.name { Factory.next(:string) }
end

View File

@ -0,0 +1,9 @@
Factory.define(:build_list) do |p|
p.association :project, :factory => :project
p.association :pl, :factory => :platform
p.association :arch, :factory => :arch
p.bpl { |bl| bl.pl }
p.project_version "1.0"
p.build_requires true
p.update_type 'security'
end

View File

@ -1,4 +1,7 @@
Factory.define(:platform) do |p| Factory.define(:platform) do |p|
p.name { Factory.next(:string) } p.name { Factory.next(:string) }
p.unixname { Factory.next(:string) } p.unixname { Factory.next(:unixname) }
p.platform_type 'main'
p.distrib_type APP_CONFIG['distr_types'].first
p.association :owner, :factory => :user
end end

View File

@ -0,0 +1,5 @@
Factory.define(:project) do |p|
p.name { Factory.next(:string) }
p.unixname { Factory.next(:unixname) }
p.association :owner, :factory => :user
end

View File

@ -0,0 +1,6 @@
Factory.define(:repository) do |p|
p.name { Factory.next(:string) }
p.unixname { Factory.next(:unixname) }
p.association :platform, :factory => :platform
p.association :owner, :factory => :user
end

View File

@ -0,0 +1,16 @@
Factory.define(:user) do |u|
u.email { Factory.next(:email) }
u.name { Factory.next(:string) }
u.uname { Factory.next(:uname) }
u.password '123456'
u.password_confirmation { |user| user.password }
end
Factory.define(:admin, :class => 'User') do |u|
u.email { Factory.next(:email) }
u.name { Factory.next(:string) }
u.uname { Factory.next(:uname) }
u.password '123456'
u.password_confirmation { |user| user.password }
u.role 'admin'
end

108
spec/models/cancan_spec.rb Normal file
View File

@ -0,0 +1,108 @@
require 'spec_helper'
require "cancan/matchers"
def admin_create
@admin = Factory(:admin)
@ability = Ability.new(@admin)
end
def user_create
@user = Factory(:user)
@ability = Ability.new(@user)
end
def guest_create
@ability = Ability.new(User.new)
end
describe CanCan do
let(:personal_platform) { Factory(:platform, :platform_type => 'personal') }
let(:personal_repository) { Factory(:repository, :platform_type => 'personal') }
let(:open_platform) { Factory(:platform, :visibility => 'open') }
let(:hidden_platform) { Factory(:platform, :visibility => 'hidden') }
context 'Site admin' do
before(:each) do
admin_create
end
it 'should manage all' do
#(@ability.can? :manage, :all).should be_true
@ability.should be_able_to(:manage, :all)
end
it 'should not be able to destroy personal platforms' do
@ability.should_not be_able_to(:destroy, personal_platform)
end
it 'should not be able to destroy personal repositories' do
@ability.should_not be_able_to(:destroy, personal_repository)
end
end
context 'Site guest' do
before(:each) do
guest_create
end
it 'should be able to read open platform' do
@ability.should be_able_to(:read, open_platform)
end
it 'should not be able to read hidden platform' do
@ability.should_not be_able_to(:read, hidden_platform)
end
it 'should be able to auto build projects' do
@ability.should be_able_to(:auto_build, Project)
end
[:status_build, :pre_build, :post_build, :circle_build, :new_bbdt].each do |action|
it "should be able to #{ action } build list" do
@ability.should be_able_to(action, BuildList)
end
end
it 'should be able to register new user' do
@ability.should be_able_to(:create, User)
end
end
context 'Project collaborators' do
before(:each) do
user_create
end
context 'with read rights' do
before(:each) do
@project = Factory(:project)
@project.relations.create!(:object_id => @user.id, :object_type => 'User', :role => 'read')
open_platform.relations.create!(:object_id => @user.id, :object_type => 'User', :role => 'read')
end
it 'should be able to read project' do
@ability.should be_able_to(:read, @project)
end
it 'should be able to read project' do
@ability.should be_able_to(:read, open_platform)
end
end
context 'with write rights' do
end
context 'with admin rights' do
before(:each) do
@project = Factory(:project, :owner => @user)
end
end
end
end