|
| 1 | +"""Unit tests for app_backup.restore_backup() chunk and lock validation. |
| 2 | +
|
| 3 | +Exercises the /api/backup/restore route through a Flask test client with |
| 4 | +multipart/form-data uploads. The module-level Redis lock helpers are patched |
| 5 | +(the functions, not redis), BACKUP_DIR is redirected to a pytest tmp dir, and |
| 6 | +no test ever completes a chunk set, so the detached restore subprocess is |
| 7 | +never spawned. |
| 8 | +""" |
| 9 | +import io |
| 10 | +import os |
| 11 | +from unittest.mock import MagicMock |
| 12 | + |
| 13 | +import pytest |
| 14 | +from flask import Flask |
| 15 | + |
| 16 | +import app_backup |
| 17 | + |
| 18 | +CONFIRMATION = "I want to restore the database from the backup. This action is not reversible" |
| 19 | + |
| 20 | + |
| 21 | +@pytest.fixture |
| 22 | +def client(): |
| 23 | + app = Flask(__name__) |
| 24 | + app.config['TESTING'] = True |
| 25 | + app.register_blueprint(app_backup.backup_bp) |
| 26 | + return app.test_client() |
| 27 | + |
| 28 | + |
| 29 | +def _form(confirmation=CONFIRMATION, chunk_num=None, total_chunks=None, with_file=True): |
| 30 | + data = {'confirmation': confirmation} |
| 31 | + if chunk_num is not None: |
| 32 | + data['chunk_num'] = str(chunk_num) |
| 33 | + if total_chunks is not None: |
| 34 | + data['total_chunks'] = str(total_chunks) |
| 35 | + if with_file: |
| 36 | + data['file'] = (io.BytesIO(b'SELECT 1;\n'), 'backup.sql') |
| 37 | + return data |
| 38 | + |
| 39 | + |
| 40 | +def _post(client, **kwargs): |
| 41 | + return client.post( |
| 42 | + '/api/backup/restore', |
| 43 | + data=_form(**kwargs), |
| 44 | + content_type='multipart/form-data', |
| 45 | + ) |
| 46 | + |
| 47 | + |
| 48 | +class TestRestoreValidation: |
| 49 | + def test_wrong_confirmation_is_400(self, client): |
| 50 | + resp = _post(client, confirmation='nope') |
| 51 | + assert resp.status_code == 400 |
| 52 | + assert 'Confirmation' in resp.get_json()['error'] |
| 53 | + |
| 54 | + def test_missing_confirmation_is_400(self, client): |
| 55 | + resp = _post(client, confirmation='') |
| 56 | + assert resp.status_code == 400 |
| 57 | + |
| 58 | + def test_missing_file_is_400(self, client): |
| 59 | + resp = _post(client, with_file=False) |
| 60 | + assert resp.status_code == 400 |
| 61 | + assert resp.get_json()['error'] == 'No file uploaded.' |
| 62 | + |
| 63 | + def test_non_integer_chunk_fields_are_400(self, client): |
| 64 | + resp = _post(client, chunk_num='abc', total_chunks='3') |
| 65 | + assert resp.status_code == 400 |
| 66 | + assert 'must be integers' in resp.get_json()['error'] |
| 67 | + |
| 68 | + @pytest.mark.parametrize('chunk_num,total_chunks', [ |
| 69 | + (0, 3), |
| 70 | + (4, 3), |
| 71 | + (2, 1), |
| 72 | + (-1, 3), |
| 73 | + (0, 0), |
| 74 | + ]) |
| 75 | + def test_chunk_num_out_of_range_is_400(self, client, chunk_num, total_chunks): |
| 76 | + resp = _post(client, chunk_num=chunk_num, total_chunks=total_chunks) |
| 77 | + assert resp.status_code == 400 |
| 78 | + assert 'Invalid chunk numbers' in resp.get_json()['error'] |
| 79 | + |
| 80 | + |
| 81 | +class TestRestoreLock: |
| 82 | + def test_first_chunk_lock_already_held_is_409(self, client, monkeypatch, tmp_path): |
| 83 | + monkeypatch.setattr(app_backup, 'BACKUP_DIR', str(tmp_path)) |
| 84 | + monkeypatch.setattr(app_backup, '_acquire_restore_lock', lambda: False) |
| 85 | + resp = _post(client, chunk_num=1, total_chunks=3) |
| 86 | + assert resp.status_code == 409 |
| 87 | + assert 'already in progress' in resp.get_json()['error'] |
| 88 | + |
| 89 | + def test_later_chunk_lock_not_held_is_409(self, client, monkeypatch, tmp_path): |
| 90 | + monkeypatch.setattr(app_backup, 'BACKUP_DIR', str(tmp_path)) |
| 91 | + monkeypatch.setattr(app_backup, '_restore_lock_held', lambda: False) |
| 92 | + resp = _post(client, chunk_num=2, total_chunks=3) |
| 93 | + assert resp.status_code == 409 |
| 94 | + assert 'Restart the upload from chunk 1' in resp.get_json()['error'] |
| 95 | + |
| 96 | + def test_later_chunk_never_tries_to_acquire(self, client, monkeypatch, tmp_path): |
| 97 | + monkeypatch.setattr(app_backup, 'BACKUP_DIR', str(tmp_path)) |
| 98 | + acquire = MagicMock(return_value=True) |
| 99 | + monkeypatch.setattr(app_backup, '_acquire_restore_lock', acquire) |
| 100 | + monkeypatch.setattr(app_backup, '_restore_lock_held', lambda: False) |
| 101 | + resp = _post(client, chunk_num=2, total_chunks=3) |
| 102 | + assert resp.status_code == 409 |
| 103 | + acquire.assert_not_called() |
| 104 | + |
| 105 | + def test_single_file_upload_lock_held_is_409(self, client, monkeypatch): |
| 106 | + monkeypatch.setattr(app_backup, '_acquire_restore_lock', lambda: False) |
| 107 | + resp = _post(client) |
| 108 | + assert resp.status_code == 409 |
| 109 | + assert 'already in progress' in resp.get_json()['error'] |
| 110 | + |
| 111 | + |
| 112 | +class TestRestoreChunkProgress: |
| 113 | + def test_intermediate_chunk_is_acknowledged(self, client, monkeypatch, tmp_path): |
| 114 | + monkeypatch.setattr(app_backup, 'BACKUP_DIR', str(tmp_path)) |
| 115 | + monkeypatch.setattr(app_backup, '_acquire_restore_lock', lambda: True) |
| 116 | + resp = _post(client, chunk_num=1, total_chunks=3) |
| 117 | + assert resp.status_code == 200 |
| 118 | + body = resp.get_json() |
| 119 | + assert body['success'] is True |
| 120 | + assert body['all_chunks_received'] is False |
| 121 | + assert body['chunk_num'] == 1 |
| 122 | + assert body['total_chunks'] == 3 |
| 123 | + assert body['received_chunks'] == [1] |
| 124 | + assert body['missing_chunks'] == [2, 3] |
| 125 | + assert os.path.exists(os.path.join(str(tmp_path), 'chunks', 'backup_1_of_3.sql')) |
| 126 | + |
| 127 | + def test_first_chunk_wipes_leftover_chunks(self, client, monkeypatch, tmp_path): |
| 128 | + monkeypatch.setattr(app_backup, 'BACKUP_DIR', str(tmp_path)) |
| 129 | + monkeypatch.setattr(app_backup, '_acquire_restore_lock', lambda: True) |
| 130 | + chunks_dir = tmp_path / 'chunks' |
| 131 | + chunks_dir.mkdir() |
| 132 | + leftover = chunks_dir / 'backup_2_of_3.sql' |
| 133 | + leftover.write_bytes(b'stale data') |
| 134 | + resp = _post(client, chunk_num=1, total_chunks=3) |
| 135 | + assert resp.status_code == 200 |
| 136 | + body = resp.get_json() |
| 137 | + assert body['received_chunks'] == [1] |
| 138 | + assert body['missing_chunks'] == [2, 3] |
| 139 | + assert not leftover.exists() |
| 140 | + |
| 141 | + def test_second_chunk_keeps_existing_chunks(self, client, monkeypatch, tmp_path): |
| 142 | + monkeypatch.setattr(app_backup, 'BACKUP_DIR', str(tmp_path)) |
| 143 | + monkeypatch.setattr(app_backup, '_restore_lock_held', lambda: True) |
| 144 | + chunks_dir = tmp_path / 'chunks' |
| 145 | + chunks_dir.mkdir() |
| 146 | + (chunks_dir / 'backup_1_of_3.sql').write_bytes(b'first chunk') |
| 147 | + resp = _post(client, chunk_num=2, total_chunks=3) |
| 148 | + assert resp.status_code == 200 |
| 149 | + body = resp.get_json() |
| 150 | + assert body['all_chunks_received'] is False |
| 151 | + assert body['received_chunks'] == [1, 2] |
| 152 | + assert body['missing_chunks'] == [3] |
| 153 | + assert (chunks_dir / 'backup_1_of_3.sql').exists() |
| 154 | + assert (chunks_dir / 'backup_2_of_3.sql').exists() |
0 commit comments