Search code examples
flaskflask-testing

Flask test client refuse arobase in data?


I'm trying to set up tests in my Flask but for an unknown reason, Flask fails when I add a parameter with arobase.

I followed the snippet here http://flask.pocoo.org/snippets/26/ and can make a successful request (it works), but when I try to do a post, it fails (with the following exception) :

def test_auth_login_fail(self):
    """ Login fail """
    result = self.client.post(self._make_url("/auth/login"), data=dict(
        email="[email protected]",
        password='cxcxz'
    ))
    self.assertEqual(result.status_code, 404)

And the exception :

Traceback (most recent call last):
  File "/var/www/html/api/apps/auth/tests/login.py", line 20, in test_auth_login_fail
    password='cxcxz'
  File "/usr/lib/python2.7/site-packages/werkzeug/test.py", line 772, in post
    return self.open(*args, **kw)
  File "/usr/lib/python2.7/site-packages/flask/testing.py", line 108, in open
    follow_redirects=follow_redirects)
  File "/usr/lib/python2.7/site-packages/werkzeug/test.py", line 736, in open
    response = self.run_wsgi_app(environ, buffered=buffered)
  File "/usr/lib/python2.7/site-packages/werkzeug/test.py", line 659, in run_wsgi_app
    rv = run_wsgi_app(self.application, environ, buffered=buffered)
  File "/usr/lib/python2.7/site-packages/werkzeug/test.py", line 855, in run_wsgi_app
    app_iter = app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/var/www/html/api/utils/middlewares.py", line 55, in __call__
    return self.app(environ, start_response)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/usr/lib/python2.7/site-packages/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/usr/lib/python2.7/site-packages/werkzeug/wrappers.py", line 827, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/usr/lib/python2.7/site-packages/werkzeug/wrappers.py", line 57, in _run_wsgi_app
    return _run_wsgi_app(*args)
  File "/usr/lib/python2.7/site-packages/werkzeug/test.py", line 855, in run_wsgi_app
    app_iter = app(environ, start_response)
TypeError: 'tuple' object is not callable

For informations, this doesn't generate an exception :

def test_auth_login_fail(self):
        """ Login fail """
        result = self.client.post(self._make_url("/auth/login"), data=dict(
            email="userwebsite.com",
            password='cxcxz'
        ))
        self.assertEqual(result.status_code, 404)

(notice the "@" removed)

I also removed the Middleware (indicated in the traceback) without much success.

What am I missing ?


Solution

  • Looks like in case of an email is correct, you return something wrong (a tuple) from your view function. And when you pass a malformed email, the value you return is ok.