1use strict;
2use warnings;
3use RT::Test tests => undef;
4
5# make an initial queue, so we have more than 1
6my $original_test_queue = new_queue("Test$$");
7
8my ($baseurl, $m) = RT::Test->started_ok;
9ok $m->login, 'logged in';
10
11diag("Check for 2 existing queues being visible");
12{
13    check_queues($m);
14}
15
16diag("Add a new queue, which won't show up until we fix the cache");
17{
18    new_queue("New Test $$");
19    check_queues($m);
20}
21
22diag("Disable an existing queue, it should stop appearing in the list");
23{
24    ok($original_test_queue->SetDisabled(1));
25    check_queues($m);
26}
27
28diag("Bring back a disabled queue");
29{
30    ok($original_test_queue->SetDisabled(0));
31    check_queues($m);
32}
33
34diag("Rename the original queue, make sure the name change is uncached");
35{
36    ok($original_test_queue->SetName("Name Change $$"));
37    check_queues($m);
38}
39
40diag("Test a user who has more limited rights Queues");
41{
42
43my $user_a = RT::Test->load_or_create_user(
44    Name => 'user_a', Password => 'password',
45);
46ok $user_a && $user_a->id, 'loaded or created user';
47
48ok( RT::Test->set_rights(
49        { Principal => $user_a, Right => [qw(SeeQueue CreateTicket)], Object => $original_test_queue },
50), 'Allow user a to see the testing queue');
51
52my $a_m = RT::Test::Web->new;
53ok $a_m->login('user_a', 'password'), 'logged in as user A';
54
55# check that they see a single queue
56check_queues($a_m,[$original_test_queue->Id],[$original_test_queue->Name]);
57
58ok( RT::Test->add_rights(
59        { Principal => $user_a, Right => [qw(SeeQueue CreateTicket)] },
60), 'add global queue viewing rights');
61
62check_queues($a_m);
63
64}
65
66diag( "Test a user who has only CreateTicket right" );
67{
68    my $user_b = RT::Test->load_or_create_user(
69        Name     => 'user_b',
70        Password => 'password',
71    );
72    ok $user_b && $user_b->id, 'loaded or created user';
73
74    ok(
75        RT::Test->add_rights(
76            { Principal => $user_b, Right => [qw(CreateTicket)] },
77        ),
78        'add global queue CreateTicket right'
79    );
80
81    my $b_m = RT::Test::Web->new;
82    ok $b_m->login( 'user_b', 'password' ), 'logged in as user B';
83    check_queues( $b_m, [], [] );
84}
85
86diag( "Test a user who has only SeeQueue right" );
87{
88    my $user_c = RT::Test->load_or_create_user(
89        Name     => 'user_c',
90        Password => 'password',
91    );
92    ok $user_c && $user_c->id, 'loaded or created user';
93
94    ok(
95        RT::Test->add_rights(
96            { Principal => $user_c, Right => [qw(SeeQueue)] },
97        ),
98        'add global queue SeeQueue right'
99    );
100
101    my $c_m = RT::Test::Web->new;
102    ok $c_m->login( 'user_c', 'password' ), 'logged in as user C';
103
104    check_queues( $c_m, [], [] );
105}
106
107diag( "Test a user starting with ShowTicket and ModifyTicket rights" );
108{
109    my $user_d = RT::Test->load_or_create_user(
110        Name     => 'user_d',
111        Password => 'password',
112    );
113    ok $user_d && $user_d->id, 'loaded or created user';
114
115    ok(
116        RT::Test->add_rights(
117            { Principal => $user_d, Right => [qw(ShowTicket ModifyTicket)] },
118        ),
119        'add global queue ShowTicket/ModifyTicket rights'
120    );
121
122    my $d_m = RT::Test::Web->new;
123    ok $d_m->login( 'user_d', 'password' ), 'logged in as user D';
124
125    for my $queue ( 1, $original_test_queue->id ) {
126        RT::Test->create_ticket(
127            Queue   => $queue,
128            Subject => "Ticket in queue $queue",
129        );
130
131        check_queues( $d_m, [], [] );
132
133        $d_m->follow_link_ok( { text => "Ticket in queue $queue" } );
134        $d_m->follow_link_ok( { text => 'Basics' } );
135        check_queues( $d_m, [$queue], ["#$queue"], $d_m->uri, 'TicketModify' );
136    }
137
138    ok(
139        RT::Test->add_rights(
140            { Principal => $user_d, Right => [qw(SeeQueue)] },
141        ),
142        'add global queue SeeQueue right'
143    );
144
145    for my $queue ( 1, $original_test_queue->id ) {
146
147        check_queues( $d_m, [], [] );
148
149        $d_m->follow_link_ok( { text => "Ticket in queue $queue" } );
150        $d_m->follow_link_ok( { text => 'Basics' } );
151        check_queues( $d_m, undef, undef, $d_m->uri, 'TicketModify' );
152    }
153
154    ok(
155        RT::Test->add_rights(
156            { Principal => $user_d, Right => [qw(CreateTicket)] },
157        ),
158        'add global queue CreateTicket right'
159    );
160
161    for my $queue ( 1, $original_test_queue->id ) {
162        check_queues( $d_m );
163        $d_m->follow_link_ok( { text => "Ticket in queue $queue" } );
164        $d_m->follow_link_ok( { text => 'Basics' } );
165        check_queues( $d_m, undef, undef, $d_m->uri, 'TicketModify' );
166    }
167}
168
169diag( "Test a user with SuperUser granted later" );
170{
171    my $user_e = RT::Test->load_or_create_user(
172        Name     => 'user_e',
173        Password => 'password',
174    );
175    ok( $user_e && $user_e->id, 'loaded or created user' );
176
177    my $m = RT::Test::Web->new;
178    ok( $m->login( 'user_e', 'password' ), 'logged in as user E' );
179
180    check_queues( $m, [], [] );
181
182    ok(
183        RT::Test->add_rights(
184            { Principal => $user_e, Right => [qw(SuperUser)] },
185        ),
186        'add SuperUser right'
187    );
188    for my $queue ( 1, $original_test_queue->id ) {
189        check_queues( $m );
190        $m->follow_link_ok( { text => "Ticket in queue $queue" } );
191        $m->follow_link_ok( { text => 'Basics' } );
192        check_queues( $m, undef, undef, $m->uri, 'TicketModify' );
193    }
194}
195
196diag( "Test a user with ShowTicket granted later" );
197{
198    my $user_f = RT::Test->load_or_create_user(
199        Name     => 'user_f',
200        Password => 'password',
201    );
202    ok( $user_f && $user_f->id, 'loaded or created user' );
203    ok(
204        RT::Test->add_rights(
205            { Principal => $user_f, Right => [qw(SeeQueue)] },
206        ),
207        'add global SeeQueue right'
208    );
209
210    my $m = RT::Test::Web->new;
211    ok( $m->login( 'user_f', 'password' ), 'logged in as user F' );
212
213    # Links in Queue List portlet
214    for my $queue ( 'General', $original_test_queue->Name ) {
215        ok( !$m->find_link( text => 'General' ), "no link to $queue ticket search" );
216    }
217
218    ok(
219        RT::Test->add_rights(
220            { Principal => $user_f, Right => [qw(ShowTicket)] },
221        ),
222        'add global ShowTicket right'
223    );
224
225    $m->reload;
226
227    # Links in Queue List portlet
228    for my $queue ( 'General', $original_test_queue->Name ) {
229        ok( $m->find_link( text => $queue ), "found link to $queue ticket search" );
230    }
231}
232
233sub new_queue {
234    my $name = shift;
235    my $new_queue = RT::Queue->new(RT->SystemUser);
236    ok($new_queue->Create( Name => $name, Description => "Testing for $name queue" ), "Created queue ".$new_queue->Name);
237    return $new_queue;
238}
239
240sub internal_queues {
241    my $internal_queues = RT::Queues->new(RT->SystemUser);
242    $internal_queues->Limit(FIELD => 'Disabled', VALUE => 0);
243    my $queuelist;
244    while ( my $q = $internal_queues->Next ) {
245        $queuelist->{$q->Id} = $q->Name;
246    }
247    return $queuelist;
248}
249
250
251# takes a WWW::Mech object and two optional arrayrefs of queue ids and names
252# compares the list of ids and names to the dropdown of Queues for the New Ticket In form
253sub check_queues {
254    my ($browser, $queue_id_list, $queue_name_list, $url, $form_name) = @_;
255    $url ||= $baseurl;
256
257    $browser->get_ok( $url, "Navigated to $url" );
258
259    # Get rid of inline-edit forms
260    my ($form) = grep { $_->action !~ m{/Helpers/TicketUpdate} } $browser->all_forms_with_fields('Subject');
261    ok( $form, "Found form" );
262
263    my ( @queue_ids, @queue_names );
264    if ( !$queue_id_list || @$queue_id_list > 0 ) {
265        ok(my $queuelist = $form->find_input('Queue','option'), "Found queue select");
266        @queue_ids = $queuelist->possible_values;
267        @queue_names = $queuelist->value_names;
268    }
269    else {
270        ok( !$form->find_input( 'Queue', 'option' ), "No queue select options" );
271    }
272
273    my $full_queue_list = internal_queues();
274    $queue_id_list = [keys %$full_queue_list] unless $queue_id_list;
275    $queue_name_list = [values %$full_queue_list] unless $queue_name_list;
276    is_deeply([sort @queue_ids],[sort @$queue_id_list],
277              "Queue list contains the expected queue ids");
278    is_deeply([sort @queue_names],[sort @$queue_name_list],
279              "Queue list contains the expected queue names");
280}
281
282done_testing;
283