Skip to content

Commit bb2ceaa

Browse files
authored
Ensure user prop set for register snapshots method (issue #964) (#966)
* Ensure user prop set for register snapshots method (issue #964) * Move ONE validation to register_snapshots method
1 parent 80ff129 commit bb2ceaa

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

ibllib/pipes/base_tasks.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,13 @@ def register_snapshots(self, unlink=False, collection=None):
523523
- TIFF files are converted to PNG format before upload. The original file is not replaced.
524524
- JPEG and PNG files are resized by Alyx.
525525
"""
526+
assert self.one and not self.one.offline, f'{self.__class__.__name__} requires an online ONE instance'
527+
if not self.one.alyx.is_logged_in:
528+
# Register snapshot requires the user field to be set, which may happen before a REST
529+
# query is made. To avoid the user field being None, we authenticate here. If the
530+
# token is cached this will simply set the user and token properties.
531+
self.one.alyx.authenticate()
532+
526533
collection = getattr(self, 'device_collection', None) if collection is None else collection
527534
collection = collection or '' # If not defined, use no collection
528535
if collection and '*' in collection:

ibllib/tests/test_pipes.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,29 @@ def test_rename_files(self):
314314
expected = ('snap.PNG', 'pic.jpeg', 'snapshot.png', 'snapshot.jpg', 'snapshot.gif')
315315
self.assertCountEqual(expected, files)
316316

317+
def test_online_validation(self):
318+
"""Test ONE validation and AlyxClient authentication."""
319+
# Test that the constructor raises an error if ONE is offline
320+
task = RegisterRawDataTask(self.session_path)
321+
self.assertRaises(AssertionError, task.register_snapshots)
322+
alyx = self.one.alyx
323+
try:
324+
self.one._web_client = None
325+
task.one = self.one
326+
with self.assertRaises(AssertionError) as e:
327+
task.register_snapshots()
328+
finally:
329+
self.one._web_client = alyx
330+
self.assertEqual(str(e.exception), 'RegisterRawDataTask requires an online ONE instance')
331+
332+
# Test that the constructor authenticates the AlyxClient
333+
assert alyx.silent is True, 'AlyxClient must be silent for this test' # ensures no user prompt
334+
alyx.user = None # should restore Alyx user
335+
assert self.one.alyx.is_logged_in is False
336+
task.register_snapshots()
337+
self.assertTrue(alyx.is_logged_in)
338+
self.assertTrue(alyx.user)
339+
317340

318341
class TestSleeplessDecorator(unittest.TestCase):
319342

0 commit comments

Comments
 (0)