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
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;
14use Test::WWW::Mechanize::Catalyst 'RestTest';
15use HTTP::Request::Common;
0b0bf911 16use JSON;
17
18my $json = JSON->new->utf8;
d2739840 19
20my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 21ok( my $schema = DBICTest->init_schema(), 'got schema' );
d2739840 22
0b0bf911 23my $artist_create_url = "$base/api/rpc/artist/create";
d2739840 24my $any_artist_create_url = "$base/api/rpc/any/artist/create";
0b0bf911 25my $producer_create_url = "$base/api/rpc/producer/create";
d2739840 26
27# test validation when no params sent
28{
0b0bf911 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 );
d2739840 43}
44
45# test default value used if default value exists
46{
0b0bf911 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' );
d2739840 59}
60
61# test create works as expected when passing required value
62{
0b0bf911 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 );
d2739840 81}
82
83# test stash config handling
84{
0b0bf911 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 );
d2739840 103}
104
105# test create returns an error as expected when passing invalid value
106{
0b0bf911 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' );
d2739840 118}
119
120done_testing();