summaryrefslogtreecommitdiffstats
path: root/t/security-risk.t
blob: cd6ce1ff360749d2bb1c88a6eb9e4e23ddef2678 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/perl
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.
use strict;
use warnings;
use 5.10.1;
use lib qw( . lib local/lib/perl5 );
use Bugzilla;

BEGIN { Bugzilla->extensions }

use Test::More;
use Test2::Tools::Mock;
use Try::Tiny;

use ok 'Bugzilla::Report::SecurityRisk';
can_ok('Bugzilla::Report::SecurityRisk', qw(new results));

sub check_open_state_mock {
  my ($state) = @_;
  return grep {/^$state$/} qw(UNCOMFIRMED NEW ASSIGNED REOPENED);
}

try {
  use Bugzilla::Report::SecurityRisk;
  my $report = Bugzilla::Report::SecurityRisk->new(
    start_date => DateTime->new(year => 2000, month => 1, day => 9),
    end_date   => DateTime->new(year => 2000, month => 1, day => 16),
    products         => ['Firefox',      'Core'],
    sec_keywords     => ['sec-critical', 'sec-high'],
    check_open_state => \&check_open_state_mock,
    initial_bug_ids  => [1,              2, 3, 4],
    initial_bugs => {
      1 => {
        id         => 1,
        product    => 'Firefox',
        sec_level  => 'sec-high',
        is_open    => 0,
        created_at => DateTime->new(year => 2000, month => 1, day => 1),
      },
      2 => {
        id         => 2,
        product    => 'Core',
        sec_level  => 'sec-critical',
        is_open    => 0,
        created_at => DateTime->new(year => 2000, month => 1, day => 1),
      },
      3 => {
        id         => 3,
        product    => 'Core',
        sec_level  => 'sec-high',
        is_open    => 1,
        created_at => DateTime->new(year => 2000, month => 1, day => 5),
      },
      4 => {
        id         => 4,
        product    => 'Firefox',
        sec_level  => 'sec-critical',
        is_open    => 1,
        created_at => DateTime->new(year => 2000, month => 1, day => 10),
      },
    },
    events => [

      # Canned event's should be in reverse chronological order.
      {
        bug_id     => 2,
        bug_when   => DateTime->new(year => 2000, month => 1, day => 14),
        field_name => 'keywords',
        removed    => '',
        added      => 'sec-critical',

      },
      {
        bug_id     => 1,
        bug_when   => DateTime->new(year => 2000, month => 1, day => 12),
        field_name => 'bug_status',
        removed    => 'ASSIGNED',
        added      => 'RESOLVED',
      },
    ],
  );
  my $actual_results   = $report->results;
  my $expected_results = [
    {
      date            => DateTime->new(year => 2000, month => 1, day => 9),
      bugs_by_product => {
        'Firefox' => {

          # Rewind the event that caused 1 to close.
          open            => [1],
          closed          => [],
          median_age_open => 8
        },
        'Core' => {

          # 2 wasn't a sec-critical bug on the report date.
          open            => [3],
          closed          => [],
          median_age_open => 4
        }
      },
      bugs_by_sec_keyword => {
        'sec-critical' => {

          # 2 wasn't a sec-crtical bug and 4 wasn't created yet on the report date.
          open            => [],
          closed          => [],
          median_age_open => 0
        },
        'sec-high' => {

          # Rewind the event that caused 1 to close.
          open            => [1, 3],
          closed          => [],
          median_age_open => 6
        }
      },
    },
    {    # The report on 2000-01-16 matches the state of initial_bugs.
      date            => DateTime->new(year => 2000, month => 1, day => 16),
      bugs_by_product => {
        'Firefox' => {open => [4], closed => [1], median_age_open => 6},
        'Core'    => {open => [3], closed => [2], median_age_open => 11}
      },
      bugs_by_sec_keyword => {
        'sec-critical' => {open => [4], closed => [2], median_age_open => 6},
        'sec-high'     => {open => [3], closed => [1], median_age_open => 11}
      },
    },
  ];

  is_deeply($actual_results, $expected_results, 'Report results are accurate');

}
catch {
  fail('got an exception during main part of test');
  diag($_);
};

done_testing;