File: pagination.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 (166 lines) | stat: -rw-r--r-- 4,507 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
use strict;
use warnings;
use RT::Test::REST2 tests => undef;

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;

my $alpha = RT::Test->load_or_create_queue( Name => 'Alpha' );
my $bravo = RT::Test->load_or_create_queue( Name => 'Bravo' );
$user->PrincipalObj->GrantRight( Right => 'SuperUser' );

my $alpha_id = $alpha->Id;
my $bravo_id = $bravo->Id;

# Default per_page (20), only 1 page.
{
    my $res = $mech->post_json("$rest_base_path/queues/all",
        [],
        'Authorization' => $auth,
    );
    is($res->code, 200);

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

# per_page = 3, only 1 page.
{
    my $res = $mech->post_json("$rest_base_path/queues/all?per_page=3",
        [],
        'Authorization' => $auth,
    );
    is($res->code, 200);

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

# per_page = 1, 3 pages, page 1.
{
    my $url = "$rest_base_path/queues/all?per_page=1";
    my $res = $mech->post_json($url,
        [],
        'Authorization' => $auth,
    );
    is($res->code, 200);

    # Ensure our use of $url as a regex works.
    $url =~ s/\?/\\?/;

    my $content = $mech->json_response;
    is($content->{count}, 1);
    is($content->{page}, 1);
    is($content->{pages}, 3);
    is($content->{per_page}, 1);
    is($content->{total}, 3);
    undef($content->{prev_page});
    like($content->{next_page}, qr[$url&page=2]);
    is(scalar @{$content->{items}}, 1);
}

# per_page = 1, 3 pages, page 2.
{
    my $url = "$rest_base_path/queues/all?per_page=1";
    my $res = $mech->post_json("$url&page=2",
        [],
        'Authorization' => $auth,
    );
    is($res->code, 200);

    # Ensure our use of $url as a regex works.
    $url =~ s/\?/\\?/;

    my $content = $mech->json_response;
    is($content->{count}, 1);
    is($content->{page}, 2);
    is($content->{pages}, 3);
    is($content->{per_page}, 1);
    is($content->{total}, 3);
    like($content->{prev_page}, qr[$url&page=1]);
    like($content->{next_page}, qr[$url&page=3]);
    is(scalar @{$content->{items}}, 1);
}

# per_page = 1, 3 pages, page 3.
{
    my $url = "$rest_base_path/queues/all?per_page=1";
    my $res = $mech->post_json("$url&page=3",
        [],
        'Authorization' => $auth,
    );
    is($res->code, 200);

    # Ensure our use of $url as a regex works.
    $url =~ s/\?/\\?/;

    my $content = $mech->json_response;
    is($content->{count}, 1);
    is($content->{page}, 3);
    is($content->{pages}, 3);
    is($content->{per_page}, 1);
    is($content->{total}, 3);
    like($content->{prev_page}, qr[$url&page=2]);
    undef($content->{next_page});
    is(scalar @{$content->{items}}, 1);
}

# Test sanity checking for the pagination parameters.
{
    my $url = "$rest_base_path/queues/all";
    for my $param ( 'per_page', 'page' ) {
    for my $value ( 'abc', '-10', '30' ) {
        # No need to test the following combination.
        next if $param eq 'per_page' && $value eq '30';

        my $res = $mech->post_json("$url?$param=$value",
        [],
        'Authorization' => $auth,
        );
        is($res->code, 200);

        my $content = $mech->json_response;
        if ($param eq 'page') {
        if ($value eq '30') {
            is($content->{count}, 0);
            is($content->{page}, 30);
            is(scalar @{$content->{items}}, 0);
            like($content->{prev_page}, qr[$url\?page=1]);
        } else {
            is($content->{count}, 3);
            is($content->{page}, 1);
            is(scalar @{$content->{items}}, 3);
            is($content->{prev_page}, undef);
        }
        }
        is($content->{pages}, 1);
        if ($param eq 'per_page') {
        if ($value eq '30') {
            is($content->{per_page}, 30);
        } else {
            is($content->{per_page}, 20);
        }
        }
        is($content->{total}, 3);
        is($content->{next_page}, undef);
    }
    }
}

done_testing;