use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rest / create.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
10 use RestTest;
11 use DBICTest;
12 use Test::More;
13 use Test::WWW::Mechanize::Catalyst 'RestTest';
14 use HTTP::Request::Common;
15 use JSON;
16
17 my $json = JSON->new->utf8;
18
19 my $mech = Test::WWW::Mechanize::Catalyst->new;
20 ok( my $schema = DBICTest->init_schema(), 'got schema' );
21
22 my $artist_create_url   = "$base/api/rest/artist";
23 my $producer_create_url = "$base/api/rest/producer";
24
25 # test validation when no params sent
26 {
27     my $test_data = $json->encode( { wrong_param => 'value' } );
28     my $req = PUT($artist_create_url);
29     $req->content_type('text/x-json');
30     $req->content_length(
31         do { use bytes; length($test_data) }
32     );
33     $req->content($test_data);
34     $mech->request($req);
35
36     cmp_ok( $mech->status, '==', 400,
37         'attempt without required params caught' );
38     my $response = $json->decode( $mech->content );
39     like(
40         $response->{messages}->[0],
41         qr/No value supplied for name and no default/,
42         'correct message returned'
43     );
44 }
45
46 # test default value used if default value exists
47 {
48     my $test_data = $json->encode( {} );
49     my $req = PUT($producer_create_url);
50     $req->content_type('text/x-json');
51     $req->content_length(
52         do { use bytes; length($test_data) }
53     );
54     $req->content($test_data);
55     $mech->request($req);
56     cmp_ok( $mech->status, '==', 200,
57         'default value used when not supplied' );
58     ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
59         'record created with default name' );
60 }
61
62 # test create works as expected when passing required value
63 {
64     my $test_data = $json->encode( { name => 'king luke' } );
65     my $req = PUT($producer_create_url);
66     $req->content_type('text/x-json');
67     $req->content_length(
68         do { use bytes; length($test_data) }
69     );
70     $req->content($test_data);
71     $mech->request($req);
72     cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
73     my $new_obj =
74         $schema->resultset('Producer')->find( { name => 'king luke' } );
75     ok( $new_obj, 'record created with specified name' );
76
77     my $response = $json->decode( $mech->content );
78     is_deeply(
79         $response->{list},
80         { $new_obj->get_columns },
81         'json for new producer returned'
82     );
83 }
84
85 # test bulk create
86 {
87     my $test_data = $json->encode(
88         { list => [ { name => 'king nperez' }, { name => 'queen perla' } ] }
89     );
90     my $req = PUT($producer_create_url);
91     $req->content_type('text/x-json');
92     $req->content_length(
93         do { use bytes; length($test_data) }
94     );
95     $req->content($test_data);
96     $mech->request($req);
97     cmp_ok( $mech->status, '==', 200, 'request with valid content okay' );
98     my $rs =
99         $schema->resultset('Producer')
100         ->search( [ { name => 'king nperez' }, { name => 'queen perla' } ] );
101     ok( $rs, 'record created with specified name' );
102
103     my $response = $json->decode( $mech->content );
104     my $expected =
105         [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ];
106     is_deeply( $response->{list}, $expected,
107         'json for bulk create returned' );
108 }
109
110 done_testing();