From 79bc417fabffb6c216b9b4c4c9e770291ab471b4 Mon Sep 17 00:00:00 2001 From: Nicolae Claudius Date: Sat, 14 Jan 2012 02:26:58 -0800 Subject: [PATCH] permission-aware wiring --- app/controllers/aaaas_controller.rb | 3 +- app/controllers/as_controller.rb | 3 +- app/controllers/cnames_controller.rb | 3 +- app/controllers/mxes_controller.rb | 3 +- app/controllers/ns_controller.rb | 3 +- app/controllers/records_controller.rb | 3 +- app/controllers/soas_controller.rb | 3 +- app/controllers/srvs_controller.rb | 3 +- app/controllers/txts_controller.rb | 3 +- app/models/domain.rb | 4 + app/models/record.rb | 3 + spec/controllers/hosts_controller_spec.rb | 226 ++++++++++-------- spec/controllers/records_controller_spec.rb | 1 - .../shared_examples/wiring_controller.rb | 41 +++- 14 files changed, 175 insertions(+), 127 deletions(-) diff --git a/app/controllers/aaaas_controller.rb b/app/controllers/aaaas_controller.rb index 935a385..21fbe01 100644 --- a/app/controllers/aaaas_controller.rb +++ b/app/controllers/aaaas_controller.rb @@ -27,7 +27,8 @@ class AaaasController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end # override to close create form after success diff --git a/app/controllers/as_controller.rb b/app/controllers/as_controller.rb index cb0f628..207624b 100644 --- a/app/controllers/as_controller.rb +++ b/app/controllers/as_controller.rb @@ -28,7 +28,8 @@ class AsController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end # override to close create form after success diff --git a/app/controllers/cnames_controller.rb b/app/controllers/cnames_controller.rb index e08ae4d..0423465 100644 --- a/app/controllers/cnames_controller.rb +++ b/app/controllers/cnames_controller.rb @@ -27,7 +27,8 @@ class CnamesController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end # override to close create form after success diff --git a/app/controllers/mxes_controller.rb b/app/controllers/mxes_controller.rb index ba12865..a8bed14 100644 --- a/app/controllers/mxes_controller.rb +++ b/app/controllers/mxes_controller.rb @@ -35,7 +35,8 @@ class MxesController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end # override to close create form after success diff --git a/app/controllers/ns_controller.rb b/app/controllers/ns_controller.rb index 68f70ae..51da3f4 100644 --- a/app/controllers/ns_controller.rb +++ b/app/controllers/ns_controller.rb @@ -28,7 +28,8 @@ class NsController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end # override to close create form after success diff --git a/app/controllers/records_controller.rb b/app/controllers/records_controller.rb index 481ed32..c7e0d87 100644 --- a/app/controllers/records_controller.rb +++ b/app/controllers/records_controller.rb @@ -55,7 +55,8 @@ class RecordsController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end end \ No newline at end of file diff --git a/app/controllers/soas_controller.rb b/app/controllers/soas_controller.rb index 857a811..816b061 100644 --- a/app/controllers/soas_controller.rb +++ b/app/controllers/soas_controller.rb @@ -23,7 +23,8 @@ class SoasController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end # override to close create form after success diff --git a/app/controllers/srvs_controller.rb b/app/controllers/srvs_controller.rb index a13b23a..dfedc2b 100644 --- a/app/controllers/srvs_controller.rb +++ b/app/controllers/srvs_controller.rb @@ -32,7 +32,8 @@ class SrvsController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end # override to close create form after success diff --git a/app/controllers/txts_controller.rb b/app/controllers/txts_controller.rb index 1c37692..e7f96b8 100644 --- a/app/controllers/txts_controller.rb +++ b/app/controllers/txts_controller.rb @@ -27,7 +27,8 @@ class TxtsController < ApplicationController end def before_create_save(record) - record.user = current_user + record.domain = nested_parent_record + record.user = record.domain_user end # override to close create form after success diff --git a/app/models/domain.rb b/app/models/domain.rb index ea0d7de..df96613 100644 --- a/app/models/domain.rb +++ b/app/models/domain.rb @@ -116,6 +116,10 @@ class Domain < ActiveRecord::Base scope :host_domains, where(:name => Settings.host_domains) + def host_domain? + Settings.host_domains.include?(name) + end + def setup(email) build_soa_record soa = soa_record diff --git a/app/models/record.rb b/app/models/record.rb index ab7f30c..618bca9 100644 --- a/app/models/record.rb +++ b/app/models/record.rb @@ -50,6 +50,9 @@ class Record < ActiveRecord::Base end end + delegate :host_domain?, :to => :domain + delegate :user, :to => :domain, :prefix => :domain + protected def prepare_name! diff --git a/spec/controllers/hosts_controller_spec.rb b/spec/controllers/hosts_controller_spec.rb index 591747e..7ecb698 100644 --- a/spec/controllers/hosts_controller_spec.rb +++ b/spec/controllers/hosts_controller_spec.rb @@ -1,144 +1,158 @@ require 'spec_helper' describe HostsController do - it_should_behave_like "wiring controller" + include_context "data" + + before do + sign_in user + @controller.stub(:nested_parent_record => host_domain) + end + + it "#new_model is wired" do + @controller.send(:new_model).user.should == user + end + + it "#before_create_save wires" do + @controller.send(:before_create_save, host_a_record) + host_a_record.user.should == user + end end # TODO implement me __END__ - # This should return the minimal set of attributes required to create a valid - # Host. As you add validations to Host, be sure to - # update the return value of this method accordingly. - def valid_attributes - {} +# This should return the minimal set of attributes required to create a valid +# Host. As you add validations to Host, be sure to +# update the return value of this method accordingly. +def valid_attributes + {} +end + +describe "GET index" do + it "assigns all hosts as @hosts" do + host = Host.create! valid_attributes + get :index + assigns(:hosts).should eq([host]) end +end - describe "GET index" do - it "assigns all hosts as @hosts" do - host = Host.create! valid_attributes - get :index - assigns(:hosts).should eq([host]) - end +describe "GET show" do + it "assigns the requested host as @host" do + host = Host.create! valid_attributes + get :show, :id => host.id.to_s + assigns(:host).should eq(host) end +end - describe "GET show" do - it "assigns the requested host as @host" do - host = Host.create! valid_attributes - get :show, :id => host.id.to_s - assigns(:host).should eq(host) - end +describe "GET new" do + it "assigns a new host as @host" do + get :new + assigns(:host).should be_a_new(Host) end +end - describe "GET new" do - it "assigns a new host as @host" do - get :new - assigns(:host).should be_a_new(Host) - end +describe "GET edit" do + it "assigns the requested host as @host" do + host = Host.create! valid_attributes + get :edit, :id => host.id.to_s + assigns(:host).should eq(host) end +end - describe "GET edit" do - it "assigns the requested host as @host" do - host = Host.create! valid_attributes - get :edit, :id => host.id.to_s - assigns(:host).should eq(host) +describe "POST create" do + describe "with valid params" do + it "creates a new Host" do + expect { + post :create, :host => valid_attributes + }.to change(Host, :count).by(1) end - end - describe "POST create" do - describe "with valid params" do - it "creates a new Host" do - expect { - post :create, :host => valid_attributes - }.to change(Host, :count).by(1) - end + it "assigns a newly created host as @host" do + post :create, :host => valid_attributes + assigns(:host).should be_a(Host) + assigns(:host).should be_persisted + end - it "assigns a newly created host as @host" do - post :create, :host => valid_attributes - assigns(:host).should be_a(Host) - assigns(:host).should be_persisted - end + it "redirects to the created host" do + post :create, :host => valid_attributes + response.should redirect_to(Host.last) + end + end - it "redirects to the created host" do - post :create, :host => valid_attributes - response.should redirect_to(Host.last) - end + describe "with invalid params" do + it "assigns a newly created but unsaved host as @host" do + # Trigger the behavior that occurs when invalid params are submitted + Host.any_instance.stub(:save).and_return(false) + post :create, :host => {} + assigns(:host).should be_a_new(Host) end - describe "with invalid params" do - it "assigns a newly created but unsaved host as @host" do - # Trigger the behavior that occurs when invalid params are submitted - Host.any_instance.stub(:save).and_return(false) - post :create, :host => {} - assigns(:host).should be_a_new(Host) - end - - it "re-renders the 'new' template" do - # Trigger the behavior that occurs when invalid params are submitted - Host.any_instance.stub(:save).and_return(false) - post :create, :host => {} - response.should render_template("new") - end + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + Host.any_instance.stub(:save).and_return(false) + post :create, :host => {} + response.should render_template("new") end end +end + +describe "PUT update" do + describe "with valid params" do + it "updates the requested host" do + host = Host.create! valid_attributes + # Assuming there are no other hosts in the database, this + # specifies that the Host created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + Host.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, :id => host.id, :host => {'these' => 'params'} + end - describe "PUT update" do - describe "with valid params" do - it "updates the requested host" do - host = Host.create! valid_attributes - # Assuming there are no other hosts in the database, this - # specifies that the Host created on the previous line - # receives the :update_attributes message with whatever params are - # submitted in the request. - Host.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) - put :update, :id => host.id, :host => {'these' => 'params'} - end - - it "assigns the requested host as @host" do - host = Host.create! valid_attributes - put :update, :id => host.id, :host => valid_attributes - assigns(:host).should eq(host) - end - - it "redirects to the host" do - host = Host.create! valid_attributes - put :update, :id => host.id, :host => valid_attributes - response.should redirect_to(host) - end + it "assigns the requested host as @host" do + host = Host.create! valid_attributes + put :update, :id => host.id, :host => valid_attributes + assigns(:host).should eq(host) end - describe "with invalid params" do - it "assigns the host as @host" do - host = Host.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - Host.any_instance.stub(:save).and_return(false) - put :update, :id => host.id.to_s, :host => {} - assigns(:host).should eq(host) - end - - it "re-renders the 'edit' template" do - host = Host.create! valid_attributes - # Trigger the behavior that occurs when invalid params are submitted - Host.any_instance.stub(:save).and_return(false) - put :update, :id => host.id.to_s, :host => {} - response.should render_template("edit") - end + it "redirects to the host" do + host = Host.create! valid_attributes + put :update, :id => host.id, :host => valid_attributes + response.should redirect_to(host) end end - describe "DELETE destroy" do - it "destroys the requested host" do + describe "with invalid params" do + it "assigns the host as @host" do host = Host.create! valid_attributes - expect { - delete :destroy, :id => host.id.to_s - }.to change(Host, :count).by(-1) + # Trigger the behavior that occurs when invalid params are submitted + Host.any_instance.stub(:save).and_return(false) + put :update, :id => host.id.to_s, :host => {} + assigns(:host).should eq(host) end - it "redirects to the hosts list" do + it "re-renders the 'edit' template" do host = Host.create! valid_attributes - delete :destroy, :id => host.id.to_s - response.should redirect_to(hosts_url) + # Trigger the behavior that occurs when invalid params are submitted + Host.any_instance.stub(:save).and_return(false) + put :update, :id => host.id.to_s, :host => {} + response.should render_template("edit") end end +end + +describe "DELETE destroy" do + it "destroys the requested host" do + host = Host.create! valid_attributes + expect { + delete :destroy, :id => host.id.to_s + }.to change(Host, :count).by(-1) + end + + it "redirects to the hosts list" do + host = Host.create! valid_attributes + delete :destroy, :id => host.id.to_s + response.should redirect_to(hosts_url) + end +end end diff --git a/spec/controllers/records_controller_spec.rb b/spec/controllers/records_controller_spec.rb index c245e31..8e94cdf 100644 --- a/spec/controllers/records_controller_spec.rb +++ b/spec/controllers/records_controller_spec.rb @@ -8,7 +8,6 @@ describe RecordsController do before do sign_in user - controller.stub(:current_user).and_return(user) end it "modifies @record when IP given" do diff --git a/spec/support/shared_examples/wiring_controller.rb b/spec/support/shared_examples/wiring_controller.rb index 1b47248..05e4407 100644 --- a/spec/support/shared_examples/wiring_controller.rb +++ b/spec/support/shared_examples/wiring_controller.rb @@ -1,21 +1,40 @@ shared_examples_for "wiring controller" do context "wiring" do include_context "data" - let(:record){Record.new} - before do - sign_in user - @controller.stub(:nested_parent_record => domain) - end - - it "#new_model is wired" do - @controller.send(:new_model).user.should == user + context "on owned domain" do + before do + sign_in user + @controller.stub(:nested_parent_record => domain) + end + + it "#new_model is wired" do + @controller.send(:new_model).user.should == domain.user + end + + it "#before_create_save wires" do + @controller.send(:before_create_save, record) + record.user.should == domain.user + end end - it "#before_create_save wires" do - @controller.send(:before_create_save, record) - record.user.should == user + context "on permitted domain" do + before do + sign_in other_user + permission # touch to ensure creation + @controller.stub(:nested_parent_record => domain) + end + + it "#new_model is wired" do + @controller.send(:new_model).user.should == domain.user + end + + it "#before_create_save wires" do + @controller.send(:before_create_save, record) + record.user.should == domain.user + end end + end end \ No newline at end of file