1from paste.auth import grantip
2from paste.fixture import *
3
4def test_make_app():
5    def application(environ, start_response):
6        start_response('200 OK', [('content-type', 'text/plain')])
7        lines = [
8            str(environ.get('REMOTE_USER')),
9            ':',
10            str(environ.get('REMOTE_USER_TOKENS')),
11            ]
12        if six.PY3:
13            lines = [line.encode('utf8') for line in lines]
14        return lines
15    ip_map = {
16        '127.0.0.1': (None, 'system'),
17        '192.168.0.0/16': (None, 'worker'),
18        '192.168.0.5<->192.168.0.8': ('bob', 'editor'),
19        '192.168.0.8': ('__remove__', '-worker'),
20        }
21    app = grantip.GrantIPMiddleware(application, ip_map)
22    app = TestApp(app)
23    return app
24
25def test_req():
26    app = test_make_app()
27    def doit(remote_addr):
28        res = app.get('/', extra_environ={'REMOTE_ADDR': remote_addr})
29        return res.body
30    assert doit('127.0.0.1') == b'None:system'
31    assert doit('192.168.15.12') == b'None:worker'
32    assert doit('192.168.0.4') == b'None:worker'
33    result = doit('192.168.0.5')
34    assert result.startswith(b'bob:')
35    assert b'editor' in result and b'worker' in result
36    assert result.count(b',') == 1
37    assert doit('192.168.0.8') == b'None:editor'
38