File: 60-with-rt.t

package info (click to toggle)
librt-client-rest-perl 1%3A0.56-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 568 kB
  • sloc: perl: 4,205; makefile: 9
file content (193 lines) | stat: -rw-r--r-- 5,570 bytes parent folder | download | duplicates (3)
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
#!perl

# 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;

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 Error qw(:try);
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 RT::Client::REST::AuthenticationFailureException with {
        $e = shift;
    };
    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 RT::Client::REST::Exception with {
        $e = shift;
    };
    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 RT::Client::REST::CouldNotCreateObjectException with {
        $e = shift;
    };
    ok(defined($user), "user $user_props{name} created successfully, id: " . $user->id);
    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 Exception::Class::Base with {
        $e = shift;
        diag("fetching user threw $e");
    };
    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 = random_string;
my $queue_id;
{
    my ($queue, $e);
    try {
        $queue = RT::Client::REST::Queue->new(
            rt => $rt, name => $queue_name,
        )->store;
        $queue_id = $queue->id;
    } catch Exception::Class::Base with {
        $e = shift;
        diag("queue store: $e");
    };
    ok($queue, "Create queue $queue_name");
    ok(!defined($e), "created queue without exception being thrown");
    try {
        $queue = RT::Client::REST::Queue->new(
            rt => $rt, id => $queue_id,
        )->retrieve;
    } catch Exception::Class::Base with {
        $e = shift;
        diag("queue retrieve $e");
    };
    is($queue->name, $queue_name, "queue name matches");
    # TODO: with 4.2.3, warning "Unknown key: disabled" is printed
}

# Create a ticket
my $ticket_id;
{
    my ($ticket, $e);
    try {
        $ticket = RT::Client::REST::Ticket->new(
            rt => $rt, queue => $queue_id, subject => random_string,
        )->store(text => random_string);
    } catch Exception::Class::Base with {
        $e = shift;
        diag("ticket store: $e");
    };
    ok(defined($ticket), "Created ticket " . $ticket->id);
    ok(!defined($e), "No exception thrown when ticket created");
    $ticket_id = $ticket->id;
}

# 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 $e;
    try {
        RT::Client::REST::Ticket->new(
            rt => $rt, id => $ticket_id,
        )->comment(
            message     => random_string,
            attachments => [ $filename ],
        );
    } catch Exception::Class::Base with {
        diag("attach to ticket: $e");
        $e = shift;
    };
    ok(!defined($e), "create attachment and no exception thrown");
    unlink $filename;
    try {
        my $ticket = RT::Client::REST::Ticket->new(
            rt => $rt, id => $ticket_id,
        );
        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, 1, "There is one attachment to ticket $ticket_id");
        my $att_iter = $atts->get_iterator;
        while (my $att = &$att_iter) {
            is($att->content, $att_contents, "Attachment content matches");
        }
    } catch Exception::Class::Base with {
        diag("attach to ticket: $e");
        $e = shift;
    };
    ok(!defined($e), "listed attachments and no exception thrown");
}

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