diff --git a/app/controllers/clients_controller.rb b/app/controllers/clients_controller.rb index dee65a7..96699fd 100644 --- a/app/controllers/clients_controller.rb +++ b/app/controllers/clients_controller.rb @@ -1,19 +1,20 @@ 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 + client_ids = Client.for_user(current_user.id).pluck(:ClientId) + @clients = Client.where(ClientId: client_ids).includes(:jobs) + @active_jobs = Job.running.where(ClientId: client_ids).group(:ClientId).count + @hosts = current_user.hosts.not_baculized end # GET /clients/1 def show; end private def set_client - @client = Client.find(params[:id]) + @client = Client.for_user(current_user.id).find(params[:id]) end end diff --git a/spec/controllers/clients_controller_spec.rb b/spec/controllers/clients_controller_spec.rb deleted file mode 100644 index edde293..0000000 --- a/spec/controllers/clients_controller_spec.rb +++ /dev/null @@ -1,40 +0,0 @@ -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/factories/host.rb b/spec/factories/host.rb index cd9e332..06ce0c7 100644 --- a/spec/factories/host.rb +++ b/spec/factories/host.rb @@ -1,31 +1,37 @@ FactoryGirl.define do factory :host do sequence(:fqdn) {|n| "lala.#{n}-#{rand(1000)}.gr" } password 'a strong password' port 1234 file_retention 1 job_retention 2 baculized false + trait :with_client do + after(:create) do |host| + create(:client, name: host.name) + end + end + trait :configured do status Host::STATUSES[:configured] job_templates { create_list :enabled_job_template, 1 } verified true end trait :updated do status Host::STATUSES[:updated] job_templates { create_list :enabled_job_template, 1 } verified true end trait :with_disabled_jobs do job_templates { create_list(:job_template, 1) } verified true end trait :with_enabled_jobs do job_templates { create_list :enabled_job_template, 1 } end end end diff --git a/spec/factories/user.rb b/spec/factories/user.rb new file mode 100644 index 0000000..ec67985 --- /dev/null +++ b/spec/factories/user.rb @@ -0,0 +1,6 @@ +FactoryGirl.define do + factory :user do + sequence(:username) { |n| "user-#{n}" } + user_type 0 + end +end diff --git a/spec/requests/clients_spec.rb b/spec/requests/clients_spec.rb new file mode 100644 index 0000000..e204114 --- /dev/null +++ b/spec/requests/clients_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe ClientsController do + let(:host) { FactoryGirl.create(:host, :with_client) } + let(:user) { FactoryGirl.create(:user) } + + before do + allow_any_instance_of(ClientsController).to receive(:current_user) { user } + host.users << user + end + + describe '#index' do + it 'fetches the host' do + get root_path + expect(response.body).to match(host.name) + end + end +end