Commit | Line | Data |
d2739840 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use lib 't/lib'; |
5 | |
6 | my $base = 'http://localhost'; |
7 | |
8 | use RestTest; |
9 | use DBICTest; |
10 | use URI; |
11 | use Test::More; |
12 | use Test::WWW::Mechanize::Catalyst 'RestTest'; |
13 | use HTTP::Request::Common; |
0b0bf911 |
14 | use JSON; |
15 | |
16 | my $json = JSON->new->utf8; |
d2739840 |
17 | |
18 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
0b0bf911 |
19 | ok( my $schema = DBICTest->init_schema(), 'got schema' ); |
d2739840 |
20 | |
0b0bf911 |
21 | my $artist_list_url = "$base/api/rest/artist"; |
d2739840 |
22 | my $filtered_artist_list_url = "$base/api/rest/bound_artist"; |
0b0bf911 |
23 | my $producer_list_url = "$base/api/rest/producer"; |
24 | my $cd_list_url = "$base/api/rest/cd"; |
25 | my $track_list_url = "$base/api/rest/track"; |
d2739840 |
26 | |
27 | # test open request |
28 | { |
0b0bf911 |
29 | my $req = GET( |
30 | $artist_list_url, |
31 | { |
32 | |
33 | }, |
34 | 'Accept' => 'text/x-json' |
35 | ); |
36 | $mech->request($req); |
37 | cmp_ok( $mech->status, '==', 200, 'open attempt okay' ); |
38 | my @expected_response = map { |
39 | { $_->get_columns } |
40 | } $schema->resultset('Artist')->all; |
41 | my $response = $json->decode( $mech->content ); |
42 | is_deeply( |
43 | $response, |
44 | { list => \@expected_response, success => 'true' }, |
45 | 'correct message returned' |
46 | ); |
d2739840 |
47 | } |
48 | |
49 | { |
0b0bf911 |
50 | my $uri = URI->new($artist_list_url); |
51 | $uri->query_form( { 'search.artistid' => 1 } ); |
52 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
53 | $mech->request($req); |
54 | cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' ); |
55 | |
56 | my @expected_response = map { |
57 | { $_->get_columns } |
58 | } $schema->resultset('Artist')->search( { artistid => 1 } )->all; |
59 | my $response = $json->decode( $mech->content ); |
60 | is_deeply( |
61 | $response, |
62 | { list => \@expected_response, success => 'true' }, |
63 | 'correct data returned' |
64 | ); |
d2739840 |
65 | } |
66 | |
67 | { |
0b0bf911 |
68 | my $uri = URI->new($artist_list_url); |
69 | $uri->query_form( { 'search.name.LIKE' => '%waul%' } ); |
70 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
71 | $mech->request($req); |
72 | cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' ); |
73 | |
74 | my @expected_response = map { |
75 | { $_->get_columns } |
76 | } $schema->resultset('Artist') |
77 | ->search( { name => { LIKE => '%waul%' } } )->all; |
78 | my $response = $json->decode( $mech->content ); |
79 | is_deeply( |
80 | $response, |
81 | { list => \@expected_response, success => 'true' }, |
82 | 'correct data returned for complex query' |
83 | ); |
d2739840 |
84 | } |
85 | |
86 | { |
0b0bf911 |
87 | my $uri = URI->new($producer_list_url); |
88 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
89 | $mech->request($req); |
90 | cmp_ok( $mech->status, '==', 200, 'open producer request okay' ); |
91 | |
92 | my @expected_response = map { |
93 | { $_->get_columns } |
94 | } $schema->resultset('Producer')->search( {}, { select => ['name'] } ) |
95 | ->all; |
96 | my $response = $json->decode( $mech->content ); |
97 | is_deeply( |
98 | $response, |
99 | { list => \@expected_response, success => 'true' }, |
100 | 'correct data returned for class with list_returns specified' |
101 | ); |
d2739840 |
102 | } |
103 | |
104 | { |
0b0bf911 |
105 | my $uri = URI->new($artist_list_url); |
106 | $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } ); |
107 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
108 | $mech->request($req); |
109 | cmp_ok( $mech->status, '==', 200, 'search related request okay' ); |
110 | |
111 | my @expected_response = map { |
112 | { $_->get_columns } |
113 | } $schema->resultset('Artist') |
114 | ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } ) |
115 | ->all; |
116 | my $response = $json->decode( $mech->content ); |
117 | is_deeply( |
118 | $response, |
119 | { list => \@expected_response, success => 'true' }, |
120 | 'correct data returned for class with select specified' |
121 | ); |
d2739840 |
122 | } |
123 | |
124 | { |
0b0bf911 |
125 | my $uri = URI->new($artist_list_url); |
126 | $uri->query_form( |
127 | { 'search.cds.title' => 'Forkful of bees', |
128 | 'list_returns.0.count' => '*', |
129 | 'as.0' => 'count' |
130 | } |
131 | ); |
132 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
133 | $mech->request($req); |
134 | cmp_ok( $mech->status, '==', 200, 'search related request okay' ); |
135 | |
136 | my @expected_response = map { |
137 | { $_->get_columns } |
138 | } $schema->resultset('Artist') |
139 | ->search( { 'cds.title' => 'Forkful of bees' }, |
140 | { select => [ { count => '*' } ], as => ['count'], join => 'cds' } ) |
141 | ->all; |
142 | my $response = $json->decode( $mech->content ); |
143 | is_deeply( |
144 | $response, |
145 | { list => \@expected_response, success => 'true' }, |
146 | 'correct data returned for count' |
147 | ); |
d2739840 |
148 | } |
149 | |
150 | { |
0b0bf911 |
151 | my $uri = URI->new($filtered_artist_list_url); |
152 | $uri->query_form( { 'search.artistid' => '2' } ); |
153 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
154 | $mech->request($req); |
155 | cmp_ok( $mech->status, '==', 200, 'search related request okay' ); |
156 | my $response = $json->decode( $mech->content ); |
157 | my @expected_response = map { |
158 | { $_->get_columns } |
159 | } $schema->resultset('Artist')->search( { 'artistid' => '1' } )->all; |
160 | is_deeply( |
161 | $response, |
162 | { list => \@expected_response, success => 'true' }, |
163 | 'correct data returned for class with setup_list_method specified' |
164 | ); |
d2739840 |
165 | } |
166 | |
02b625cd |
167 | { |
0b0bf911 |
168 | my $uri = URI->new($cd_list_url); |
169 | $uri->query_form( |
170 | { 'search.tracks.position' => '1', |
171 | 'search.artist.name' => 'Caterwauler McCrae' |
172 | } |
173 | ); |
174 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
175 | $mech->request($req); |
176 | cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' ); |
177 | my $response = $json->decode( $mech->content ); |
178 | my @expected_response = map { |
179 | { $_->get_columns } |
180 | } $schema->resultset('CD')->search( |
181 | { 'artist.name' => 'Caterwauler McCrae', |
182 | 'tracks.position' => 1, |
183 | }, |
184 | { join => [qw/ artist tracks /], } |
185 | )->all; |
186 | is_deeply( |
187 | $response, |
188 | { list => \@expected_response, success => 'true' }, |
189 | 'correct data returned for multiple search params' |
190 | ); |
02b625cd |
191 | } |
192 | |
4a805f62 |
193 | # page specified in controller config (RT#56226) |
194 | { |
0b0bf911 |
195 | my $uri = URI->new($track_list_url); |
4a805f62 |
196 | $uri->query_form(); |
197 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
198 | $mech->request($req); |
199 | cmp_ok( $mech->status, '==', 200, 'get first page ok' ); |
0b0bf911 |
200 | my $response = $json->decode( $mech->content ); |
201 | my @expected_response = map { |
202 | { $_->get_columns } |
203 | } $schema->resultset('Track')->search( undef, { page => 1, } )->all; |
204 | is_deeply( |
205 | $response, |
206 | { list => \@expected_response, success => 'true', totalcount => 15 }, |
207 | 'correct data returned for static configured paging' |
208 | ); |
4a805f62 |
209 | } |
210 | |
11ba2ccc |
211 | { |
0b0bf911 |
212 | my $uri = URI->new($artist_list_url); |
213 | $uri->query_form( { 'search.cds.track.title' => 'Suicidal' } ); |
11ba2ccc |
214 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
215 | $mech->request($req); |
0b0bf911 |
216 | cmp_ok( $mech->status, '==', 400, |
217 | 'attempt with nonexisting relationship fails' ); |
218 | my $response = $json->decode( $mech->content ); |
219 | is_deeply( |
220 | $response->{messages}, |
221 | ['track is neither a relationship nor a column'], |
222 | 'correct error message returned' |
223 | ); |
11ba2ccc |
224 | } |
225 | |
226 | { |
0b0bf911 |
227 | my $uri = URI->new($artist_list_url); |
228 | $uri->query_form( { 'search.cds.tracks.foo' => 'Bar' } ); |
11ba2ccc |
229 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
230 | $mech->request($req); |
0b0bf911 |
231 | cmp_ok( $mech->status, '==', 400, |
232 | 'attempt with nonexisting column fails' ); |
233 | my $response = $json->decode( $mech->content ); |
234 | is_deeply( |
235 | $response->{messages}, |
236 | ['a database error has occured.'], |
237 | 'correct error message returned' |
238 | ); |
11ba2ccc |
239 | } |
240 | |
ff26acf7 |
241 | { |
0b0bf911 |
242 | my $uri = URI->new($artist_list_url); |
243 | $uri->query_form( { 'search.cds.tracks.title.like' => 'Boring%' } ); |
ff26acf7 |
244 | my $req = GET( $uri, 'Accept' => 'text/x-json' ); |
245 | $mech->request($req); |
246 | cmp_ok( $mech->status, '==', 200, 'attempt with sql function ok' ); |
0b0bf911 |
247 | my $response = $json->decode( $mech->content ); |
248 | my @expected_response = map { |
249 | { $_->get_columns } |
250 | } $schema->resultset('Artist') |
251 | ->search( { 'tracks.title' => { 'like' => 'Boring%' }, }, |
252 | { join => { cds => 'tracks' }, } )->all; |
253 | is_deeply( |
254 | $response, |
255 | { list => \@expected_response, success => 'true' }, |
256 | 'correct data returned for search with sql function' |
257 | ); |
ff26acf7 |
258 | } |
259 | |
d2739840 |
260 | done_testing(); |