aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/controller/root.t
blob: b5f8ba031b802a22a62e44db288a6ce15d334be0 (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
use FixMyStreet::TestMech;
use Test::MockModule;

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

my @urls = (
    "/",
    "/contact",
    "/about/faq",
    "/around?longitude=-1.351488&latitude=51.847235"
);


FixMyStreet::override_config {
    LOGIN_REQUIRED => 0,
    MAPIT_URL => 'http://mapit.uk/'
}, sub {
    subtest 'LOGIN_REQUIRED = 0 behaves correctly' => sub {
        foreach my $url (@urls) {
            $mech->get_ok($url);
            is $mech->res->code, 200, "got 200 for page";
            is $mech->res->previous, undef, 'No redirect';
        }
    };
};


FixMyStreet::override_config {
    LOGIN_REQUIRED => 1,
    MAPIT_URL => 'http://mapit.uk/'
}, sub {
    subtest 'LOGIN_REQUIRED = 1 redirects to /auth if not logged in' => sub {
        foreach my $url (@urls) {
            $mech->get_ok($url);
            is $mech->res->code, 200, "got 200 for final destination";
            is $mech->res->previous->code, 302, "got 302 for redirect";
            is $mech->uri->path, '/auth';
        }
    };

    subtest 'LOGIN_REQUIRED = 1 does not redirect if logged in' => sub {
        $mech->log_in_ok('user@example.org');
        foreach my $url (@urls) {
            $mech->get_ok($url);
            is $mech->res->code, 200, "got 200 for final destination";
            is $mech->res->previous, undef, 'No redirect';
        }
        $mech->log_out_ok;
    };

    subtest 'LOGIN_REQUIRED = 1 allows whitelisted URLs' => sub {
        my @whitelist = (
            '/auth',
            '/js/translation_strings.en-gb.js'
        );

        foreach my $url (@whitelist) {
            $mech->get_ok($url);
            is $mech->res->code, 200, "got 200 for final destination";
            is $mech->res->previous, undef, 'No redirect';
        }
    };

    subtest 'LOGIN_REQUIRED = 1 404s blacklisted URLs' => sub {
        my @blacklist = (
            '/offline/appcache',
        );

        foreach my $url (@blacklist) {
            $mech->get($url);
            ok !$mech->res->is_success(), "want a bad response";
            is $mech->res->code, 404, "got 404";
        }
    };
};

subtest "check_login_disallowed cobrand hook" => sub {
    warn '#' x 50 . "\n";
    my $cobrand = Test::MockModule->new('FixMyStreet::Cobrand::Default');
    $cobrand->mock('check_login_disallowed', sub {
            my $self = shift;
            return 0 if $self->{c}->req->path eq 'auth';
            return 1;
        }
    );

    $mech->get_ok('/');
    is $mech->uri->path_query, '/auth?r=', 'redirects to auth page';
};

done_testing();
n> @rewindable_io.rewind end # Closes this RewindableInput object without closing the originally # wrapped IO oject. Cleans up any temporary resources that this RewindableInput # has created. # # This method may be called multiple times. It does nothing on subsequent calls. def close if @rewindable_io if @unlinked @rewindable_io.close else @rewindable_io.close! end @rewindable_io = nil end end private # Ruby's Tempfile class has a bug. Subclass it and fix it. class Tempfile < ::Tempfile def _close @tmpfile.close if @tmpfile @data[1] = nil if @data @tmpfile = nil end end def make_rewindable # Buffer all data into a tempfile. Since this tempfile is private to this # RewindableInput object, we chmod it so that nobody else can read or write # it. On POSIX filesystems we also unlink the file so that it doesn't # even have a file entry on the filesystem anymore, though we can still # access it because we have the file handle open. @rewindable_io = Tempfile.new('RackRewindableInput') @rewindable_io.chmod(0000) @rewindable_io.set_encoding(Encoding::BINARY) if @rewindable_io.respond_to?(:set_encoding) @rewindable_io.binmode if filesystem_has_posix_semantics? @rewindable_io.unlink @unlinked = true end buffer = "" while @io.read(1024 * 4, buffer) entire_buffer_written_out = false while !entire_buffer_written_out written = @rewindable_io.write(buffer) entire_buffer_written_out = written == buffer.size if !entire_buffer_written_out buffer.slice!(0 .. written - 1) end end end @rewindable_io.rewind end def filesystem_has_posix_semantics? RUBY_PLATFORM !~ /(mswin|mingw|cygwin|java)/ end end end