Version 2.008001
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / create.t
CommitLineData
d2739840 1use strict;
2use warnings;
3
4use lib 't/lib';
5
6my $base = 'http://localhost';
7my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
8
9use RestTest;
10use DBICTest;
11use Test::More;
12use Test::WWW::Mechanize::Catalyst 'RestTest';
13use HTTP::Request::Common;
a5949bfd 14use JSON::MaybeXS;
0b0bf911 15
a5949bfd 16my $json = JSON::MaybeXS->new(utf8 => 1);
d2739840 17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
0b0bf911 19ok( my $schema = DBICTest->init_schema(), 'got schema' );
d2739840 20
0b0bf911 21my $artist_create_url = "$base/api/rpc/artist/create";
d2739840 22my $any_artist_create_url = "$base/api/rpc/any/artist/create";
0b0bf911 23my $producer_create_url = "$base/api/rpc/producer/create";
d2739840 24
25# test validation when no params sent
26{
0b0bf911 27 my $req = POST(
28 $artist_create_url,
29 { wrong_param => 'value' },
30 'Accept' => 'text/json'
31 );
32 $mech->request( $req, $content_type );
33 cmp_ok( $mech->status, '==', 400,
34 'attempt without required params caught' );
35 my $response = $json->decode( $mech->content );
36 like(
37 $response->{messages}->[0],
38 qr/No value supplied for name and no default/,
39 'correct message returned'
40 );
d2739840 41}
42
43# test default value used if default value exists
44{
0b0bf911 45 my $req = POST(
46 $producer_create_url,
47 {
48
49 },
50 'Accept' => 'text/json'
51 );
52 $mech->request( $req, $content_type );
53 cmp_ok( $mech->status, '==', 200,
54 'default value used when not supplied' );
55 ok( $schema->resultset('Producer')->find( { name => 'fred' } ),
56 'record created with default name' );
d2739840 57}
58
59# test create works as expected when passing required value
60{
0b0bf911 61 my $req = POST(
62 $producer_create_url,
63 { name => 'king luke' },
64 'Accept' => 'text/json'
65 );
66 $mech->request( $req, $content_type );
67 cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
68
69 my $new_obj =
70 $schema->resultset('Producer')->find( { name => 'king luke' } );
71 ok( $new_obj, 'record created with specified name' );
72
73 my $response = $json->decode( $mech->content );
74 is_deeply(
75 $response->{list},
76 { $new_obj->get_columns },
77 'json for new producer returned'
78 );
d2739840 79}
80
81# test stash config handling
82{
0b0bf911 83 my $req = POST(
84 $any_artist_create_url,
85 { name => 'queen monkey' },
86 'Accept' => 'text/json'
87 );
88 $mech->request( $req, $content_type );
89 cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
90
91 my $new_obj =
92 $schema->resultset('Artist')->find( { name => 'queen monkey' } );
93 ok( $new_obj, 'record created with specified name' );
94
95 my $response = $json->decode( $mech->content );
96 is_deeply(
97 $response,
98 { success => 'true' },
99 'json for new artist returned'
100 );
d2739840 101}
102
103# test create returns an error as expected when passing invalid value
104{
fd6fb6c9 105 my $data = {
106 producerid => 100,
107 name => 'Producer',
108 };
0b0bf911 109
fd6fb6c9 110 my $req = POST($producer_create_url, $data, 'Accept' => 'text/json');
111 $mech->request( $req, $content_type );
112 cmp_ok( $mech->status, '==', 200, 'create with pk value ok' );
113 my $response = $json->decode( $mech->content );
114 is_deeply(
115 $response,
116 { success => 'true', list => $data },
117 'json for producer with pk value ok'
0b0bf911 118 );
fd6fb6c9 119 # try to insert same data again, as this seems to be the only way to
120 # force an insert to fail for SQLite.
121 # It accepts too long columns as well as wrong datatypes without errors.
122 # The bind with datatype of newer DBIC versions numifies non-integer
123 # values passed as pk value too.
0b0bf911 124 $mech->request( $req, $content_type );
125 cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );
d2739840 126}
127
128done_testing();