File: Generic.t

package info (click to toggle)
znuny 6.5.18-1
  • links: PTS
  • area: non-free
  • in suites: forky, sid
  • size: 205,344 kB
  • sloc: perl: 1,038,694; xml: 74,551; javascript: 65,276; sql: 23,574; sh: 417; makefile: 63
file content (436 lines) | stat: -rw-r--r-- 13,614 bytes parent folder | download | duplicates (4)
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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
# --
# Copyright (C) 2021 Znuny GmbH, https://znuny.org/
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

use strict;
use warnings;
use utf8;

use vars (qw($Self));

use MIME::Base64;

use Kernel::System::VariableCheck qw(:all);

$Kernel::OM->ObjectParamAdd(
    'Kernel::System::UnitTest::Helper' => {
        RestoreDatabase => 1,
    },
);

my $ConfigObject             = $Kernel::OM->Get('Kernel::Config');
my $HelperObject             = $Kernel::OM->Get('Kernel::System::UnitTest::Helper');
my $ZnunyHelperObject        = $Kernel::OM->Get('Kernel::System::ZnunyHelper');
my $UnitTestWebserviceObject = $Kernel::OM->Get('Kernel::System::UnitTest::Webservice');
my $CustomerCompanyObject    = $Kernel::OM->Get('Kernel::System::CustomerCompany');
my $TicketObject             = $Kernel::OM->Get('Kernel::System::Ticket');
my $ArticleObject            = $Kernel::OM->Get('Kernel::System::Ticket::Article');

my $Home = $ConfigObject->Get('Home');

# Enable base-64-encoding for certain fields
$ConfigObject->Set(
    Key   => 'GenericInterface::Invoker::Ticket::Generic::PrepareRequest::Base64EncodedFields',
    Value => {
        Generic => 'Articles->Body;CustomerCompany->CustomerCompanyCity;Owner',
    },
);

# Enable removal of certain fields
$ConfigObject->Set(
    Key   => 'GenericInterface::Invoker::Ticket::Generic::PrepareRequest::OmittedFields',
    Value => {
        Generic => 'Articles->IsVisibleForCustomer;CustomerCompany->CustomerCompanyStreet;Queue;ArticleID',
    },
);

$ZnunyHelperObject->_WebserviceCreate(
    Webservices => {
        Generic => $Home . '/scripts/test/sample/Webservice/ZnunyGeneric.yml',
    },
);

my @DynamicFields = (
    {
        Name       => 'InvokerGenericTest',
        Label      => 'InvokerGenericTest',
        ObjectType => 'Ticket',
        FieldType  => 'Text',
        Config     => {
            DefaultValue => "",
        },
    },
);

$ZnunyHelperObject->_DynamicFieldsCreate(@DynamicFields);

my %CustomerUserData = $HelperObject->TestCustomerUserDataGet(
    Language => 'de',
);

my %UserData = $HelperObject->TestUserDataGet(
    Groups   => [ 'admin', 'users' ],
    Language => 'de'
);

$CustomerCompanyObject->CustomerCompanyAdd(
    CustomerID             => 'example.com',
    CustomerCompanyName    => 'New Customer Inc.',
    CustomerCompanyStreet  => '5201 Blue Lagoon Drive',
    CustomerCompanyZIP     => '33126',
    CustomerCompanyCity    => 'Miami',
    CustomerCompanyCountry => 'USA',
    CustomerCompanyURL     => 'http://www.example.org',
    CustomerCompanyComment => 'some comment',
    ValidID                => 1,
    UserID                 => 1,
);

my $ServiceID = $ZnunyHelperObject->_ServiceCreateIfNotExists(
    Name => 'ttt-service',
);

my $TypeID = $ZnunyHelperObject->_TypeCreateIfNotExists(
    Name => 'ttt-type',
);

my $SLAID = $ZnunyHelperObject->_SLACreateIfNotExists(
    Name => 'ttt-sla',
);

my $QueueID = $ZnunyHelperObject->_QueueCreateIfNotExists(
    Name    => 'ttt-queue',
    GroupID => 1,
);

# HandleResponse
my $TicketID        = $HelperObject->TicketCreate();
my $ArticleIDEvent1 = $HelperObject->ArticleCreate(
    TicketID => $TicketID,
);
my $ArticleIDEvent2 = $HelperObject->ArticleCreate(
    TicketID => $TicketID,
);

my $ResultPrepareRequest = $UnitTestWebserviceObject->InvokerFunctionCall(
    Webservice => 'Generic',
    Invoker    => 'Generic',
    Function   => 'PrepareRequest',
    Data       => {
        Data => {
            Event    => 'TicketCreate',
            TicketID => $TicketID,
        },
    },
);

