README.md 4.48 KB
Newer Older
Xavier Guimard's avatar
Xavier Guimard committed
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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
# NAME

test-lib.pm - Test framework for LLNG portal

# SYNOPSIS

    use Test::More;
    use strict;
    use IO::String;
    
    require 't/test-lib.pm';
    
    my $res;
    
    my $client = LLNG::Manager::Test->new( {
        ini => {
            logLevel => 'error',
            #...
        }
      }
    );
    
    ok(
        $res = $client->_post(
            '/',
            IO::String->new('user=dwho&password=dwho'),
            length => 23
        ),
        'Auth query'
    );
    count(1);
    expectOK($res);
    my $id = expectCookie($res);
    
    clean_sessions();
    done_testing( count() );

# DESCRIPTION

This test library permits to simulate browser navigation.

## Functions

In these functions, `$res` is the result of a `LLNG::Manager::Test::_get()` or
`LLNG::Manager::Test::_post()` call _(see below)_.

#### count($inc)

Returns number of tests done. Increment test number if an argument is given

#### explain( $result, $expected\_result )

Used to display error if test fails:

    ok( $res->[0] == 302, 'Get redirection' ) or
      explain( $res->[0], 302 );

#### clean\_sessions()

Clean sessions created during tests

#### expectRedirection( $res, $location )

Verify that request result is a redirection to $location. $location can be:

- a string: location must match exactly
- a regexp: location must match this regexp. In this case, the list of
matching strings are returned. Example:

        my( $uri, $query ) = expectRedirection( $res, qr#http://host(/[^\?]*)?(.*)$# );

#### expectAutoPost(@args)

Same behaviour as `expectForm()` but verify also that form method is post.

TODO: verify javascript

#### expectForm( $res, $hostRe, $uriRe, @requiredFields )

Verify form in HTML result and return ( $host, $uri, $query, $method ):

- verify that a GET/POST form exists
- if a $hostRe regexp is given, verify that form target matches and
populates $host. Skipped if $hostRe eq "#"
- if a $uriRe regexp is given, verify that form target matches and
populates $uri
- if @requiredFields exists, verify that each element is an input name
- build form-url-encoded string looking at parameters/values and store it
in $query

#### expectAuthenticatedAs($user)

Verify that result has a `Lm-Remote-User` header and value is $user

#### expectOK($res)

Verify that returned code is 200

#### expectBadRequest($res)

Verify that returned code is 400. Note that it works only for Ajax request
(see below).

#### expectReject( $res, $code )

Verify that returned code is 401 and JSON result contains `error:"$code"`.
Note that it works only for Ajax request (see below).

#### expectCookie( $res, $cookieName )

Check if a `Set-Cookie` exists and set a cookie named $cookieName. Return
its value.

#### exceptCspFormOK( $res, $host )

Verify that `Content-Security-Policy` header allows to connect to $host.

#### getCookies($res)

Returns an hash ref with names => values of cookies set by server.

#### getHeader( $res, $hname )

Returns value of first header named $hname in $res response.

#### getRedirection($res)

Returns value of `Location` header.

#### getUser($res)

Returns value of `Lm-Remote-User` header.

## LLNG::Manager::Test Class

### Accessors

- app: built application
- class: class to test (default Lemonldap::NG::Portal::Main)
- p: portal object
- ini: initialization parameters ($defaultIni values + given parameters)

### Methods

#### logout($id)

Launch a `/?logout=1` request an test:

- if response is 200
- if cookie 'lemonldap' and 'lemonldappdata' have no value
- if a GET request with previous cookie value _($i)_ is rejected

#### \_get( $path, %args )

Simulates a GET requests to $path. Accepted arguments:

- accept: accepted content, default to Ajax request. Use 'text/html'
to test content _(to launch a `expectForm()` for example)_.
- cookie: full cookie string
- custom: additional headers (hash ref only)
- ip: remote address. Default to 127.0.0.1
- method: default to GET. Only GET/DELETE values are acceptable
(use `_post()` if you want to launch a POST/PUT request)
- query: query string
- referer
- remote\_user: REMOTE\_USER header value

#### \_post( $path, $body, %args )

Same as `_get` except that a body is required. $body must be a file handle.
Example with IO::String:

    ok( 
        $res = $client->_post(
            '/',
            IO::String->new('user=dwho&password=dwho'),
            length => 23
        ),
        'Auth query'
    );

#### \_delete( $path, %args )

Call `_get()` with method set to DELETE.

#### \_put( $path, $body, %args )

Call `_post()` with method set to PUT