Example #1
0
def add_reserveds(package_name, reserved_list, name_prefix=''):
    """Add reserved names to Reserved table.

    Parameters
    ----------
    name_prefix : str
    package_name : str
    reserved_list : list
    """
    # Add package name as a module reserved name
    try:
        reserved_row = get_reserved(None)
        save_reserved(reserved_row,
                      name=package_name,
                      primary_package=package_name)
    except IntegrityError as e:
        # Continue if name already in db, o/w raise error
        if 'unique' not in e.message.lower():
            raise
    # Add package reserved names
    for reserved_name in reserved_list:
        if reserved_name == '__init__.py':
            continue
        reserved_row = get_reserved(None)
        try:
            save_reserved(reserved_row,
                          name=''.join([name_prefix, reserved_name]),
                          primary_package=package_name)
        except IntegrityError as e:
            # Continue if name already in db, o/w raise error
            if 'unique' not in e.message.lower():
                raise
Example #2
0
def test_search_reserveds():
    from logic.reserved import get_reserved, save_reserved, \
        search_reserveds

    reserved_row = get_reserved(1)
    assert reserved_row.name == u'reserved_one'

    # Find One
    reserved_list = search_reserveds(u'reserved')
    count = 0
    for d in reserved_list:
        count += 1
        assert 'one' in d['name']
    assert count == 1

    # Add a reserved, id = 2
    reserved_row = get_reserved(None)
    assert 2 == save_reserved(reserved_row, name=u'reserved_two')

    # Find One and Two
    reserved_list = search_reserveds(u'res%erved &_')
    count = 0
    for d in reserved_list:
        count += 1
        assert 'reserved' in d['name']
    assert count == 2

    # Find only Two
    reserved_list = search_reserveds(u'two')
    count = 0
    for d in reserved_list:
        count += 1
        assert 'two' in d['name']
    assert count == 1

    # Find none
    reserved_list = search_reserveds(u'id xxx')
    count = 0
    for d in reserved_list:
        count += 1
    assert count == 0

    # Find all with blank search
    reserved_list = search_reserveds(u'')
    count = 0
    for d in reserved_list:
        count += 1
        assert 'reserved' in d['name']
    assert count == 2

    # Find all with None search
    reserved_list = search_reserveds(None)
    count = 0
    for d in reserved_list:
        count += 1
        assert 'reserved' in d['name']
    assert count == 2
Example #3
0
def test_get_reserved():

    # Get good reserved_id
    reserved_row = get_reserved(1)
    assert reserved_row.id == 1
    assert reserved_row.name == u'reserved_one'

    # Fail at getting bad reserved_id
    with pytest.raises(DoesNotExist):
        get_reserved(999)
def test_create_decision():
    from logic.reserved import Reserved
    from logic.reserved import get_reserved

    time_before = datetime.datetime.now()
    reserved_row = get_reserved(None)
    time_after = datetime.datetime.now()

    assert not reserved_row.id
    assert reserved_row.name == Reserved.name.default
    assert time_before <= reserved_row.created_timestamp <= time_after

    # Fail at bad parameter
    with pytest.raises(DoesNotExist):
        get_reserved(999)
Example #5
0
def test_save_reserved_add():
    from logic.reserved import save_reserved, get_reserved

    reserved_row = get_reserved(None)
    assert save_reserved(
        reserved_row,
        name=u'Reserved_Three') == 2

    assert get_reserved(2).id == 2, 'Record not present or wrong id'

    # Change to Test Change Reserved
    save_reserved(
        reserved_row,
        name=u'Test_Change_Reserved')

    assert u'Test_Change_Reserved' == get_reserved(2).name
Example #6
0
def session(database, request):
    from logic.identifier import Identifier, get_identifier, save_identifier
    from logic.reserved import Reserved, get_reserved, save_reserved
    from data.base import obfuscatedb

    obfuscatedb.create_tables([Identifier,
                               Reserved], safe=True)

    # Add a decision, decision_id = 1
    identifier_row = get_identifier(None)
    assert 1 == save_identifier(
        identifier_row,
        name=u'identifier_one',
        obfuscated_name=u'aaa')

    reserved_row = get_reserved(None)
    assert 1 == save_reserved(
        reserved_row,
        name=u'reserved_one',
        primary_package=u'module')

    assert get_identifier(1).name == 'identifier_one'
    assert get_identifier(1).obfuscated_name == 'aaa'
    assert get_identifier(2).name == 'reserved_one'
    assert get_identifier(2).obfuscated_name == 'reserved_one'

    def teardown():
        obfuscatedb.drop_tables([Identifier,
                                 Reserved], safe=True)

    request.addfinalizer(teardown)
    return session
