use JSON instead of JSON::Any to get rid of the CPAN Testers failures when only JSON...
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / 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 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
10
11 use RestTest;
12 use DBICTest;
13 use Test::More;
14 use Test::WWW::Mechanize::Catalyst 'RestTest';
15 use HTTP::Request::Common;
16 use JSON;
17
18 my $json = JSON->new->utf8;
19
20 my $mech = Test::WWW::Mechanize::Catalyst->new;
21 ok( my $schema = DBICTest->init_schema(), 'got schema' );
22
23 my $artist_create_url     = "$base/api/rpc/artist/create";
24 my $any_artist_create_url = "$base/api/rpc/any/artist/create";
25 my $producer_create_url   = "$base/api/rpc/producer/create";
26
27 # test validation when no params sent
28 {
29     my $req = POST(
30         $artist_create_url,
31         { wrong_param => 'value' },
32         'Accept' => 'text/json'
33     );
34     $mech->request( $req, $content_type );
35     cmp_ok( $mech->status, '==', 400,
36         'attempt without required params caught' );
37     my $response = $json->decode( $mech->content );
38     like(
39         $response->{messages}->[0],
40         qr/No value supplied for name and no default/,
41         'correct message returned'
42     );
43 }
44
45 # test default value used if default value exists
46 {
47     my $req = POST(
48         $producer_create_url,
49         {
50
51         },
52         'Accept' => 'text/json'
53     );
54     $mech->request( $req, $content_type );
55     cmp_ok( $mech->status, '==', 200,
56         'default value used when not supplied' );
57     ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
58         'record created with default name' );
59 }
60
61 # test create works as expected when passing required value
62 {
63     my $req = POST(
64         $producer_create_url,
65         { name => 'king luke' },
66         'Accept' => 'text/json'
67     );
68     $mech->request( $req, $content_type );
69     cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
70
71     my $new_obj =
72         $schema->resultset('Producer')->find( { name => 'king luke' } );
73     ok( $new_obj, 'record created with specified name' );
74
75     my $response = $json->decode( $mech->content );
76     is_deeply(
77         $response->{list},
78         { $new_obj->get_columns },
79         'json for new producer returned'
80     );
81 }
82
83 # test stash config handling
84 {
85     my $req = POST(
86         $any_artist_create_url,
87         { name => 'queen monkey' },
88         'Accept' => 'text/json'
89     );
90     $mech->request( $req, $content_type );
91     cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
92
93     my $new_obj =
94         $schema->resultset('Artist')->find( { name => 'queen monkey' } );
95     ok( $new_obj, 'record created with specified name' );
96
97     my $response = $json->decode( $mech->content );
98     is_deeply(
99         $response,
100         { success => 'true' },
101         'json for new artist returned'
102     );
103 }
104
105 # test create returns an error as expected when passing invalid value
106 {
107     my $long_string = '-' x 1024;
108
109     my $req = POST(
110         $producer_create_url,
111         {   producerid => $long_string,
112             name       => $long_string,
113         },
114         'Accept' => 'text/json'
115     );
116     $mech->request( $req, $content_type );
117     cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );
118 }
119
120 done_testing();