Commit | Line | Data |
d2739840 |
1 | use 5.6.0; |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use lib 't/lib'; |
7 | |
8 | my $base = 'http://localhost'; |
9 | |
10 | use RestTest; |
11 | use DBICTest; |
12 | use Test::More; |
13 | use Test::WWW::Mechanize::Catalyst 'RestTest'; |
14 | use HTTP::Request::Common; |
15 | use JSON::Any; |
16 | |
17 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
18 | ok(my $schema = DBICTest->init_schema(), 'got schema'); |
19 | |
20 | my $artist_create_url = "$base/api/rest/artist"; |
21 | my $producer_create_url = "$base/api/rest/producer"; |
22 | |
23 | # test validation when no params sent |
24 | { |
25 | my $test_data = JSON::Any->Dump({ wrong_param => 'value' }); |
26 | my $req = PUT( $artist_create_url ); |
27 | $req->content_type('text/x-json'); |
28 | $req->content_length( |
29 | do { use bytes; length( $test_data ) } |
30 | ); |
31 | $req->content( $test_data ); |
32 | $mech->request($req); |
33 | |
34 | cmp_ok( $mech->status, '==', 400, 'attempt without required params caught' ); |
35 | my $response = JSON::Any->Load( $mech->content); |
36 | like($response->{messages}->[0], qr/No value supplied for name and no default/, 'correct message returned' ); |
37 | } |
38 | |
39 | # test default value used if default value exists |
40 | { |
41 | my $test_data = JSON::Any->Dump({}); |
42 | my $req = PUT( $producer_create_url ); |
43 | $req->content_type('text/x-json'); |
44 | $req->content_length( |
45 | do { use bytes; length( $test_data ) } |
46 | ); |
47 | $req->content( $test_data ); |
48 | $mech->request($req); |
49 | cmp_ok( $mech->status, '==', 200, 'default value used when not supplied' ); |
50 | ok($schema->resultset('Producer')->find({ name => 'fred' }), 'record created with default name'); |
51 | } |
52 | |
53 | # test create works as expected when passing required value |
54 | { |
55 | my $test_data = JSON::Any->Dump({ name => 'king luke' }); |
56 | my $req = PUT( $producer_create_url ); |
57 | $req->content_type('text/x-json'); |
58 | $req->content_length( |
59 | do { use bytes; length( $test_data ) } |
60 | ); |
61 | $req->content( $test_data ); |
62 | $mech->request($req); |
63 | cmp_ok( $mech->status, '==', 200, 'request with valid content okay' ); |
64 | my $new_obj = $schema->resultset('Producer')->find({ name => 'king luke' }); |
65 | ok($new_obj, 'record created with specified name'); |
66 | |
67 | my $response = JSON::Any->Load( $mech->content); |
68 | is_deeply( $response->{list}, { $new_obj->get_columns }, 'json for new producer returned' ); |
69 | } |
70 | |
71 | # test bulk create |
72 | { |
73 | my $test_data = JSON::Any->Dump({ list => [{ name => 'king nperez' }, { name => 'queen perla'}] }); |
74 | my $req = PUT( $producer_create_url ); |
75 | $req->content_type('text/x-json'); |
76 | $req->content_length( |
77 | do { use bytes; length( $test_data ) } |
78 | ); |
79 | $req->content( $test_data ); |
80 | $mech->request($req); |
81 | cmp_ok( $mech->status, '==', 200, 'request with valid content okay' ); |
82 | my $rs = $schema->resultset('Producer')->search([ { name => 'king nperez' }, { name => 'queen perla' } ]); |
83 | ok($rs, 'record created with specified name'); |
84 | |
85 | my $response = JSON::Any->Load( $mech->content); |
86 | my $expected = [ map { my %foo = $_->get_inflated_columns; \%foo; } $rs->all ]; |
87 | is_deeply( $response->{list}, $expected, 'json for bulk create returned' ); |
88 | } |
89 | |
90 | done_testing(); |