use Test::More;
use strict;

require 't/test-lib.pm';

my $res;

my $client = LLNG::Manager::Test->new( {
        ini => {
            logLevel         => 'error',
            authentication   => 'Demo',
            checkStateSecret => 'x',
            checkState       => 1,
            userDB           => 'Same',
        }
    }
);

ok( $res = $client->_get( '/checkstate', accept => 'application/json' ),
    'Test no secret' );
expectReject( $res, 500, "Bad secret" );

ok(
    $res = $client->_get(
        '/checkstate',
        accept => 'application/json',
        query  => 'secret=xx'
    ),
    'Test bad secret, no user auth'
);
expectReject( $res, 500, "Bad secret" );

ok(
    $res = $client->_get(
        '/checkstate',
        accept => 'application/json',
        query  => 'secret=x'
    ),
    'Test correct secret, no user auth'
);
my $j = expectJSON($res);
is( $j->{result}, 1, "response has a result key with value 1" );

ok(
    $res = $client->_get(
        '/checkstate',
        accept => 'application/json',
        query  => 'user=dwho&password=dwho'
    ),
    'Test no secret with user auth'
);
expectReject( $res, 500, "Bad secret" );

ok(
    $res = $client->_get(
        '/checkstate',
        accept => 'application/json',
        query  => 'secret=xx&user=dwho&password=dwho'
    ),
    'Test incorrect secret with user auth'
);
expectReject( $res, 500, "Bad secret" );

ok(
    $res = $client->_get(
        '/checkstate',
        accept => 'application/json',
        query  => 'secret=x&user=dwho&password=davros'
    ),
    'Test correct secret with bad user auth'
);
expectReject( $res, 500, "Bad result during auth: 5" );

is( $j->{result}, 1, "response has a result key with value 1" );
ok(
    $res = $client->_get(
        '/checkstate',
        accept => 'application/json',
        query  => 'secret=x&user=dwho&password=dwho'
    ),
    'Test correct secret with good user auth'
);
$j = expectJSON($res);
is( $j->{result}, 1, "response has a result key with value 1" );

done_testing();
