aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/sendreport/inspection_required.t
blob: f9d40d39f15df79e68a75d56f34a3b65c474ddd7 (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
use strict;
use warnings;

use Test::More;

use FixMyStreet;
use FixMyStreet::DB;
use FixMyStreet::TestMech;
use FixMyStreet::SendReport::Email;
use mySociety::Locale;

ok( my $mech = FixMyStreet::TestMech->new, 'Created mech object' );

my $user = $mech->create_user_ok( 'user@example.com' );

my $body = $mech->create_body_ok( 2237, 'Oxfordshire County Council', id => 2237 );
# $body->update({ send_method => 'Email' });

my $contact = $mech->create_contact_ok(
    body_id => $body->id,
    category => 'Pothole',
    email => 'test@example.org',
);
$contact->set_extra_metadata(inspection_required => 1);
$contact->update;

my @reports = $mech->create_problems_for_body( 1, $body->id, 'Test', {
    cobrand => 'oxfordshire',
    category => $contact->category,
    user => $user,
});
my $report = $reports[0];

subtest 'Report isn’t sent if uninspected' => sub {
    $mech->clear_emails_ok;

    FixMyStreet::DB->resultset('Problem')->send_reports();

    $mech->email_count_is( 0 );
    is $report->whensent, undef, 'Report hasn’t been sent';
};

subtest 'Report is sent when inspected' => sub {
    $mech->clear_emails_ok;
    $report->set_extra_metadata(inspected => 1);
    $report->update;

    FixMyStreet::DB->resultset('Problem')->send_reports();

    $report->discard_changes;
    $mech->email_count_is( 1 );
    ok $report->whensent, 'Report marked as sent';
};

subtest 'Uninspected report is sent when made by trusted user' => sub {
    $mech->clear_emails_ok;
    $report->unset_extra_metadata('inspected');
    $report->whensent( undef );
    $report->update;

    $user->user_body_permissions->find_or_create({
        body => $body,
        permission_type => 'trusted',
    });
    ok  $user->has_permission_to('trusted', $report->bodies_str_ids), 'User can make trusted reports';

    FixMyStreet::DB->resultset('Problem')->send_reports();

    $report->discard_changes;
    $mech->email_count_is( 1 );
    ok $report->whensent, 'Report marked as sent';
    is $report->get_extra_metadata('inspected'), undef, 'Report not marked as inspected';
};

subtest 'Uninspected report isn’t sent when user rep is too low' => sub {
    $mech->clear_emails_ok;
    $report->whensent( undef );
    $report->update;

    $user->user_body_permissions->delete;
    $user->set_extra_metadata(reputation => 15);
    $user->update;

    $contact->set_extra_metadata(reputation_threshold => 20);
    $contact->update;

    FixMyStreet::DB->resultset('Problem')->send_reports();

    $report->discard_changes;
    $mech->email_count_is( 0 );
    is $report->whensent, undef, 'Report hasn’t been sent';
};

subtest 'Uninspected report is sent when user rep is high enough' => sub {
    $user->set_extra_metadata(reputation => 21);
    $user->update;

    FixMyStreet::DB->resultset('Problem')->send_reports();

    $report->discard_changes;
    $mech->email_count_is( 1 );
    ok $report->whensent, 'Report marked as sent';
    is $report->get_extra_metadata('inspected'), undef, 'Report not marked as inspected';
};

done_testing();

END {
    $mech->delete_user($user);
    $mech->delete_body($body);
}