|
| 1 | +import time |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from addons.osfstorage.settings import DEFAULT_REGION_NAME |
| 6 | +from api_tests.utils import create_test_file |
| 7 | +from framework.auth import signing |
| 8 | +from osf.models import DownloadEvent |
| 9 | +from osf.utils.download_telemetry import derive_user_region, record_download |
| 10 | +from osf_tests.factories import AuthUserFactory, ProjectFactory |
| 11 | +from tests.base import OsfTestCase |
| 12 | + |
| 13 | + |
| 14 | +class TestZipDownloadTelemetry(OsfTestCase): |
| 15 | + """Folder and project zips, recorded from the WaterButler callback. |
| 16 | +
|
| 17 | + Zips are requested straight from WaterButler, so this callback is the only place we |
| 18 | + hear about them. |
| 19 | + """ |
| 20 | + |
| 21 | + def setUp(self): |
| 22 | + super().setUp() |
| 23 | + self.user = AuthUserFactory() |
| 24 | + self.node = ProjectFactory(creator=self.user) |
| 25 | + self.url = self.node.api_url_for('create_waterbutler_log') |
| 26 | + |
| 27 | + def build_payload(self, materialized='/', action='download_zip', **action_meta): |
| 28 | + meta = dict( |
| 29 | + bytes_downloaded=2048, |
| 30 | + completed=True, |
| 31 | + ip='198.51.100.7', |
| 32 | + source='files', |
| 33 | + tz='Europe/Kyiv', |
| 34 | + ) |
| 35 | + meta.update(action_meta) |
| 36 | + options = { |
| 37 | + 'auth': {'id': self.user._id}, |
| 38 | + 'action': action, |
| 39 | + 'provider': 'osfstorage', |
| 40 | + 'time': time.time() + 1000, |
| 41 | + 'metadata': { |
| 42 | + 'nid': self.node._id, |
| 43 | + 'materialized': materialized, |
| 44 | + 'path': materialized, |
| 45 | + 'kind': 'folder', |
| 46 | + 'provider': 'osfstorage', |
| 47 | + }, |
| 48 | + 'action_meta': meta, |
| 49 | + } |
| 50 | + message, signature = signing.default_signer.sign_payload(options) |
| 51 | + return {'payload': message, 'signature': signature} |
| 52 | + |
| 53 | + def test_project_zip_is_recorded(self): |
| 54 | + res = self.app.put(self.url, json=self.build_payload(materialized='/')) |
| 55 | + |
| 56 | + assert res.status_code == 200 |
| 57 | + event = DownloadEvent.objects.get() |
| 58 | + assert event.download_type == DownloadEvent.PROJECT |
| 59 | + assert event.resource_guid == self.node._id |
| 60 | + assert event.user == self.user |
| 61 | + assert event.size_bytes == 2048 |
| 62 | + assert event.zip_completed is True |
| 63 | + assert event.ip == '198.51.100.7' |
| 64 | + assert event.source_area == 'files' |
| 65 | + assert event.user_region == 'Europe/Kyiv' |
| 66 | + |
| 67 | + def test_folder_zip_is_recorded_with_its_path(self): |
| 68 | + self.app.put(self.url, json=self.build_payload(materialized='/data/raw/')) |
| 69 | + |
| 70 | + event = DownloadEvent.objects.get() |
| 71 | + assert event.download_type == DownloadEvent.FOLDER_ZIP |
| 72 | + assert event.path == '/data/raw/' |
| 73 | + |
| 74 | + def test_incomplete_zip_is_recorded_as_incomplete(self): |
| 75 | + self.app.put(self.url, json=self.build_payload(completed=False)) |
| 76 | + |
| 77 | + assert DownloadEvent.objects.get().zip_completed is False |
| 78 | + |
| 79 | + def test_mfr_render_is_not_recorded(self): |
| 80 | + self.app.put(self.url, json=self.build_payload(is_mfr_render=True)) |
| 81 | + |
| 82 | + assert not DownloadEvent.objects.exists() |
| 83 | + |
| 84 | + def test_single_file_action_is_not_recorded_here(self): |
| 85 | + """Single files are caught at the redirect view — recording them here too would |
| 86 | + double count every one of them.""" |
| 87 | + self.app.put(self.url, json=self.build_payload(action='download_file')) |
| 88 | + |
| 89 | + assert not DownloadEvent.objects.exists() |
| 90 | + |
| 91 | + def test_oversized_source_is_truncated_to_the_column(self): |
| 92 | + self.app.put(self.url, json=self.build_payload(source='f' * 500)) |
| 93 | + |
| 94 | + assert len(DownloadEvent.objects.get().source_area) == 128 |
| 95 | + |
| 96 | + def test_callback_still_succeeds_when_recording_fails(self, ): |
| 97 | + with pytest.MonkeyPatch.context() as patch: |
| 98 | + patch.setattr( |
| 99 | + 'addons.base.views.record_download', |
| 100 | + lambda **kwargs: (_ for _ in ()).throw(ValueError('boom')), |
| 101 | + ) |
| 102 | + res = self.app.put(self.url, json=self.build_payload()) |
| 103 | + |
| 104 | + assert res.status_code == 200 |
| 105 | + |
| 106 | + |
| 107 | +class TestSingleFileDownloadTelemetry(OsfTestCase): |
| 108 | + """Single files, recorded at the redirect view before we 302 on to WaterButler.""" |
| 109 | + |
| 110 | + def setUp(self): |
| 111 | + super().setUp() |
| 112 | + self.user = AuthUserFactory() |
| 113 | + self.node = ProjectFactory(creator=self.user) |
| 114 | + self.file = create_test_file(self.node, self.user, size=4096) |
| 115 | + self.guid = self.file.get_guid()._id |
| 116 | + |
| 117 | + def test_download_is_recorded_with_link_tags(self): |
| 118 | + res = self.app.get( |
| 119 | + f'/download/{self.guid}/?source=file-detail&tz=Europe%2FKyiv', |
| 120 | + auth=self.user.auth, |
| 121 | + ) |
| 122 | + |
| 123 | + assert res.status_code == 302 |
| 124 | + event = DownloadEvent.objects.get() |
| 125 | + assert event.download_type == DownloadEvent.FILE |
| 126 | + assert event.resource_guid == self.node._id |
| 127 | + assert event.user == self.user |
| 128 | + assert event.source_area == 'file-detail' |
| 129 | + assert event.user_region == 'Europe/Kyiv' |
| 130 | + |
| 131 | + def test_size_and_region_come_from_the_file_version(self): |
| 132 | + self.app.get(f'/download/{self.guid}/', auth=self.user.auth) |
| 133 | + |
| 134 | + event = DownloadEvent.objects.get() |
| 135 | + assert event.size_bytes == 4096 |
| 136 | + assert event.storage_region == self.node.osfstorage_region.name |
| 137 | + |
| 138 | + def test_zip_completed_is_unset_for_single_files(self): |
| 139 | + """Only zips stream through WaterButler, so nothing reports completion here.""" |
| 140 | + self.app.get(f'/download/{self.guid}/', auth=self.user.auth) |
| 141 | + |
| 142 | + assert DownloadEvent.objects.get().zip_completed is None |
| 143 | + |
| 144 | + def test_anonymous_download_is_recorded_without_a_user(self): |
| 145 | + self.node.is_public = True |
| 146 | + self.node.save() |
| 147 | + |
| 148 | + self.app.get(f'/download/{self.guid}/') |
| 149 | + |
| 150 | + event = DownloadEvent.objects.get() |
| 151 | + assert event.user is None |
| 152 | + |
| 153 | + def test_mfr_render_is_not_recorded(self): |
| 154 | + self.app.get(f'/download/{self.guid}/?mode=render', auth=self.user.auth) |
| 155 | + |
| 156 | + assert not DownloadEvent.objects.exists() |
| 157 | + |
| 158 | + def test_download_still_succeeds_when_recording_fails(self): |
| 159 | + with pytest.MonkeyPatch.context() as patch: |
| 160 | + patch.setattr( |
| 161 | + 'addons.base.views.record_download', |
| 162 | + lambda **kwargs: (_ for _ in ()).throw(ValueError('boom')), |
| 163 | + ) |
| 164 | + res = self.app.get(f'/download/{self.guid}/', auth=self.user.auth) |
| 165 | + |
| 166 | + assert res.status_code == 302 |
| 167 | + |
| 168 | + |
| 169 | +class TestUserRegionDerivation: |
| 170 | + """The fallback chain, most to least trustworthy.""" |
| 171 | + |
| 172 | + class FakeUser: |
| 173 | + def __init__(self, timezone): |
| 174 | + self.timezone = timezone |
| 175 | + |
| 176 | + def test_live_browser_timezone_wins(self): |
| 177 | + user = self.FakeUser('America/New_York') |
| 178 | + assert derive_user_region('Europe/Kyiv', user, 'Germany') == 'Europe/Kyiv' |
| 179 | + |
| 180 | + def test_falls_back_to_profile_timezone(self): |
| 181 | + user = self.FakeUser('America/New_York') |
| 182 | + assert derive_user_region('', user, 'Germany') == 'America/New_York' |
| 183 | + |
| 184 | + def test_default_profile_timezone_is_not_a_signal(self): |
| 185 | + """Every user has Etc/UTC until they change it, so it says nothing.""" |
| 186 | + user = self.FakeUser('Etc/UTC') |
| 187 | + assert derive_user_region('', user, 'Germany') == 'Germany' |
| 188 | + |
| 189 | + def test_falls_back_to_storage_region(self): |
| 190 | + assert derive_user_region('', None, 'Germany') == 'Germany' |
| 191 | + |
| 192 | + def test_default_storage_region_is_not_a_signal(self): |
| 193 | + assert derive_user_region('', None, DEFAULT_REGION_NAME) == '' |
| 194 | + |
| 195 | + def test_unknown_is_empty(self): |
| 196 | + assert derive_user_region('', None, '') == '' |
| 197 | + |
| 198 | + |
| 199 | +@pytest.mark.django_db |
| 200 | +class TestRecordDownloadNeverRaises: |
| 201 | + |
| 202 | + def test_enqueue_failure_is_swallowed(self, monkeypatch): |
| 203 | + def explode(*args, **kwargs): |
| 204 | + raise ValueError('boom') |
| 205 | + |
| 206 | + monkeypatch.setattr('osf.utils.download_telemetry.enqueue_postcommit_task', explode) |
| 207 | + |
| 208 | + record_download(download_type=DownloadEvent.FILE, resource_guid='abcde') |
| 209 | + |
| 210 | + assert not DownloadEvent.objects.exists() |
0 commit comments