cleaned up delete tests
[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;
16use JSON::Any;
17
18my $mech = Test::WWW::Mechanize::Catalyst->new;
19ok(my $schema = DBICTest->init_schema(), 'got schema');
20
21my $artist_create_url = "$base/api/rpc/artist/create";
22my $any_artist_create_url = "$base/api/rpc/any/artist/create";
23my $producer_create_url = "$base/api/rpc/producer/create";
24
25# test validation when no params sent
26{
27 my $req = POST( $artist_create_url, {
28 wrong_param => 'value'
29 }, 'Accept' => 'text/json' );
30 $mech->request($req, $content_type);
31 cmp_ok( $mech->status, '==', 400, 'attempt without required params caught' );
32 my $response = JSON::Any->Load( $mech->content);
33 like( $response->{messages}->[0], qr/No value supplied for name and no default/, 'correct message returned' );
34}
35
36# test default value used if default value exists
37{
38 my $req = POST( $producer_create_url, {
39
40 }, 'Accept' => 'text/json' );
41 $mech->request($req, $content_type);
42 cmp_ok( $mech->status, '==', 200, 'default value used when not supplied' );
43 ok($schema->resultset('Producer')->find({ name => 'fred' }), 'record created with default name');
44}
45
46# test create works as expected when passing required value
47{
48 my $req = POST( $producer_create_url, {
49 name => 'king luke'
50 }, 'Accept' => 'text/json' );
51 $mech->request($req, $content_type);
52 cmp_ok( $mech->status, '==', 200, 'param value used when supplied' );
53
54 my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' });
55 ok($new_obj, 'record created with specified name');
56
57 my $response = JSON::Any->Load( $mech->content);
58 is_deeply( $response->{list}, { $new_obj->get_columns }, 'json for new producer returned' );
59}
60
61# test stash config handling
62{
d2739840 63 my $req = POST( $any_artist_create_url, {
64 name => 'queen monkey'
65 }, 'Accept' => 'text/json' );
66 $mech->request($req, $content_type);
67 cmp_ok( $mech->status, '==', 200, 'stashed config okay' );
68
69 my $new_obj = $schema->resultset('Artist')->find({ name => 'queen monkey' });
70 ok($new_obj, 'record created with specified name');
71
72 my $response = JSON::Any->Load( $mech->content);
73 is_deeply( $response, { success => 'true' }, 'json for new artist returned' );
74}
75
76# test create returns an error as expected when passing invalid value
77{
78 my $long_string = '-' x 1024;
79
80 my $req = POST( $producer_create_url, {
81 producerid => $long_string,
82 name => $long_string,
83 }, 'Accept' => 'text/json' );
84 $mech->request($req, $content_type);
85 cmp_ok( $mech->status, '==', 400, 'invalid param value produces error' );
86}
87
88done_testing();