<s> from __future__ import unicode_literals <EOL> from contextlib import contextmanager <EOL> from functools import partial <EOL> from unittest import TestCase <EOL> from wtforms . fields import TextField <EOL> from wtforms . form import Form <EOL> from wtforms . csrf . core import CSRF <EOL> from wtforms . csrf . session import SessionCSRF <EOL> from tests . common import DummyPostData <EOL> import datetime <EOL> import hashlib <EOL> import hmac <EOL> class DummyCSRF ( CSRF ) : <EOL> def generate_csrf_token ( self , csrf_token_field ) : <EOL> return '<STR_LIT>' <EOL> class FakeSessionRequest ( object ) : <EOL> def __init__ ( self , session ) : <EOL> self . session = session <EOL> class TimePin ( SessionCSRF ) : <EOL> """<STR_LIT>""" <EOL> pinned_time = None <EOL> @ classmethod <EOL> @ contextmanager <EOL> def pin_time ( cls , value ) : <EOL> original = cls . pinned_time <EOL> cls . pinned_time = value <EOL> yield <EOL> cls . pinned_time = original <EOL> def now ( self ) : <EOL> return self . pinned_time <EOL> class SimplePopulateObject ( object ) : <EOL> a = None <EOL> csrf_token = None <EOL> class DummyCSRFTest ( TestCase ) : <EOL> class F ( Form ) : <EOL> class Meta : <EOL> csrf = True <EOL> csrf_class = DummyCSRF <EOL> a = TextField ( ) <EOL> def test_base_class ( self ) : <EOL> self . assertRaises ( NotImplementedError , self . F , meta = { '<STR_LIT>' : CSRF } ) <EOL> def test_basic_impl ( self ) : <EOL> form = self . F ( ) <EOL> assert '<STR_LIT>' in form <EOL> assert not form . validate ( ) <EOL> self . assertEqual ( form . csrf_token . _value ( ) , '<STR_LIT>' ) <EOL> form = self . F ( DummyPostData ( csrf_token = '<STR_LIT>' ) ) <EOL> assert form . validate ( ) <EOL> def test_csrf_off ( self ) : <EOL> form = self . F ( meta = { '<STR_LIT>' : False } ) <EOL> assert '<STR_LIT>' not in form <EOL> def test_rename ( self ) : <EOL> form = self . F ( meta = { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> assert '<STR_LIT>' in form <EOL> assert '<STR_LIT>' not in form <EOL> def test_no_populate ( self ) : <EOL> obj = SimplePopulateObject ( ) <EOL> form = self . F ( a = '<STR_LIT:test>' , csrf_token = '<STR_LIT>' ) <EOL> form . populate_obj ( obj ) <EOL> assert obj . csrf_token is None <EOL> self . assertEqual ( obj . a , '<STR_LIT:test>' ) <EOL> class SessionCSRFTest ( TestCase ) : <EOL> class F ( Form ) : <EOL> class Meta : <EOL> csrf = True <EOL> csrf_secret = b'<STR_LIT>' <EOL> a = TextField ( ) <EOL> class NoTimeLimit ( F ) : <EOL> class Meta : <EOL> csrf_time_limit = None <EOL> class Pinned ( F ) : <EOL> class Meta : <EOL> csrf_class = TimePin <EOL> def test_various_failures ( self ) : <EOL> self . assertRaises ( TypeError , self . F ) <EOL> self . assertRaises ( Exception , self . F , meta = { '<STR_LIT>' : None } ) <EOL> def test_no_time_limit ( self ) : <EOL> session = { } <EOL> form = self . _test_phase1 ( self . NoTimeLimit , session ) <EOL> expected_csrf = hmac . new ( b'<STR_LIT>' , session [ '<STR_LIT>' ] . encode ( '<STR_LIT:ascii>' ) , digestmod = hashlib . sha1 ) . hexdigest ( ) <EOL> self . assertEqual ( form . csrf_token . current_token , '<STR_LIT>' + expected_csrf ) <EOL> self . _test_phase2 ( self . NoTimeLimit , session , form . csrf_token . current_token ) <EOL> def test_with_time_limit ( self ) : <EOL> session = { } <EOL> form = self . _test_phase1 ( self . F , session ) <EOL> self . _test_phase2 ( self . F , session , form . csrf_token . current_token ) <EOL> def test_detailed_expected_values ( self ) : <EOL> """<STR_LIT>""" <EOL> session = { '<STR_LIT>' : '<STR_LIT>' } <EOL> dt = partial ( datetime . datetime , <NUM_LIT> , <NUM_LIT:1> , <NUM_LIT:15> ) <EOL> with TimePin . pin_time ( dt ( <NUM_LIT:8> , <NUM_LIT:11> , <NUM_LIT:12> ) ) : <EOL> form = self . _test_phase1 ( self . Pinned , session ) <EOL> token = form . csrf_token . current_token <EOL> self . assertEqual ( token , '<STR_LIT>' ) <EOL> with TimePin . pin_time ( dt ( <NUM_LIT:8> , <NUM_LIT> ) ) : <EOL> form = self . _test_phase2 ( self . Pinned , session , token ) <EOL> new_token = form . csrf_token . current_token <EOL> self . assertNotEqual ( new_token , token ) <EOL> self . assertEqual ( new_token , '<STR_LIT>' ) <EOL> with TimePin . pin_time ( dt ( <NUM_LIT:8> , <NUM_LIT> ) ) : <EOL> form = self . _test_phase2 ( self . Pinned , session , token , False ) <EOL> assert not form . validate ( ) <EOL> self . assertEqual ( form . csrf_token . errors , [ '<STR_LIT>' ] ) <EOL> self . _test_phase2 ( self . Pinned , session , new_token ) <EOL> with TimePin . pin_time ( dt ( <NUM_LIT:8> , <NUM_LIT> ) ) : <EOL> bad_token = '<STR_LIT>' <EOL> form = self . _test_phase2 ( self . Pinned , session , bad_token , False ) <EOL> assert not form . validate ( ) <EOL> def _test_phase1 ( self , form_class , session ) : <EOL> form = form_class ( meta = { '<STR_LIT>' : session } ) <EOL> assert not form . validate ( ) <EOL> assert form . csrf_token . errors <EOL> assert '<STR_LIT>' in session <EOL> return form <EOL> def _test_phase2 ( self , form_class , session , token , must_validate = True ) : <EOL> form = form_class ( <EOL> formdata = DummyPostData ( csrf_token = token ) , <EOL> meta = { '<STR_LIT>' : session } <EOL> ) <EOL> if must_validate : <EOL> assert form . validate ( ) <EOL> return form </s>