my $Result = $UnitTestWebserviceObject->InvokerFunctionCall(
    Webservice => 'Generic',
    Invoker    => 'Generic',
    Function   => 'HandleResponse',
    Data       => {
        ResponseSuccess => 1,
        Data            => {
            OTRS_TicketDynamicFieldSet_InvokerGenericTest => 'Hallo',
            OTRS_TicketTitleUpdate                        => 'invoker test title',
            OTRS_TicketServiceSet                         => 'ttt-service',
            OTRS_TicketTypeSet                            => 'ttt-type',
            OTRS_TicketQueueSet                           => 'ttt-queue',
            OTRS_TicketSLASet                             => 'ttt-sla',
            OTRS_TicketCustomerSet                        => 'example.com;' . $CustomerUserData{UserLogin},
            OTRS_TicketStateSet                           => 'closed successful',
            OTRS_TicketOwnerSet                           => $UserData{UserLogin},
            OTRS_TicketResponsibleSet                     => $UserData{UserLogin},
            OTRS_TicketPrioritySet                        => '5 very high',
            OTRS_TicketHistoryAdd                         => {
                HistoryComment => 'OTRS_TicketHistoryAdd Comment',
                HistoryType    => 'AddNote',
            },
            OTRS_TicketArticleAdd => {
                SenderType           => 'agent',
                IsVisibleForCustomer => 1,
                From                 => 'Some Agent <email@example.com>',
                To                   => 'Some Customer A <customer-a@example.com>',
                Cc                   => 'Some Customer B <customer-b@example.com>',
                Bcc                  => 'Some Customer C <customer-c@example.com>',
                ReplyTo              => 'Some Customer B <customer-b@example.com>',
                Subject              => 'some short description',
                Body                 => 'the message text',
                MessageID            => '<asdasdasd.123@example.com>',
                InReplyTo            => '<asdasdasd.12@example.com>',
                References           => '<asdasdasd.1@example.com> <asdasdasd.12@example.com>',
                ContentType          => 'text/plain; charset=ISO-8859-15',
                HistoryType          => 'OwnerUpdate',
                HistoryComment       => 'Some free text!',
            },
            OTRS_TicketArticleCreateEvent => "$ArticleIDEvent1 , $ArticleIDEvent2"
        },
    },
    ObjectModifyFunction => sub {
        my (%Params) = @_;

        $Params{Object}->{BackendObject}->{RequestData} = $ResultPrepareRequest->{Data};

        return 1;
    },
);

$Self->True(
    $Result->{Success},
    'Handle response executed successfully',
);

my %Ticket = $TicketObject->TicketGet(
    TicketID      => $TicketID,
    DynamicFields => 1,
    UserID        => 1,
);

my %ExpectedData = (
    DynamicField_InvokerGenericTest => 'Hallo',
    Title                           => 'invoker test title',
    Service                         => 'ttt-service',
    Type                            => 'ttt-type',
    Queue                           => 'ttt-queue',
    SLA                             => 'ttt-sla',
    CustomerID                      => 'example.com',
    CustomerUserID                  => $CustomerUserData{UserLogin},
    State                           => 'closed successful',
    Owner                           => $UserData{UserLogin},
    Responsible                     => $UserData{UserLogin},
    Priority                        => '5 very high',
);

for my $Key ( sort keys %ExpectedData ) {
    $Self->Is(
        $Ticket{$Key},
        $ExpectedData{$Key},
        "Ticket $Key filled correctly.",
    );
}

# check OTRS_TicketHistoryAdd

my @HistoryLines = $TicketObject->HistoryGet(
    TicketID => $TicketID,
    UserID   => 1,
);

@HistoryLines = grep { 'OTRS_TicketHistoryAdd Comment' eq $_->{Name} } @HistoryLines;

$Self->True(
    @HistoryLines,
    'OTRS_TicketHistoryAdd',
);

%ExpectedData = (
    Name        => 'OTRS_TicketHistoryAdd Comment',
    HistoryType => 'AddNote',
);

for my $Key ( sort keys %ExpectedData ) {
    $Self->Is(
        $HistoryLines[0]->{$Key},
        $ExpectedData{$Key},
        "TicketHistory $Key filled correctly.",
    );
}

# OTRS_TicketArticleAdd

my @ArticleIDs = $ArticleObject->ArticleIndex(
    TicketID => $TicketID,
);

my %Article = $ArticleObject->ArticleGet(
    TicketID  => $TicketID,
    ArticleID => $ArticleIDs[-1],
);

$Self->IsDeeply(
    \%Article,
    {
        ArticleID              => $ArticleIDs[-1],
        ArticleNumber          => 3,
        Bcc                    => 'Some Customer C <customer-c@example.com>',
        Body                   => 'the message text',
        Cc                     => 'Some Customer B <customer-b@example.com>',
        ChangeBy               => 1,
        ChangeTime             => $Article{ChangeTime},
        Charset                => 'ISO-8859-15',
        CommunicationChannel   => 'Internal',
        CommunicationChannelID => 3,
        ContentCharset         => 'ISO-8859-15',
        ContentType            => 'text/plain; charset=ISO-8859-15',
        CreateBy               => 1,
        CreateTime             => $Article{CreateTime},
        From                   => 'Some Agent <email@example.com>',
        InReplyTo              => '<asdasdasd.12@example.com>',
        IncomingTime           => $Article{IncomingTime},
        IsVisibleForCustomer   => 1,
        MessageID              => '<asdasdasd.123@example.com>',
        MimeType               => 'text/plain',
        References             => '<asdasdasd.1@example.com> <asdasdasd.12@example.com>',
        ReplyTo                => 'Some Customer B <customer-b@example.com>',
        SenderType             => 'agent',
        SenderTypeID           => '1',
        Subject                => 'some short description',
        TicketID               => $TicketID,
        To                     => 'Some Customer A <customer-a@example.com>'
    },
    'OTRS_TicketArticleAdd',
);

