23-Auth-and-password-REST.t 2.89 KB
Newer Older
1 2 3
use strict;
use IO::String;
use Test::More;
4
use lib 'inc';
5
use LWP::UserAgent;
6
use LWP::Protocol::PSGI;
7 8 9 10 11 12
use JSON qw(to_json from_json);

BEGIN {
    require 't/test-lib.pm';
}

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
LWP::Protocol::PSGI->register(
    sub {
        my $req = Plack::Request->new(@_);
        ok( $req->uri =~ m#^http://ws/(auth|user|confirm|modify)#,
            ' ' . ucfirst($1) . ' REST request' )
          or explain( $req->uri, 'http://ws/(auth|user)' );
        my $type = $1;
        count(1);
        my $res = from_json( $req->content );
        ok( $res->{user} eq 'dwho', ' User is dwho' );
        count(1);

        if ( $type eq 'auth' ) {
            ok( $res->{password} eq 'dwho', ' Password is dwho' )
              or explain( $res, 'password: dwho' );
            count(1);
            return [
                200,
                [ 'Content-Type' => 'application/json' ],
                ['{"result":true,"info":{"uid":"dwho"}}']
            ];
        }
        elsif ( $type eq 'modify' ) {
            ok( $res->{password} eq 'test', ' Password is test' );
            count(1);
            return [
                200, [ 'Content-Type' => 'application/json' ],
                ['{"result":true}']
            ];
        }
        elsif ( $type eq 'confirm' ) {
            ok( $res->{password} eq 'dwho', ' Password is dwho' );
            count(1);
            return [
                200, [ 'Content-Type' => 'application/json' ],
                ['{"result":true}']
            ];
        }
        elsif ( $type eq 'user' ) {
            return [
                200,
                [ 'Content-Type' => 'application/json' ],
                ['{"result":true,"info":{"cn":"dwho"}}']
            ];
        }
        else {
            fail('Unknwon URL');
            count(1);
        }
        return [ 500, [], [] ];
    }
);

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
my $res;

my $client = LLNG::Manager::Test->new(
    {
        ini => {
            logLevel          => 'error',
            useSafeJail       => 1,
            authentication    => 'REST',
            userDB            => 'Same',
            passwordDB        => 'REST',
            restAuthUrl       => 'http://ws/auth',
            restUserDBUrl     => 'http://ws/user',
            restPwdConfirmUrl => 'http://ws/confirm',
            restPwdModifyUrl  => 'http://ws/modify',
        }
    }
);

ok(
    $res = $client->_post(
        '/', IO::String->new('user=dwho&password=dwho'),
        length => 23,
        accept => 'text/html',
    ),
    'Auth query'
);
count(1);
expectRedirection( $res, 'http://auth.example.com/' );
my $id = expectCookie($res);
ok(
    $res = $client->_post(
        '/',
        IO::String->new(
            'oldpassword=dwho&newpassword=test&confirmpassword=test'),
        cookie => "lemonldap=$id",
        accept => 'application/json',
        length => 54
    ),
    'Change password'
);
count(1);
expectOK($res);
$client->logout($id);

clean_sessions();

done_testing( count() );