8 my $base = 'http://localhost';
9 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok(my $schema = DBICTest->init_schema(), 'got schema');
21 my $track = $schema->resultset('Track')->first;
22 my %original_cols = $track->get_columns;
24 my $track_url = "$base/api/rest/track/";
25 my $track_update_url = $track_url . $track->id;
26 my $tracks_update_url = $track_url;
28 # test invalid track id caught
30 foreach my $wrong_id ('sdsdsdsd', 3434234) {
31 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
32 my $test_data = JSON::Any->Dump({ title => 'value' });
33 my $req = POST( $incorrect_url, Content => $test_data );
34 $req->content_type('text/x-json');
37 cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
39 my $response = JSON::Any->Load( $mech->content);
40 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
42 $track->discard_changes;
43 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
47 # validation when no params sent
49 my $test_data = JSON::Any->Dump({ wrong_param => 'value' });
50 my $req = POST( $track_update_url, Content => $test_data );
51 $req->content_type('text/x-json');
54 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
56 my $response = JSON::Any->Load( $mech->content);
57 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
59 $track->discard_changes;
60 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
64 my $test_data = JSON::Any->Dump({ title => undef });
65 my $req = POST( $track_update_url, Content => $test_data );
66 $req->content_type('text/x-json');
68 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
70 $track->discard_changes;
71 isnt($track->title, $original_cols{title}, 'Title changed');
72 is($track->title, undef, 'Title changed to undef');
76 my $test_data = JSON::Any->Dump({ title => 'monkey monkey', 'cd' => { year => 2009 } });
77 my $req = POST( $track_update_url, Content => $test_data );
78 $req->content_type('text/x-json');
81 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
83 $track->discard_changes;
84 is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
85 is($track->cd->year, 2009, 'related row updated');
89 # order to get a stable order of rows
90 my $tracks_rs = $schema->resultset('Track')->search(undef, { order_by => 'trackid', rows => 3 });
91 my $test_data = JSON::Any->Dump({ list => [map +{ id => $_->id, title => 'Track ' . $_->id }, $tracks_rs->all] });
92 my $req = PUT( $tracks_update_url, Content => $test_data );
93 $req->content_type('text/x-json');
95 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
98 while (my $track = $tracks_rs->next) {
99 is($track->title, 'Track ' . $track->id, 'Title changed');