<s> from __future__ import with_statement <EOL> import re <EOL> from . base import TestCase , MyForm , to_unicode <EOL> csrf_token_input = re . compile ( <EOL> r'<STR_LIT>' <EOL> ) <EOL> def get_csrf_token ( data ) : <EOL> match = csrf_token_input . search ( to_unicode ( data ) ) <EOL> assert match <EOL> return match . groups ( ) [ <NUM_LIT:0> ] <EOL> class TestValidateOnSubmit ( TestCase ) : <EOL> def test_not_submitted ( self ) : <EOL> response = self . client . get ( "<STR_LIT:/>" ) <EOL> assert b'<STR_LIT>' not in response . data <EOL> def test_submitted_not_valid ( self ) : <EOL> self . app . config [ '<STR_LIT>' ] = False <EOL> response = self . client . post ( "<STR_LIT:/>" , data = { } ) <EOL> assert b'<STR_LIT>' not in response . data <EOL> def test_submitted_and_valid ( self ) : <EOL> self . app . config [ '<STR_LIT>' ] = False <EOL> response = self . client . post ( "<STR_LIT:/>" , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> assert b'<STR_LIT>' in response . data <EOL> class TestValidateWithoutSubmit ( TestCase ) : <EOL> def test_unsubmitted_valid ( self ) : <EOL> class obj : <EOL> name = "<STR_LIT:foo>" <EOL> with self . app . test_request_context ( ) : <EOL> assert MyForm ( obj = obj , csrf_enabled = False ) . validate ( ) <EOL> fake_session = { } <EOL> t = MyForm ( csrf_context = fake_session ) . generate_csrf_token ( <EOL> fake_session <EOL> ) <EOL> assert MyForm ( <EOL> obj = obj , csrf_token = t , <EOL> csrf_context = fake_session ) . validate ( ) <EOL> class TestHiddenTag ( TestCase ) : <EOL> def test_hidden_tag ( self ) : <EOL> response = self . client . get ( "<STR_LIT>" ) <EOL> assert to_unicode ( response . data ) . count ( '<STR_LIT>' ) == <NUM_LIT:5> <EOL> assert b'<STR_LIT>' in response . data <EOL> class TestCSRF ( TestCase ) : <EOL> def test_csrf_token ( self ) : <EOL> response = self . client . get ( "<STR_LIT:/>" ) <EOL> snippet = ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> assert snippet in to_unicode ( response . data ) <EOL> def test_invalid_csrf ( self ) : <EOL> response = self . client . post ( "<STR_LIT:/>" , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> assert b'<STR_LIT>' not in response . data <EOL> assert b'<STR_LIT>' in response . data <EOL> def test_csrf_disabled ( self ) : <EOL> self . app . config [ '<STR_LIT>' ] = False <EOL> response = self . client . post ( "<STR_LIT:/>" , data = { "<STR_LIT:name>" : "<STR_LIT>" } ) <EOL> assert b'<STR_LIT>' in response . data <EOL> def test_validate_twice ( self ) : <EOL> response = self . client . post ( "<STR_LIT>" , data = { } ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> def test_ajax ( self ) : <EOL> response = self . client . post ( <EOL> "<STR_LIT>" , data = { "<STR_LIT:name>" : "<STR_LIT>" } , <EOL> headers = { '<STR_LIT>' : '<STR_LIT>' } <EOL> ) <EOL> assert response . status_code == <NUM_LIT:200> <EOL> def test_valid_csrf ( self ) : <EOL> response = self . client . get ( "<STR_LIT:/>" ) <EOL> csrf_token = get_csrf_token ( response . data ) <EOL> response = self . client . post ( "<STR_LIT:/>" , data = { "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : csrf_token } ) <EOL> assert b'<STR_LIT>' in response . data <EOL> def test_double_csrf ( self ) : <EOL> response = self . client . get ( "<STR_LIT:/>" ) <EOL> csrf_token = get_csrf_token ( response . data ) <EOL> response = self . client . post ( "<STR_LIT>" , data = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : csrf_token <EOL> } ) <EOL> assert response . data == b'<STR_LIT:OK>' <EOL> def test_valid_csrf_data ( self ) : <EOL> with self . app . test_request_context ( ) : <EOL> form = MyForm ( ) <EOL> csrf_token = get_csrf_token ( form . csrf_token ( ) ) <EOL> assert form . validate_csrf_data ( csrf_token ) </s>
