File: articles.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 (184 lines) | stat: -rw-r--r-- 5,699 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
use strict;
use warnings;
use RT::Test::REST2 tests => undef;
use Test::Deep;

my $mech = RT::Test::REST2->mech;

my $auth           = RT::Test::REST2->authorization_header;
my $rest_base_path = '/REST/2.0';
my $user           = RT::Test::REST2->user;

# Empty DB
{
    my $res = $mech->post_json(
        "$rest_base_path/articles",
        [ { field => 'id', operator => '>', value => 0 } ],
        'Authorization' => $auth,
    );
    is( $res->code,                    200 );
    is( $mech->json_response->{count}, 0 );
}

# Missing Class
{
    my $res = $mech->post_json(
        "$rest_base_path/article",
        { Name => 'Article creation using REST', },
        'Authorization' => $auth,
    );
    is( $res->code,                      400 );
    is( $mech->json_response->{message}, 'Invalid Class' );
}

# Article Creation
my ( $article_url, $article_id );
{
    my $payload = {
        Name    => 'Article creation using REST',
        Summary => 'Article summary',
        Class   => 'General',
    };

    # Rights Test - No CreateArticle
    my $res = $mech->post_json( "$rest_base_path/article", $payload, 'Authorization' => $auth, );
    is( $res->code, 403 );

    # Rights Test - With CreateArticle
    $user->PrincipalObj->GrantRight( Right => 'CreateArticle' );
    $res = $mech->post_json( "$rest_base_path/article", $payload, 'Authorization' => $auth, );
    is( $res->code, 201 );
    ok( $article_url = $res->header('location') );
    ok( ($article_id) = $article_url =~ qr[/article/(\d+)] );
}

# Article Display
{
    # Rights Test - No ShowArticle
    my $res = $mech->get( $article_url, 'Authorization' => $auth, );
    is( $res->code, 403 );
}

# Rights Test - With ShowArticle
{
    $user->PrincipalObj->GrantRight( Right => 'ShowArticle' );

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

    my $content = $mech->json_response;
    is( $content->{id},   $article_id );
    is( $content->{Name}, 'Article creation using REST' );

    ok( exists $content->{$_} ) for qw(Creator Created LastUpdated LastUpdatedBy Name Summary);

    my $links = $content->{_hyperlinks};
    is( scalar @$links, 2 );

    is( $links->[0]{ref},  'self' );
    is( $links->[0]{id},   1 );
    is( $links->[0]{type}, 'article' );
    like( $links->[0]{_url}, qr[$rest_base_path/article/$article_id$] );

    is( $links->[1]{ref}, 'history' );
    like( $links->[1]{_url}, qr[$rest_base_path/article/$article_id/history$] );

    my $class = $content->{Class};
    is( $class->{id},   1 );
    is( $class->{type}, 'class' );
    like( $class->{_url}, qr{$rest_base_path/class/1$} );

    my $creator = $content->{Creator};
    is( $creator->{id},   'test' );
    is( $creator->{type}, 'user' );
    like( $creator->{_url}, qr{$rest_base_path/user/test$} );

    my $updated_by = $content->{LastUpdatedBy};
    is( $updated_by->{id},   'test' );
    is( $updated_by->{type}, 'user' );
    like( $updated_by->{_url}, qr{$rest_base_path/user/test$} );
}

# Article Search
{
    my $res = $mech->post_json(
        "$rest_base_path/articles",
        [ { field => 'id', operator => '>', value => 0 } ],
        'Authorization' => $auth,
    );
    is( $res->code, 200 );
    my $content = $mech->json_response;
    is( $content->{count},             1 );
    is( $content->{page},              1 );
    is( $content->{per_page},          20 );
    is( $content->{total},             1 );
    is( scalar @{ $content->{items} }, 1 );

    my $article = $content->{items}->[0];
    is( $article->{type}, 'article' );
    is( $article->{id},   1 );
    like( $article->{_url}, qr{$rest_base_path/article/1$} );
}

# Article Update
{
    my $payload = { Name => 'Article update using REST', };

    # Rights Test - No ModifyArticle
    my $res = $mech->put_json( $article_url, $payload, 'Authorization' => $auth, );
TODO: {
        local $TODO = "RT ->Update isn't introspectable";
        is( $res->code, 403 );
    }
    is_deeply( $mech->json_response, ['Article Article creation using REST: Permission Denied'] );

    $user->PrincipalObj->GrantRight( Right => 'ModifyArticle' );

    $res = $mech->put_json( $article_url, $payload, 'Authorization' => $auth, );
    is( $res->code, 200 );
    is_deeply(
        $mech->json_response,
        [   'Article Article update using REST: Name changed from "Article creation using REST" to "Article update using REST"'
        ]
    );

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

    my $content = $mech->json_response;
    is( $content->{Name}, 'Article update using REST' );

    # update again with no changes
    $res = $mech->put_json( $article_url, $payload, 'Authorization' => $auth, );
    is( $res->code, 200 );
    is_deeply( $mech->json_response, [] );

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

    $content = $mech->json_response;
    is( $content->{Name}, 'Article update using REST' );
}

# Transactions
{
    my $res = $mech->get( $article_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},             2 );
    is( $content->{page},              1 );
    is( $content->{per_page},          20 );
    is( $content->{total},             undef, 'No total count');
    is( scalar @{ $content->{items} }, 2 );

    for my $txn ( @{ $content->{items} } ) {
        is( $txn->{type}, 'transaction' );
        like( $txn->{_url}, qr{$rest_base_path/transaction/\d+$} );
    }
}

done_testing;