Example #7
0
def test_save_reserved_bad_column_name():
    from logic.reserved import save_reserved, get_reserved

    reserved_row = get_reserved(None)
    with pytest.raises(AttributeError):
        assert save_reserved(
            reserved_row,
            name=u'Reserved One BAD FIELD NAME FOR NOTES',
            notesxxx='Does not exist')
Example #8
0
def test_save_reserved_duplicate():
    from logic.reserved import save_reserved, get_reserved

    # Fail at duplicate add
    reserved_row = get_reserved(None)
    assert not reserved_row.id
    with pytest.raises(IntegrityError):
        assert save_reserved(
            reserved_row,
            name=u'reserved_one')
Example #9
0
def test_logic_reserved_delete():
    from logic.reserved import delete_reserved, get_reserved, \
        save_reserved

    reserved_row = get_reserved(None)
    assert save_reserved(reserved_row, name=u'Reserved Two')

    assert get_reserved(2).id == 2, 'Record not present or wrong id'

    # Delete an existing reserved
    row = get_reserved(2)
    assert delete_reserved(row) == 1

    with pytest.raises(DoesNotExist):
        assert get_reserved(2).id == 2, 'Record not found'

    # Fail to delete a non-existing reserved
    with pytest.raises(DoesNotExist):
        assert delete_reserved(row)
Example #10
0
def test_logic_reserved_delete():
    from logic.reserved import delete_reserved, get_reserved, \
        save_reserved

    reserved_row = get_reserved(None)
    assert save_reserved(
        reserved_row,
        name=u'Reserved Two')

    assert get_reserved(2).id == 2, 'Record not present or wrong id'

    # Delete an existing reserved
    row = get_reserved(2)
    assert delete_reserved(row) == 1

    with pytest.raises(DoesNotExist):
        assert get_reserved(2).id == 2, 'Record not found'

    # Fail to delete a non-existing reserved
    with pytest.raises(DoesNotExist):
        assert delete_reserved(row)
Example #11
0
def test_save_reserved_reassign():
    from logic.reserved import save_reserved, get_reserved
    from logic.identifier import get_identifier_by_name, save_identifier, \
        get_identifier

    identifier_row = get_identifier(None)
    save_identifier(
        identifier_row,
        name='some_ident',
        obfuscated_name='Reserved'
        )
    assert get_identifier_by_name('some_ident').obfuscated_name == 'Reserved'

    reserved_row = get_reserved(None)
    save_reserved(
        reserved_row,
        name=u'Reserved')

    assert get_identifier_by_name('some_ident').obfuscated_name != 'Reserved'
Example #12
0
def test_get_dir(tmpdir):
    # Setup reserved folder
    reserved_row = get_reserved(None)
    assert 2 == save_reserved(
        reserved_row,
        name=u'/rsv_dir',
        primary_package='rsv_pkg')

    reserved_row = get_reserved(None)
    assert 3 == save_reserved(
        reserved_row,
        name=u'~rsv_file.py',
        primary_package='rsv_file_pkg')

    reserved_row = get_reserved(None)
    assert 4 == save_reserved(
        reserved_row,
        name=u'#no_obf',
        primary_package='no_obf_pkg')

    reserved_row = get_reserved(None)
    assert 5 == save_reserved(
        reserved_row,
        name=u'=no_obf_file.py')

    # Setup folders and files
    f = io.open(join(str(tmpdir), 'test.py'), 'w')
    f.close()

    f = io.open(join(str(tmpdir), 'rsv_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv0')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv0a.py'), 'w')
    f.close()
    f = io.open(join(included_dir, 'not_rsv0b.py'), 'w')
    f.close()
    included_dir = join(str(tmpdir), 'not_rsv0', 'not_rsv0.1')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv0.1.py'), 'w')
    f.close()

    excluded_dir = join(str(tmpdir), 'rsv_dir')
    os.mkdir(excluded_dir)
    excluded_dir = join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir')
    os.mkdir(excluded_dir)
    f = io.open(join(excluded_dir, 'rsv_subdir_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv_file')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf')
    os.mkdir(included_dir)
    included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf', 'no_obf_1')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'no_obf_1.py'), 'w')
    f.close()

    f = io.open(join(str(tmpdir), 'no_obf_file.py'), 'w')
    f.close()

    # Test reading directory structure
    get_files_gen = file_gen(str(tmpdir))
    num_lines = 0
    for file_name, dir_name, is_reserved_dir, is_reserved_file, \
            do_obfuscate in get_files_gen:
        if file_name == 'test.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0a.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0b.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0.1.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0', 'not_rsv0.1')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv_file.py':
            assert dir_name == join(str(tmpdir), 'not_rsv_file')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'rsv_subdir_file.py':
            assert dir_name == join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir')
            assert is_reserved_dir
            assert is_reserved_file
            assert not do_obfuscate
            assert get_reserved_by_name(
                ''.join(['/', 'rsv_dir_subdir'])).primary_package == 'rsv_pkg'
        if file_name == 'rsv_file.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert is_reserved_file
            assert not do_obfuscate
        if file_name == 'no_obf_1.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0', 'no_obf', 
                                    'no_obf_1')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert not do_obfuscate
            assert get_reserved_by_name(
                ''.join(['#', 'no_obf_1'])).primary_package == 'no_obf'
        if file_name == 'no_obf_file.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert not is_reserved_file
            assert not do_obfuscate
        num_lines += 1
    assert num_lines == 9
    assert get_reserved_by_name('~rsv_subdir_file.py').primary_package == \
        'rsv_dir_subdir'
    assert get_identifier_by_name('no_obf_1').name == 'no_obf_1'
