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";
27 # test invalid track id caught
29 foreach my $wrong_id ('sdsdsdsd', 3434234) {
30 my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update";
31 my $req = POST( $incorrect_url, {
35 $mech->request($req, $content_type);
36 cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
38 my $response = JSON::Any->Load( $mech->content);
39 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
41 $track->discard_changes;
42 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
46 # validation when no params sent
48 my $req = POST( $track_update_url, {
49 wrong_param => 'value'
51 $mech->request($req, $content_type);
52 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
54 my $response = JSON::Any->Load( $mech->content);
55 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
57 $track->discard_changes;
58 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
62 my $req = POST( $track_update_url, {
63 wrong_param => 'value'
65 $mech->request($req, $content_type);
66 cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
68 my $response = JSON::Any->Load( $mech->content);
69 is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
71 $track->discard_changes;
72 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
76 my $req = POST( $track_update_url, {
79 $mech->request($req, $content_type);
80 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
82 $track->discard_changes;
83 isnt($track->title, $original_cols{title}, 'Title changed');
84 is($track->title, '', 'Title changed to undef');
88 my $req = POST( $track_update_url, {
89 title => 'monkey monkey'
91 $mech->request($req, $content_type);
92 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
94 $track->discard_changes;
95 is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
99 my $req = POST( $track_update_url, {
100 title => 'sheep sheep',
103 $mech->request($req, $content_type);
104 cmp_ok( $mech->status, '==', 200, 'Update with key with value and related key okay' );
106 $track->discard_changes;
107 is($track->title, 'sheep sheep', 'Title changed');
108 is($track->cd->year, '2009', 'Related field changed"');
112 my $req = POST( $any_track_update_url, {
115 $mech->request($req, $content_type);
116 cmp_ok( $mech->status, '==', 200, 'Stash update okay' );
118 $track->discard_changes;
119 is($track->title, 'baa', 'Title changed');
123 my $req = POST( $any_track_update_url, {
126 $mech->request($req, $content_type);
127 cmp_ok( $mech->status, '==', 200, 'Position update okay' );
129 $track->discard_changes;
130 is($track->get_column('position'), '14', 'Position changed');