File: transaction-customfields.t

package info (click to toggle)
request-tracker5 5.0.3%2Bdfsg-3~deb12u3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 77,648 kB
  • sloc: javascript: 187,930; perl: 79,061; sh: 1,302; makefile: 471; python: 37; php: 15
file content (178 lines) | stat: -rw-r--r-- 5,676 bytes parent folder | download | duplicates (2)
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
use strict;
use warnings;
use RT::Test::REST2 tests => undef;
use Test::Deep;

my $mech = RT::Test::REST2->mech;
my ( $baseurl, $m ) = RT::Test->started_ok;
diag "Started server at $baseurl";

my $auth = RT::Test::REST2->authorization_header;
my $rest_base_path = '/REST/2.0';
my $admin = RT::Test::REST2->user;
$admin->PrincipalObj->GrantRight( Right => 'SuperUser' );

my $queue = RT::Test->load_or_create_queue( Name => "General" );

my( $id, $msg);

my $cf = RT::CustomField->new( RT->SystemUser );
my $cfid;
($cfid, $msg) = $cf->Create(Name => 'TxnCF', Type => 'FreeformSingle', MaxValues => '0', LookupType => RT::Transaction->CustomFieldLookupType );
ok($cfid,$msg);

($id,$msg) = $cf->AddToObject($queue);
ok($id,$msg);

my $ticket = RT::Ticket->new(RT->SystemUser);
my ( $ticket1_id, $transid );
($ticket1_id, $transid, $msg) = $ticket->Create(Queue => $queue->id, Subject => 'TxnCF test',);
ok( $ticket1_id, $msg );

my $res = $mech->get("$rest_base_path/ticket/$ticket1_id", 'Authorization' => $auth);
is( $res->code, 200, 'Fetched ticket via REST2 API');

{
    my $payload = { Content         => "reply one",
                    ContentType     => "text/plain",
                    TxnCustomFields => { "TxnCF" => "txncf value one"},
                  };
    my $res = $mech->post_json("$rest_base_path/ticket/$ticket1_id/correspond", $payload, 'Authorization' => $auth);
    is( $res->code, 201, 'correspond response code is 201');
    is_deeply( $mech->json_response, [ "Correspondence added", "Custom fields updated" ], 'message is "Correspondence Added"');

    my $ticket = RT::Ticket->new(RT->SystemUser);
    my ( $ret, $msg ) = $ticket->Load( $ticket1_id );
    ok( $ret, $msg );
    my $txns = $ticket->Transactions;
    $txns->Limit( FIELD => 'Type', VALUE => 'Correspond' );
    my $txn = $txns->Last;
    ok( $txn->Id, "Found Correspond transaction" );
    is( $txn->FirstCustomFieldValue('TxnCF'), "txncf value one", 'Found transaction custom field');
}

{
    my @warnings;
    local $SIG{__WARN__} = sub {
        push @warnings, @_;
    };
    my $payload = { Content         => "reply two",
                    ContentType     => "text/plain",
                    TxnCustomFields => { "not a real CF name" => "txncf value"},
                  };
    my $res = $mech->post_json("$rest_base_path/ticket/$ticket1_id/correspond", $payload, 'Authorization' => $auth);

    is( scalar @warnings, 1, 'Got one warning' );
    like(
        $warnings[0],
        qr/Unable to load transaction custom field: not a real CF name/,
        'Got the unable to load warning'
    );

    is( $res->code, 201, 'Correspond response code is 201 because correspond succeeded');
    is_deeply( $mech->json_response, [ "Correspondence added", "Unable to load transaction custom field: not a real CF name" ], 'Bogus cf name');
}


# Test as a user.
my $user = RT::Test::REST2->user;

$user->PrincipalObj->GrantRight( Right => 'CreateTicket' );
$user->PrincipalObj->GrantRight( Right => 'ModifyTicket' );
$user->PrincipalObj->GrantRight( Right => 'ReplyToTicket' );
$user->PrincipalObj->GrantRight( Right => 'SeeQueue' );
$user->PrincipalObj->GrantRight( Right => 'ShowTicket' );
$user->PrincipalObj->GrantRight( Right => 'ShowTicketComments' );
$user->PrincipalObj->GrantRight( Right => 'SeeCustomField' );
$user->PrincipalObj->GrantRight( Right => 'ModifyCustomField' );

my ($ticket_url, $ticket_id);
{
    my $payload = {
        Subject => 'Ticket for CF test',
        Queue   => 'General',
        Content => 'Ticket for CF test content',
    };

    my $res = $mech->post_json("$rest_base_path/ticket",
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 201);
    ok($ticket_url = $res->header('location'));
    ok(($ticket_id) = $ticket_url =~ qr[/ticket/(\d+)]);

    # We need the hypermedia URLs...
    $res = $mech->get($ticket_url,
        'Authorization' => $auth,
    );
    is($res->code, 200);

    $payload = {
        Subject => 'Add Txn with CF',
        Content => 'Content',
        ContentType => 'text/plain',
        'TxnCustomFields' => {
            'TxnCF' => 'Txn CustomField',
         },
    };

    $res = $mech->post_json($mech->url_for_hypermedia('correspond'),
        $payload,
        'Authorization' => $auth,
    );
    is($res->code, 201);
    my $response = $mech->json_response;


    my $response_value = bag(
        re(qr/Correspondence added|Message added/), 'Custom fields updated',
    );

    cmp_deeply($mech->json_response, $response_value, 'Response containts correct strings');
}

# Look for the Transaction with our CustomField set.
{
    my $res = $mech->get($ticket_url,
        'Authorization' => $auth,
    );
    is($res->code, 200);

    $res = $mech->get($mech->url_for_hypermedia('history'),
        'Authorization' => $auth,
    );
    is($res->code, 200);

    my $content = $mech->json_response;
    is($content->{count}, 3);
    is($content->{page}, 1);
    is($content->{per_page}, 20);
    is($content->{total}, 3);
    is(scalar @{$content->{items}}, 3);

    # Check the correspond txn (0 = create, 1 = correspond)
    my $txn = @{ $content->{items} }[1];

    $res = $mech->get($txn->{_url},
        'Authorization' => $auth,
    );
    is($res->code, 200);

    $content = $mech->json_response;
    like($content->{Data}, qr/^Add Txn with CF/);

    cmp_deeply(
        $content->{CustomFields},
        [   {   'values' => ['Txn CustomField'],
                'type'   => 'customfield',
                'id'     => $cfid,
                '_url'   => ignore(),
                'name'   => 'TxnCF',
            }
        ],
        'Txn is set'
    );
}

done_testing();