aboutsummaryrefslogtreecommitdiffstats
path: root/t/script/inactive.t
blob: 26eab33e11e023c91690389e9409703c3a804f4b (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
use FixMyStreet::TestMech;
use Test::Output;

use_ok 'FixMyStreet::Script::Inactive';

my $in = FixMyStreet::Script::Inactive->new( anonymize => 6, email => 3 );
my $mech = FixMyStreet::TestMech->new;

my $user = $mech->create_user_ok('test@example.com');
my $t = DateTime->new(year => 2016, month => 1, day => 1, hour => 12);
$user->last_active($t);
$user->update;

my $user_inactive = $mech->create_user_ok('inactive@example.com');
$t = DateTime->now->subtract(months => 4);
$user_inactive->last_active($t);
$user_inactive->update;

my @problems;
for (my $m = 1; $m <= 12; $m++) {
    my $t = DateTime->new(year => 2017, month => $m, day => 1, hour => 12);
    push @problems, $mech->create_problems_for_body(1, 2237, 'Title', {
        dt => $t,
        lastupdate => "$t",
        state => $m % 2 ? 'fixed - user' : 'confirmed',
        cobrand => $m % 3 ? 'default' : 'bromley',
    });
}

$mech->create_comment_for_problem($problems[0], $user, 'Name', 'Update', 0, 'confirmed', $problems[0]->state);
FixMyStreet::DB->resultset("Alert")->create({ alert_type => 'new_updates', parameter => $problems[2]->id, user => $user });

subtest 'Anonymization of inactive fixed/closed reports' => sub {
    $in->reports;

    my $count = FixMyStreet::DB->resultset("Problem")->search({ user_id => $user->id })->count;
    is $count, 6, 'Six non-anonymised';

    my $comment = FixMyStreet::DB->resultset("Comment")->first;
    my $alert = FixMyStreet::DB->resultset("Alert")->first;
    is $comment->anonymous, 1, 'Comment anonymized';
    is $comment->user->email, 'removed-automatically@example.org', 'Comment user anonymized';
    is $alert->user->email, 'removed-automatically@example.org', 'Alert anonymized';
    isnt $alert->whendisabled, undef, 'Alert disabled';

    $mech->create_comment_for_problem($problems[0], $user, 'Name 2', 'Update', 0, 'confirmed', $problems[0]->state);
    $comment = FixMyStreet::DB->resultset("Comment")->search({ name => 'Name 2' })->first;

    $in->reports;
    $comment->discard_changes;
    is $comment->anonymous, 1, 'Comment anonymized';
    is $comment->user->email, 'removed-automatically@example.org', 'Comment user anonymized';
};

subtest 'Test operating on one cobrand only' => sub {
    FixMyStreet::override_config {
        ALLOWED_COBRANDS => 'bromley'
    }, sub {
        my $in = FixMyStreet::Script::Inactive->new( cobrand => 'bromley', close => 1 );
        $in->reports;
        # Reports not a multiple of 2 are fixed, reports a multiple of 3 are bromley
        $problems[2]->discard_changes;
        is $problems[2]->get_extra_metadata('closed_updates'), 1, 'Closed to updates';
        $problems[4]->discard_changes;
        is $problems[4]->get_extra_metadata('closed_updates'), undef, 'Not closed to updates';
        $problems[6]->discard_changes;
        is $problems[6]->get_extra_metadata('closed_updates'), undef, 'Not closed to updates';
        $problems[8]->discard_changes;
        is $problems[8]->get_extra_metadata('closed_updates'), 1, 'Closed to updates';
    };
};

subtest 'Closing updates on inactive fixed/closed reports' => sub {
    my $in = FixMyStreet::Script::Inactive->new( close => 1 );
    $in->reports;
    $problems[4]->discard_changes;
    is $problems[4]->get_extra_metadata('closed_updates'), 1, 'Closed to updates';
    $mech->get_ok("/report/" . $problems[4]->id);
    $mech->content_contains('now closed to updates');
};

subtest 'Deleting reports' => sub {
    my $in = FixMyStreet::Script::Inactive->new( delete => 6 );
    $in->reports;

    my $count = FixMyStreet::DB->resultset("Problem")->count;
    is $count, 6, 'Six left';

    $mech->get("/report/" . $problems[2]->id);
    is $mech->res->code, 404;
};

subtest 'Anonymization of inactive users' => sub {
    my $in = FixMyStreet::Script::Inactive->new( anonymize => 6, email => 3, verbose => 1 );
    stdout_is { $in->users } "Anonymizing user #" . $user->id . "\nEmailing user #" . $user_inactive->id . "\n", 'users dealt with first time';

    my $email = $mech->get_email;
    like $email->as_string, qr/inactive\@example.com/, 'Inactive email sent';
    $mech->clear_emails_ok;

    $user->discard_changes;
    is $user->email, 'removed-' . $user->id . '@example.org', 'User has been anonymized';

    stdout_is { $in->users } '', 'No output second time';

    $mech->email_count_is(0); # No further email sent

    $user->discard_changes;
    is $user->email, 'removed-' . $user->id . '@example.org', 'User has been anonymized';
};

done_testing;