use warnings;

package LocalApp;

use Mouse;

extends 'Lemonldap::NG::Handler::PSGI::Try';

sub init {
    my ($self) = @_;
    $self->SUPER::init( $_[1] ) or return 0;

    # Register sub-routes that must not be overwritten
    $self->addUnauthRoute(
        myapp => { public => 'publicPage' },
        ['GET']
    );

# addAuthRouteWithRedirect with sub-routes must preserve existing unauth sub-routes
    $self->addAuthRouteWithRedirect(
        myapp => { '*' => 'my' },
        ['GET']
    );

    $self->addAuthRouteWithRedirect( '*' => 'my' );
    return 1;
}

sub my {
    return [ 200, [], ['OK'] ];
}

sub publicPage {
    return [ 200, [], ['PUBLIC'] ];
}

package main;

use Test::More;
use strict;
use IO::String;

use Lemonldap::NG::Portal::Main::Constants qw(
  PE_FIRSTACCESS
);

require 't/test-lib.pm';

my $res;

my $client = register(
    'portal',
    sub {
        LLNG::Manager::Test->new( {
                ini => {
                    logLevel    => 'error',
                    useSafeJail => 1,
                    cda         => 1,
                    logger      => 'Lemonldap::NG::Common::Logger::Std',
                }
            }
        );
    }
);

# CDA with unauthentified user
ok(
    $res = $client->_get(
        '/',
        query  => 'url=aHR0cDovL3Rlc3QuZXhhbXBsZS5vcmcv',
        accept => 'text/html',
    ),
    'Unauth CDA request'
);
my ( $host, $url, $query ) = expectForm( $res, undef, undef, 'url' );
ok( $query =~ /\burl=aHR0cDovL3Rlc3QuZXhhbXBsZS5vcmcv\b/, ' check url value' );
count(2);

# Authentification
$query .= '&user=dwho&password=dwho';
ok(
    $res = $client->_post(
        '/'    => IO::String->new($query),
        length => length($query),
        accept => 'text/html',
    ),
    'Post credentials'
);
count(1);

($query) =
  expectRedirection( $res, qr#^http://test.example.org/\?(lemonldapcda=.*)$# );

# Handler part
use_ok('Lemonldap::NG::Handler::PSGI');
use_ok('Lemonldap::NG::Common::PSGI::Cli::Lib');
count(2);

my ( $cli, $app );
$app = register( 'app', sub { LocalApp->run( $client->ini ) } );

ok(
    $res = $app->( {
            'HTTP_ACCEPT'          => 'text/html',
            'SCRIPT_NAME'          => '/',
            'SERVER_NAME'          => '127.0.0.1',
            'QUERY_STRING'         => $query,
            'HTTP_CACHE_CONTROL'   => 'max-age=0',
            'HTTP_ACCEPT_LANGUAGE' => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
            'PATH_INFO'            => '/',
            'REQUEST_METHOD'       => 'GET',
            'REQUEST_URI'          => "/?$query",
            'X_ORIGINAL_URI'       => "/?$query",
            'SERVER_PORT'          => '80',
            'SERVER_PROTOCOL'      => 'HTTP/1.1',
            'HTTP_USER_AGENT'      =>
              'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
            'REMOTE_ADDR' => '127.0.0.1',
            'HTTP_HOST'   => 'test.example.org',
            'VHOSTTYPE'   => 'CDA',
        }
    ),
    'Push cda cookie'
);
count(1);
expectRedirection( $res, 'http://test.example.org/' );
my $cid = expectCookie($res);

ok(
    $res = $app->( {
            'HTTP_ACCEPT'          => 'text/html',
            'SCRIPT_NAME'          => '/',
            'SERVER_NAME'          => '127.0.0.1',
            'HTTP_COOKIE'          => "lemonldap=$cid",
            'HTTP_CACHE_CONTROL'   => 'max-age=0',
            'HTTP_ACCEPT_LANGUAGE' => 'fr,fr-FR;q=0.8,en-US;q=0.5,en;q=0.3',
            'PATH_INFO'            => '/',
            'REQUEST_METHOD'       => 'GET',
            'REQUEST_URI'          => "/",
            'X_ORIGINAL_URI'       => "/",
            'SERVER_PORT'          => '80',
            'SERVER_PROTOCOL'      => 'HTTP/1.1',
            'HTTP_USER_AGENT'      =>
              'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
            'REMOTE_ADDR' => '127.0.0.1',
            'HTTP_HOST'   => 'test.example.org',
            'VHOSTTYPE'   => 'CDA',
        }
    ),
    'Authenticated query'
);
count(1);
expectOK($res);
expectAuthenticatedAs( $res, 'dwho' );

# Test that addAuthRouteWithRedirect does not overwrite existing unauth sub-routes
# /myapp/public should be accessible without auth (registered before addAuthRouteWithRedirect)
ok(
    $res = $app->( {
            'HTTP_ACCEPT'        => 'text/html',
            'SCRIPT_NAME'        => '/',
            'SERVER_NAME'        => '127.0.0.1',
            'HTTP_CACHE_CONTROL' => 'max-age=0',
            'PATH_INFO'          => '/myapp/public',
            'REQUEST_METHOD'     => 'GET',
            'REQUEST_URI'        => '/myapp/public',
            'X_ORIGINAL_URI'     => '/myapp/public',
            'SERVER_PORT'        => '80',
            'SERVER_PROTOCOL'    => 'HTTP/1.1',
            'HTTP_USER_AGENT'    =>
              'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
            'REMOTE_ADDR' => '127.0.0.1',
            'HTTP_HOST'   => 'test.example.org',
            'VHOSTTYPE'   => 'CDA',
        }
    ),
    'Unauth request to /myapp/public'
);
count(1);
ok( $res->[0] == 200, '/myapp/public returns 200 without auth' )
  or explain( $res, '200 expected' );
ok( $res->[2]->[0] eq 'PUBLIC', '/myapp/public returns PUBLIC content' );
count(2);

# /myapp/other should redirect to portal (unauthenticated, covered by wildcard redirect)
ok(
    $res = $app->( {
            'HTTP_ACCEPT'        => 'text/html',
            'SCRIPT_NAME'        => '/',
            'SERVER_NAME'        => '127.0.0.1',
            'HTTP_CACHE_CONTROL' => 'max-age=0',
            'PATH_INFO'          => '/myapp/other',
            'REQUEST_METHOD'     => 'GET',
            'REQUEST_URI'        => '/myapp/other',
            'X_ORIGINAL_URI'     => '/myapp/other',
            'SERVER_PORT'        => '80',
            'SERVER_PROTOCOL'    => 'HTTP/1.1',
            'HTTP_USER_AGENT'    =>
              'Mozilla/5.0 (VAX-4000; rv:36.0) Gecko/20350101 Firefox',
            'REMOTE_ADDR' => '127.0.0.1',
            'HTTP_HOST'   => 'test.example.org',
            'VHOSTTYPE'   => 'CDA',
        }
    ),
    'Unauth request to /myapp/other'
);
count(1);
ok( $res->[0] == 302, '/myapp/other redirects to portal (302)' )
  or explain( $res, '302 expected' );
count(1);

clean_sessions();

done_testing( count() );

