File: 60-with-rt.t

package info (click to toggle)
librt-client-rest-perl 1%3A0.72-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 588 kB
  • sloc: perl: 4,883; makefile: 9
file content (391 lines) | stat: -rw-r--r-- 10,371 bytes parent folder | download
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#!perl
# vim: softtabstop=4 tabstop=4 shiftwidth=4 ft=perl expandtab smarttab

# This test is for testing RT::Client::REST with a real instance of RT.
# This is so that we can verify bug reports and compare functionality
# (and bugs) between different versions of RT.

use strict;
use warnings;

use Test::More;
use File::Spec::Functions qw/ splitpath /;

BEGIN {
    unless ( $ENV{RELEASE_TESTING} ) {
        plan( skip_all => 'these tests are for release candidate testing' );
    }

    if ( grep { not defined $ENV{$_} } (qw(RTSERVER RTPASS RTUSER)) ) {
        plan( skip_all => 'one of RTSERVER, RTPASS, or RTUSER is not set' );
    }
}

{
    # We will only use letters, because this string may be used for names of
    # queues and users in RT and we don't want to fail because of RT rules.
    my @chars = ( 'a' .. 'z', 'A' .. 'Z' );

    sub random_string {
        my $retval = '';
        for ( 1 .. 10 ) {
            $retval .= $chars[ int( rand( scalar(@chars) ) ) ];
        }
        return $retval;
    }
}

plan 'no_plan';

use Try::Tiny;
use File::Temp qw(tempfile);
use RT::Client::REST;
use RT::Client::REST::Queue;
use RT::Client::REST::User;

my $rt = RT::Client::REST->new( server => $ENV{RTSERVER}, );
ok( $rt, 'RT instance is created' );

# Log in with wrong credentials and see that we get expected error
{
    my $e;
    try {
        $rt->login(
            username => $ENV{RTUSER},
            password => 'WRONG' . $ENV{RTPASS}
        );
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('RT::Client::REST::AuthenticationFailureException') ) {
            $e = $_;
        }
        else {
            $_->rethrow;
        }
    };
    ok( defined($e),
        'Logging in with wrong credentials throws expected error' );
}

# Now log in successfully
{
    my $e;
    try {
        $rt->login( username => $ENV{RTUSER}, password => $ENV{RTPASS} );
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('RT::Client::REST::Exception') ) {
            $e = $_;
        }
        else {
            $_->rethrow;
        }
    };
    ok( !defined($e), 'login is successful' );
}

# Create a user
my $user_id;
my %user_props = (
    name      => random_string(),
    password  => random_string(),
    comments  => random_string(),
    real_name => random_string(),
);
{
    my ( $user, $e );
    try {
        $user = RT::Client::REST::User->new(
            rt => $rt,
            %user_props,
        )->store;
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('RT::Client::REST::CouldNotCreateObjectException') ) {
            $e = $_;
        }
        else {
            $_->rethrow;
        }
    };
    ok( defined($user),
        "user $user_props{name} created successfully, id: "
          . ( defined $user ? $user->id : 'UNDEF' ) );
    ok( !defined($e), '...and no exception was thrown' );
    $user_id = $user->id;
}

