Browse Source

naming refactor, indexes sqls tests

pull/1/head
Nicolae Claudius 13 years ago
parent
commit
b607be9ecf
  1. 10
      app/models/user.rb
  2. 4
      spec/controllers/domains_controller_spec.rb
  3. 30
      spec/models/ability_spec.rb
  4. 9
      spec/models/domain_spec.rb
  5. 5
      spec/models/record_spec.rb
  6. 16
      spec/support/shared_context/data.rb
  7. 2
      spec/support/shared_examples/wiring_controller.rb

10
app/models/user.rb

@ -29,8 +29,14 @@ class User < ActiveRecord::Base
delegate :can?, :cannot?, :to => :ability
def ability
@ability ||= Ability.new(:user => self)
def ability(options = {:reload => false})
options[:reload] ? _ability : (@ability ||= _ability)
end
protected
def _ability
Ability.new(:user => self)
end
end

4
spec/controllers/domains_controller_spec.rb

@ -25,11 +25,11 @@ describe DomainsController do
# a domain who's parent domain is in our system
context "subdomain" do
before do
sign_in other_user
sign_in user2
end
it "is wired with the user of the parent domain by #before_create_save" do
subdomain = build(:domain, :user => other_user, :name => "x.#{domain.name}")
subdomain = build(:domain, :user => user2, :name => "x.#{domain.name}")
@controller.send(:before_create_save, subdomain)
subdomain.user.should == user
end

30
spec/models/ability_spec.rb

@ -15,10 +15,10 @@ describe Ability do
end
it "denies other user to manage my domains and their records, and my hosts" do
other_user.should_not be_able_to(:manage, domain)
other_user.should_not be_able_to(:manage, a_record)
other_user.should_not be_able_to(:manage, soa_record)
other_user.should_not be_able_to(:manage, host_a_record)
user2.should_not be_able_to(:manage, domain)
user2.should_not be_able_to(:manage, a_record)
user2.should_not be_able_to(:manage, soa_record)
user2.should_not be_able_to(:manage, host_a_record)
end
it "allows admin to manage other user's hosts" do
@ -32,25 +32,25 @@ describe Ability do
end
it "allows other user to manage user's domains and records, if permitted" do
other_user.should be_able_to(:manage, domain)
other_user.should be_able_to(:manage, a_record)
other_user.should be_able_to(:manage, soa_record)
user2.should be_able_to(:manage, domain)
user2.should be_able_to(:manage, a_record)
user2.should be_able_to(:manage, soa_record)
end
it "denies third user to manage user's permitted domains and records" do
third_user.should_not be_able_to(:manage, domain)
third_user.should_not be_able_to(:manage, a_record)
third_user.should_not be_able_to(:manage, soa_record)
user3.should_not be_able_to(:manage, domain)
user3.should_not be_able_to(:manage, a_record)
user3.should_not be_able_to(:manage, soa_record)
end
it "allows other user to manage user's permitted subdomains" do
other_user.should be_able_to(:manage, subdomain)
other_user.should be_able_to(:manage, subsubdomain)
user2.should be_able_to(:manage, subdomain)
user2.should be_able_to(:manage, subsubdomain)
end
it "denies third user to manage other user's permitted subdomains" do
third_user.should_not be_able_to(:manage, subdomain)
third_user.should_not be_able_to(:manage, subsubdomain)
user3.should_not be_able_to(:manage, subdomain)
user3.should_not be_able_to(:manage, subsubdomain)
end
end
@ -60,7 +60,7 @@ describe Ability do
end
it "denies other user to manage my domain's permissions" do
other_user.should_not be_able_to(:manage, permission)
user2.should_not be_able_to(:manage, permission)
end
end

9
spec/models/domain_spec.rb

