test_frontend.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. from unittest import mock
  2. import pytest
  3. from mopidy import models
  4. from mopidy_webhooks import frontend as frontend_lib
  5. @pytest.fixture
  6. def frontend():
  7. config = {
  8. "webhooks": {
  9. "urls": "http://127.0.0.1/receiver/,http://127.0.0.1/receiver/two/",
  10. "tokens": "secrettoken,anotherone",
  11. }
  12. }
  13. core = mock.sentinel.core
  14. return frontend_lib.WebhooksFrontend(config, core)
  15. def test_on_start_creates_lastfm_network(pylast_mock, frontend):
  16. frontend.on_start()
  17. assert frontend.webhook_urls == [
  18. "http://127.0.0.1/receiver/",
  19. "http://127.0.0.1/receiver/two/",
  20. ]
  21. assert frontend.webhook_tokens == ["secrettoken", "anotherone"]
  22. def test_on_start_stops_actor_on_error(pylast_mock, frontend):
  23. pylast_mock.NetworkError = pylast.NetworkError
  24. pylast_mock.LastFMNetwork.side_effect = pylast.NetworkError(None, "foo")
  25. frontend.stop = mock.Mock()
  26. frontend.on_start()
  27. frontend.stop.assert_called_with()
  28. def test_track_playback_started_updates_now_playing(pylast_mock, frontend):
  29. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  30. artists = [models.Artist(name="ABC"), models.Artist(name="XYZ")]
  31. album = models.Album(name="The Collection")
  32. track = models.Track(
  33. name="One Two Three",
  34. artists=artists,
  35. album=album,
  36. track_no=3,
  37. length=180432,
  38. musicbrainz_id="123-456",
  39. )
  40. tl_track = models.TlTrack(track=track, tlid=17)
  41. frontend.track_playback_started(tl_track)
  42. frontend.lastfm.update_now_playing.assert_called_with(
  43. "ABC, XYZ",
  44. "One Two Three",
  45. duration="180",
  46. album="The Collection",
  47. track_number="3",
  48. mbid="123-456",
  49. )
  50. def test_track_playback_started_has_default_values(pylast_mock, frontend):
  51. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  52. track = models.Track()
  53. tl_track = models.TlTrack(track=track, tlid=17)
  54. frontend.track_playback_started(tl_track)
  55. frontend.lastfm.update_now_playing.assert_called_with(
  56. "", "", duration="0", album="", track_number="0", mbid=""
  57. )
  58. def test_track_playback_started_catches_pylast_error(pylast_mock, frontend):
  59. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  60. pylast_mock.NetworkError = pylast.NetworkError
  61. frontend.lastfm.update_now_playing.side_effect = pylast.NetworkError(
  62. None, "foo"
  63. )
  64. track = models.Track()
  65. tl_track = models.TlTrack(track=track, tlid=17)
  66. frontend.track_playback_started(tl_track)
  67. def test_track_playback_ended_scrobbles_played_track(pylast_mock, frontend):
  68. frontend.last_start_time = 123
  69. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  70. artists = [models.Artist(name="ABC"), models.Artist(name="XYZ")]
  71. album = models.Album(name="The Collection")
  72. track = models.Track(
  73. name="One Two Three",
  74. artists=artists,
  75. album=album,
  76. track_no=3,
  77. length=180432,
  78. musicbrainz_id="123-456",
  79. )
  80. tl_track = models.TlTrack(track=track, tlid=17)
  81. frontend.track_playback_ended(tl_track, 150000)
  82. frontend.lastfm.scrobble.assert_called_with(
  83. "ABC, XYZ",
  84. "One Two Three",
  85. "123",
  86. duration="180",
  87. album="The Collection",
  88. track_number="3",
  89. mbid="123-456",
  90. )
  91. def test_track_playback_ended_has_default_values(pylast_mock, frontend):
  92. frontend.last_start_time = 123
  93. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  94. track = models.Track(length=180432)
  95. tl_track = models.TlTrack(track=track, tlid=17)
  96. frontend.track_playback_ended(tl_track, 150000)
  97. frontend.lastfm.scrobble.assert_called_with(
  98. "", "", "123", duration="180", album="", track_number="0", mbid=""
  99. )
  100. def test_does_not_scrobble_tracks_shorter_than_30_sec(pylast_mock, frontend):
  101. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  102. track = models.Track(length=20432)
  103. tl_track = models.TlTrack(track=track, tlid=17)
  104. frontend.track_playback_ended(tl_track, 20432)
  105. assert frontend.lastfm.scrobble.call_count == 0
  106. def test_does_not_scrobble_if_played_less_than_half(pylast_mock, frontend):
  107. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  108. track = models.Track(length=180432)
  109. tl_track = models.TlTrack(track=track, tlid=17)
  110. frontend.track_playback_ended(tl_track, 60432)
  111. assert frontend.lastfm.scrobble.call_count == 0
  112. def test_does_scrobble_if_played_not_half_but_240_sec(pylast_mock, frontend):
  113. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  114. track = models.Track(length=880432)
  115. tl_track = models.TlTrack(track=track, tlid=17)
  116. frontend.track_playback_ended(tl_track, 241432)
  117. assert frontend.lastfm.scrobble.call_count == 1
  118. def test_track_playback_ended_catches_pylast_error(pylast_mock, frontend):
  119. frontend.lastfm = mock.Mock(spec=pylast.LastFMNetwork)
  120. pylast_mock.NetworkError = pylast.NetworkError
  121. frontend.lastfm.scrobble.side_effect = pylast.NetworkError(None, "foo")
  122. track = models.Track(length=180432)
  123. tl_track = models.TlTrack(track=track, tlid=17)
  124. frontend.track_playback_ended(tl_track, 150000)