<s> import re <EOL> import time <EOL> from django import template <EOL> from django . contrib . auth . models import User <EOL> from django . core . urlresolvers import reverse <EOL> from django . test import TestCase <EOL> from urlcrypt . lib import generate_login_token , decode_login_token , encode_token , base64url_encode <EOL> from urlcrypt . conf import URLCRYPT_LOGIN_URL , URLCRYPT_USE_RSA_ENCRYPTION <EOL> class UrlCryptTests ( TestCase ) : <EOL> def setUp ( self ) : <EOL> super ( UrlCryptTests , self ) . setUp ( ) <EOL> self . test_user = User . objects . create_user ( '<STR_LIT:test>' , '<STR_LIT>' , '<STR_LIT:test>' ) <EOL> def test_login_token ( self ) : <EOL> token = generate_login_token ( self . test_user , u'<STR_LIT>' ) <EOL> data = decode_login_token ( token ) <EOL> self . assertEquals ( data [ '<STR_LIT>' ] , self . test_user . id ) <EOL> self . assertEquals ( data [ '<STR_LIT:url>' ] , u'<STR_LIT>' ) <EOL> def test_blank_unicode_password ( self ) : <EOL> self . test_user . password = u"<STR_LIT>" <EOL> self . test_user . save ( ) <EOL> self . assertEqual ( type ( self . test_user . password ) , type ( u"<STR_LIT>" ) ) <EOL> self . test_login_token ( ) <EOL> def test_rsa ( self ) : <EOL> if URLCRYPT_USE_RSA_ENCRYPTION : <EOL> from urlcrypt import rsa <EOL> assert rsa . decrypt ( rsa . encrypt ( "<STR_LIT:test>" ) ) == "<STR_LIT:test>" <EOL> assert rsa . decrypt ( rsa . encrypt ( "<STR_LIT:test>" * <NUM_LIT:100> ) ) == "<STR_LIT:test>" * <NUM_LIT:100> <EOL> def test_login_token_failed_hax0r ( self ) : <EOL> fake_token = '<STR_LIT>' <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , args = ( fake_token , ) ) ) <EOL> self . assertRedirects ( response , URLCRYPT_LOGIN_URL ) <EOL> fake_token = base64url_encode ( encode_token ( [ str ( self . test_user . id ) , reverse ( '<STR_LIT>' ) , str ( int ( time . time ( ) ) ) ] ) ) <EOL> response = self . client . get ( reverse ( '<STR_LIT>' , args = ( fake_token , ) ) ) <EOL> self . assertRedirects ( response , URLCRYPT_LOGIN_URL ) <EOL> def assert_login_url ( self , encoded_url , expected_url ) : <EOL> response = self . client . get ( expected_url ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT> ) <EOL> response = self . client . get ( encoded_url ) <EOL> self . assertRedirects ( response , expected_url ) <EOL> response = self . client . get ( expected_url ) <EOL> self . assertEquals ( response . status_code , <NUM_LIT:200> ) <EOL> def test_url_encoded_template_tag ( self ) : <EOL> text = """<STR_LIT>""" <EOL> t = template . Template ( text ) <EOL> c = template . Context ( { '<STR_LIT>' : self . test_user } ) <EOL> encoded_url = t . render ( c ) . strip ( ) <EOL> self . assert_login_url ( encoded_url , reverse ( '<STR_LIT>' ) ) <EOL> def test_url_encoded_template_tag_with_args ( self ) : <EOL> text = """<STR_LIT>""" <EOL> t = template . Template ( text ) <EOL> c = template . Context ( { '<STR_LIT>' : self . test_user } ) <EOL> encoded_url = t . render ( c ) . strip ( ) <EOL> self . assert_login_url ( encoded_url , reverse ( '<STR_LIT>' , args = ( self . test_user . username , ) ) ) <EOL> def test_url_encoded_template_tag_with_as_var ( self ) : <EOL> text = """<STR_LIT>""" <EOL> t = template . Template ( text ) <EOL> c = template . Context ( { '<STR_LIT>' : self . test_user } ) <EOL> match = re . match ( "<STR_LIT>" , t . render ( c ) . strip ( ) ) <EOL> self . assertTrue ( bool ( match ) ) <EOL> self . assert_login_url ( match . group ( <NUM_LIT:1> ) , reverse ( '<STR_LIT>' , args = ( self . test_user . username , ) ) ) <EOL> def test_encode_url_string_template_tag ( self ) : <EOL> text = """<STR_LIT>""" <EOL> some_url = '<STR_LIT>' % reverse ( '<STR_LIT>' , args = ( self . test_user . username , ) ) <EOL> t = template . Template ( text ) <EOL> c = template . Context ( { '<STR_LIT>' : self . test_user , '<STR_LIT>' : some_url } ) <EOL> encoded_url = t . render ( c ) . strip ( ) <EOL> self . assert_login_url ( encoded_url , reverse ( '<STR_LIT>' , args = ( self . test_user . username , ) ) ) </s>