# Retrieve the user we just created and verify its properties
{
    my $user = RT::Client::REST::User->new( rt => $rt, id => $user_id );
    my $e;
    try {
        $user->retrieve;
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("fetching user threw $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( !defined($e), 'fetched user without exception being thrown' );
    while ( my ( $prop, $val ) = each(%user_props) ) {
        next if $prop eq 'password';    # This property comes back obfuscated
        is( $user->$prop, $val, "user property `$prop' matches" );
    }
}

# Create a queue
my $queue_name = 'A queue named ' . random_string();
my $queue_id;
my $queue;
{
    my $e;
    try {
        $queue = RT::Client::REST::Queue->new(
            rt   => $rt,
            name => $queue_name,
        )->store;
        $queue_id = $queue->id;
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("test queue store: $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( $queue,       "Create test queue '$queue_name'" );
    ok( !defined($e), 'created test queue without exception being thrown' );
}
{
    my $e;
    try {
        $queue = RT::Client::REST::Queue->new(
            rt => $rt,
            id => $queue_id,
        )->retrieve;
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("queue retrieve $e");
        }
        else {
            $_->rethrow;
        }
    };
    is( $queue->name, $queue_name, 'test queue name matches' );

    # TODO: with 4.2.3, warning "Unknown key: disabled" is printed
}

# Create a ticket
my $ticket;
{
    my $e;
    my $subject = 'This is a subject ' . random_string();
    try {
        $ticket = RT::Client::REST::Ticket->new(
            rt      => $rt,
            queue   => $queue_id,
            subject => $subject,
        )->store( text => 'Some random text ' . random_string() );
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("ticket store: $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( defined($ticket),
        "Created ticket '$subject' ID " . ( defined $ticket ? $ticket->id : 'UNDEF' ) );
    ok( !defined($e), 'No exception thrown when ticket created' );
}

# Attach something to the ticket and verify its count and contents
{
    my $att_contents = "dude this is a text attachment\n";
    my ( $fh, $filename ) = tempfile;
    $fh->print($att_contents);
    $fh->close;
    my $message = 'This is a message ' . random_string(),
    my $e;
    try {
        $ticket->comment(
            message     => $message,
            attachments => [$filename],
        );
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("attach to ticket: $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( !defined($e), 'Create attachment and no exception thrown' );
    unlink $filename;
    $e = undef;
    try {
        my $atts = $ticket->attachments;

        # XXX With RT 4.2.3, the count is 4. Is it the same with previous
        # versions or is this a change in behavior?
        is( $atts->count, 4, 'There are 4 attachment to ticket ' . $ticket->id );
        my $att_iter = $atts->get_iterator;
        my $basename = (splitpath($filename))[2];
        my ($att) = grep { $_->file_name eq $basename } &$att_iter;
        if ($att) {
            ok(1, "Found attachment with filename: $basename");
            is( $att->content, $att_contents, 'Attachment content matches' );
        }
        else {
            ok(0, "Found attachment with filename: $basename");
        }

    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("attach to ticket: $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( !defined($e), 'listed attachments and no exception thrown' );
}
# Comment with HTML
{
    my $message = sprintf('Some <b>html</b> message text <pre>%s</pre>', random_string());
    my $e;
    try {
        $ticket->comment(
            message => $message,
            html    => 1
        );
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("attach to ticket: $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( !defined($e), 'Add html comment and no exception thrown' );
    try {
        my $atts = $ticket->attachments;
        my $att_iter = $atts->get_iterator;
        my $att = (&$att_iter)[-1];
        if ($att) {
            ok(1, 'Retrieved final attachment');
            is( $att->content_type, 'text/html', 'Content-Type is text/html' );
        }
        else {
            ok(0, 'Retrieved final attachment');
        }

    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("attach to ticket: $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( !defined($e), 'listed attachments and no exception thrown' );
}

# Search for tickets (with format s)
{
    my (@results, $e);
    try {
        @results = $rt->search(
            type => 'ticket',
            query => "Queue='$queue_name'",
            format => 's'
        )
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("searching for tickets (with format s): $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( scalar @results > 0, 'Found some results (with format s)' );
    is_deeply( \@results, [[ $ticket->id, $ticket->subject ]], 'Search results as expected (with format s)' );
    ok( !defined($e), 'No exception thrown when searching tickets (with format s)' );
}

# Delete the ticket
{
    my $e;
    try {
        $ticket->status('deleted');
        $ticket->store;
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("delete ticket: $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( !defined($e), 'ticket deleted and no exception thrown' );
}

# TODO: RT 90112: Attachment retrieval returns wrongly decoded files

# Disable the queue
{
    my $e;
    try {
        $queue->disabled(1);
        $queue->store;
    }
    catch {
        die $_ unless blessed $_ && $_->can('rethrow');
        if ( $_->isa('Exception::Class::Base') ) {
            $e = $_;
            diag("disable test queue: $e");
        }
        else {
            $_->rethrow;
        }
    };
    ok( !defined($e), 'disabled queue without exception being thrown' );
}