diff --git a/app/models/host_ansible_role.rb b/app/models/host_ansible_role.rb index f902de79..f784477c 100644 --- a/app/models/host_ansible_role.rb +++ b/app/models/host_ansible_role.rb @@ -2,10 +2,16 @@ # Join model that hosts the connection between hosts and ansible_roles class HostAnsibleRole < ApplicationRecord + audited associated_with: :host + belongs_to_host belongs_to :ansible_role acts_as_list scope: :host validates :ansible_role_id, :presence => true, :uniqueness => { :scope => :host_id } + + def to_label + ansible_role.name + end end diff --git a/test/unit/host_ansible_role_test.rb b/test/unit/host_ansible_role_test.rb index 5b2211ba..f3773dce 100644 --- a/test/unit/host_ansible_role_test.rb +++ b/test/unit/host_ansible_role_test.rb @@ -17,4 +17,34 @@ class HostAnsibleRoleTest < ActiveSupport::TestCase end should validate_uniqueness_of(:ansible_role_id).scoped_to(:host_id) end + + describe 'auditing' do + setup do + @host = FactoryBot.create(:host) + @ansible_role = FactoryBot.create(:ansible_role) + @host_ansible_role = FactoryBot.create(:host_ansible_role, :with_auditing, host_id: @host.id, ansible_role_id: @ansible_role.id) + @audits = @host_ansible_role.audits + end + + test 'should audit creation of a host ansible role' do + assert_equal 1, @audits.size + assert_associated_ids + assert_equal 'create', @audits.last.action + end + + test 'should audit deletion of a host ansible role' do + @audits.clear + @host_ansible_role.destroy + + assert_equal 1, @audits.size + assert_associated_ids + assert_equal 'destroy', @audits.last.action + end + + def assert_associated_ids + audit = @audits.last + assert_equal @ansible_role.id, audit.audited_changes['ansible_role_id'] + assert_equal @host.id, audit.audited_changes['host_id'] + end + end end