correct resultset_class param name in the API.pm synopsis
[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;
0b0bf911 14use JSON;
15
16my $json = JSON->new->utf8;
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{
0b0bf911 105 my $long_string = '-' x 1024;
106
107 my $req = POST(
108 $producer_create_url,
109 { producerid => $long_string,
110 name => $long_string,
111 },
112 'Accept' => 'text/json'
113 );
114 $mech->request( $req, $content_type );
115 cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );
d2739840 116}
117
118done_testing();