Skip to content

Commit

Permalink
Refactored disable_csrf for using with contextmanager decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
n.lyubchich committed Aug 17, 2015
1 parent 3cdb7ee commit 8797ac4
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions project/tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from contextlib import contextmanager
from flask import url_for
from flask.ext.testing import TestCase
from project import create_app as app_factory
Expand All @@ -6,15 +7,11 @@
from project.models import User


class DisableCsrf:
def __init__(self, app):
self.app = app

def __enter__(self):
self.app.config['WTF_CSRF_ENABLED'] = False

def __exit__(self, exc_type, exc_val, exc_tb):
self.app.config['WTF_CSRF_ENABLED'] = True
@contextmanager
def disable_csrf(app):
app.config['WTF_CSRF_ENABLED'] = False
yield
app.config['WTF_CSRF_ENABLED'] = True


class ProjectTestCase(TestCase):
Expand All @@ -25,7 +22,7 @@ def log_in(self):
user = User.query.get(1)
# assume that login is equal to password
credentials = {"login": user.login, "password": user.login}
with DisableCsrf(self.app):
with disable_csrf(self.app):
self.client.post(url_for("auth.login"), data=credentials)

# noinspection PyAttributeOutsideInit
Expand Down

0 comments on commit 8797ac4

Please sign in to comment.