summaryrefslogtreecommitdiff
path: root/rt/t/web/ticket_owner_autocomplete.t
blob: 3aa3f282fe03d3bf453fd5ed4c863cd95c74bf49 (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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/usr/bin/perl

use strict;
use warnings;

use RT::Test nodata => 1, tests => 43;
use JSON qw(from_json);

my $queue = RT::Test->load_or_create_queue( Name => 'Regression' );
ok $queue && $queue->id, 'loaded or created queue';

my $user_a = RT::Test->load_or_create_user(
    Name => 'user_a', Password => 'password',
);
ok $user_a && $user_a->id, 'loaded or created user';

my $user_b = RT::Test->load_or_create_user(
    Name => 'user_b', Password => 'password',
);
ok $user_b && $user_b->id, 'loaded or created user';

RT->Config->Set( AutocompleteOwners => 1 );
my ($baseurl, $agent_a) = RT::Test->started_ok;

ok( RT::Test->set_rights(
    { Principal => $user_a, Right => [qw(SeeQueue ShowTicket CreateTicket ReplyToTicket)] },
    { Principal => $user_b, Right => [qw(SeeQueue ShowTicket OwnTicket)] },
), 'set rights');

ok $agent_a->login('user_a', 'password'), 'logged in as user A';

diag "current user has no right to own, nobody selected as owner on create";
{
    $agent_a->get_ok('/', 'open home page');
    $agent_a->form_name('CreateTicketInQueue');
    $agent_a->select( 'Queue', $queue->id );
    $agent_a->submit;

    $agent_a->content_contains('Create a new ticket', 'opened create ticket page');
    my $form = $agent_a->form_name('TicketCreate');
    is $form->value('Owner'), RT->Nobody->Name, 'correct owner selected';
    autocomplete_lacks( 'RT::Queue-'.$queue->id, 'user_a' );
    $agent_a->submit;

    $agent_a->content_like(qr/Ticket \d+ created in queue/i, 'created ticket');
    my ($id) = ($agent_a->content =~ /Ticket (\d+) created in queue/);
    ok $id, 'found id of the ticket';

    my $ticket = RT::Ticket->new( RT->SystemUser );
    $ticket->Load( $id );
    ok $ticket->id, 'loaded the ticket';
    is $ticket->Owner, RT->Nobody->id, 'correct owner';
}

diag "user can chose owner of a new ticket";
{
    $agent_a->get_ok('/', 'open home page');
    $agent_a->form_name('CreateTicketInQueue');
    $agent_a->select( 'Queue', $queue->id );
    $agent_a->submit;

    $agent_a->content_contains('Create a new ticket', 'opened create ticket page');
    my $form = $agent_a->form_name('TicketCreate');
    is $form->value('Owner'), RT->Nobody->Name, 'correct owner selected';

    autocomplete_contains( 'RT::Queue-'.$queue->id, 'user_b' );
    $form->value('Owner', $user_b->Name);
    $agent_a->submit;

    $agent_a->content_like(qr/Ticket \d+ created in queue/i, 'created ticket');
    my ($id) = ($agent_a->content =~ /Ticket (\d+) created in queue/);
    ok $id, 'found id of the ticket';

    my $ticket = RT::Ticket->new( RT->SystemUser );
    $ticket->Load( $id );
    ok $ticket->id, 'loaded the ticket';
    is $ticket->Owner, $user_b->id, 'correct owner';
}

my $agent_b = RT::Test::Web->new;
ok $agent_b->login('user_b', 'password'), 'logged in as user B';

diag "user A can not change owner after create";
{
    my $ticket = RT::Ticket->new( $user_a );
    my ($id, $txn, $msg) = $ticket->Create(
        Queue => $queue->id,
        Owner => $user_b->id,
        Subject => 'test',
    );
    ok $id, 'created a ticket #'. $id or diag "error: $msg";
    is $ticket->Owner, $user_b->id, 'correct owner';

    # try the following group of tests twice with different agents(logins)
    my $test_cb = sub {
        my $agent = shift;
        $agent->get("/Ticket/Modify.html?id=$id");
        my $form = $agent->form_name('TicketModify');
        is $form->value('Owner'), $user_b->Name, 'correct owner selected';
        $form->value('Owner', RT->Nobody->Name);
        $agent->submit;

        $agent->content_contains(
            'Permission Denied',
            'no way to change owner after create if you have no rights'
        );

        my $ticket = RT::Ticket->new( RT->SystemUser );
        $ticket->Load( $id );
        ok $ticket->id, 'loaded the ticket';
        is $ticket->Owner, $user_b->id, 'correct owner';
    };

    $test_cb->($agent_a);
    diag "even owner(user B) can not change owner";
    $test_cb->($agent_b);
}

diag "on reply correct owner is selected";
{
    my $ticket = RT::Ticket->new( $user_a );
    my ($id, $txn, $msg) = $ticket->Create(
        Queue => $queue->id,
        Owner => $user_b->id,
        Subject => 'test',
    );
    ok $id, 'created a ticket #'. $id or diag "error: $msg";
    is $ticket->Owner, $user_b->id, 'correct owner';

    $agent_a->goto_ticket( $id );
    $agent_a->follow_link_ok( { id => 'page-actions-reply' }, 'Reply' );

    my $form = $agent_a->form_number(3);
    is $form->value('Owner'), 'user_b', 'current user selected';
    $agent_a->submit;

    $ticket = RT::Ticket->new( RT->SystemUser );
    $ticket->Load( $id );
    ok $ticket->id, 'loaded the ticket';
    is $ticket->Owner, $user_b->id, 'correct owner';
}

sub autocomplete {
    my $limit = shift;
    my $agent = shift;
    $agent->get_ok("/Helpers/Autocomplete/Owners?term=&limit=$limit&return=Name", "fetched autocomplete values");
    return from_json($agent->content);
}

sub autocomplete_contains {
    my $limit = shift;
    my $expected = shift;
    my $agent = shift;
    
    unless ( $agent ) {
        $agent = RT::Test::Web->new;
        $agent->login('user_a', 'password');
    }
    
    my $results = autocomplete( $limit, $agent );

    my %seen;
    $seen{$_->{value}}++ for @$results;
    $expected = [$expected] unless ref $expected eq 'ARRAY';
    is((scalar grep { not $seen{$_} } @$expected), 0, "got all expected values");
}

sub autocomplete_lacks {
    my $limit = shift;
    my $lacks = shift;
    my $agent = shift;
    
    unless ( $agent ) {
        $agent = RT::Test::Web->new;
        $agent->login('user_a', 'password');
    }
    
    my $results = autocomplete( $limit, $agent );

    my %seen;
    $seen{$_->{value}}++ for @$results;
    $lacks = [$lacks] unless ref $lacks eq 'ARRAY';
    is((scalar grep { $seen{$_} } @$lacks), 0, "didn't get any unexpected values");
}