aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/model/user.t
blob: 929020d4bb7af1649f4f3468028cca176ba3577f (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 FixMyStreet::TestMech;
use FixMyStreet::DB;
use Catalyst::Test 'FixMyStreet::App';
use HTTP::Request::Common;
use Test::Exception;

my $mech = FixMyStreet::TestMech->new();
$mech->log_in_ok('test@example.com');

my ($problem) = $mech->create_problems_for_body(1, '2504', 'Title', { anonymous => 'f' });
is $problem->user->latest_anonymity, 0, "User's last report was not anonymous";

FixMyStreet::override_config {
    ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
    MAPIT_URL => 'http://mapit.uk/',
}, sub {
    $mech->get_ok('/around?pc=sw1a1aa');
    $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" );
    $mech->content_like(qr/may_show_name[^>]*checked/);
};

($problem) = $mech->create_problems_for_body(1, '2504', 'Title', { anonymous => 't' });
is $problem->user->latest_anonymity, 1, "User's last report was anonymous";

create_update($problem, anonymous => 'f');
is $problem->user->latest_anonymity, 0, "User's last update was not anonyous";

create_update($problem, anonymous => 't');
is $problem->user->latest_anonymity, 1, "User's last update was anonymous";

subtest "Sign user up for alerts" => sub {
    my $user = $problem->user;

    my $alert_exists =  $user->alert_for_problem( $problem->id );
    is !defined( $alert_exists ), 1, "No current alerts exist";

    my $options = {
      cobrand      => 'default',
      lang         => 'en-gb',
    };
    $user->create_alert($problem->id, $options);
    my $alert = $user->alert_for_problem( $problem->id );

    is defined( $alert ), 1, "User is signed up for alerts";
    is $alert->confirmed, 1, "Alert is confirmed";

    $alert->delete();

    $user->alerts->create({
        alert_type   => 'new_updates',
        parameter    => $problem->id,
    });

    $user->create_alert($problem->id, $options);

    my $new_alert = $user->alert_for_problem( $problem->id );
    is $alert->confirmed, 1, "Already created alert is confirmed";
};

FixMyStreet::override_config {
    ALLOWED_COBRANDS => [ { fixmystreet => '.' } ],
    MAPIT_URL => 'http://mapit.uk/',
}, sub {
    $mech->get_ok('/around?pc=sw1a1aa');
    $mech->follow_link_ok( { text_regex => qr/skip this step/i, }, "follow 'skip this step' link" );
    $mech->content_like(qr/may_show_name[^>c]*>/);
};

subtest 'Check non-existent methods on user object die' => sub {
    my $c = ctx_request(POST '/auth', { username => $problem->user->email, password_sign_in => 'secret' });
    throws_ok(
        sub { $c->user->is_super_user },
        qr/Can't locate object method 'is_super_user'/,
        'attempt to call non-existent method'
    );
};

subtest 'OIDC ids can be manipulated correctly' => sub {
    my $user = $problem->user;

    is $user->oidc_ids, undef, 'user starts with no OIDC ids';

    $user->add_oidc_id("fixmystreet:1234:5678");
    is_deeply $user->oidc_ids, ["fixmystreet:1234:5678"], 'OIDC id added correctly';

    $user->add_oidc_id("mycobrand:0123:abcd");
    is_deeply [ sort @{$user->oidc_ids} ], ["fixmystreet:1234:5678", "mycobrand:0123:abcd"], 'Second OIDC id added correctly';

    $user->add_oidc_id("mycobrand:0123:abcd");
    is_deeply [ sort @{$user->oidc_ids} ], ["fixmystreet:1234:5678", "mycobrand:0123:abcd"], 'Adding existing OIDC id does not add duplicate';

    $user->remove_oidc_id("mycobrand:0123:abcd");
    is_deeply $user->oidc_ids, ["fixmystreet:1234:5678"], 'OIDC id can be removed OK';

    $user->remove_oidc_id("mycobrand:0123:abcd");
    is_deeply $user->oidc_ids, ["fixmystreet:1234:5678"], 'Removing non-existent OIDC id has no effect';

    $user->remove_oidc_id("fixmystreet:1234:5678");
    is $user->oidc_ids, undef, 'Removing last OIDC id results in undef';

};

done_testing();

sub create_update {
    my ($problem, %params) = @_;
    my $dt = DateTime->now()->add(days => 1);
    return FixMyStreet::DB->resultset('Comment')->find_or_create({
        problem_id => $problem->id,
        user_id => $problem->user_id,
        name => 'Other User',
        mark_fixed => 'false',
        text => 'This is some update text',
        state => 'confirmed',
        anonymous => 'f',
        created => $dt->ymd . ' ' . $dt->hms,
        %params,
    });
}