8 my $base = 'http://localhost';
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
18 my $json = JSON->new->utf8;
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
23 my $artist_list_url = "$base/api/rest/artist";
24 my $filtered_artist_list_url = "$base/api/rest/bound_artist";
25 my $producer_list_url = "$base/api/rest/producer";
26 my $cd_list_url = "$base/api/rest/cd";
27 my $track_list_url = "$base/api/rest/track";
36 'Accept' => 'text/x-json'
39 cmp_ok( $mech->status, '==', 200, 'open attempt okay' );
40 my @expected_response = map {
42 } $schema->resultset('Artist')->all;
43 my $response = $json->decode( $mech->content );
46 { list => \@expected_response, success => 'true' },
47 'correct message returned'
52 my $uri = URI->new($artist_list_url);
53 $uri->query_form( { 'search.artistid' => 1 } );
54 my $req = GET( $uri, 'Accept' => 'text/x-json' );
56 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
58 my @expected_response = map {
60 } $schema->resultset('Artist')->search( { artistid => 1 } )->all;
61 my $response = $json->decode( $mech->content );
64 { list => \@expected_response, success => 'true' },
65 'correct data returned'
70 my $uri = URI->new($artist_list_url);
71 $uri->query_form( { 'search.name.LIKE' => '%waul%' } );
72 my $req = GET( $uri, 'Accept' => 'text/x-json' );
74 cmp_ok( $mech->status, '==', 200, 'attempt with basic search okay' );
76 my @expected_response = map {
78 } $schema->resultset('Artist')
79 ->search( { name => { LIKE => '%waul%' } } )->all;
80 my $response = $json->decode( $mech->content );
83 { list => \@expected_response, success => 'true' },
84 'correct data returned for complex query'
89 my $uri = URI->new($producer_list_url);
90 my $req = GET( $uri, 'Accept' => 'text/x-json' );
92 cmp_ok( $mech->status, '==', 200, 'open producer request okay' );
94 my @expected_response = map {
96 } $schema->resultset('Producer')->search( {}, { select => ['name'] } )
98 my $response = $json->decode( $mech->content );
101 { list => \@expected_response, success => 'true' },
102 'correct data returned for class with list_returns specified'
107 my $uri = URI->new($artist_list_url);
108 $uri->query_form( { 'search.cds.title' => 'Forkful of bees' } );
109 my $req = GET( $uri, 'Accept' => 'text/x-json' );
110 $mech->request($req);
111 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
113 my @expected_response = map {
115 } $schema->resultset('Artist')
116 ->search( { 'cds.title' => 'Forkful of bees' }, { join => 'cds' } )
118 my $response = $json->decode( $mech->content );
121 { list => \@expected_response, success => 'true' },
122 'correct data returned for class with select specified'
127 my $uri = URI->new($artist_list_url);
129 { 'search.cds.title' => 'Forkful of bees',
130 'list_returns.0.count' => '*',
134 my $req = GET( $uri, 'Accept' => 'text/x-json' );
135 $mech->request($req);
136 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
138 my @expected_response = map {
140 } $schema->resultset('Artist')
141 ->search( { 'cds.title' => 'Forkful of bees' },
142 { select => [ { count => '*' } ], as => ['count'], join => 'cds' } )
144 my $response = $json->decode( $mech->content );
147 { list => \@expected_response, success => 'true' },
148 'correct data returned for count'
153 my $uri = URI->new($filtered_artist_list_url);
154 $uri->query_form( { 'search.artistid' => '2' } );
155 my $req = GET( $uri, 'Accept' => 'text/x-json' );
156 $mech->request($req);
157 cmp_ok( $mech->status, '==', 200, 'search related request okay' );
158 my $response = $json->decode( $mech->content );
159 my @expected_response = map {
161 } $schema->resultset('Artist')->search( { 'artistid' => '1' } )->all;
164 { list => \@expected_response, success => 'true' },
165 'correct data returned for class with setup_list_method specified'
170 my $uri = URI->new($cd_list_url);
172 { 'search.tracks.position' => '1',
173 'search.artist.name' => 'Caterwauler McCrae'
176 my $req = GET( $uri, 'Accept' => 'text/x-json' );
177 $mech->request($req);
178 cmp_ok( $mech->status, '==', 200, 'search multiple params request okay' );
179 my $response = $json->decode( $mech->content );
180 my @expected_response = map {
182 } $schema->resultset('CD')->search(
183 { 'artist.name' => 'Caterwauler McCrae',
184 'tracks.position' => 1,
186 { join => [qw/ artist tracks /], }
190 { list => \@expected_response, success => 'true' },
191 'correct data returned for multiple search params'
195 # page specified in controller config (RT#56226)
197 my $uri = URI->new($track_list_url);
199 my $req = GET( $uri, 'Accept' => 'text/x-json' );
200 $mech->request($req);
201 cmp_ok( $mech->status, '==', 200, 'get first page ok' );
202 my $response = $json->decode( $mech->content );
203 my @expected_response = map {
205 } $schema->resultset('Track')->search( undef, { page => 1, } )->all;
208 { list => \@expected_response, success => 'true', totalcount => 15 },
209 'correct data returned for static configured paging'
214 my $uri = URI->new($artist_list_url);
215 $uri->query_form( { 'search.cds.track.title' => 'Suicidal' } );
216 my $req = GET( $uri, 'Accept' => 'text/x-json' );
217 $mech->request($req);
218 cmp_ok( $mech->status, '==', 400,
219 'attempt with nonexisting relationship fails' );
220 my $response = $json->decode( $mech->content );
222 $response->{messages},
223 ['track is neither a relationship nor a column'],
224 'correct error message returned'
229 my $uri = URI->new($artist_list_url);
230 $uri->query_form( { 'search.cds.tracks.foo' => 'Bar' } );
231 my $req = GET( $uri, 'Accept' => 'text/x-json' );
232 $mech->request($req);
233 cmp_ok( $mech->status, '==', 400,
234 'attempt with nonexisting column fails' );
235 my $response = $json->decode( $mech->content );
237 $response->{messages},
238 ['a database error has occured.'],
239 'correct error message returned'
244 my $uri = URI->new($artist_list_url);
245 $uri->query_form( { 'search.cds.tracks.title.like' => 'Boring%' } );
246 my $req = GET( $uri, 'Accept' => 'text/x-json' );
247 $mech->request($req);
248 cmp_ok( $mech->status, '==', 200, 'attempt with sql function ok' );
249 my $response = $json->decode( $mech->content );
250 my @expected_response = map {
252 } $schema->resultset('Artist')
253 ->search( { 'tracks.title' => { 'like' => 'Boring%' }, },
254 { join => { cds => 'tracks' }, } )->all;
257 { list => \@expected_response, success => 'true' },
258 'correct data returned for search with sql function'