Files
galaxy/test/unit/test_security_helper.py
T
John Chilton 76d3c7aece PEP-8 fixes, var name fixes, and unit tests for SecurityHelper.
Tests cover nearly all functionality - in particular newly added per-kind key generation.
2014-03-16 19:55:47 -05:00

54 lines
1.8 KiB
Python

from galaxy.web import security
test_helper_1 = security.SecurityHelper( id_secret="sec1" )
test_helper_2 = security.SecurityHelper( id_secret="sec2" )
def test_encode_decode():
# Different ids are encoded differently
assert test_helper_1.encode_id( 1 ) != test_helper_1.encode_id( 2 )
# But decoding and encoded id brings back to original id
assert 1 == test_helper_1.decode_id( test_helper_1.encode_id( 1 ) )
def test_per_kind_encode_deocde():
# Different ids are encoded differently
assert test_helper_1.encode_id( 1, kind="k1" ) != test_helper_1.encode_id( 2, kind="k1" )
# But decoding and encoded id brings back to original id
assert 1 == test_helper_1.decode_id( test_helper_1.encode_id( 1, kind="k1" ), kind="k1" )
def test_different_secrets_encode_differently():
assert test_helper_1.encode_id( 1 ) != test_helper_2.encode_id( 1 )
def test_per_kind_encodes_id_differently():
assert test_helper_1.encode_id( 1 ) != test_helper_2.encode_id( 1, kind="new_kind" )
def test_encode_dict():
test_dict = dict(
id=1,
other=2,
history_id=3,
)
encoded_dict = test_helper_1.encode_dict_ids( test_dict )
assert encoded_dict[ "id" ] == test_helper_1.encode_id( 1 )
assert encoded_dict[ "other" ] == 2
assert encoded_dict[ "history_id" ] == test_helper_1.encode_id( 3 )
def test_guid_generation():
guids = set()
for i in range( 100 ):
guids.add( test_helper_1.get_new_guid() )
assert len( guids ) == 100 # Not duplicate guids generated.
def test_encode_decode_guid():
session_key = test_helper_1.get_new_guid()
encoded_key = test_helper_1.encode_guid( session_key )
decoded_key = test_helper_1.decode_guid( encoded_key ).encode( "utf-8" )
assert session_key == decoded_key, "%s != %s" % ( session_key, decoded_key )