@ -47,7 +47,7 @@ describe Domain do
end
it "has parent_domain" do
subdomain = build(:domain, :user => other_user, :name => "x.#{domain.name}")
subdomain = build(:domain, :user => user2, :name => "x.#{domain.name}")
subdomain.parent_domain.should == domain
end
@ -60,17 +60,18 @@ describe Domain do
User.do_as(user) do
# stub a parent domain on another user account, with no permissions present
mock_domain = mock(:user_id => third_user.id, :user => third_user, :name => 'x')
mock_domain = mock(:user_id => user3.id, :user => user3, :name => 'x')
domain.stub(:parent_domain).and_return(mock_domain)
domain.should have(1).errors_on(:name)
end
end
it "queries domains corectly in index" do
query = Domain.accessible_by(user.ability)
permission3
query = Domain.accessible_by(user.ability(:reload => true))
wheres = query.where_values
joins = query.joins_values.map{|j| [j._name, j._type]}
wheres.should == ["(`permissions`.`user_id` = #{user.id}) OR (`domains`.`user_id` = #{user.id})"]
wheres.should == ["(`domains`.`name_reversed` = '#{domain3.name_reversed}.%') OR ((`permissions`.`user_id` = #{user.id}) OR (`domains`.`user_id` = #{user.id}))"]
joins.should == [[:permissions, Arel::Nodes::OuterJoin]]
end

5
spec/models/record_spec.rb

@ -35,11 +35,12 @@ describe Record do
end
it "queries A records corectly in index" do
query = A.accessible_by(user.ability)
permission3
query = A.accessible_by(user.ability(:reload => true))
wheres = query.where_values
joins = query.joins_values
wheres.size.should == 2
wheres.second.should == "(`permissions`.`user_id` = #{user.id}) OR ((`records`.`user_id` = #{user.id}) OR (`domains`.`user_id` = #{user.id}))"
wheres.second.should == "(`domains`.`name_reversed` = '#{domain3.name_reversed}.%') OR ((`permissions`.`user_id` = #{user.id}) OR ((`records`.`user_id` = #{user.id}) OR (`domains`.`user_id` = #{user.id})))"
record_joins_expectations(joins)
end

16
spec/support/shared_context/data.rb

@ -1,8 +1,8 @@
shared_context "data" do
let(:admin){create(:user)} # admin is a user that owns host domains
let(:user){create(:user)} # a regular user
let(:other_user){create(:user)}
let(:third_user){create(:user)}
let(:user2){create(:user)}
let(:user3){create(:user)}
def make_domain(options)
domain = build(:domain, options)
@ -11,15 +11,25 @@ shared_context "data" do
domain.soa_record.update_serial!
domain
end
# user's domains
let(:domain){make_domain(:user => user)}
let(:subdomain){make_domain(:name => "sub.#{domain.name}", :user => user)}
let(:subsubdomain){make_domain(:name => "sub.#{subdomain.name}", :user => user)}
# third user's domains
let(:domain3){make_domain(:user => user3)}
let(:subdomain3){make_domain(:name => "sub.#{domain.name}", :user => user3)}
let(:subsubdomain3){make_domain(:name => "sub.#{subdomain.name}", :user => user3)}
# admin's host domain (like entrydns.org)
let(:host_domain){make_domain(:user => admin, :name => Settings.host_domains.first)}
let(:a_record){create(:a, :content => '127.0.0.1', :domain => domain)}
let(:soa_record){domain.soa_record}
let(:host_a_record){create(:a, :content => '127.0.0.1', :domain => host_domain, :user => user)}
let(:permission){create(:permission, :domain => domain, :user => other_user)}
let(:permission){create(:permission, :domain => domain, :user => user2)}
let(:permission3){create(:permission, :domain => domain3, :user => user)}
end

2
spec/support/shared_examples/wiring_controller.rb

@ -21,7 +21,7 @@ shared_examples_for "wiring controller" do
context "on permitted domain" do
before do
sign_in other_user
sign_in user2
permission # touch to ensure creation
@controller.stub(:nested_parent_record => domain)
end

Loading…
Cancel
Save