aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/json.t
blob: e8f9c0e09849453e4ed1c2a2f2865b4ed7299afa (plain)
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
use strict;
use warnings;

use Test::More;

use FixMyStreet::TestMech;
my $mech = FixMyStreet::TestMech->new;

subtest "check that a bad request produces the appropriate response" => sub {

    my $bad_date = "Invalid dates supplied";
    my $mad_date = "Start date after end date";
    my $bad_type = "Invalid type supplied";

    my %tests = (
        '?'                                                => $bad_date,
        '?foo=bar'                                         => $bad_date,
        '?start_date=&end_date='                     => $bad_date,
        '?start_date=bad&end_date=2000-02-01'        => $bad_date,
        '?start_date=2000-01-01&end_date=bad'        => $bad_date,
        '?start_date=2000-02-31&end_date=2000-02-01' => $bad_date,
        '?start_date=2000-01-01&end_date=2000-02-31' => $bad_date,
          
        '?start_date=2000-02-01&end_date=2000-01-01' => $mad_date,
          
        '?start_date=2000-01-01&end_date=2000-02-01'    => $bad_type,
        '/foo?type=foo&start_date=2000-01-01&end_date=2000-02-01' => $bad_type,
    );

    foreach my $q ( sort keys %tests ) {
        is_deeply                            #
          $mech->get_ok_json("/json/problems$q"),    #
          { error => $tests{$q} },           #
          "correct error for query '$q'";
    }

};

is_deeply                                    #
  $mech->get_ok_json(
    "/json/problems/new?start_date=2000-01-01&end_date=2000-02-01"),    #
  [],                                                                        #
  "correct response";

# put an entry in the database for this test
my $user = $mech->create_user_ok('test@example.com');

my $problem_args = {
    postcode  => 'sw1a 1aa',
    bodies_str => '2501',
    areas     => ',105164,11806,11827,2247,2501,34817,42011,66045,70786,8519,',
    category  => 'test category',
    title     => 'Test title',
    detail    => 'Test detail',
    used_map  => 't',
    name      => 'Test Name',
    created   => '2000-01-01 12:00:00',
    confirmed => '2000-01-01 12:01:00',
    state     => 'confirmed',
    lang      => 'en-gb',
    service   => '',
    cobrand   => '',
    cobrand_data       => '',
    lastupdate         => '2000-01-01 12:00:00',
    whensent           => undef,
    send_questionnaire => 't',
    latitude           => '51.4531988729771',
    longitude          => '-0.23021896608596',
};
my $problem      = $user->add_to_problems( { %$problem_args, anonymous => 0 } );
my $anon_problem = $user->add_to_problems( { %$problem_args, anonymous => 1, confirmed => '2000-01-01 12:02:00' } );

ok $problem,      "created normal test problem";
ok $anon_problem, "created anon test problem";

is_deeply    #
  $mech->get_ok_json(
    "/json/problems/new?start_date=2000-01-01&end_date=2000-02-01"),    #
  [
    {
        'state'     => 'confirmed',
        'longitude' => -0.23021896608596,
        'latitude'  => 51.4531988729771,
        'used_map'  => 1,
        'anonymous' => 0,
        'category'  => 'test category',
        'confirmed' => '2000-01-01 12:01:00',
        'lastupdate' => '2000-01-01 12:00:00',
        'council'   => 'Wandsworth Borough Council',
        'detail'    => 'Test detail',
        'id'        => $problem->id,
        'name'      => 'Test Name',
        'service'   => 'Web interface',
        'title'     => 'Test title',
        'whensent'  => undef
    },
    {
        'state'     => 'confirmed',
        'longitude' => -0.23021896608596,
        'latitude'  => 51.4531988729771,
        'used_map'  => 1,
        'anonymous' => 1,
        'category'  => 'test category',
        'confirmed' => '2000-01-01 12:02:00',
        'lastupdate' => '2000-01-01 12:00:00',
        'council'   => 'Wandsworth Borough Council',
        'detail'    => 'Test detail',
        'id'        => $anon_problem->id,
        'name'      => '',
        'service'   => 'Web interface',
        'title'     => 'Test title',
        'whensent'  => undef
    }
  ],
  "correct response";

$mech->delete_user($user);

done_testing();