Fixed some typos and code cleanups
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / update.t
CommitLineData
d2739840 1use 5.6.0;
2
3use strict;
4use warnings;
5
6use lib 't/lib';
7
8my $base = 'http://localhost';
9my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
10
11use RestTest;
12use DBICTest;
13use Test::More tests => 15;
14use Test::WWW::Mechanize::Catalyst 'RestTest';
15use HTTP::Request::Common;
16use JSON::Any;
17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
19ok(my $schema = DBICTest->init_schema(), 'got schema');
20
21my $track = $schema->resultset('Track')->first;
22my %original_cols = $track->get_columns;
23
24my $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' });
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}