Browse Source

naming refactor, indexes sqls tests

pull/1/head
Nicolae Claudius 13 years ago
parent
commit
b607be9ecf
  1. 14
      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

14
app/models/user.rb

@ -29,8 +29,14 @@ class User < ActiveRecord::Base
delegate :can?, :cannot?, :to => :ability delegate :can?, :cannot?, :to => :ability
def ability def ability(options = {:reload => false})
@ability ||= Ability.new(:user => self) options[:reload] ? _ability : (@ability ||= _ability)
end end
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 # a domain who's parent domain is in our system
context "subdomain" do context "subdomain" do
before do before do
sign_in other_user sign_in user2
end end
it "is wired with the user of the parent domain by #before_create_save" do 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) @controller.send(:before_create_save, subdomain)
subdomain.user.should == user subdomain.user.should == user
end end

30
spec/models/ability_spec.rb

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

9
spec/models/domain_spec.rb

@ -47,7 +47,7 @@ describe Domain do
end end
it "has parent_domain" do 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 subdomain.parent_domain.should == domain
end end
@ -60,17 +60,18 @@ describe Domain do
User.do_as(user) do User.do_as(user) do
# stub a parent domain on another user account, with no permissions present # 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.stub(:parent_domain).and_return(mock_domain)
domain.should have(1).errors_on(:name) domain.should have(1).errors_on(:name)
end end
end end
it "queries domains corectly in index" do 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 wheres = query.where_values
joins = query.joins_values.map{|j| [j._name, j._type]} 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]] joins.should == [[:permissions, Arel::Nodes::OuterJoin]]
end end

5
spec/models/record_spec.rb

@ -35,11 +35,12 @@ describe Record do
end end
it "queries A records corectly in index" do 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 wheres = query.where_values
joins = query.joins_values joins = query.joins_values
wheres.size.should == 2 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) record_joins_expectations(joins)
end end

16
spec/support/shared_context/data.rb

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

2
spec/support/shared_examples/wiring_controller.rb

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

Loading…
Cancel
Save