librelist archives

« back to archive

400 Bad request on Unit test

400 Bad request on Unit test

From:
azwar akbar
Date:
2012-06-12 @ 02:45
Hello..

I have problem with with unit test.
The story is about testing login function. In Flask example tutorial
(flaskr) it combined with SQLite
on setup and tearDown function. I'm using pymongo for my application and
still don't have idea to set it up.
So for this moment I leave database command on setup and tearDown function,
because I don't need
database test yet.

Shortly, here is my code:

login.html
----------


{% extends "layout.html" %}
{% block body %}
  {% if error %}{{ error }}{% endif %}
        *<div* class="content"*>*
          *<div* class="row"*>*
              *<div* class="login-form"*>*
                  *<h2>*Login*</h2>*
                  *<form* action="{{ url_for('login') }}" method="post"*>*
                      *<fieldset>*
                          *<div* class="clearfix"*>*
                              *<input* type="text"
placeholder="Username" name="username"*/>*
                          *</div>*
                          *<div* class="clearfix"*>*
                              *<input* type="password"
placeholder="Password" name="password"*/>*
                          *</div>*
                          *<input* type="hidden" value="{% if data
%}{{ data['next_url'] }}{% endif %}" name="next_url"*/>*
                          *<button* class="btn primary"
type="submit"*>*Sign in*</button>*
                      *</fieldset>*
                  *</form>*
              *</div>*
          *</div>*
      *</div>*
{% endblock %}

login function in my flask application (app.py)

@app.route('/login', methods*=*['GET', 'POST'])*def* login():
    error *=* None

    *if* request.method *==* 'GET':
        data *=* {
            'page_title':'Login',
            'next_url':request.args.get('next')
            }

        *if* request.args.get('next') *is* None:
            data['next_url'] *=* '/dashboard'

        *return* render_template('login.html', data *=* data)
    *else*:
        username *=*  request.form['username']
        password *=*  request.form['password']

        userauth *=* db.userauth.find_one({'username':username,
'password':password})

        *if* userauth *is* *not* None:
            session['username'] *=* username
            session['role'] *=* userauth['role']
            *return* redirect(request.form['next_url'])
        *else*:
            error *=* "Username atau password salah"
            *return* render_template('login.html', error *=* error)


my test case application file app_test.py

import osfrom app import appimport unittestimport tempfile

*class* ShipMovementTestCase(unittest.TestCase):

    *def* setUp(self):
        *""" Setup database kosong"""*
        *# self.db_fd, app.app.config['DATABASE'] = tempfile.mkstemp()*
        app.config['TESTING'] *=* True
        self.app *=* app.test_client()
        *# app.init_db()*

    *def* tearDown(self):
        *""" Kosongkan database """*
        *# os.close(self.db_fd)*
        *# os.unlink(app.app.config['DATABASE'])*
        *pass*

    *def* login_view(self):
        *return* self.app.get('/login')

    *def* login(self, username, password):
        *return* self.app.post('/login', data*=*dict(
            username*=*username,
            password*=*password
        ), follow_redirects*=*True)

    *def* logout(self):
        *return* self.app.get('/logout', follow_redirects*=*True)

    *# fungsi untuk testing*
    *def* test_login_logout(self):
        rv *=* self.login('admin', 'rahasia123')
        *# assert 'admin' in rv.data*
        *print*(rv.data)*****if* __name__ *==* '__main__':
    unittest.main()



When I run this test file, I got this message:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>400 Bad Request</title>
<h1>Bad Request</h1>
<p>The browser (or proxy) sent a request that this server could not
understand.</p>

Can anyone help please.

Your advice and suggestion will be appreciate.
-- 


regards,
Azwar Akbar

Re: [flask] 400 Bad request on Unit test

From:
Simon Sapin
Date:
2012-06-12 @ 06:36
Le 12/06/2012 04:45, azwar akbar a écrit :
>
> When I run this test file, I got this message:
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
> <title>400 Bad Request</title>
> <h1>Bad Request</h1>
> <p>The browser (or proxy) sent a request that this server could not
> understand.</p>
>
> Can anyone help please.

Flask typically does this when you have something like 
request.form['foo'] in you code, but the browser did not send a 'foo' key.

Try setting TRAP_BAD_REQUEST_ERRORS to True in your app’s config and 
running in debug mode. You should get a traceback in the debugger.

(This is only for development. Leave the config to False and no debug 
mode in production!)

Regards,
-- 
Simon Sapin

Re: [flask] 400 Bad request on Unit test

From:
azwar akbar
Date:
2012-06-12 @ 15:55
On Tue, Jun 12, 2012 at 1:36 PM, Simon Sapin <simon.sapin@exyr.org> wrote:

> Le 12/06/2012 04:45, azwar akbar a écrit :
> >
> > When I run this test file, I got this message:
> >
> > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
> > <title>400 Bad Request</title>
> > <h1>Bad Request</h1>
> > <p>The browser (or proxy) sent a request that this server could not
> > understand.</p>
> >
> > Can anyone help please.
>
> Flask typically does this when you have something like
> request.form['foo'] in you code, but the browser did not send a 'foo' key.
>
> Try setting TRAP_BAD_REQUEST_ERRORS to True in your app’s config and
> running in debug mode. You should get a traceback in the debugger.
>

Hi  Simon, thank you very much for the advice.
I have been look at traceback debuger, and I found that my test login
parameters are only
pass 2 parameters (username and password), I forgot to pass next_url
parameter (3rd parameter) as I wrote in login template.
So there are 3 form request on app file, but I only pass 2 parameters in
testing file.

Thanks again for the advice, it's very helpful.



>
> (This is only for development. Leave the config to False and no debug
> mode in production!)
>
> Regards,
> --
> Simon Sapin
>
>
>


-- 


regards,
Azwar Akbar