# PrepareRequest

$TicketID = $HelperObject->TicketCreate(
    CustomerUser => $CustomerUserData{UserLogin},
    CustomerID   => 'example.com',
    ServiceID    => $ServiceID,
    TypeID       => $TypeID,
    SLAID        => $SLAID,
);

my $ArticleID1 = $HelperObject->ArticleCreate(
    TicketID => $TicketID,
    Body     => 'Body of article 1.',
);
my $ArticleID2 = $HelperObject->ArticleCreate(
    TicketID => $TicketID,
    Body     => 'Body of article 2.',
);
my $ArticleID3 = $HelperObject->ArticleCreate(
    TicketID => $TicketID,
    Body     => 'Body of article 3.',
);

$Result = $UnitTestWebserviceObject->InvokerFunctionCall(
    Webservice => 'Generic',
    Invoker    => 'Generic',
    Function   => 'PrepareRequest',
    Data       => {
        Data => {
            Event     => 'ArticleCreate',
            TicketID  => $TicketID,
            ArticleID => $ArticleID3,
        },
    },
);

$Self->IsNot(
    $Result->{Data}->{Event}->{Event},
    undef,
    'event name is filled',
);
$Self->True(
    IsHashRefWithData( $Result->{Data}->{Event} ) ? 1 : 0,
    'Event data is filled',
);
$Self->True(
    IsHashRefWithData( $Result->{Data}->{Ticket} ) ? 1 : 0,
    'Ticket data is filled',
);

my @ExpectedData = (
    'CustomerUser', 'CustomerCompany', 'QueueData', 'TypeData', 'PriorityData', 'ServiceData',
    'SLAData', 'OwnerData', 'ResponsibleData', 'CreateByData', 'Article'
);

for my $Key ( sort @ExpectedData ) {
    $Self->True(
        IsHashRefWithData( $Result->{Data}->{Ticket}->{$Key} ) ? 1 : 0,
        "$Key data is filled",
    );
}

$Self->Is(
    $Result->{Data}->{Ticket}->{CreateByData}->{UserPw},
    undef,
    'CreateByData->UserPw got removed',
);
$Self->Is(
    $Result->{Data}->{Ticket}->{ResponsibleData}->{UserPw},
    undef,
    'ResponsibleData->UserPw got removed',
);
$Self->Is(
    $Result->{Data}->{Ticket}->{OwnerData}->{UserPw},
    undef,
    'OwnerData->UserPw got removed',
);
$Self->Is(
    $Result->{Data}->{Ticket}->{CustomerUser}->{UserPassword},
    undef,
    'CustomerUser->UserPassword got removed',
);

#
# Check base-64-encoding of values of configured fields.
#
my @ArticleBodiesBase64 = (
    encode_base64('Body of article 1.'),
    encode_base64('Body of article 2.'),
    encode_base64('Body of article 3.'),
);

my $CustomerCompanyCityBase64 = encode_base64('Miami');
my $OwnerBase64               = encode_base64('root@localhost');

my $ArticleIndex = 0;
for my $Article ( @{ $Result->{Data}->{Ticket}->{Articles} } ) {
    $Self->Is(
        $Article->{Body},
        $ArticleBodiesBase64[$ArticleIndex],
        "Body of article $ArticleIndex must be base-64 encoded.",
    );

    $ArticleIndex++;
}

$Self->Is(
    $Result->{Data}->{Ticket}->{CustomerCompany}->{CustomerCompanyCity},
    $CustomerCompanyCityBase64,
    'City of customer company must be base-64 encoded.',
);

$Self->Is(
    $Result->{Data}->{Ticket}->{Owner},
    $OwnerBase64,
    'Owner must be base-64 encoded.',
);

#
# Check absence of omitted fields.
#

#        Generic => 'Articles->IsVisibleForCustomer;CustomerCompany->CustomerCompanyStreet;Queue;ArticleID',
$ArticleIndex = 0;
for my $Article ( @{ $Result->{Data}->{Ticket}->{Articles} } ) {
    $Self->False(
        exists $Article->{IsVisibleForCustomer},
        "Articles must not contain field 'IsVisibleForCustomer'.",
    );

    $ArticleIndex++;
}

$Self->False(
    exists $Result->{Data}->{Event}->{ArticleID},
    "Event hash must not contain key ArticleID.",
);

$Self->False(
    exists $Result->{Data}->{Ticket}->{CustomerCompany}->{CustomerCompanyStreet},
    "Customer company must not contain field 'CustomerCompanyStreet'.",
);

$Self->False(
    exists $Result->{Data}->{Ticket}->{Queue},
    "Ticket must not contain field 'Queue'.",
);

1;