I am trying to write some unit tests for an example Flask application, as I am new to Flask. I am trying to test if a login function works correctly. My relevant unit test code looks like this:
def login(self, name, password):
return self.app.post('/', data={
'password': password, 'name': name},
follow_redirects=True)
def test_users_cannot_login_unless_registered(self):
response = self.login('foo', 'bar')
self.assertIn(b'Invalid username or password.', response.data)
When I manually test the app, I get an error message of 'Invalid username or password'. However, when I run the unit tests, I get the following Failure:
Traceback (most recent call last):
File "tests.py", line 55, in test_users_cannot_login_unless_registered
self.assertIn(b'Invalid username or password.', response.data)
AssertionError: 'Invalid username or password.' not found in '<!DOCTYPE html>\n<html>\n<head>\n\t<title>Welcome to FlaskTaskr!</title>\n\t<link rel="stylesheet" href="/static/css/main.css">\n\t</head>\n\t<body>\n\t\t<div class="page">\n\n\t\t\t\n\n\t\t\t\n\t\t\t<div class="error"><strong>ERROR:</strong> Both fields are required.</div>\n\t\t\t\n\n\t\t\t<br>\n\n\t\t\t\n\n<h1>Welcome to FlaskTaskr.</h1>\n<div class="lead">Please sign in to access your task list.</div>\n<form class="form-signin" role="form" method="post" action="/">\n\t<input id="csrf_token" name="csrf_token" type="hidden" value="1450903611##20d216a4c9ad935bc78678f060bfd898a7ead69f">\n\t<p>\n\t\t<input id="name" name="name" placeholder="name" type="text" value="foofoo">\n\t\t<span class="error">\n\t\t\t\n\t\t</span>\n\t</p>\n\t<p>\n\t\t<input id="password" name="password" placeholder="password" type="password" value="">\n\t\t<span class="error">\n\t\t\t\n\t\t</span>\n\t</p>\n\t<button class="btn btn-sm btn-success" type="submit">Sign in</button>\n\t<br>\n\t<br>\n\t<p><em>Need an account? </em><a href="/register">Sign up!</a></p>\n</form>\n\n\n\n\t\t</div>\n\t</body>\n\t</html>'
The presence of the text 'Both fields are required', seems to suggest to me that the password is not being passed to the application. Here is the html/jinja2 code that I am using to the generate the page:
{% extends "_base.html" %}
{% block content %}
<h1>Welcome to FlaskTaskr.</h1>
<div class="lead">Please sign in to access your task list.</div>
<form class="form-signin" role="form" method="post" action="/">
{{ form.csrf_token }}
<p>
{{ form.name(placeholder="name") }}
<span class="error">
{% if form.name.errors %}
{% for error in form.name.errors %}
{{ error }}
{% endfor %}
{% endif %}
</span>
</p>
<p>
{{ form.password(placeholder="password") }}
<span class="error">
{% if form.password.errors %}
{% for error in form.password.errors %}
{{ error }}
{% endfor %}
{% endif %}
</span>
</p>
<button class="btn btn-sm btn-success" type="submit">Sign in</button>
<br>
<br>
<p><em>Need an account? </em><a href="/register">Sign up!</a></p>
</form>
{% endblock %}
Any ideas on what the problem might be or where to look for the issue?
Thanks!
Aucun commentaire:
Enregistrer un commentaire