from masonite.request import Request
from masonite.view import View
from masonite.auth.Csrf import Csrf
from masonite.app import App
from masonite.middleware import CsrfMiddleware
from masonite.testsuite.TestSuite import generate_wsgi
import pytest
from masonite.exceptions import InvalidCSRFToken


class TestCSRFMiddleware:

    def setup_method(self):
        self.app = App()
        self.request = Request(generate_wsgi())
        self.view = View(self.app)
        self.app.bind('Request', self.request)

        self.request = self.app.make('Request')

        self.middleware = CsrfMiddleware(self.request, Csrf(self.request), self.view)

    def test_middleware_shares_correct_input(self):
        self.middleware.before()
        assert 'csrf_field' in self.view.dictionary
        assert self.view.dictionary['csrf_field'].startswith("<input type='hidden' name='__token' value='")

    def test_middleware_throws_exception_on_post(self):
        self.request.environ['REQUEST_METHOD'] = 'POST'
        self.middleware.exempt = []
        with pytest.raises(InvalidCSRFToken):
            self.middleware.before()

    def test_incoming_token_does_not_throw_exception_with_token(self):
        self.request.environ['REQUEST_METHOD'] = 'POST'
        self.request.request_variables.update({'__token': self.request.get_cookie('csrf_token')})
        self.middleware.exempt = []
        self.middleware.before()

[((14, 19, 14, 24), 'masonite.app.App', 'App', ({}, {}), '()', False, 'from masonite.app import App\n'), ((16, 20, 16, 34), 'masonite.view.View', 'View', ({(16, 25, 16, 33): 'self.app'}, {}), '(self.app)', False, 'from masonite.view import View\n'), ((15, 31, 15, 46), 'masonite.testsuite.TestSuite.generate_wsgi', 'generate_wsgi', ({}, {}), '()', False, 'from masonite.testsuite.TestSuite import generate_wsgi\n'), ((21, 55, 21, 73), 'masonite.auth.Csrf.Csrf', 'Csrf', ({(21, 60, 21, 72): 'self.request'}, {}), '(self.request)', False, 'from masonite.auth.Csrf import Csrf\n'), ((31, 13, 31, 44), 'pytest.raises', 'pytest.raises', ({(31, 27, 31, 43): 'InvalidCSRFToken'}, {}), '(InvalidCSRFToken)', False, 'import pytest\n')]