Example #13
0
def test_get_dir(tmpdir):
    # Setup reserved folder
    reserved_row = get_reserved(None)
    assert 2 == save_reserved(reserved_row,
                              name=u'/rsv_dir',
                              primary_package='rsv_pkg')

    reserved_row = get_reserved(None)
    assert 3 == save_reserved(reserved_row,
                              name=u'~rsv_file.py',
                              primary_package='rsv_file_pkg')

    reserved_row = get_reserved(None)
    assert 4 == save_reserved(reserved_row,
                              name=u'#no_obf',
                              primary_package='no_obf_pkg')

    reserved_row = get_reserved(None)
    assert 5 == save_reserved(reserved_row, name=u'=no_obf_file.py')

    # Setup folders and files
    f = io.open(join(str(tmpdir), 'test.py'), 'w')
    f.close()

    f = io.open(join(str(tmpdir), 'rsv_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv0')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv0a.py'), 'w')
    f.close()
    f = io.open(join(included_dir, 'not_rsv0b.py'), 'w')
    f.close()
    included_dir = join(str(tmpdir), 'not_rsv0', 'not_rsv0.1')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv0.1.py'), 'w')
    f.close()

    excluded_dir = join(str(tmpdir), 'rsv_dir')
    os.mkdir(excluded_dir)
    excluded_dir = join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir')
    os.mkdir(excluded_dir)
    f = io.open(join(excluded_dir, 'rsv_subdir_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv_file')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'not_rsv_file.py'), 'w')
    f.close()

    included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf')
    os.mkdir(included_dir)
    included_dir = join(str(tmpdir), 'not_rsv0', 'no_obf', 'no_obf_1')
    os.mkdir(included_dir)
    f = io.open(join(included_dir, 'no_obf_1.py'), 'w')
    f.close()

    f = io.open(join(str(tmpdir), 'no_obf_file.py'), 'w')
    f.close()

    # Test reading directory structure
    get_files_gen = file_gen(str(tmpdir))
    num_lines = 0
    for file_name, dir_name, is_reserved_dir, is_reserved_file, \
            do_obfuscate in get_files_gen:
        if file_name == 'test.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0a.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0b.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv0.1.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0', 'not_rsv0.1')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'not_rsv_file.py':
            assert dir_name == join(str(tmpdir), 'not_rsv_file')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert do_obfuscate
        if file_name == 'rsv_subdir_file.py':
            assert dir_name == join(str(tmpdir), 'rsv_dir', 'rsv_dir_subdir')
            assert is_reserved_dir
            assert is_reserved_file
            assert not do_obfuscate
            assert get_reserved_by_name(''.join(
                ['/', 'rsv_dir_subdir'])).primary_package == 'rsv_pkg'
        if file_name == 'rsv_file.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert is_reserved_file
            assert not do_obfuscate
        if file_name == 'no_obf_1.py':
            assert dir_name == join(str(tmpdir), 'not_rsv0', 'no_obf',
                                    'no_obf_1')
            assert not is_reserved_dir
            assert not is_reserved_file
            assert not do_obfuscate
            assert get_reserved_by_name(''.join(
                ['#', 'no_obf_1'])).primary_package == 'no_obf'
        if file_name == 'no_obf_file.py':
            assert dir_name == str(tmpdir)
            assert not is_reserved_dir
            assert not is_reserved_file
            assert not do_obfuscate
        num_lines += 1
    assert num_lines == 9
    assert get_reserved_by_name('~rsv_subdir_file.py').primary_package == \
        'rsv_dir_subdir'
    assert get_identifier_by_name('no_obf_1').name == 'no_obf_1'