lingthio / Flask-User

Customizable User Authorization & User Management: Register, Confirm, Login, Change username/password, Forgot password and more.
http://flask-user.readthedocs.io/
MIT License
1.06k stars 294 forks source link

How do you get around flash problems during tests? #191

Open vincentwhales opened 7 years ago

vincentwhales commented 7 years ago
class AuthTestConfig(TestingConfig):
  TESTING = True
  LOGIN_DISABLED = False

class AccessControlTest(unittest.TestCase):

  def setUp(self):
    self.app = create_app(AuthTestConfig)
    self.app_context = self.app.app_context()
    self.app_context.push()
    self.client = self.app.test_client(use_cookies=True)
    db.create_all()

    self.fclient = TstClient(self.client, db)

  def tearDown(self):
    db.session.remove()
    db.drop_all()
    self.app_context.pop()

  def test_anon_visits_member_redirects_to_login(self):
    um = self.app.user_manager
    self.fclient.get_valid_page(url_for(um.after_logout_endpoint))
    self.fclient.get_invalid_page(url_for(um.after_login_endpoint), "You must be signed in to access ")

This gives the error, AssertionError: POST http://testing/user/member did not contain 'You must be signed in to access ' error

I looked response data and saw that flash is not displaying anything.

Any idea how you can get flash to work during tests?

lingthio commented 6 years ago

Does your layout.html displays flash messages? For example:

        {%- with messages = get_flashed_messages(with_categories=true) -%}
            {% if messages %}
                {% for category, message in messages %}
                    {% if category=='error' %}
                        {% set category='danger' %}
                    {% endif %}
                    <div class="alert alert-{{category}}">{{ message|safe }}</div>
                {% endfor %}
            {% endif %}
        {%- endwith %}