fix ticketing system error on bootstrap of new install
[freeside.git] / rt / t / security / CVE-2011-2084-modifyscrips-templates.t
1 use strict;
2 use warnings;
3
4 use RT::Test tests => undef;
5
6 sub set_fails {
7     my $col  = shift;
8     my $obj  = shift;
9     my $to   = ref $_[0] ? +shift->Id : shift;
10     my $from = $obj->$col;
11     my $meth = "Set$col";
12
13     my ($ok, $msg) = $obj->$meth($to);
14     ok !$ok, "$meth denied: $msg";
15     is $obj->$col, $from, "$col left alone";
16 }
17
18 sub set_ok {
19     my $col  = shift;
20     my $obj  = shift;
21     my $to   = ref $_[0] ? +shift->Id : shift;
22     my $from = $obj->$col;
23     my $meth = "Set$col";
24
25     my ($ok, $msg) = $obj->$meth($to);
26     ok $ok, "$meth allowed: $msg";
27     is $obj->$col, $to, "$col updated";
28 }
29
30 my $qa = RT::Test->load_or_create_queue( Name => 'Queue A' );
31 my $qb = RT::Test->load_or_create_queue( Name => 'Queue B' );
32 ok $qa->id, "created Queue A";
33 ok $qb->id, "created Queue B";
34
35 my $user = RT::Test->load_or_create_user( Name => 'testuser' );
36 my $cu   = RT::CurrentUser->new( $user );
37 ok $user->id, "created testuser";
38
39 diag "ModifyScrips";
40 {
41     my $scrip = RT::Scrip->new( RT->SystemUser );
42     my ($scrip_id, $msg) = $scrip->Create(
43         Description     => 'Testing',
44         Queue           => $qa->Id,
45         ScripCondition  => 'User Defined',
46         ScripAction     => 'User Defined',
47         Template        => 'Blank',
48         CustomIsApplicableCode  => 'if ($self->TicketObj->Subject =~ /fire/) { return (1);} else { return(0)}',
49         CustomPrepareCode       => '1;',
50         CustomCommitCode        => 'warn "scrip fired!";',
51     );
52     ok $scrip_id, $msg;
53
54     RT::Test->set_rights(
55         { Principal => $user, Right => 'ShowScrips' },
56         { Principal => $user, Right => 'ModifyScrips', Object => $qa },
57     );
58
59     $scrip = RT::Scrip->new( $cu );
60     $scrip->Load( $scrip_id );
61     ok $scrip->id, "loaded scrip as test user";
62     ok $scrip->IsAdded( $qa->Id ), 'queue is A';
63
64     ok +($scrip->SetName('Testing ModifyScrips'));
65
66     for my $value ($qb->id, 0, undef, '') {
67         my ($ok, $why) = $scrip->AddToObject( $value );
68         my $disp = (defined($value) ? "'$value'" : "undef");
69         ok( !$ok, "Correctly not added to $disp: $why" );
70     }
71
72     RT::Test->add_rights( Principal => $user, Right => 'ModifyScrips', Object => $qb );
73
74     for my $value ($qb->id, 0, undef, '') {
75         my ($ok, $why) = $scrip->AddToObject( $value );
76         my $disp = (defined($value) ? "'$value'" : "undef");
77         if ($value) {
78             ok( $ok, "Correctly added to $disp: $why" );
79         } else {
80             ok( !$ok, "Correctly not added to $disp: $why" );
81         }
82     }
83
84     RT::Test->add_rights( Principal => $user, Right => 'ModifyScrips' );
85
86     my ($ok, $why) = $scrip->AddToObject( 0 );
87     ok( $ok, "Correctly added globally: $why" );
88
89     set_fails( Template => $scrip => "Autoreply" );
90
91     RT::Test->add_rights( Principal => $user, Right => 'ShowTemplate' );
92
93     set_ok( Template => $scrip => "Autoreply" );
94     is $scrip->Template, 'Autoreply', 'template name is right';
95 }
96
97 diag "ModifyTemplate";
98 {
99     RT::Test->set_rights(
100         { Principal => $user, Right => 'ShowTemplate' },
101         { Principal => $user, Right => 'ModifyTemplate', Object => $qa },
102     );
103
104     my $template = RT::Template->new( RT->SystemUser );
105     my ($id, $msg) = $template->Create(
106         Queue   => $qa->Id,
107         Name    => 'Testing',
108         Type    => 'Perl',
109         Content => "\n\nThis is a test template.\n",
110     );
111     ok $id, $msg;
112
113     $template = RT::Template->new( $cu );
114     $template->Load( $id );
115     ok $template->id, "loaded template as test user";
116     is $template->Queue, $qa->Id, 'queue is A';
117
118     ok +($template->SetName('Testing ModifyTemplate'));
119
120     set_fails( Queue => $template => $qb );
121     set_fails( Queue => $template => 0 );
122
123     RT::Test->add_rights( Principal => $user, Right => 'ModifyTemplate', Object => $qb );
124
125     set_fails( Queue => $template => $qb );
126     set_fails( Queue => $template => 0 );
127
128     RT::Test->add_rights( Principal => $user, Right => 'ModifyTemplate' );
129
130     set_fails( Queue => $template => 0 );
131 }
132
133 done_testing;