import unittest import responses from beets.test.helper import PluginTestCase from beetsplug.plexupdate import get_music_section, update_plex class PlexUpdateTest(PluginTestCase): plugin = "plexupdate" def add_response_get_music_section(self, section_name="Music"): """Create response for mocking the get_music_section function.""" escaped_section_name = section_name.replace('"', '\\"') body = ( '' '' '' '' "" '' '' "" '' '' "" "" ) status = 200 content_type = "text/xml;charset=utf-8" responses.add( responses.GET, "http://localhost:32400/library/sections", body=body, status=status, content_type=content_type, ) def add_response_update_plex(self): """Create response for mocking the update_plex function.""" body = "" status = 200 content_type = "text/html" responses.add( responses.GET, "http://localhost:32400/library/sections/2/refresh", body=body, status=status, content_type=content_type, ) def setUp(self): super().setUp() self.config["plex"] = {"host": "localhost", "port": 32400} @responses.activate def test_get_music_section(self): # Adding response. self.add_response_get_music_section() # Test if section key is "2" out of the mocking data. self.assertEqual( get_music_section( self.config["plex"]["host"], self.config["plex"]["port"], self.config["plex"]["token"], self.config["plex"]["library_name"].get(), self.config["plex"]["secure"], self.config["plex"]["ignore_cert_errors"], ), "2", ) @responses.activate def test_get_named_music_section(self): # Adding response. self.add_response_get_music_section("My Music Library") self.assertEqual( get_music_section( self.config["plex"]["host"], self.config["plex"]["port"], self.config["plex"]["token"], "My Music Library", self.config["plex"]["secure"], self.config["plex"]["ignore_cert_errors"], ), "2", ) @responses.activate def test_update_plex(self): # Adding responses. self.add_response_get_music_section() self.add_response_update_plex() # Testing status code of the mocking request. self.assertEqual( update_plex( self.config["plex"]["host"], self.config["plex"]["port"], self.config["plex"]["token"], self.config["plex"]["library_name"].get(), self.config["plex"]["secure"], self.config["plex"]["ignore_cert_errors"], ).status_code, 200, ) def suite(): return unittest.TestLoader().loadTestsFromName(__name__) if __name__ == "__main__": unittest.main(defaultTest="suite")