RT 4.0.22
[freeside.git] / rt / t / security / CVE-2011-2084-cf-values.t
1 use strict;
2 use warnings;
3
4 use RT::Test tests => undef;
5 use JSON qw(decode_json);
6
7 my ($base, $m) = RT::Test->started_ok;
8
9 my $cf1 = RT::Test->load_or_create_custom_field(
10     Name            => 'cf1',
11     Type            => 'Select',
12     MaxValues       => 1,
13     Queue           => 0,
14 );
15 ok $cf1->id, "created cf1";
16
17 my $cf2 = RT::Test->load_or_create_custom_field(
18     Name            => 'cf2',
19     Type            => 'Select',
20     MaxValues       => 1,
21     Queue           => 0,
22 );
23 ok $cf2->id, "created cf2";
24
25 ok( $cf1->AddValue( Name => "cf1 value $_" ) ) for qw(a b c);
26 ok( $cf2->AddValue( Name => "cf2 value $_" ) ) for qw(x y z);
27
28 sub ac {
29     my (%args) = (
30         CF          => $cf1->id,
31         Term        => "%",
32         Context     => undef,
33         ContextId   => undef,
34         ContextType => undef,
35         @_
36     );
37     $args{term} = delete $args{Term};
38
39     if (my $obj = delete $args{Context}) {
40         $args{ContextId}   = $obj->Id  unless defined $args{ContextId};
41         $args{ContextType} = ref($obj) unless defined $args{ContextType};
42     }
43
44     $args{"Object---CustomField-$args{CF}-Values"} = "";
45     delete $args{CF};
46
47     delete $args{$_} for grep {not defined $args{$_}} keys %args;
48
49     my $URI = URI->new("$base/Helpers/Autocomplete/CustomFieldValues");
50     $URI->query_form( %args );
51     $m->get_ok($URI, "GET to autocompleter");
52     return decode_json($m->content);
53 }
54
55 $m->login;
56 is_deeply ac(CF => 12345, ContextId => 1, ContextType => "RT::Queue"),
57     [], 'nothing for invalid CF';
58
59 is_deeply ac(),
60     [], "Nothing without a context id";
61 is_deeply ac( ContextId => 12345, ContextType => "RT::Queue"),
62     [], "Nothing with invalid contextid id";
63 is_deeply ac( ContextId => 12, ContextType => "RT::User"),
64     [], "Nothing with invalid contextid type";
65
66
67
68 my $user = RT::Test->load_or_create_user(
69     Name        => 'user',
70     Password    => 'password',
71     Privileged  => 1,
72 );
73 my $queue = RT::Test->load_or_create_queue( Name => 'CF Test' );
74 ok $queue->id, 'found or created queue';
75 my $ticket = RT::Test->create_ticket(
76     Queue => $queue->id,
77     Subject => "CF application",
78 );
79 ok $queue->id, 'created ticket';
80
81 $m->logout;
82 $m->login('user','password');
83
84 is_deeply ac( Context => $queue ), [], 'queue context, no permissions, no result';
85 is_deeply ac( Context => $ticket ), [], 'ticket context, no permissions, no result';
86
87 ok( RT::Test->set_rights(
88     { Principal => $user, Right => [qw(SeeCustomField)], Object => $queue },
89 ), 'add queue level CF viewing rights');
90
91 my $cfvalues = [ ( map { { value => "cf1 value $_" , label => "cf1 value $_" } } qw(a b c) ) ];
92 is_deeply ac( Context => $queue ), $cfvalues, 'queue context, with permissions get result';
93 is_deeply ac( Context => $ticket ), $cfvalues, 'ticket context, with permissions get result';
94
95 {
96     diag "Switching to non-global CFs";
97     my $globalq = RT::Queue->new( RT->SystemUser );
98     my ($status, $msg) = $cf1->RemoveFromObject( $globalq );
99     ok($status, "Removed CF1 globally: $msg");
100     ($status, $msg) = $cf1->AddToObject( $queue );
101     ok($status, "Added CF1 to queue @{[$queue->id]}: $msg");
102     ($status, $msg) = $cf2->RemoveFromObject( $globalq );
103     ok($status, "Removed CF2 globally: $msg");
104 }
105
106 is_deeply ac( CF => $cf2->id, Context => $queue ), [], 'queue context, but not applied, get no result';
107 is_deeply ac( CF => $cf2->id, Context => $ticket ), [], 'ticket context, but not applied, get no result';
108
109 is_deeply ac( Context => $queue ), $cfvalues, 'queue context, applied correctly, get result';
110 is_deeply ac( Context => $ticket ), $cfvalues, 'ticket context, applied correctly, get result';
111
112
113
114 diag "Ticket-level rights";
115
116 ok( RT::Test->set_rights(
117     { Principal => "Owner", Right => [qw(SeeCustomField)], Object => $queue },
118     { Principal => $user,   Right => [qw(OwnTicket SeeTicket)], Object => RT->System },
119 ), 'add owner level CF viewing rights');
120
121 is_deeply ac( Context => $queue ), [], 'queue context, but not owner';
122 is_deeply ac( Context => $ticket ), [], 'ticket context, but not owner';
123
124 my ($status, $msg) = $ticket->SetOwner( $user->id );
125 ok( $status, "Set owner to user: $msg" );
126
127 is_deeply ac( Context => $queue ), [], 'queue context is not enough';
128 is_deeply ac( Context => $ticket ), $cfvalues, 'ticket context, get values';
129
130
131 undef $m;
132 done_testing;