[Bps-public-commit] rt-extension-rest2 branch, transaction-cfs, repushed

Michel Rodriguez michel at bestpractical.com
Thu Aug 8 17:11:12 EDT 2019


The branch transaction-cfs was deleted and repushed:
       was 6c318c3a48259c947046fd0298638a030e223c54
       now a0afa6790079e9bb6396cb8d9c0b2e9177c35935

1:  6c318c3 ! 1:  a0afa67 Added Transaction Custom Field updates on Correspond and Comment
    @@ -143,3 +143,122 @@
      sub create_path {
          my $self = shift;
          my $id = $self->created_transaction->Id;
    +
    +diff --git a/xt/transaction-customfields.t b/xt/transaction-customfields.t
    +new file mode 100644
    +--- /dev/null
    ++++ b/xt/transaction-customfields.t
    +@@
    ++
    ++use strict;
    ++use warnings;
    ++use RT::Extension::REST2::Test tests => undef;
    ++use Test::Deep;
    ++
    ++my $mech = RT::Extension::REST2::Test->mech;
    ++
    ++my $auth = RT::Extension::REST2::Test->authorization_header;
    ++my $rest_base_path = '/REST/2.0';
    ++my $admin = RT::Extension::REST2::Test->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 => 'Freeform', 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 $transid;
    ++($id,$transid, $msg) = $ticket->Create(Queue => $queue->id, Subject => 'TxnCF test',);
    ++ok($id,$msg);
    ++
    ++my $res = $mech->get("$rest_base_path/ticket/$id", 'Authorization' => $auth);
    ++    is( $res->code, 200);
    ++
    ++my $tnb=0;
    ++{   $tnb++;
    ++    my $payload = { Content         => "reply $tnb",
    ++                    ContentType     => "text/plain",
    ++                    TxnCustomFields => { "TxnCF" => "txncf value $tnb"},
    ++                  };
    ++    my $res = $mech->post_json("$rest_base_path/ticket/$id/correspond", $payload, 'Authorization' => $auth);
    ++    is( $res->code, 201, 'correspond response code is 201');
    ++    is_deeply( $mech->json_response, [ "Correspondence added" ], 'message is "Correspondence Added"');
    ++
    ++    my $txn= last_txn( $id, 'Correspond');
    ++    is( $txn->{CustomFields}->{$cfid}->[0], "txncf value 1", 'CustomField by id');
    ++}
    ++
    ++{
    ++    my $payload = { Content         => "reply #2",
    ++                    ContentType     => "text/plain",
    ++                    TxnCustomFields => { "not a real CF name" => "txncf value"},
    ++                  };
    ++    my $res = $mech->post_json("$rest_base_path/ticket/$id/correspond", $payload, 'Authorization' => $auth);
    ++    is( $res->code, 400, 'correspond response code is 400');
    ++    is( $mech->json_response->{message}, "unknown transaction custom field: not a real CF name", 'wrong cf name');
    ++}
    ++
    ++done_testing(); exit;
    ++
    ++# this is clumsy and brittle, and should probably be replaced by direct access to the data through RT::Test
    ++
    ++sub last_txn {
    ++    my( $ticket_id, $type ) = @_;
    ++    $type ||= 'Correspond';
    ++    my $ticket_res  = $mech->get("$rest_base_path/ticket/$id", 'Authorization' => $auth);
    ++
    ++    # build the list of txn
    ++    my $history_url = link_of_type( $mech->json_response, 'history' );
    ++    $mech->get( $history_url, 'Authorization' => $auth);
    ++    my $content= $mech->json_response;
    ++    my @items= @{$content->{items}};
    ++    if( $content->{total} >  $content->{per_page}) {
    ++        my $last_page = int( $content->{total} / $content->{per_page} ) + 1;
    ++        foreach my $page (2..$last_page) {
    ++            $mech->get( "$history_url?page=$page", 'Authorization' => $auth);
    ++            $content= $mech->json_response;
    ++            push @items, @{$content->{items}};
    ++        }
    ++    }
    ++
    ++    # get the txn record for the last action of type $type
    ++    my $last_txn;
    ++    foreach my $txn (reverse @items) {
    ++        $mech->get( $txn->{_url}, 'Authorization' => $auth );
    ++        $content= $mech->json_response;
    ++        #use DDP; warn "txn content:\n"; p $content;
    ++        if( $content->{Type} eq $type ) {
    ++            $last_txn = $content;
    ++            last;
    ++        }
    ++    }
    ++
    ++    if( $last_txn) {
    ++        return $last_txn;
    ++    }
    ++    else {
    ++        return;
    ++    }
    ++}
    ++
    ++sub link_of_type {
    ++    my( $json, $ref)= @_;
    ++    my $links = $json->{_hyperlinks};
    ++    foreach my $link (@$links) {
    ++        if( $link->{ref} eq $ref) {
    ++            return $link->{_url};
    ++        }
    ++    }
    ++    return;
    ++}
    ++
    ++



More information about the Bps-public-commit mailing list