diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb index 7cbdf29..d6907a0 100644 --- a/app/controllers/hosts_controller.rb +++ b/app/controllers/hosts_controller.rb @@ -1,63 +1,64 @@ class HostsController < ApplicationController before_action :fetch_host, only: [:show, :edit, :update, :destroy, :submit_config, :revoke] # GET /hosts/new def new @host = Host.new end # POST /hosts def create @host = Host.new(fetch_params) if @host.save + current_user.hosts << @host redirect_to host_path @host else render :new end end # GET /hosts/1 def show; end # GET /hosts/1/edit def edit; end # PATCH /hosts/1 def update updates = fetch_params.slice(:port, :password) if updates.present? && @host.update_attributes(updates) @host.recalculate redirect_to host_path @host else render :edit end end # DELETE /hosts/1 def destroy @host.destroy redirect_to root_path end # POST /hosts/1/submit_config def submit_config @host.dispatch_to_bacula redirect_to host_path(@host) end # DELETE /hosts/1/revoke def revoke @host.remove_from_bacula redirect_to root_path end private def fetch_host - @host = Host.includes(job_templates: [:fileset, :schedule]).find(params[:id]) + @host = current_user.hosts.includes(job_templates: [:fileset, :schedule]).find(params[:id]) end def fetch_params params.require(:host).permit(:fqdn, :port, :password) end end diff --git a/spec/controllers/hosts_controller_spec.rb b/spec/controllers/hosts_controller_spec.rb index a508dc9..81b4775 100644 --- a/spec/controllers/hosts_controller_spec.rb +++ b/spec/controllers/hosts_controller_spec.rb @@ -1,128 +1,142 @@ require 'spec_helper' describe HostsController do + let(:user) { FactoryGirl.create(:user) } + before { controller.stub(:current_user) { user } } + describe 'GET #new' do before { get :new } it 'initializes a host' do expect(assigns(:host)).to be end it 'renders' do expect(response).to render_template(:new) end end describe 'PATCH #update' do let!(:host) { FactoryGirl.create(:host) } + before { host.users << user } + context 'with valid params' do let(:params) do { id: host.id, host: { port: 9999, password: 'wrong_pass' } } end it 'updates the host' do expect { patch :update, params }. to change { [host.reload.port, host.reload.password] }. to([9999, 'wrong_pass']) end it 'redirects to host_show' do patch :update, params expect(response).to redirect_to(host_path(host)) end end context 'with fqdn in params' do let(:params) do { id: host.id, host: { fqdn: 'another.host.gr' } } end it 'does not update the host' do expect { patch :update, params }. to_not change { host.reload.fqdn } end it 'renders the edit page' do patch :update, params expect(response).to render_template(:edit) end end end describe 'POST #create' do context 'with valid params' do let(:params) do { host: FactoryGirl.build(:host).attributes.symbolize_keys. slice(:password, :fqdn, :port) } end it 'creates the host' do expect { post :create, params }. to change { Host.count }.by(1) end it 'redirects to root' do post :create, params expect(response).to redirect_to(host_path(Host.last)) end + + it 'assigns the host to the user' do + expect { post :create, params }. + to change { user.hosts(true).count }.from(0).to(1) + end end context 'with invalid params' do let(:params) do { host: FactoryGirl.build(:host).attributes.symbolize_keys. slice(:fqdn, :port) } end before { post :create, params } it 'initializes a host with errors' do expect(assigns(:host)).to be end it 'renders :new' do expect(response).to render_template(:new) end end end describe 'POST #submit_config' do let(:host) { FactoryGirl.create(:host, :configured) } let(:params) { { id: host.id } } + before { host.users << user } + it 'redirects to root' do post :submit_config, params expect(response).to redirect_to(host_path(host)) end it 'calls submit_config_to_bacula on host' do Host.any_instance.should_receive(:dispatch_to_bacula) post :submit_config, params end end describe 'DELETE #revoke' do let(:host) { FactoryGirl.create(:host, status: Host::STATUSES[:for_removal]) } let(:params) { { id: host.id } } + before { host.users << user } + it 'redirects to root' do delete :revoke, params expect(response).to redirect_to(root_path) end it 'calls remove_from_bacula on host' do Host.any_instance.should_receive(:remove_from_bacula) delete :revoke, params end end end