wrolpi/modules/repos/test/test_extras.py
Roland 03e3476631
repos: history, Download ZIP, and submodules
* GET /api/repos/<id>/log: the checked out branch's commits, newest
  first, a page at a time, with the total.  The repo page has a
  Files | History toggle (the tree is only listed for Files).
* GET /api/repos/<id>/archive.zip: `git archive HEAD` streamed to the
  browser; the file name is reduced to safe characters.  Everything is
  read from the database before the response starts, because the
  request's session closes once a streamed response begins.
* repository.submodules (default off): submodules are checked out with
  the repo's mode and the same git restrictions, and removed when the
  option is turned off.  A submodule that cannot be downloaded (e.g. an
  ssh URL) does not stop the repo from updating; the download is
  retried with the submodule's error.
* The README speed test bound is loose enough for a busy machine.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-06 16:56:09 -06:00

186 lines
8.2 KiB
Python

import io
import subprocess
import zipfile
from http import HTTPStatus
import pytest
from mock import mock
from modules.repos import lib
from modules.repos.models import Repository
from wrolpi.downloader import Download, DownloadStatus
def git(directory, *args) -> str:
return subprocess.run(['git', '-c', 'safe.directory=*', *args], cwd=directory, check=True,
capture_output=True).stdout.decode().strip()
async def update(test_session, repos_download_manager):
download = test_session.query(Download).one()
download.renew()
test_session.commit()
await repos_download_manager.wait_for_all_downloads()
test_session.expire_all()
return test_session.query(Download).one()
@pytest.mark.asyncio
async def test_log(test_session, async_client, git_remote_factory, repos_download_manager):
"""The history of the checked out branch, newest first, a page at a time."""
remote = git_remote_factory()
second = remote.commit({'a.txt': 'a'}, 'Second')
third = remote.commit({'b.txt': 'b'}, 'Third: with a | and a\ttab')
repo = lib.create_repository(test_session, remote.url)
await repos_download_manager.wait_for_all_downloads()
request, response = await async_client.get(f'/api/repos/{repo.id}/log?limit=2')
assert response.status_code == HTTPStatus.OK
assert response.json['total'] == 3
assert [(i['sha'], i['message']) for i in response.json['commits']] == \
[(third, 'Third: with a | and a\ttab'), (second, 'Second')]
assert response.json['commits'][0]['author'] == 'WROLPi Test'
assert response.json['commits'][0]['date']
request, response = await async_client.get(f'/api/repos/{repo.id}/log?limit=2&offset=2')
assert [i['message'] for i in response.json['commits']] == ['Initial commit']
request, response = await async_client.get(f'/api/repos/{repo.id}/log?limit=nope')
assert response.status_code == HTTPStatus.BAD_REQUEST
@pytest.mark.asyncio
async def test_log_not_downloaded(test_session, async_client, test_wrolpi_config):
repo = lib.create_repository(test_session, 'https://github.com/a/b')
request, response = await async_client.get(f'/api/repos/{repo.id}/log')
assert response.status_code == HTTPStatus.NOT_FOUND
request, response = await async_client.get(f'/api/repos/{repo.id}/archive.zip')
assert response.status_code == HTTPStatus.NOT_FOUND
@pytest.mark.asyncio
async def test_archive(test_session, async_client, git_remote_factory, repos_download_manager):
"""A ZIP of the checked out commit; local changes and `.git` are not in it."""
remote = git_remote_factory(files={'README.md': '# Example', 'src/main.c': 'int main;'})
repo = lib.create_repository(test_session, remote.url, name='my "repo"\r\nX-Evil: 1')
await repos_download_manager.wait_for_all_downloads()
test_session.expire_all()
(repo.directory / 'untracked.txt').write_text('local')
request, response = await async_client.get(f'/api/repos/{repo.id}/archive.zip')
assert response.status_code == HTTPStatus.OK
assert response.headers['content-type'] == 'application/zip'
# The name is safe in a header.
name = f'my_repo_X-Evil_1-{repo.head_sha[:7]}'
assert response.headers['content-disposition'] == f'attachment; filename="{name}.zip"'
assert 'x-evil' not in response.headers
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
assert sorted(archive.namelist()) == [f'{name}/', f'{name}/README.md', f'{name}/src/', f'{name}/src/main.c']
assert archive.read(f'{name}/src/main.c') == b'int main;'
@pytest.mark.asyncio
async def test_submodules(test_session, git_remote_factory, repos_download_manager):
"""Submodules are only downloaded when the Repo asks for them, and are removed when it no longer does."""
library = git_remote_factory('library', files={'lib.c': 'v1'})
remote = git_remote_factory()
remote.add_submodule('vendor/library', library)
repo = lib.create_repository(test_session, remote.url)
await repos_download_manager.wait_for_all_downloads()
submodule = repo.directory / 'vendor/library'
assert not (submodule / 'lib.c').exists()
lib.update_repository(test_session, repo.id, submodules=True)
download = await update(test_session, repos_download_manager)
assert download.status == DownloadStatus.complete
assert (submodule / 'lib.c').read_text() == 'v1'
# The submodule has its history, like the Repo.
assert git(submodule, 'rev-list', '--count', 'HEAD') == '1'
# A new submodule commit is checked out when the Repo points at it.
library.commit({'lib.c': 'v2'}, 'v2')
remote.git('update-index', '--cacheinfo', f'160000,{library.head},vendor/library')
remote.git('commit', '--quiet', '-m', 'Bump library')
await update(test_session, repos_download_manager)
assert (submodule / 'lib.c').read_text() == 'v2'
lib.update_repository(test_session, repo.id, submodules=False)
await update(test_session, repos_download_manager)
assert not (submodule / 'lib.c').exists()
@pytest.mark.asyncio
async def test_submodule_failure(test_session, git_remote_factory, repos_download_manager):
"""A submodule which cannot be downloaded (e.g. an ssh URL) does not stop the Repo from updating."""
library = git_remote_factory('library')
remote = git_remote_factory()
remote.add_submodule('vendor/library', library)
# Rewrite the submodule's URL to one git may not use.
(remote.path / '.gitmodules').write_text(
'[submodule "vendor/library"]\n\tpath = vendor/library\n\turl = ssh://git@git.example.com/owner/library\n')
(remote.path / 'new.txt').write_text('new')
# Not `remote.commit`, whose `add --all` would delete the submodule (it is not checked out in the remote).
remote.git('add', '.gitmodules', 'new.txt')
remote.git('commit', '--quiet', '-m', 'ssh submodule')
head = remote.head
repo = lib.create_repository(test_session, remote.url, submodules=True)
await repos_download_manager.wait_for_all_downloads()
test_session.expire_all()
download = test_session.query(Download).one()
assert download.status == DownloadStatus.deferred
assert 'its submodules were not' in download.error
# The Repo itself is current.
assert repo.head_sha == head
assert (repo.directory / 'new.txt').is_file()
@pytest.mark.asyncio
async def test_submodules_api_and_config(test_session, async_client, test_wrolpi_config, await_switches):
body = '{"url": "https://github.com/a/b", "submodules": true}'
request, response = await async_client.post('/api/repos', content=body)
assert response.status_code == HTTPStatus.CREATED
assert response.json['repo']['submodules'] is True
repo_id = response.json['repo']['id']
await await_switches()
assert lib.get_repos_config().repos[0]['submodules'] is True
request, response = await async_client.put(f'/api/repos/{repo_id}', content='{"submodules": false}')
assert response.json['repo']['submodules'] is False
# Restored from the config.
config = lib.get_repos_config()
data = config.read_config_file()
data['repos'][0]['submodules'] = True
data['version'] += 1
config.write_config_data(data, config.get_file())
config.import_config()
test_session.expire_all()
assert test_session.query(Repository).one().submodules is True
@pytest.mark.asyncio
async def test_archive_after_session_closes(test_session, async_client, git_remote_factory, repos_download_manager):
"""In production the request's session is closed once a streamed response starts; the ZIP must not need it."""
from sanic import Request
remote = git_remote_factory()
repo = lib.create_repository(test_session, remote.url)
await repos_download_manager.wait_for_all_downloads()
original_respond = Request.respond
async def respond_and_close_session(self, *args, **kwargs):
response = await original_respond(self, *args, **kwargs)
test_session.expunge_all()
return response
with mock.patch.object(Request, 'respond', respond_and_close_session):
request, response = await async_client.get(f'/api/repos/{repo.id}/archive.zip')
assert response.status_code == HTTPStatus.OK
with zipfile.ZipFile(io.BytesIO(response.content)) as archive:
assert any(i.endswith('/README.md') for i in archive.namelist())