import unittest

from mopidy import settings
from mopidy.frontends.mpd import server

class MpdServerTest(unittest.TestCase):
    def setUp(self):
        self.server = server.MpdServer(None)

    def test_format_hostname_prefixes_ipv4_addresses_when_ipv6_available(self):
        server.socket.has_ipv6 = True
        self.assertEqual(self.server._format_hostname('0.0.0.0'),
            '::ffff:0.0.0.0')
        self.assertEqual(self.server._format_hostname('127.0.0.1'),
            '::ffff:127.0.0.1')

    def test_format_hostname_does_nothing_when_only_ipv4_available(self):
        server.socket.has_ipv6 = False
        self.assertEquals(self.server._format_hostname('0.0.0.0'), '0.0.0.0')

class MpdSessionTest(unittest.TestCase):
    def setUp(self):
        self.session = server.MpdSession(None, None, (None, None), None)

    def tearDown(self):
        settings.runtime.clear()

    def test_found_terminator_catches_decode_error(self):
        # Pressing Ctrl+C in a telnet session sends a 0xff byte to the server.
        self.session.input_buffer = ['\xff']
        self.session.found_terminator()
        self.assertEqual(len(self.session.input_buffer), 0)

    def test_authentication_with_valid_password_is_accepted(self):
        settings.MPD_SERVER_PASSWORD = u'topsecret'
        authed, response = self.session.check_password(u'password "topsecret"')
        self.assertTrue(authed)
        self.assertEqual(u'OK', response)

    def test_authentication_with_invalid_password_is_not_accepted(self):
        settings.MPD_SERVER_PASSWORD = u'topsecret'
        authed, response = self.session.check_password(u'password "secret"')
        self.assertFalse(authed)
        self.assertEqual(u'ACK [3@0] {password} incorrect password', response)

    def test_authentication_with_anything_when_password_check_turned_off(self):
        settings.MPD_SERVER_PASSWORD = None
        authed, response = self.session.check_password(u'any request at all')
        self.assertTrue(authed)
        self.assertEqual(None, response)

    def test_anything_when_not_authenticated_should_fail(self):
        settings.MPD_SERVER_PASSWORD = u'topsecret'
        authed, response = self.session.check_password(u'any request at all')
        self.assertFalse(authed)
        self.assertEqual(
            u'ACK [4@0] {any} you don\'t have permission for "any"', response)

    def test_close_is_allowed_without_authentication(self):
        settings.MPD_SERVER_PASSWORD = u'topsecret'
        authed, response = self.session.check_password(u'close')
        self.assertFalse(authed)
        self.assertEqual(None, response)

    def test_commands_is_allowed_without_authentication(self):
        settings.MPD_SERVER_PASSWORD = u'topsecret'
        authed, response = self.session.check_password(u'commands')
        self.assertFalse(authed)
        self.assertEqual(None, response)

    def test_notcommands_is_allowed_without_authentication(self):
        settings.MPD_SERVER_PASSWORD = u'topsecret'
        authed, response = self.session.check_password(u'notcommands')
        self.assertFalse(authed)
        self.assertEqual(None, response)

    def test_ping_is_allowed_without_authentication(self):
        settings.MPD_SERVER_PASSWORD = u'topsecret'
        authed, response = self.session.check_password(u'ping')
        self.assertFalse(authed)
        self.assertEqual(None, response)

@@ -54,141 +54,418 @@
 idy.
-frontends.mpd import server%0A%0Aclass MpdServerTest(unittest.TestCase):%0A    def setUp(self):%0A        self.server = server.MpdServer(None
+backends.dummy import DummyBackend%0Afrom mopidy.frontends.mpd import server%0Afrom mopidy.mixers.dummy import DummyMixer%0A%0Aclass MpdServerTest(unittest.TestCase):%0A    def setUp(self):%0A        self.backend = DummyBackend.start().proxy()%0A        self.mixer = DummyMixer.start().proxy()%0A        self.server = server.MpdServer()%0A%0A    def tearDown(self):%0A        self.backend.stop().get()%0A        self.mixer.stop().get(
 )%0A%0A 
@@ -1024,32 +1024,132 @@
 ef setUp(self):%0A
+        self.backend = DummyBackend.start().proxy()%0A        self.mixer = DummyMixer.start().proxy()%0A
         self.ses
@@ -1197,22 +1197,16 @@
 e, None)
-, None
 )%0A%0A    d
@@ -1216,32 +1216,98 @@
 tearDown(self):%0A
+        self.backend.stop().get()%0A        self.mixer.stop().get()%0A
         settings

Update MPD/server