Klaus Demo klaus / af02129
Merge pull request #171 from jimmyppi/wsgi_env Expose all app arguments as env variables Jonas Haag authored 3 years ago GitHub committed 3 years ago
4 changed file(s) with 123 addition(s) and 36 deletion(s). Raw diff Collapse all Expand all
0 import os
1 from distutils.util import strtobool
2
3
4 def get_args_from_env():
5 repos = os.environ.get('KLAUS_REPOS', [])
6 if repos:
7 repos = repos.split()
8 args = (
9 repos,
10 os.environ['KLAUS_SITE_NAME']
11 )
12 kwargs = dict(
13 htdigest_file=os.environ.get('KLAUS_HTDIGEST_FILE'),
14 use_smarthttp=strtobool(os.environ.get('KLAUS_USE_SMARTHTTP', '0')),
15 require_browser_auth=strtobool(
16 os.environ.get('KLAUS_REQUIRE_BROWSER_AUTH', '0')),
17 disable_push=strtobool(os.environ.get('KLAUS_DISABLE_PUSH', '0')),
18 unauthenticated_push=strtobool(
19 os.environ.get('KLAUS_UNAUTHENTICATED_PUSH', '0')),
20 ctags_policy=os.environ.get('KLAUS_CTAGS_POLICY', 'none')
21 )
22 return args, kwargs
0 import os
10 from klaus import make_app
1 from .app_args import get_args_from_env
22
3 if 'KLAUS_HTDIGEST_FILE' in os.environ:
4 with open(os.environ['KLAUS_HTDIGEST_FILE']) as file:
5 application = make_app(
6 os.environ['KLAUS_REPOS'].split(),
7 os.environ['KLAUS_SITE_NAME'],
8 os.environ.get('KLAUS_USE_SMARTHTTP'),
9 file,
10 )
3 args, kwargs = get_args_from_env()
4
5 if kwargs['htdigest_file']:
6 with open(kwargs['htdigest_file']) as file:
7 kwargs['htdigest_file'] = file
8 application = make_app(*args, **kwargs)
119 else:
12 application = make_app(
13 os.environ['KLAUS_REPOS'].split(),
14 os.environ['KLAUS_SITE_NAME'],
15 os.environ.get('KLAUS_USE_SMARTHTTP'),
16 None,
17 )
10 application = make_app(*args, **kwargs)
55 from io import open, StringIO
66
77 from klaus import make_app
8 from .app_args import get_args_from_env
9
810
911 # Shared state between poller and application wrapper
1012 class _:
5456 if 'KLAUS_REPOS' in os.environ:
5557 warnings.warn("use KLAUS_REPOS_ROOT instead of KLAUS_REPOS for the autoreloader apps", DeprecationWarning)
5658
57 if 'KLAUS_HTDIGEST_FILE' in os.environ:
59 args, kwargs = get_args_from_env()
60 repos_root = os.environ.get('KLAUS_REPOS_ROOT') or os.environ['KLAUS_REPOS']
61 args = (repos_root,) + args[1:]
62
63 if kwargs['htdigest_file']:
5864 # Cache the contents of the htdigest file, the application will not read
5965 # the file like object until later when called.
60 with open(os.environ['KLAUS_HTDIGEST_FILE'], encoding='utf-8') as htdigest_file:
61 htdigest_io = StringIO(htdigest_file.read())
62 application = make_autoreloading_app(
63 os.environ.get('KLAUS_REPOS_ROOT') or os.environ['KLAUS_REPOS'],
64 os.environ['KLAUS_SITE_NAME'],
65 os.environ.get('KLAUS_USE_SMARTHTTP'),
66 htdigest_io,
67 )
68 else:
69 application = make_autoreloading_app(
70 os.environ.get('KLAUS_REPOS_ROOT') or os.environ['KLAUS_REPOS'],
71 os.environ['KLAUS_SITE_NAME'],
72 os.environ.get('KLAUS_USE_SMARTHTTP'),
73 )
66 with open(kwargs['htdigest_file'], encoding='utf-8') as htdigest_file:
67 kwargs['htdigest_file'] = StringIO(htdigest_file.read())
68
69 application = make_autoreloading_app(*args, **kwargs)
33 except ImportError:
44 pass
55
6 import pytest
7
8 from klaus.contrib import app_args
69 from .utils import *
710 from .test_make_app import can_reach_unauth, can_push_auth
811
912
10 def test_wsgi():
13 def check_env(env, expected_args, expected_kwargs):
14 os.environ.update(env)
15 args, kwargs = app_args.get_args_from_env()
16 assert args == expected_args
17 assert kwargs == expected_kwargs
18
19
20 def test_missing_in_env(monkeypatch):
21 """Test that KeyError is raised when required env var is missing"""
22 monkeypatch.setattr(os, 'environ', os.environ.copy())
23 with pytest.raises(KeyError):
24 args, kwargs = app_args.get_args_from_env()
25
26
27 def test_minimum_env(monkeypatch):
28 """Test to provide only required env var"""
29 monkeypatch.setattr(os, 'environ', os.environ.copy())
30 check_env(
31 {'KLAUS_SITE_NAME': TEST_SITE_NAME},
32 ([], TEST_SITE_NAME),
33 dict(
34 htdigest_file=None,
35 use_smarthttp=False,
36 require_browser_auth=False,
37 disable_push=False,
38 unauthenticated_push=False,
39 ctags_policy='none')
40 )
41
42
43 def test_complete_env(monkeypatch):
44 """Test to provide all supported env var"""
45 monkeypatch.setattr(os, 'environ', os.environ.copy())
46 check_env(
47 {
48 'KLAUS_REPOS': TEST_REPO,
49 'KLAUS_SITE_NAME': TEST_SITE_NAME,
50 'KLAUS_HTDIGEST_FILE': HTDIGEST_FILE,
51 'KLAUS_USE_SMARTHTTP': 'yes',
52 'KLAUS_REQUIRE_BROWSER_AUTH': '1',
53 'KLAUS_DISABLE_PUSH': 'false',
54 'KLAUS_UNAUTHENTICATED_PUSH': '0',
55 'KLAUS_CTAGS_POLICY': 'ALL'
56 },
57 ([TEST_REPO], TEST_SITE_NAME),
58 dict(
59 htdigest_file=HTDIGEST_FILE,
60 use_smarthttp=True,
61 require_browser_auth=True,
62 disable_push=False,
63 unauthenticated_push=False,
64 ctags_policy='ALL')
65 )
66
67
68 def test_unsupported_boolean_env(monkeypatch):
69 """Test that unsupported boolean env var raises ValueError"""
70 monkeypatch.setattr(os, 'environ', os.environ.copy())
71 with pytest.raises(ValueError):
72 check_env(
73 {
74 'KLAUS_REPOS': TEST_REPO,
75 'KLAUS_SITE_NAME': TEST_SITE_NAME,
76 'KLAUS_HTDIGEST_FILE': HTDIGEST_FILE,
77 'KLAUS_USE_SMARTHTTP': 'unsupported',
78 }, (), {}
79 )
80
81
82 def test_wsgi(monkeypatch):
83 """Test start of wsgi app"""
84 monkeypatch.setattr(os, 'environ', os.environ.copy())
1185 os.environ['KLAUS_REPOS'] = TEST_REPO
1286 os.environ['KLAUS_SITE_NAME'] = TEST_SITE_NAME
13 os.environ.pop('KLAUS_HTDIGEST_FILE', None)
14 os.environ.pop('KLAUS_USE_SMARTHTTP', None)
1587 from klaus.contrib import wsgi
1688 with serve_app(wsgi.application):
1789 assert can_reach_unauth()
90 assert not can_push_auth()
1891
1992 os.environ['KLAUS_HTDIGEST_FILE'] = HTDIGEST_FILE
2093 os.environ['KLAUS_USE_SMARTHTTP'] = 'yes'
2194 reload(wsgi)
2295 with serve_app(wsgi.application):
96 assert can_reach_unauth()
2397 assert can_push_auth()
2498
2599
26 def test_wsgi_autoreload():
100 def test_wsgi_autoreload(monkeypatch):
101 """Test start of wsgi autoreload app"""
102 monkeypatch.setattr(os, 'environ', os.environ.copy())
27103 os.environ['KLAUS_REPOS_ROOT'] = TEST_REPO_ROOT
28104 os.environ['KLAUS_SITE_NAME'] = TEST_SITE_NAME
29 os.environ.pop('KLAUS_HTDIGEST_FILE', None)
30 os.environ.pop('KLAUS_USE_SMARTHTTP', None)
31105 from klaus.contrib import wsgi_autoreload
32106 with serve_app(wsgi_autoreload.application):
33107 assert can_reach_unauth()
108 assert not can_push_auth()
34109
35110 os.environ['KLAUS_HTDIGEST_FILE'] = HTDIGEST_FILE
36111 os.environ['KLAUS_USE_SMARTHTTP'] = 'yes'