diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index 006e9f4..dee65a7 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -1,18 +1,19 @@ class ClientsController < ApplicationController before_action :set_client, only: :show # GET /clients def index @clients = Client.includes(:jobs).all @active_jobs = Job.running.group(:ClientId).count + @hosts = Host.not_baculized end # GET /clients/1 def show; end private def set_client @client = Client.find(params[:id]) end end diff --git a/app/controllers/hosts_controller.rb b/app/controllers/hosts_controller.rb index 84b9fc1..6937f76 100644 --- a/app/controllers/hosts_controller.rb +++ b/app/controllers/hosts_controller.rb @@ -1,22 +1,43 @@ class HostsController < ApplicationController # before_action :fetch_params, only: :create + before_action :fetch_host, only: [:show, :edit, :update, :destroy] + # GET /hosts def new @host = Host.new end + # POST /hosts def create @host = Host.new(fetch_params) if @host.save redirect_to root_path else render :new end end + # GET /hosts/1 + def show; end + + # GET /hosts/1/edit + def edit; end + + # PUT /hosts/1 + def update;end + + # DELETE /hosts/1 + def destroy + #@host.destroy + end + private + def fetch_host + @host = Host.find(params[:id]) + end + def fetch_params - params.require(:host).permit(:name, :fqdn, :port, :password) + params.require(:host).permit(:fqdn, :port, :password) end end diff --git a/config/routes.rb b/config/routes.rb index c624f02..61803d0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,6 @@ Rails.application.routes.draw do resources :clients, only: [:index, :show] - resources :hosts, only: [:new, :create] + resources :hosts, only: [:new, :create, :show, :edit, :update, :destroy] root 'clients#index' end diff --git a/spec/controllers/clients_controller_spec.rb b/spec/controllers/clients_controller_spec.rb new file mode 100644 index 0000000..edde293 --- /dev/null +++ b/spec/controllers/clients_controller_spec.rb @@ -0,0 +1,40 @@ +require 'spec_helper' + +describe ClientsController do + describe 'GET #index' do + let!(:job) { FactoryGirl.create(:job) } + let!(:running_job) { FactoryGirl.create(:job, :running) } + + before { get :index } + + it 'fetches clients' do + expect(assigns(:clients)).to be + end + + it 'fetches hosts' do + expect(assigns(:hosts)).to be + end + + it 'fetches active jobs' do + expect(assigns(:active_jobs)).to eq(Hash[running_job.client_id, 1]) + end + + it 'renders' do + expect(response).to render_template(:index) + end + end + + describe 'GET #show' do + let(:client) { FactoryGirl.create(:client) } + + before { get :show, id: client.id } + + it 'fetches client' do + expect(assigns(:client)).to eq(client) + end + + it 'renders' do + expect(response).to render_template(:show) + end + end +end diff --git a/spec/controllers/hosts_controller_spec.rb b/spec/controllers/hosts_controller_spec.rb index b3580ff..e284881 100644 --- a/spec/controllers/hosts_controller_spec.rb +++ b/spec/controllers/hosts_controller_spec.rb @@ -1,55 +1,55 @@ require 'spec_helper' describe HostsController do 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 'POST #create' do context 'with valid params' do let(:params) do { host: FactoryGirl.build(:host).attributes.symbolize_keys. - slice(:password, :name, :fqdn, :port) + 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 end context 'with invalid params' do let(:params) do { host: FactoryGirl.build(:host).attributes.symbolize_keys. - slice(:name, :fqdn, :port) + 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 end diff --git a/spec/factories/client.rb b/spec/factories/client.rb new file mode 100644 index 0000000..b0a8e85 --- /dev/null +++ b/spec/factories/client.rb @@ -0,0 +1,9 @@ +FactoryGirl.define do + factory :client do + sequence(:name) { |i| "Bacula Client #{i}" } + uname 'Linux' + auto_prune 1 + file_retention 30 + job_retention 40 + end +end diff --git a/spec/factories/job.rb b/spec/factories/job.rb new file mode 100644 index 0000000..cd82b42 --- /dev/null +++ b/spec/factories/job.rb @@ -0,0 +1,14 @@ +FactoryGirl.define do + factory :job do + sequence(:Job) { |i| "Job #{i}" } + sequence(:Name) { |i| "Job name #{i}" } + type 'B' + level 'F' + job_status 'f' + client + end + + trait :running do + job_status 'R' + end +end diff --git a/spec/routing/host_routing_spec.rb b/spec/routing/host_routing_spec.rb index eee61ba..34e2f29 100644 --- a/spec/routing/host_routing_spec.rb +++ b/spec/routing/host_routing_spec.rb @@ -1,11 +1,27 @@ require 'spec_helper' describe HostsController do it 'routes GET /hosts/new' do - expect(get('/hosts/new')).to route_to( { controller: 'hosts', action: 'new' }) + expect(get('/hosts/new')).to route_to(controller: 'hosts', action: 'new') end it 'routes POST /hosts' do - expect(post('/hosts')).to route_to( { controller: 'hosts', action: 'create' }) + expect(post('/hosts')).to route_to(controller: 'hosts', action: 'create') + end + + it 'routes GET /hosts/1' do + expect(get('/hosts/1')).to route_to(controller: 'hosts', action: 'show', id: '1') + end + + it 'routes GET /hosts/1/edit' do + expect(get('/hosts/1/edit')).to route_to(controller: 'hosts', action: 'edit', id: '1') + end + + it 'routes PUT /hosts/1' do + expect(put('/hosts/1')).to route_to(controller: 'hosts', action: 'update', id: '1') + end + + it 'routes DELETE /hosts/1' do + expect(delete('/hosts/1')).to route_to(controller: 'hosts', action: 'destroy', id: '1') end end