Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 79 additions & 17 deletions tests/components/plex/mock_classes.py
Original file line number Diff line number Diff line change
@@ -1,35 +1,97 @@
"""Mock classes used in tests."""
from homeassistant.const import CONF_HOST, CONF_PORT
from homeassistant.components.plex.const import CONF_SERVER, CONF_SERVER_IDENTIFIER

MOCK_HOST_1 = "1.2.3.4"
MOCK_PORT_1 = 32400
MOCK_HOST_2 = "4.3.2.1"
MOCK_PORT_2 = 32400
MOCK_SERVERS = [
{
CONF_HOST: "1.2.3.4",
CONF_PORT: 32400,
CONF_SERVER: "Plex Server 1",
CONF_SERVER_IDENTIFIER: "unique_id_123",
},
{
CONF_HOST: "4.3.2.1",
CONF_PORT: 32400,
CONF_SERVER: "Plex Server 2",
CONF_SERVER_IDENTIFIER: "unique_id_456",
},
]


class MockAvailableServer: # pylint: disable=too-few-public-methods
"""Mock avilable server objects."""
class MockResource:
"""Mock a PlexAccount resource."""

def __init__(self, name, client_id):
def __init__(self, index):
"""Initialize the object."""
self.name = name
self.clientIdentifier = client_id # pylint: disable=invalid-name
self.name = MOCK_SERVERS[index][CONF_SERVER]
self.clientIdentifier = MOCK_SERVERS[index][ # pylint: disable=invalid-name
CONF_SERVER_IDENTIFIER
]
self.provides = ["server"]
self._mock_plex_server = MockPlexServer(index)
self._connections = []
for connection in range(2):
self._connections.append(MockConnection(connection))

@property
def connections(self):
"""Mock the resource connection listing method."""
return self._connections

def connect(self):
"""Mock the resource connect method."""
return self._mock_plex_server


class MockConnection: # pylint: disable=too-few-public-methods
"""Mock a single account resource connection object."""

def __init__(self, ssl):
def __init__(self, index, ssl=True):
"""Initialize the object."""
prefix = "https" if ssl else "http"
self.httpuri = f"{prefix}://{MOCK_HOST_1}:{MOCK_PORT_1}"
self.uri = "{prefix}://{MOCK_HOST_2}:{MOCK_PORT_2}"
self.local = True
self.httpuri = (
f"http://{MOCK_SERVERS[index][CONF_HOST]}:{MOCK_SERVERS[index][CONF_PORT]}"
)
self.uri = f"{prefix}://{MOCK_SERVERS[index][CONF_HOST]}:{MOCK_SERVERS[index][CONF_PORT]}"
# Only first server is local
self.local = not bool(index)


class MockPlexAccount:
"""Mock a PlexAccount instance."""

def __init__(self, servers=1):
"""Initialize the object."""
self._resources = []
for index in range(servers):
self._resources.append(MockResource(index))

def resource(self, name):
"""Mock the PlexAccount resource lookup method."""
return [x for x in self._resources if x.name == name][0]

def resources(self):
"""Mock the PlexAccount resources listing method."""
return self._resources


class MockConnections: # pylint: disable=too-few-public-methods
"""Mock a list of resource connections."""
class MockPlexServer:
"""Mock a PlexServer instance."""

def __init__(self, ssl=False):
def __init__(self, index=0, ssl=True):
"""Initialize the object."""
self.connections = [MockConnection(ssl)]
host = MOCK_SERVERS[index][CONF_HOST]
port = MOCK_SERVERS[index][CONF_PORT]
self.friendlyName = MOCK_SERVERS[index][ # pylint: disable=invalid-name
CONF_SERVER
]
self.machineIdentifier = MOCK_SERVERS[index][ # pylint: disable=invalid-name
CONF_SERVER_IDENTIFIER
]
prefix = "https" if ssl else "http"
self._baseurl = f"{prefix}://{host}:{port}"

@property
def url_in_use(self):
"""Return URL used by PlexServer."""
return self._baseurl
Loading