File: test_grantip.py

package info (click to toggle)
paste 3.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,472 kB
  • sloc: python: 19,960; javascript: 8,028; makefile: 47; sh: 24
file content (36 lines) | stat: -rw-r--r-- 1,246 bytes parent folder | download
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
from paste.auth import grantip
from paste.fixture import TestApp

def _make_app():
    def application(environ, start_response):
        start_response('200 OK', [('content-type', 'text/plain')])
        lines = [
            str(environ.get('REMOTE_USER')),
            ':',
            str(environ.get('REMOTE_USER_TOKENS')),
            ]
        lines = [line.encode('utf8') for line in lines]
        return lines
    ip_map = {
        '127.0.0.1': (None, 'system'),
        '192.168.0.0/16': (None, 'worker'),
        '192.168.0.5<->192.168.0.8': ('bob', 'editor'),
        '192.168.0.8': ('__remove__', '-worker'),
        }
    app = grantip.GrantIPMiddleware(application, ip_map)
    app = TestApp(app)
    return app

def test_req():
    app = _make_app()
    def doit(remote_addr):
        res = app.get('/', extra_environ={'REMOTE_ADDR': remote_addr})
        return res.body
    assert doit('127.0.0.1') == b'None:system'
    assert doit('192.168.15.12') == b'None:worker'
    assert doit('192.168.0.4') == b'None:worker'
    result = doit('192.168.0.5')
    assert result.startswith(b'bob:')
    assert b'editor' in result and b'worker' in result
    assert result.count(b',') == 1
    assert doit('192.168.0.8') == b'None:editor'