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 | my $content_type = [ 'Content-Type', 'application/x-www-form-urlencoded' ]; |
10 | |
11 | use RestTest; |
12 | use DBICTest; |
13 | use Test::More; |
14 | use Test::WWW::Mechanize::Catalyst 'RestTest'; |
15 | use HTTP::Request::Common; |
16 | use JSON::Any; |
17 | |
18 | my $mech = Test::WWW::Mechanize::Catalyst->new; |
19 | ok(my $schema = DBICTest->init_schema(), 'got schema'); |
20 | |
21 | my $track = $schema->resultset('Track')->first; |
22 | my %original_cols = $track->get_columns; |
23 | |
24 | my $track_update_url = "$base/api/rpc/track/id/" . $track->id . "/update"; |
25 | my $any_track_update_url = "$base/api/rpc/any/track/id/" . $track->id . "/update"; |
26 | |
27 | # test invalid track id caught |
28 | { |
29 | foreach my $wrong_id ('sdsdsdsd', 3434234) { |
30 | my $incorrect_url = "$base/api/rpc/track/id/" . $wrong_id . "/update"; |
31 | my $req = POST( $incorrect_url, { |
32 | title => 'value' |
33 | }); |
34 | |
35 | $mech->request($req, $content_type); |
36 | cmp_ok( $mech->status, '==', 400, 'Attempt with invalid track id caught' ); |
37 | |
38 | my $response = JSON::Any->Load( $mech->content); |
39 | like( $response->{messages}->[0], qr/No object found for id/, 'correct message returned' ); |
40 | |
41 | $track->discard_changes; |
42 | is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred'); |
43 | } |
44 | } |
45 | |
46 | # validation when no params sent |
47 | { |
48 | my $req = POST( $track_update_url, { |
49 | wrong_param => 'value' |
50 | }); |
51 | $mech->request($req, $content_type); |
52 | cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' ); |
53 | |
54 | my $response = JSON::Any->Load( $mech->content); |
55 | is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' ); |
56 | |
57 | $track->discard_changes; |
58 | is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred'); |
59 | } |
60 | |
61 | { |
62 | my $req = POST( $track_update_url, { |
63 | wrong_param => 'value' |
64 | }); |
65 | $mech->request($req, $content_type); |
66 | cmp_ok( $mech->status, '==', 400, 'Update with no keys causes error' ); |
67 | |
68 | my $response = JSON::Any->Load( $mech->content); |
69 | is_deeply( $response->{messages}, ['No valid keys passed'], 'correct message returned' ); |
70 | |
71 | $track->discard_changes; |
72 | is_deeply({ $track->get_columns }, \%original_cols, 'no update occurred'); |
73 | } |
74 | |
75 | { |
76 | my $req = POST( $track_update_url, { |
77 | title => undef |
78 | }); |
79 | $mech->request($req, $content_type); |
80 | cmp_ok( $mech->status, '==', 200, 'Update with key with no value okay' ); |
81 | |
82 | $track->discard_changes; |
83 | isnt($track->title, $original_cols{title}, 'Title changed'); |
84 | is($track->title, '', 'Title changed to undef'); |
85 | } |
86 | |
87 | { |
88 | my $req = POST( $track_update_url, { |
89 | title => 'monkey monkey' |
90 | }); |
91 | $mech->request($req, $content_type); |
92 | cmp_ok( $mech->status, '==', 200, 'Update with key with value okay' ); |
93 | |
94 | $track->discard_changes; |
95 | is($track->title, 'monkey monkey', 'Title changed to "monkey monkey"'); |
96 | } |
97 | |
98 | { |
99 | my $req = POST( $track_update_url, { |
100 | title => 'sheep sheep', |
101 | 'cd.year' => '2009' |
102 | }); |
103 | $mech->request($req, $content_type); |
104 | cmp_ok( $mech->status, '==', 200, 'Update with key with value and related key okay' ); |
105 | |
106 | $track->discard_changes; |
107 | is($track->title, 'sheep sheep', 'Title changed'); |
108 | is($track->cd->year, '2009', 'Related field changed"'); |
109 | } |
110 | |
111 | { |
112 | my $req = POST( $any_track_update_url, { |
113 | title => 'baa' |
114 | }); |
115 | $mech->request($req, $content_type); |
116 | cmp_ok( $mech->status, '==', 200, 'Stash update okay' ); |
117 | |
118 | $track->discard_changes; |
119 | is($track->title, 'baa', 'Title changed'); |
120 | } |
121 | |
122 | { |
123 | my $req = POST( $any_track_update_url, { |
124 | position => '14' |
125 | }); |
126 | $mech->request($req, $content_type); |
127 | cmp_ok( $mech->status, '==', 200, 'Position update okay' ); |
128 | |
129 | $track->discard_changes; |
130 | is($track->get_column('position'), '14', 'Position changed'); |
131 | } |
132 | |
133 | done_testing(); |