piwigo: Return proper error for wrong user/password.
authorElrond <elrond+mediagoblin.org@samba-tng.org>
Mon, 20 May 2013 15:50:04 +0000 (17:50 +0200)
committerElrond <elrond+mediagoblin.org@samba-tng.org>
Mon, 20 May 2013 17:36:28 +0000 (19:36 +0200)
And fix tests.

mediagoblin/plugins/piwigo/tools.py
mediagoblin/plugins/piwigo/views.py
mediagoblin/tests/test_piwigo.py

index 30bb4b977447ed471ec50a6be4900d42744b374a..484ea5317348cfd5a66ccef3e152bf8f7243d479 100644 (file)
@@ -84,7 +84,8 @@ def response_xml(result):
         err = ET.SubElement(r, "err")
         err.set("code", str(result.code))
         err.set("msg", result.msg)
-        status = result.code
+        if result.code >= 100 and result.code < 600:
+            status = result.code
     else:
         _fill_element(r, result)
     return Response(ET.tostring(r, encoding="utf-8", xml_declaration=True),
index 5633f1361a0909dc5087901b765c745684ef62d0..1c655bf5053bcf633a6920deb86b7f1affb3d665 100644 (file)
@@ -28,8 +28,8 @@ from mediagoblin.media_types import sniff_media
 from mediagoblin.submit.lib import check_file_field, prepare_queue_task, \
     run_process_media
 
-from .tools import CmdTable, PwgNamedArray, response_xml, check_form, \
-    PWGSession
+from .tools import CmdTable, response_xml, check_form, \
+    PWGSession, PwgNamedArray, PwgError
 from .forms import AddSimpleForm, AddForm
 
 
@@ -40,15 +40,14 @@ _log = logging.getLogger(__name__)
 def pwg_login(request):
     username = request.form.get("username")
     password = request.form.get("password")
-    _log.debug("Login for %r/%r...", username, password)
     user = request.db.User.query.filter_by(username=username).first()
     if not user:
         _log.info("User %r not found", username)
         fake_login_attempt()
-        return False
+        return PwgError(999, 'Invalid username/password')
     if not user.check_login(password):
         _log.warn("Wrong password for %r", username)
-        return False
+        return PwgError(999, 'Invalid username/password')
     _log.info("Logging %r in", username)
     request.session["user_id"] = user.id
     request.session.save()
index 71d36ddbe57ad96348506ea0b22ae71898923aa6..16ad01114c89e9d0abe0cd16546c1032b4689ce9 100644 (file)
@@ -44,11 +44,13 @@ class Test_PWG(object):
     def test_session(self):
         resp = self.do_post("pwg.session.login",
             {"username": u"nouser", "password": "wrong"})
-        assert resp.body ==  XML_PREFIX + '<rsp stat="ok">0</rsp>'
+        assert resp.body == XML_PREFIX \
+            + '<rsp stat="fail"><err code="999" msg="Invalid username/password"/></rsp>'
 
         resp = self.do_post("pwg.session.login",
             {"username": self.username, "password": "wrong"})
-        assert resp.body ==  XML_PREFIX + '<rsp stat="ok">0</rsp>'
+        assert resp.body == XML_PREFIX \
+            + '<rsp stat="fail"><err code="999" msg="Invalid username/password"/></rsp>'
 
         resp = self.do_get("pwg.session.getStatus")
         assert resp.body == XML_PREFIX \