Commit | Line | Data |
d2739840 |
1 | use strict; |
2 | use warnings; |
3 | |
4 | use lib 't/lib'; |
5 | |
6 | my $base = 'http://localhost'; |
7 | |
8 | use RestTest; |
9 | use DBICTest; |
10 | use Test::More; |
11 | use Test::WWW::Mechanize::Catalyst 'RestTest'; |
12 | use HTTP::Request::Common; |
a5949bfd |
13 | use JSON::MaybeXS; |
0b0bf911 |
14 | |
a5949bfd |
15 | my $json = JSON::MaybeXS->new(utf8 => 1); |
d2739840 |
16 | |
17 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
0b0bf911 |
18 | ok( my $schema = DBICTest->init_schema(), 'got schema' ); |
d2739840 |
19 | |
0b0bf911 |
20 | my $artist_create_url = "$base/api/rest/artist"; |
d2739840 |
21 | my $producer_create_url = "$base/api/rest/producer"; |
7f3ed652 |
22 | my $track_create_url = "$base/api/rest/track"; |
d2739840 |
23 | |
24 | # test validation when no params sent |
25 | { |
0b0bf911 |
26 | my $test_data = $json->encode( { wrong_param => 'value' } ); |
27 | my $req = PUT($artist_create_url); |
28 | $req->content_type('text/x-json'); |
29 | $req->content_length( |
30 | do { use bytes; length($test_data) } |
31 | ); |
32 | $req->content($test_data); |
33 | $mech->request($req); |
34 | |
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 $test_data = $json->encode( {} ); |
48 | my $req = PUT($producer_create_url); |
49 | $req->content_type('text/x-json'); |
50 | $req->content_length( |
51 | do { use bytes; length($test_data) } |
52 | ); |
53 | $req->content($test_data); |
54 | $mech->request($req); |
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 $test_data = $json->encode( { name => 'king luke' } ); |
64 | my $req = PUT($producer_create_url); |
65 | $req->content_type('text/x-json'); |
66 | $req->content_length( |
67 | do { use bytes; length($test_data) } |
68 | ); |
69 | $req->content($test_data); |
70 | $mech->request($req); |
71 | cmp_ok( $mech->status, '==', 200, 'request with valid content okay' ); |
72 | my $new_obj = |
73 | $schema->resultset('Producer')->find( { name => 'king luke' } ); |
74 | ok( $new_obj, 'record created with specified name' ); |
d2739840 |
75 | |
0b0bf911 |
76 | my $response = $json->decode( $mech->content ); |
77 | is_deeply( |
78 | $response->{list}, |
79 | { $new_obj->get_columns }, |
80 | 'json for new producer returned' |
81 | ); |
d2739840 |
82 | } |
83 | |
84 | # test bulk create |
85 | { |
0b0bf911 |
86 | my $test_data = $json->encode( |
87 | { list => [ { name => 'king nperez' }, { name => 'queen perla' } ] } |
88 | ); |
89 | my $req = PUT($producer_create_url); |
90 | $req->content_type('text/x-json'); |
91 | $req->content_length( |
92 | do { use bytes; length($test_data) } |
93 | ); |
94 | $req->content($test_data); |
95 | $mech->request($req); |
96 | cmp_ok( $mech->status, '==', 200, 'request with valid content okay' ); |
97 | my $rs = |
98 | $schema->resultset('Producer') |
99 | ->search( [ { name => 'king nperez' }, { name => 'queen perla' } ] ); |
100 | ok( $rs, 'record created with specified name' ); |
d2739840 |
101 | |
0b0bf911 |
102 | my $response = $json->decode( $mech->content ); |
103 | my $expected = |
104 | [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ]; |
105 | is_deeply( $response->{list}, $expected, |
106 | 'json for bulk create returned' ); |
d2739840 |
107 | } |
108 | |
7f3ed652 |
109 | # test bulk create with database error |
110 | { |
111 | my $test_data = $json->encode({ |
112 | list => [ |
113 | { |
114 | cd => 2, |
115 | position => 4, |
116 | title => "Foobar", |
117 | }, |
118 | { |
119 | cd => 2, |
120 | # set position column to NULL to force error |
121 | position => undef, |
122 | title => "Foobar", |
123 | }, |
124 | ] |
125 | }); |
126 | #my $req = PUT( $tracks_update_url, Content => $test_data ); |
127 | my $req = PUT($track_create_url); |
128 | $req->content_type('text/x-json'); |
129 | $req->content_length( |
130 | do { use bytes; length($test_data) } |
131 | ); |
132 | $req->content($test_data); |
133 | $mech->request($req); |
134 | cmp_ok( $mech->status, '==', 400, 'bulk request with one invalid object fails' ); |
135 | my $rs = |
136 | $schema->resultset('Track') |
137 | ->search( [ { cd => 2 } ] ); |
138 | is( $rs->count, 3, 'no records created' ); |
139 | |
140 | my $response = $json->decode( $mech->content ); |
a5949bfd |
141 | is( $response->{success}, JSON::MaybeXS::false, |
7f3ed652 |
142 | 'success property returns unquoted false' ); |
143 | like( |
144 | $response->{messages}->[0], |
145 | qr/a database error has occured/, |
146 | 'correct message returned' |
147 | ); |
148 | } |
149 | |
d2739840 |
150 | done_testing(); |