Implement 'password' command which will be reached post-auth

This commit is contained in:
Stein Magnus Jodal 2011-01-21 00:17:01 +01:00
parent 37b1ec018f
commit 8f0e00e1d7
3 changed files with 29 additions and 4 deletions

View File

@ -39,6 +39,11 @@ class MpdArgError(MpdAckError):
super(MpdArgError, self).__init__(*args, **kwargs) super(MpdArgError, self).__init__(*args, **kwargs)
self.error_code = 2 # ACK_ERROR_ARG self.error_code = 2 # ACK_ERROR_ARG
class MpdPasswordError(MpdAckError):
def __init__(self, *args, **kwargs):
super(MpdPasswordError, self).__init__(*args, **kwargs)
self.error_code = 3 # ACK_ERROR_PASSWORD
class MpdUnknownCommand(MpdAckError): class MpdUnknownCommand(MpdAckError):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super(MpdUnknownCommand, self).__init__(*args, **kwargs) super(MpdUnknownCommand, self).__init__(*args, **kwargs)

View File

@ -1,5 +1,6 @@
from mopidy import settings
from mopidy.frontends.mpd.protocol import handle_pattern from mopidy.frontends.mpd.protocol import handle_pattern
from mopidy.frontends.mpd.exceptions import MpdNotImplemented from mopidy.frontends.mpd.exceptions import MpdPasswordError
@handle_pattern(r'^close$') @handle_pattern(r'^close$')
def close(frontend): def close(frontend):
@ -33,7 +34,11 @@ def password_(frontend, password):
This is used for authentication with the server. ``PASSWORD`` is This is used for authentication with the server. ``PASSWORD`` is
simply the plaintext password. simply the plaintext password.
""" """
raise MpdNotImplemented # TODO # You will not get to this code without being authenticated. This is for
# when you are already authenticated, and are sending additional 'password'
# requests.
if settings.MPD_SERVER_PASSWORD != password:
raise MpdPasswordError(u'incorrect password', command=u'password')
@handle_pattern(r'^ping$') @handle_pattern(r'^ping$')
def ping(frontend): def ping(frontend):

View File

@ -1,5 +1,6 @@
import unittest import unittest
from mopidy import settings
from mopidy.backends.dummy import DummyBackend from mopidy.backends.dummy import DummyBackend
from mopidy.frontends.mpd import dispatcher from mopidy.frontends.mpd import dispatcher
from mopidy.mixers.dummy import DummyMixer from mopidy.mixers.dummy import DummyMixer
@ -9,6 +10,9 @@ class ConnectionHandlerTest(unittest.TestCase):
self.b = DummyBackend(mixer_class=DummyMixer) self.b = DummyBackend(mixer_class=DummyMixer)
self.h = dispatcher.MpdDispatcher(backend=self.b) self.h = dispatcher.MpdDispatcher(backend=self.b)
def tearDown(self):
settings.runtime.clear()
def test_close(self): def test_close(self):
result = self.h.handle_request(u'close') result = self.h.handle_request(u'close')
self.assert_(u'OK' in result) self.assert_(u'OK' in result)
@ -21,9 +25,20 @@ class ConnectionHandlerTest(unittest.TestCase):
result = self.h.handle_request(u'kill') result = self.h.handle_request(u'kill')
self.assert_(u'OK' in result) self.assert_(u'OK' in result)
def test_password(self): def test_valid_password_is_accepted(self):
settings.MPD_SERVER_PASSWORD = u'topsecret'
result = self.h.handle_request(u'password "topsecret"')
self.assert_(u'OK' in result)
def test_invalid_password_is_not_accepted(self):
settings.MPD_SERVER_PASSWORD = u'topsecret'
result = self.h.handle_request(u'password "secret"') result = self.h.handle_request(u'password "secret"')
self.assert_(u'ACK [0@0] {} Not implemented' in result) self.assert_(u'ACK [3@0] {password} incorrect password' in result)
def test_any_password_is_not_accepted_when_password_check_turned_off(self):
settings.MPD_SERVER_PASSWORD = False
result = self.h.handle_request(u'password "secret"')
self.assert_(u'ACK [3@0] {password} incorrect password' in result)
def test_ping(self): def test_ping(self):
result = self.h.handle_request(u'ping') result = self.h.handle_request(u'ping')