aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/my.t
blob: 673addf0cc3086764552c371191b7f7b722ecb45 (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
use FixMyStreet::TestMech;
my $mech = FixMyStreet::TestMech->new;

$mech->get_ok('/my');
is $mech->uri->path, '/auth', "got sent to the sign in page";

$mech->get_ok('/my/anonymize');
is $mech->uri->path, '/auth', "got sent to the sign in page";

my @problems = $mech->create_problems_for_body(3, 1234, 'Test Title');
$problems[1]->update({anonymous => 1});

my $other_user = FixMyStreet::DB->resultset('User')->find_or_create({ email => 'another@example.com' });
my @other = $mech->create_problems_for_body(1, 1234, 'Another Title', { user => $other_user });

my $user = $mech->log_in_ok( 'test@example.com' );
$mech->get_ok('/my');
is $mech->uri->path, '/my', "stayed on '/my' page";

$mech->content_contains('Test Title');
$mech->content_lacks('Another Title');

my @update;
my $i = 0;
foreach ($user, $user, $other_user) {
    $update[$i] = FixMyStreet::DB->resultset('Comment')->create({
        text => 'this is an update',
        user => $_,
        state => 'confirmed',
        problem => $problems[0],
        mark_fixed => 0,
        confirmed => \'current_timestamp',
        anonymous => $i % 2,
    });
    $i++;
}

foreach (
    { type => 'problem', id => 0, result => 404, desc => 'nothing' },
    { type => 'problem', obj => $problems[0], result => 200, desc => 'own report' },
    { type => 'problem', obj => $problems[1], result => 400, desc => 'already anon report' },
    { type => 'problem', obj => $other[0], result => 400, desc => 'other user report' },
    { type => 'update', id => -1, result => 400, desc => 'non-existent update' },
    { type => 'update', obj => $update[0], result => 200, desc => 'own update' },
    { type => 'update', obj => $update[1], result => 400, desc => 'already anon update' },
    { type => 'update', obj => $update[2], result => 400, desc => 'other user update' },
) {
    my $id = $_->{id} // $_->{obj}->id;
    $mech->get("/my/anonymize?$_->{type}=$id");
    is $mech->res->code, $_->{result}, "Got $_->{result} fetching $_->{desc}";
    if ($_->{result} == 200) {
        $mech->submit_form_ok( { button => 'hide' }, 'Submit button to hide name' );
        $_->{obj}->discard_changes;
        is $_->{obj}->anonymous, 1, 'Object now made anonymous';
        $_->{obj}->update({anonymous => 0});
    }
}

$mech->get("/my/anonymize?problem=" . $problems[0]->id);
$mech->submit_form_ok( { button => 'hide_everywhere' }, 'Submit button to hide name everywhere' );
is $problems[0]->discard_changes->anonymous, 1, 'Problem from form made anonymous';
is $problems[2]->discard_changes->anonymous, 1, 'Other user problem made anonymous';
is $update[0]->discard_changes->anonymous, 1, 'User update made anonymous';

done_testing();