aboutsummaryrefslogtreecommitdiffstats
path: root/t/open311/post-service-request-updates.t
blob: 57b8f9a2a430b64e32f4c6fcd1c220f3d0d95dab (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
#!/usr/bin/env perl

use FixMyStreet::TestMech;

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

use_ok( 'Open311::PostServiceRequestUpdates' );

my $o = Open311::PostServiceRequestUpdates->new( site => 'fixmystreet.com' );

my $params = {
    send_method => 'Open311',
    send_comments => 1,
    api_key => 'KEY',
    endpoint => 'endpoint',
    jurisdiction => 'home',
};
my $bromley = $mech->create_body_ok(2482, 'Bromley', { %$params, send_extended_statuses => 1, id => 5 });
my $oxon = $mech->create_body_ok(2237, 'Oxfordshire', { %$params, id => 55 });
my $bucks = $mech->create_body_ok(2217, 'Buckinghamshire', $params);
my $lewisham = $mech->create_body_ok(2492, 'Lewisham', $params);

subtest 'Check Open311 params' => sub {
  FixMyStreet::override_config {
    ALLOWED_COBRANDS => ['fixmystreet', 'bromley', 'buckinghamshire', 'lewisham', 'oxfordshire'],
  }, sub {
    my $result = {
        endpoint => 'endpoint',
        jurisdiction => 'home',
        api_key => 'KEY',
        extended_statuses => undef,
    };
    my %conf = $o->open311_params($bromley);
    is_deeply \%conf, {
        %$result,
        extended_statuses => 1,
        endpoints => { service_request_updates => 'update.xml', update => 'update.xml' },
        fixmystreet_body => $bromley,
    }, 'Bromley params match';
    %conf = $o->open311_params($oxon);
    is_deeply \%conf, {
        %$result,
        use_customer_reference => 1,
        fixmystreet_body => $oxon,
    }, 'Oxfordshire params match';
    %conf = $o->open311_params($bucks);
    is_deeply \%conf, {
        %$result,
        mark_reopen => 1,
        fixmystreet_body => $bucks,
    }, 'Bucks params match';
    %conf = $o->open311_params($lewisham);
    is_deeply \%conf, {
        %$result,
        fixmystreet_body => $lewisham,
    }, 'Lewisham params match';
  };
};

my $other_user = $mech->create_user_ok('test2@example.com', title => 'MRS');

sub c {
    my ($p, $user) = @_;
    my $c = $mech->create_comment_for_problem($p, $user || $p->user, 'Name', 'Update text', 'f', 'confirmed', 'confirmed', { confirmed => \'current_timestamp' });
    return $c;
}

sub p_and_c {
    my ($body, $user) = @_;

    my $prob_params = { send_method_used => 'Open311', whensent => \'current_timestamp', external_id => 1 };
    my ($p) = $mech->create_problems_for_body(1, $body->id, 'Title', $prob_params);
    my $c = c($p, $user);
    return ($p, $c);
}

my ($p1, $c1) = p_and_c($bromley, $other_user);
my ($p2, $c2) = p_and_c($oxon);
my ($p3, $c3a) = p_and_c($bucks);
my $c3b = c($p3, $other_user);
my ($p4, $c4) = p_and_c($lewisham);

subtest 'Send comments' => sub {
  FixMyStreet::override_config {
    ALLOWED_COBRANDS => ['fixmystreet', 'bromley', 'buckinghamshire', 'lewisham', 'oxfordshire'],
  }, sub {
    $o->send;
    $c3a->discard_changes;
    is $c3a->extra, undef, 'Bucks update by owner was sent';
    $c3b->discard_changes;
    is $c3b->extra->{cobrand_skipped_sending}, 1, 'Bucks update by other was not';
    $c1->discard_changes;
    is $c1->extra->{title}, "MRS", 'Title set on Bromley update';
    $c2->discard_changes;
    is $c2->send_fail_count, 0, 'Oxfordshire update skipped entirely';
  };
};

subtest 'Oxfordshire gets an ID' => sub {
  FixMyStreet::override_config {
    ALLOWED_COBRANDS => ['fixmystreet', 'bromley', 'buckinghamshire', 'lewisham', 'oxfordshire'],
  }, sub {
    $p2->set_extra_metadata(customer_reference => 'ABC');
    $p2->update;
    $o->send;
    $c2->discard_changes;
    is $c2->send_fail_count, 1, 'Oxfordshire update tried to send, failed';
  };
};


done_testing();