1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More;
use Test::Warn;
use FixMyStreet::App;
use CGI::Simple;
use HTTP::Response;
use DateTime;
use FindBin;
use lib "$FindBin::Bin/../perllib";
use lib "$FindBin::Bin/../commonlib/perllib";
use_ok( 'Open311' );
my $o = Open311->new();
ok $o, 'created object';
my $err_text = <<EOT
<?xml version="1.0" encoding="utf-8"?><errors><error><code>400</code><description>Service Code cannot be null -- can't proceed with the request.</description></error></errors>
EOT
;
is $o->_process_error( $err_text ), "400: Service Code cannot be null -- can't proceed with the request.\n", 'error text parsing';
is $o->_process_error( '503 - service unavailable' ), 'unknown error', 'error text parsing of bad error';
my $o2 = Open311->new( endpoint => 'http://192.168.50.1/open311/', jurisdiction => 'example.org' );
my $u = FixMyStreet::App->model('DB::User')->new( { email => 'test@example.org', name => 'A User' } );
my $p = FixMyStreet::App->model('DB::Problem')->new( {
latitude => 1,
longitude => 1,
title => 'title',
detail => 'detail',
user => $u,
} );
my $expected_error = qr{.*request failed: 500 Can.t connect to 192.168.50.1:80 \([^)]*\).*};
warning_like {$o2->send_service_request( $p, { url => 'http://example.com/' }, 1 )} $expected_error, 'warning generated on failed call';
my $dt = DateTime->now();
my $user = FixMyStreet::App->model('DB::User')->new( {
email => 'test@example.com',
} );
my $problem = FixMyStreet::App->model('DB::Problem')->new( {
id => 80,
external_id => 81,
state => 'confirmed',
} );
my $comment = FixMyStreet::App->model('DB::Comment')->new( {
id => 38362,
user => $user,
problem => $problem,
anonymous => 0,
text => 'this is a comment',
confirmed => $dt,
} );
subtest 'testing posting updates' => sub {
my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>' );
is $results->{ res }, 248, 'got update id';
my $req = $o->test_req_used;
my $c = CGI::Simple->new( $results->{ req }->content );
is $c->param('description'), 'this is a comment', 'email correct';
is $c->param('email'), 'test@example.com', 'email correct';
is $c->param('status'), 'OPEN', 'status correct';
is $c->param('service_request_id_ext'), 80, 'external request id correct';
is $c->param('service_request_id'), 81, 'request id correct';
is $c->param('public_anonymity_required'), 'FALSE', 'anon status correct';
is $c->param('updated_datetime'), $dt, 'correct date';
};
foreach my $test (
{
desc => 'fixed is CLOSED',
state => 'fixed',
anon => 0,
status => 'CLOSED',
},
{
desc => 'fixed - user is CLOSED',
state => 'fixed - user',
anon => 0,
status => 'CLOSED',
},
{
desc => 'fixed - council is CLOSED',
state => 'fixed - council',
anon => 0,
status => 'CLOSED',
},
{
desc => 'closed is CLOSED',
state => 'closed',
anon => 0,
status => 'CLOSED',
},
{
desc => 'investigating is OPEN',
state => 'investigating',
anon => 0,
status => 'OPEN',
},
{
desc => 'planned is OPEN',
state => 'planned',
anon => 0,
status => 'OPEN',
},
{
desc => 'in progress is OPEN',
state => 'in progress',
anon => 0,
status => 'OPEN',
},
{
desc => 'anonymous set to true',
state => 'confirmed',
anon => 1,
status => 'OPEN',
},
) {
subtest $test->{desc} => sub {
$comment->problem->state( $test->{state} );
$comment->anonymous( $test->{anon} );
my $results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id>248</update_id></request_update></service_request_updates>' );
my $c = CGI::Simple->new( $results->{ req }->content );
is $c->param('status'), $test->{status}, 'correct status';
is $c->param('public_anonymity_required'), $test->{anon} ? 'TRUE' : 'FALSE', 'correct anonymity';
};
}
subtest 'No update id in reponse' => sub {
my $results;
warning_like {
$results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><service_request_updates><request_update><update_id></update_id></request_update></service_request_updates>' )
} qr/Failed to submit comment \d+ over Open311/, 'correct error message';
is $results->{ res }, 0, 'No update_id is a failure';
};
subtest 'error reponse' => sub {
my $results;
warning_like {
$results = make_update_req( $comment, '<?xml version="1.0" encoding="utf-8"?><errors><error><code>400</code><description>There was an error</description</error></errors>' )
} qr/Failed to submit comment \d+ over Open311.*There was an error/, 'correct error messages';
is $results->{ res }, 0, 'error in response is a failure';
};
done_testing();
sub make_update_req {
my $comment = shift;
my $xml = shift;
my $o = Open311->new( test_mode => 1, end_point => 'http://localhost/o311' );
my $test_res = HTTP::Response->new();
$test_res->code( 200 );
$test_res->message( 'OK' );
$test_res->content( $xml );
$o->test_get_returns( {
'update.xml' => $test_res
} );
my $res = $o->post_service_request_update( $comment );
my $req = $o->test_req_used;
return { res => $res, req => $req };
}
|