diff --git a/db/schema.rb b/db/schema.rb index 27384bb..d6d5c2d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,141 +1,141 @@ # encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. # # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application database on another # system, you should be using db:schema:load, not running all the migrations # from scratch. The latter is a flawed and unsustainable approach (the more migrations # you'll amass, the slower it'll run and the greater likelihood for issues). # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160427195249) do +ActiveRecord::Schema.define(version: 20160912175151) do create_table "configuration_settings", force: true do |t| t.string "job", default: "{}" t.string "client", default: "{}" t.datetime "created_at" t.datetime "updated_at" t.string "pool", default: "{}" end create_table "faqs", force: true do |t| t.string "title" t.text "body" t.integer "priority", default: 0 t.datetime "created_at" t.datetime "updated_at" end create_table "filesets", force: true do |t| t.string "name" t.integer "host_id" t.text "exclude_directions" t.text "include_directions" t.datetime "created_at" t.datetime "updated_at" end add_index "filesets", ["host_id"], name: "index_filesets_on_host_id", using: :btree create_table "hosts", force: true do |t| - t.binary "name", limit: 255, null: false - t.binary "fqdn", limit: 255, null: false - t.integer "port", null: false - t.integer "file_retention", null: false - t.integer "job_retention", null: false + t.binary "name", limit: 255, null: false + t.binary "fqdn", limit: 255, null: false + t.integer "port", null: false + t.integer "file_retention", null: false + t.integer "job_retention", null: false t.datetime "created_at" t.datetime "updated_at" t.string "password" - t.boolean "baculized", default: false, null: false + t.boolean "baculized", default: false, null: false t.datetime "baculized_at" t.integer "status", limit: 1, default: 0 t.integer "client_id" t.boolean "verified", default: false t.datetime "verified_at" t.integer "verifier_id" t.string "job_retention_period_type" t.string "file_retention_period_type" t.integer "origin", limit: 1 t.string "email_recipients", default: "[]" t.integer "quota", limit: 8, default: 104857600 end add_index "hosts", ["name"], name: "index_hosts_on_name", unique: true, length: {"name"=>128}, using: :btree create_table "invitations", force: true do |t| t.integer "user_id" t.integer "host_id" t.string "verification_code" t.datetime "created_at" t.datetime "updated_at" end add_index "invitations", ["user_id", "verification_code"], name: "index_invitations_on_user_id_and_verification_code", using: :btree create_table "job_templates", force: true do |t| t.string "name", null: false t.integer "job_type", limit: 1 t.integer "host_id" t.integer "fileset_id" t.integer "schedule_id" t.datetime "created_at" t.datetime "updated_at" - t.boolean "enabled", default: false + t.boolean "enabled", default: true t.binary "restore_location" t.boolean "baculized", default: false t.datetime "baculized_at" t.string "client_before_run_file" t.string "client_after_run_file" end create_table "ownerships", force: true do |t| t.integer "user_id" t.integer "host_id" t.datetime "created_at" t.datetime "updated_at" end create_table "schedule_runs", force: true do |t| t.integer "schedule_id" t.integer "level", limit: 1 t.string "month" t.string "day" t.string "time" t.datetime "created_at" t.datetime "updated_at" end add_index "schedule_runs", ["schedule_id"], name: "index_schedule_runs_on_schedule_id", using: :btree create_table "schedules", force: true do |t| t.string "name" t.string "runs" t.integer "host_id" end add_index "schedules", ["host_id"], name: "index_schedules_on_host_id", using: :btree create_table "users", force: true do |t| t.string "username", null: false t.string "email" t.integer "user_type", limit: 1, null: false t.boolean "enabled", default: false t.datetime "created_at" t.datetime "updated_at" t.string "identifier" t.string "password_hash" t.datetime "login_at" t.datetime "hosts_updated_at" t.string "temp_hosts", default: "[]" t.string "token" t.boolean "moderator", default: false end add_index "users", ["identifier"], name: "index_users_on_identifier", using: :btree add_index "users", ["password_hash"], name: "index_users_on_password_hash", using: :btree add_index "users", ["token"], name: "index_arch.users_on_token", using: :btree end diff --git a/spec/factories/host.rb b/spec/factories/host.rb index 06ce0c7..3815920 100644 --- a/spec/factories/host.rb +++ b/spec/factories/host.rb @@ -1,37 +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 } + job_templates { create_list :job_template, 1 } verified true end trait :updated do status Host::STATUSES[:updated] - job_templates { create_list :enabled_job_template, 1 } + job_templates { create_list :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 } + job_templates { create_list :job_template, 1 } end end end diff --git a/spec/factories/job_template.rb b/spec/factories/job_template.rb index 21294c2..a7de946 100644 --- a/spec/factories/job_template.rb +++ b/spec/factories/job_template.rb @@ -1,24 +1,19 @@ FactoryGirl.define do factory :jobtemplate, class: JobTemplate do host fileset sequence(:name) { |n| "Job #{n}" } trait :backup do job_type :backup schedule end trait :restore do job_type :restore restore_location '/tmp' end - trait :enabled do - enabled true - end - factory :job_template, traits: [:backup] - factory :enabled_job_template, traits: [:backup, :enabled] end end diff --git a/spec/models/job_template_spec.rb b/spec/models/job_template_spec.rb index d2243ea..4f67909 100644 --- a/spec/models/job_template_spec.rb +++ b/spec/models/job_template_spec.rb @@ -1,158 +1,166 @@ require 'spec_helper' describe JobTemplate do context 'validates' do it 'name must be present' do expect(JobTemplate.new).to have(2).errors_on(:name) end it 'name must be unique on host\'s scope' do job_1 = FactoryGirl.create(:job_template, name: 'a name') job_2 = FactoryGirl.build(:job_template, name: 'a name') job_3 = FactoryGirl.build(:job_template, name: 'a name', host: job_1.host) expect(job_2).to be_valid expect(job_3).to_not be_valid end it 'fileset_id must be present' do expect(JobTemplate.new).to have(1).errors_on(:fileset_id) end it 'schedule_id must be present' do expect(JobTemplate.new).to have(1).errors_on(:schedule_id) end it 'schedule_id must NOT be present for :restore jobs' do expect(JobTemplate.new(job_type: :restore)).to have(0).errors_on(:schedule_id) end end # automatic assignments context 'when no job_type is given' do let(:job_template) { FactoryGirl.create(:job_template) } it 'sets the job_type to :backup' do expect(job_template).to be_backup end end + context 'when enabled is not given' do + let(:job_template) { FactoryGirl.create(:job_template) } + + it 'sets the enabled field to true' do + expect(job_template).to be_enabled + end + end + context 'when enabling a job' do [:pending, :dispatched].each do |status| context "of a #{status} host" do let(:host) { FactoryGirl.create(:host) } let!(:job) { FactoryGirl.create(:job_template, host: host) } before { host.update_column(:status, Host::STATUSES[status]) } it 'becomes configured' do expect { job.enabled = true job.save }.to change { host.reload.human_status_name }.from(status.to_s).to('configured') end end end context 'of a configured host' do let(:host) { FactoryGirl.create(:host) } let!(:job) { FactoryGirl.create(:job_template, host: host) } before { host.update_column(:status, Host::STATUSES[:configured]) } it 'stays configured' do expect { job.enabled = true job.save }.to_not change { host.reload.human_status_name } end end context 'of a updated host' do let(:host) { FactoryGirl.create(:host) } let!(:job) { FactoryGirl.create(:job_template, host: host) } before { host.update_column(:status, Host::STATUSES[:updated]) } it 'stays updated' do expect { job.enabled = true job.save }.to_not change { host.reload.human_status_name } end end [:deployed, :redispatched].each do |status| context "of a #{status} host" do let(:host) { FactoryGirl.create(:host) } let!(:job) { FactoryGirl.create(:job_template, host: host) } before { host.update_column(:status, Host::STATUSES[status]) } it 'becomes updated' do expect { job.enabled = true job.save }.to change { host.reload.human_status_name }.from(status.to_s).to('updated') end end end end describe '#to_bacula_config_array' do let(:job_template) do FactoryGirl.create(:job_template, client_before_run_file: 'test', client_after_run_file: 'test2') end subject { job_template.to_bacula_config_array } it 'has a Job structure' do expect(subject.first).to eq('Job {') expect(subject.last).to eq('}') end ConfigurationSetting.current_job_settings.each do |k, v| it "assigns #{k.capitalize} param" do expect(subject).to include(" #{k.capitalize} = #{v}") end end it 'assigns Enabled param' do expect(subject).to include(" Enabled = #{job_template.enabled_human}") end it 'assigns Name param prefixed with the host\'s name' do expect(subject).to include(" Name = \"#{job_template.name_for_config}\"") end it 'assigns FileSet param' do expect(subject).to include(" FileSet = \"#{job_template.fileset.name_for_config}\"") end it 'assigns Client param' do expect(subject).to include(" Client = \"#{job_template.host.name}\"") end it 'assigns Type param' do expect(subject).to include(" Type = \"#{job_template.job_type.capitalize}\"") end it 'assigns Schedule param' do expect(subject).to include(" Schedule = \"#{job_template.schedule.name_for_config}\"") end it 'assigns Client Run After Job param' do expect(subject).to include(" Client Run After Job = \"#{job_template.client_after_run_file}\"") end it 'assigns Client Run After Job param' do expect(subject).to include(" Client Run Before Job = \"#{job_template.client_before_run_file}\"") end end end