Browse Source

domain parent upstream lookup, tests fixes, refactor

pull/1/head
Nicolae Claudius 13 years ago
parent
commit
08a1404ab6
  1. 26
      app/models/domain.rb
  2. 35
      spec/models/ability_spec.rb
  3. 11
      spec/models/domain_spec.rb

26
app/models/domain.rb

@ -61,18 +61,11 @@ class Domain < ActiveRecord::Base
end end
end end
# Returns the first immediate parent, if exists (and caches the search)
def parent_domain def parent_domain
return nil if name.nil? return nil if name.nil?
@parent_domain ||= {} @parent_domain ||= {}
@parent_domain[name] ||= begin @parent_domain[name] ||= _parent_domain
segments = name.split('.')
if segments.size >= 2
domain_name = segments[1..-1].join('.')
Domain.find_by_name(domain_name)
else
nil
end
end
end end
# If current user present authorize it # If current user present authorize it
@ -156,4 +149,19 @@ class Domain < ActiveRecord::Base
ns2.content = Settings.ns.second ns2.content = Settings.ns.second
ns3.content = Settings.ns.third ns3.content = Settings.ns.third
end end
protected
# Returns the first immediate parent, if exists (does not cache the search)
# For example "sub.sub.domain.com"'s parent might be "sub.domain.com" or "domain.com"
def _parent_domain
segments = name.split('.')
while segments.size > 1
segments.shift
domain = Domain.find_by_name(segments.join('.'))
return domain if domain.present?
end
return nil
end
end end

35
spec/models/ability_spec.rb

@ -29,29 +29,42 @@ describe Ability do
context "permitted" do context "permitted" do
before do before do
permission # ensure permission to domain User.do_as(user) do
domain
subdomain
subsubdomain
permission # ensure permission to domain
end
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
user2.should be_able_to(crud, domain) User.do_as(user2) do
user2.should be_able_to(crud, a_record) user2.should be_able_to(crud, domain)
user2.should be_able_to(crud, soa_record) user2.should be_able_to(crud, a_record)
user2.should be_able_to(crud, soa_record)
end
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
user3.should_not be_able_to(crud, domain) User.do_as(user3) do
user3.should_not be_able_to(crud, a_record) user3.should_not be_able_to(crud, domain)
user3.should_not be_able_to(crud, soa_record) user3.should_not be_able_to(crud, a_record)
user3.should_not be_able_to(crud, soa_record)
end
end end
it "allows other user to manage user's permitted subdomains" do it "allows other user to manage user's permitted subdomains" do
user2.should be_able_to(crud, subdomain) User.do_as(user2) do
user2.should be_able_to(crud, subsubdomain) user2.should be_able_to(crud, subdomain)
user2.should be_able_to(crud, subsubdomain)
end
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
user3.should_not be_able_to(crud, subdomain) User.do_as(user3) do
user3.should_not be_able_to(crud, subsubdomain) user3.should_not be_able_to(crud, subdomain)
user3.should_not be_able_to(crud, subsubdomain)
end
end end
end end

11
spec/models/domain_spec.rb

@ -25,8 +25,8 @@ describe Domain do
it "updates name to records when name changed" do it "updates name to records when name changed" do
domain.update_attributes(:name => "changed#{domain.name}") domain.update_attributes(:name => "changed#{domain.name}")
domain.soa_record.name.should == domain.name domain.soa_record.name.should == domain.name
domain.records.all.size.should == Settings.ns.count + 1 domain.records.count.should == Settings.ns.count + 1
for record in domain.records.all domain.records.each do |record|
record.name.should =~ /#{domain.name}$/ record.name.should =~ /#{domain.name}$/
end end
(domain.soa_record.serial % 10).should == 0 (domain.soa_record.serial % 10).should == 0
@ -67,7 +67,12 @@ 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 => user3.id, :user => user3, :name => 'x') mock_domain = mock(
:user_id => user3.id,
:user => user3,
:name => 'x',
:can_be_managed_by_current_user? => false
)
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

Loading…
Cancel
Save