2024-01-29 14:06:15 -05:00
|
|
|
# Copyright 2024 (c) Anna Schumaker.
|
|
|
|
"""Tests our ListenBrainz client thread."""
|
|
|
|
import emmental.listenbrainz.thread
|
2024-01-30 16:34:00 -05:00
|
|
|
import io
|
|
|
|
import liblistenbrainz
|
2024-01-29 14:06:15 -05:00
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
2024-01-30 16:34:00 -05:00
|
|
|
@unittest.mock.patch("sys.stdout", new_callable=io.StringIO)
|
2024-01-29 14:06:15 -05:00
|
|
|
class TestThread(unittest.TestCase):
|
|
|
|
"""ListenBrainz Thread test case."""
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
"""Set up common variables."""
|
|
|
|
self.thread = emmental.listenbrainz.thread.Thread()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
"""Clean up."""
|
|
|
|
self.thread.stop()
|
|
|
|
|
2024-01-30 16:34:00 -05:00
|
|
|
def test_init(self, mock_stdout: io.StringIO):
|
2024-01-29 14:06:15 -05:00
|
|
|
"""Test that the ListenBrainz thread was initialized properly."""
|
|
|
|
self.assertIsInstance(self.thread, emmental.thread.Thread)
|
2024-01-30 16:34:00 -05:00
|
|
|
self.assertIsInstance(self.thread._client,
|
|
|
|
liblistenbrainz.client.ListenBrainz)
|
|
|
|
|
2024-01-30 16:34:00 -05:00
|
|
|
def test_clear_user_token(self, mock_stdout: io.StringIO):
|
|
|
|
"""Test clearing the user token."""
|
|
|
|
with unittest.mock.patch.object(self.thread._client,
|
|
|
|
"set_auth_token") as mock_set_auth:
|
|
|
|
self.thread.clear_user_token()
|
|
|
|
self.assertFalse(self.thread.ready.is_set())
|
|
|
|
self.assertEqual(self.thread._task, {"op": "clear-token"})
|
|
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
|
|
"listenbrainz: clearing user token\n")
|
|
|
|
|
|
|
|
self.thread.ready.wait()
|
|
|
|
mock_set_auth.assert_called_with(None, check_validity=False)
|
|
|
|
self.assertEqual(self.thread.get_result(),
|
|
|
|
{"op": "clear-token", "valid": True})
|
|
|
|
|
2024-01-30 16:34:00 -05:00
|
|
|
def test_set_user_token(self, mock_stdout: io.StringIO):
|
|
|
|
"""Test setting the user auth token."""
|
|
|
|
with unittest.mock.patch.object(self.thread._client,
|
|
|
|
"set_auth_token") as mock_set_auth:
|
|
|
|
self.thread.set_user_token("abcde")
|
|
|
|
self.assertFalse(self.thread.ready.is_set())
|
|
|
|
self.assertEqual(self.thread._task,
|
|
|
|
{"op": "set-token", "token": "abcde"})
|
|
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
|
|
"listenbrainz: setting user token\n")
|
|
|
|
|
|
|
|
self.thread.ready.wait()
|
|
|
|
mock_set_auth.assert_called_with("abcde")
|
|
|
|
self.assertEqual(self.thread.get_result(),
|
|
|
|
{"op": "set-token", "token": "abcde",
|
|
|
|
"valid": True})
|
|
|
|
|
|
|
|
def test_set_user_token_exceptions(self, mock_stdout: io.StringIO):
|
|
|
|
"""Test exception handling when setting the user auth token."""
|
|
|
|
with unittest.mock.patch.object(self.thread._client,
|
|
|
|
"set_auth_token") as mock_set_auth:
|
|
|
|
mock_set_auth.side_effect = \
|
|
|
|
liblistenbrainz.errors.InvalidAuthTokenException()
|
|
|
|
self.thread.set_user_token("abcde")
|
|
|
|
self.thread.ready.wait()
|
|
|
|
self.assertEqual(self.thread.get_result(),
|
|
|
|
{"op": "set-token", "token": "abcde",
|
|
|
|
"valid": False})
|
|
|
|
self.assertEqual(mock_stdout.getvalue(),
|
|
|
|
"listenbrainz: setting user token\n" +
|
|
|
|
"listenbrainz: user token is invalid\n")
|