1 new commit in galaxy-central: https://bitbucket.org/galaxy/galaxy-central/commits/35e90d31c3c7/ Changeset: 35e90d31c3c7 User: jmchilton Date: 2014-03-17 01:55:47 Summary: PEP-8 fixes, var name fixes, and unit tests for SecurityHelper. Tests cover nearly all functionality - in particular newly added per-kind key generation. Affected #: 2 files diff -r 2727f16171610e0e27420d780e9a5ed56f1908f5 -r 35e90d31c3c78e4710a0f73a0126ddbd9ca8397a lib/galaxy/web/security/__init__.py --- a/lib/galaxy/web/security/__init__.py +++ b/lib/galaxy/web/security/__init__.py @@ -1,5 +1,7 @@ import collections -import os, os.path, logging +import os +import os.path +import logging import pkg_resources pkg_resources.require( "pycrypto" ) @@ -13,6 +15,7 @@ if os.path.exists( "/dev/urandom" ): # We have urandom, use it as the source of random data random_fd = os.open( "/dev/urandom", os.O_RDONLY ) + def get_random_bytes( nbytes ): value = os.read( random_fd, nbytes ) # Normally we should get as much as we need @@ -39,7 +42,7 @@ self.id_cipher = Blowfish.new( self.id_secret ) per_kind_id_secret_base = config.get( 'per_kind_id_secret_base', self.id_secret ) - self.id_ciphers_for_key = _cipher_cache( per_kind_id_secret_base ) + self.id_ciphers_for_kind = _cipher_cache( per_kind_id_secret_base ) def encode_id( self, obj_id, kind=None ): id_cipher = self.__id_cipher( kind ) @@ -84,7 +87,7 @@ if not kind: id_cipher = self.id_cipher else: - id_cipher = self.id_ciphers_for_key[ kind ] + id_cipher = self.id_ciphers_for_kind[ kind ] return id_cipher diff -r 2727f16171610e0e27420d780e9a5ed56f1908f5 -r 35e90d31c3c78e4710a0f73a0126ddbd9ca8397a test/unit/test_security_helper.py --- /dev/null +++ b/test/unit/test_security_helper.py @@ -0,0 +1,53 @@ +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 ) Repository URL: https://bitbucket.org/galaxy/galaxy-central/ -- This is a commit notification from bitbucket.org. You are receiving this because you have the service enabled, addressing the recipient of this email.