ad4c02578b643c9249dadccb7d83739e5ee8ecfd
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / update.t
1 use 5.6.0;
2
3 use strict;
4 use warnings;
5
6 use lib 't/lib';
7
8 my $base = 'http://localhost';
9 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
10
11 use RestTest;
12 use DBICTest;
13 use Test::More tests => 16;
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
16 use JSON::Any;
17
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok(my $schema = DBICTest->init_schema(), 'got schema');
20
21 my $track = $schema->resultset('Track')->first;
22 my %original_cols = $track->get_columns;
23
24 my $track_update_url = "$base/api/rest/track/" . $track->id;
25
26 # test invalid track id caught
27 {
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');
33                 $mech->request($req);
34
35                 cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' );
36                 
37                 my $response = JSON::Any->Load( $mech->content);
38                 like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' );
39                 
40                 $track->discard_changes;
41                 is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
42         }
43 }
44
45 # validation when no params sent
46 {
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');
50         $mech->request($req);
51
52         cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' );
53
54         my $response = JSON::Any->Load( $mech->content);
55         is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' );
56
57         $track->discard_changes;
58         is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred');
59 }
60
61 {
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');
65         $mech->request($req);
66         cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' );
67
68         $track->discard_changes;
69         isnt($track->title, $original_cols{title}, 'Title changed');
70         is($track->title, undef, 'Title changed to undef');
71 }
72
73 {
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');
77         $mech->request($req);
78
79         cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' );
80
81         $track->discard_changes;
82         is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"');
83         is($track->cd->year, 2009, 'related row updated');
84 }