8 my $base = 'http://localhost';
9 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
13 use Test::More tests => 16;
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/rest/track/" . $track->id;
26 # test invalid track id caught
28 foreach my $wrong_id ('sdsdsdsd', 3434234) {
29 my $incorrect_url = "$base/api/rest/track/" . $wrong_id;
30 my $test_data = JSON::Any->Dump({ title => 'value' });
31 my $req = POST( $incorrect_url, Content => $test_data );
32 $req->content_type('text/x-json');
35 cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
37 my $response = JSON::Any->Load( $mech->content);
38 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
40 $track->discard_changes;
41 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
45 # validation when no params sent
47 my $test_data = JSON::Any->Dump({ wrong_param => 'value' });
48 my $req = POST( $track_update_url, Content => $test_data );
49 $req->content_type('text/x-json');
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 $test_data = JSON::Any->Dump({ title => undef });
63 my $req = POST( $track_update_url, Content => $test_data );
64 $req->content_type('text/x-json');
66 cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
68 $track->discard_changes;
69 isnt($track->title, $original_cols{title}, 'Title changed');
70 is($track->title, undef, 'Title changed to undef');
74 my $test_data = JSON::Any->Dump({ title => 'monkey monkey', 'cd' => { year => 2009 } });
75 my $req = POST( $track_update_url, Content => $test_data );
76 $req->content_type('text/x-json');
79 cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
81 $track->discard_changes;
82 is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
83 is($track->cd->year, 2009, 'related row updated');