0

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="user@website.com",
        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 ?

Cyril N.
  • 38,875
  • 36
  • 142
  • 243

1 Answers1

1

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.

Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
  • Ok this is exactly the reason, but ... I don't understand why. I return this : `return jsonify({...}), 400` and the tuple is to add the status code. I thought that the response was a truly http response, so with a status code of 400, not a tuple. How can I fix that ? – Cyril N. Jan 13 '16 at 13:30
  • 1
    You can't use a tuple to specify (body, status_code) pair. Use [make_response](http://flask.pocoo.org/docs/0.10/api/#flask.make_response) instead. – Ivan Velichko Jan 13 '16 at 13:35
  • That's odd that I can't because it was working (but you are right, it works better with `make_response`, even though it works the same (from an http point of view) of returning a tuple) – Cyril N. Jan 13 '16 at 13:45