Skip to content

Commit a52631a

Browse files
committed
Move site directory init check to app creation
1 parent e90358e commit a52631a

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

test_webhook.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,9 @@ async def test_update_repo(tmp_path_factory):
6565
assert dest_commit == src_commit
6666

6767

68-
async def test_github_webhook_errors(aiohttp_client, monkeypatch):
68+
async def test_github_webhook_errors(aiohttp_client, monkeypatch, tmp_path):
6969
"""Test invalid inputs to webhook."""
70+
monkeypatch.setenv('SITE_DIR', str(tmp_path))
7071
client = await aiohttp_client(create_app())
7172

7273
# Only /gh/<repo-name> exists.
@@ -142,12 +143,12 @@ async def test_github_webhook_errors(aiohttp_client, monkeypatch):
142143

143144
async def test_github_webhook_valid(aiohttp_client, monkeypatch, tmp_path):
144145
"""Test valid input to webhook."""
146+
monkeypatch.setenv('SITE_DIR', str(tmp_path))
145147
client = await aiohttp_client(create_app())
146148

147149
# Do no actual work, since that's tested above.
148150
monkeypatch.setattr(webhook, 'verify_signature',
149151
mock.Mock(verify_signature, return_value=True))
150-
monkeypatch.setenv('SITE_DIR', str(tmp_path))
151152
ur_mock = mock.Mock(update_repo, return_value=None)
152153
monkeypatch.setattr(webhook, 'update_repo', ur_mock)
153154

webhook.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,8 @@ async def github_webhook(request: web.Request):
156156
delivery, ref, expected_branch)
157157
return web.Response(status=200)
158158

159-
site_dir = Path(os.environ.get('SITE_DIR', 'sites')).resolve()
160-
checkout = (site_dir / repository).resolve()
161-
if not checkout.is_relative_to(site_dir):
159+
checkout = (request.app['site_dir'] / repository).resolve()
160+
if not checkout.is_relative_to(request.app['site_dir']):
162161
raise web.HTTPBadRequest(
163162
reason=(f'{delivery}: Checkout for {organization}/{repository} '
164163
'does not exist'))
@@ -176,16 +175,18 @@ async def github_webhook(request: web.Request):
176175

177176
def create_app():
178177
"""Create the aiohttp app and setup routes."""
178+
site_dir = Path(os.environ.get('SITE_DIR', 'sites')).resolve()
179+
assert site_dir.is_dir()
180+
179181
app = web.Application()
182+
app['site_dir'] = site_dir
180183
app.add_routes([
181184
web.post('/gh/{repo}', github_webhook),
182185
])
183186
return app
184187

185188

186189
if __name__ == '__main__':
187-
assert Path(os.environ.get('SITE_DIR', 'sites')).is_dir()
188-
189190
if len(sys.argv) > 1:
190191
from urllib.parse import urlparse
191192
url = sys.argv[1]

0 commit comments

Comments
 (0)