7d7fe41fa288e5623680d6c1c087ef2e9af4c479
[catagits/Catalyst-Controller-DBIC-API.git] / t / rpc / create.t
1 use strict;
2 use warnings;
3
4 use lib 't/lib';
5
6 my $base = 'http://localhost';
7 my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ];
8
9 use RestTest;
10 use DBICTest;
11 use Test::More;
12 use Test::WWW::Mechanize::Catalyst 'RestTest';
13 use HTTP::Request::Common;
14 use JSON;
15
16 my $json = JSON->new->utf8;
17
18 my $mech = Test::WWW::Mechanize::Catalyst->new;
19 ok( my $schema = DBICTest->init_schema(), 'got schema' );
20
21 my $artist_create_url     = "$base/api/rpc/artist/create";
22 my $any_artist_create_url = "$base/api/rpc/any/artist/create";
23 my $producer_create_url   = "$base/api/rpc/producer/create";
24
25 # test validation when no params sent
26 {
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     );
41 }
42
43 # test default value used if default value exists
44 {
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' );
57 }
58
59 # test create works as expected when passing required value
60 {
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     );
79 }
80
81 # test stash config handling
82 {
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     );
101 }
102
103 # test create returns an error as expected when passing invalid value
104 {
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' );
116 }
117
118 done_testing();