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_update_url = "$base/api/rpc/track/id/" . $track->id . "/update";
25 my $any_track_update_url = "$base/api/rpc/any/track/id/" . $track->id . "/update";
26 my $tracks_update_url = "$base/api/rpc/track/update";
28 # test invalid track id caught
30 foreach my $wrong_id ('sdsdsdsd', 3434234) {
31 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
32 my $req = POST( $incorrect_url, {
36 $mech->request($req, $content_type);
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 $req = POST( $track_update_url, {
50 wrong_param => 'value'
52 $mech->request($req, $content_type);
53 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
55 my $response = JSON::Any->Load( $mech->content);
56 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
58 $track->discard_changes;
59 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
63 my $req = POST( $track_update_url, {
64 wrong_param => 'value'
66 $mech->request($req, $content_type);
67 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
69 my $response = JSON::Any->Load( $mech->content);
70 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
72 $track->discard_changes;
73 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
77 my $req = POST( $track_update_url, {
80 $mech->request($req, $content_type);
81 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
83 $track->discard_changes;
84 isnt($track->title, $original_cols{title}, 'Title changed');
85 is($track->title, '', 'Title changed to undef');
89 my $req = POST( $track_update_url, {
90 title => 'monkey monkey'
92 $mech->request($req, $content_type);
93 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
95 $track->discard_changes;
96 is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
100 my $req = POST( $track_update_url, {
101 title => 'sheep sheep',
104 $mech->request($req, $content_type);
105 cmp_ok( $mech->status, '==', 200, 'Update with key with value and related key okay' );
107 $track->discard_changes;
108 is($track->title, 'sheep sheep', 'Title changed');
109 is($track->cd->year, '2009', 'Related field changed"');
113 my $req = POST( $any_track_update_url, {
116 $mech->request($req, $content_type);
117 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
119 $track->discard_changes;
120 is($track->title, 'baa', 'Title changed');
124 my $req = POST( $any_track_update_url, {
127 $mech->request($req, $content_type);
128 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
130 $track->discard_changes;
131 is($track->get_column('position'), '14', 'Position changed');
135 # order to get a stable order of rows
136 my $tracks_rs = $schema->resultset('Track')->search(undef, { order_by => 'trackid', rows => 3 });
137 my $test_data = JSON::Any->Dump({ list => [map +{ id => $_->id, title => 'Track ' . $_->id }, $tracks_rs->all] });
138 my $req = POST( $tracks_update_url, Content => $test_data );
139 $req->content_type('text/x-json');
140 $mech->request($req);
141 cmp_ok( $mech->status, '==', 200, 'Attempt to update three tracks ok' );
144 while (my $track = $tracks_rs->next) {
145 is($track->title, 'Track ' . $track->id, 'Title changed');