Project

General

Profile

Download (4.74 KB) Statistics
| Branch: | Tag: | Revision:
87c40d2e Ohad Levy
require 'test_helper'

class ReportTest < ActiveSupport::TestCase
ff1cc6b1 Ohad Levy
def setup
e07f9a12 Dominic Cleal
User.current = users :admin
9b41cf08 Ohad Levy
end

ab866a31 Ori Rabin
test "should expire reports created 1 week ago" do
bd058e67 Ori Rabin
report_count = 3
ab866a31 Ori Rabin
Message.delete_all
Source.delete_all
8c6bc83e Marek Hulan
FactoryBot.create_list(:report, report_count, :with_logs)
FactoryBot.create_list(:report, report_count, :with_logs, :old_report)
9d43fc71 Michael Moll
assert_equal report_count * 2, Report.count
assert_difference('Report.count', -1 * report_count) do
assert_difference(['Log.count', 'Message.count', 'Source.count'], -1 * report_count * 5) do
ab866a31 Ori Rabin
Report.expire
end
end
end
54592c2f Dominic Cleal
describe '.my_reports' do
setup do
8c6bc83e Marek Hulan
@target_host = FactoryBot.create(:host, :with_hostgroup, :with_reports, :report_count => 2)
46edaeb9 Dominic Cleal
@target_reports = @target_host.reports
8c6bc83e Marek Hulan
@other_host = FactoryBot.create(:host, :with_hostgroup, :with_reports, :report_count => 2)
46edaeb9 Dominic Cleal
@other_reports = @other_host.reports
54592c2f Dominic Cleal
end

test 'returns all reports for admin' do
as_admin do
assert_empty (@target_reports + @other_reports).map(&:id) - Report.my_reports.map(&:id)
end
end

test 'returns visible reports for unlimited user' do
8c6bc83e Marek Hulan
user_role = FactoryBot.create(:user_user_role)
FactoryBot.create(:filter, :role => user_role.role, :permissions => Permission.where(:name => 'view_hosts'), :unlimited => true)
54592c2f Dominic Cleal
collection = as_user(user_role.owner) { Report.my_reports }
assert_empty (@target_reports + @other_reports).map(&:id) - collection.map(&:id)
end

test 'returns visible reports for filtered user' do
5f606e11 Daniel Lobato Garcia
setup_user('view', 'hosts',
"hostgroup_id = #{@target_host.hostgroup_id}")
assert_equal @target_reports.map(&:id).sort, Report.my_reports.map(&:id).sort
54592c2f Dominic Cleal
end
9926db42 Dominic Cleal
test "only return reports from host in user's taxonomies" do
8c6bc83e Marek Hulan
user_role = FactoryBot.create(:user_user_role)
FactoryBot.create(:filter, :role => user_role.role, :permissions => Permission.where(:name => 'view_hosts'), :search => "hostgroup_id = #{@target_host.hostgroup_id}")
9926db42 Dominic Cleal
8c6bc83e Marek Hulan
orgs = FactoryBot.create_pair(:organization)
locs = FactoryBot.create_pair(:location)
9ae8fa19 Michael Moll
@target_host.update(:location => locs.last, :organization => orgs.last)
@target_host.hostgroup.update(:locations => [locs.last], :organizations => [orgs.last])
9926db42 Dominic Cleal
9ae8fa19 Michael Moll
user_role.owner.update(:locations => [locs.first], :organizations => [orgs.first])
9926db42 Dominic Cleal
as_user user_role.owner do
assert_equal [], Report.my_reports.map(&:id).sort
end

9ae8fa19 Michael Moll
user_role.owner.update(:locations => [locs.last], :organizations => [orgs.last])
9926db42 Dominic Cleal
as_user user_role.owner do
assert_equal @target_reports.map(&:id).sort, Report.my_reports.map(&:id).sort
end
end

test "only return reports from host in admin's currently selected taxonomy" do
8c6bc83e Marek Hulan
user = FactoryBot.create(:user, :admin)
orgs = FactoryBot.create_pair(:organization)
locs = FactoryBot.create_pair(:location)
9ae8fa19 Michael Moll
@target_host.update(:location => locs.last, :organization => orgs.last)
9926db42 Dominic Cleal
as_user user do
in_taxonomy(orgs.first) do
in_taxonomy(locs.first) do
refute_includes Report.my_reports, @target_reports.first
end
end

in_taxonomy(orgs.last) do
in_taxonomy(locs.last) do
assert_includes Report.my_reports, @target_reports.first
end
end
end
end
54592c2f Dominic Cleal
end
e8d6d2d6 Shlomi Zadok
describe 'Report STI' do
test "Report has default type" do
report = Report.new
assert_equal('ConfigReport', report.type)
end

test '.expire should delete only the class which calls it' do
8c6bc83e Marek Hulan
FactoryBot.create_list(:config_report, 5, :old_report)
FactoryBot.create_list(:report, 5, :old_report, :type => 'TestReport')
e8d6d2d6 Shlomi Zadok
TestReport.expire
refute(TestReport.all.any?)
assert(ConfigReport.all.any?)
end

test '#metrics with metrics should return empty hash' do
ea98080f Dominic Cleal
report = ConfigReport.import read_json_fixture('reports/empty.json')
e8d6d2d6 Shlomi Zadok
assert_equal({}, report.metrics)
ea98080f Dominic Cleal
report = ConfigReport.import read_json_fixture('reports/no-logs.json')
e8d6d2d6 Shlomi Zadok
refute_equal({}, report.metrics)
9d43fc71 Michael Moll
assert_equal({'success' => 1, 'total' => 1, 'failure' => 0}, report.metrics['events'])
e8d6d2d6 Shlomi Zadok
report = TestReport.new
assert_equal({}, report.metrics)
end
640d7ee7 Justin Sherrill
test 'can view host reports as non-admin user' do
8c6bc83e Marek Hulan
report = FactoryBot.create(:config_report)
640d7ee7 Justin Sherrill
setup_user('view', 'hosts', "name = #{report.host.name}")
f4459c11 David Davis
setup_user('view', 'config_reports')
640d7ee7 Justin Sherrill
assert_includes ConfigReport.authorized('view_config_reports').my_reports, report
end
21466223 Shlomi Zadok
test 'Inherited children can search' do
5c835ff7 Dominic Cleal
assert_nothing_raised do
21466223 Shlomi Zadok
TestReport.search_for('blah')
end
end
e8d6d2d6 Shlomi Zadok
end
87c40d2e Ohad Levy
end
e8d6d2d6 Shlomi Zadok
72a0eeab Michael Moll
class TestReport < Report
end