Exemplo n.º 1
0
def test_anonymize_database_unsupported_table_strategy(query_factory, execution):
    with pytest.raises(UnsupportedTableStrategyError) as e_info:
        provider = PostgreSqlProvider("1.2.3.4", "root", "password", "db_name")
        database_strategy = DatabaseStrategy([
                Mock(table_name="table1", strategy_type="DEFINITELY_NOT_A_SUPPORTED_STRATEGY_TYPE"),
        ])
        provider.anonymize_database(database_strategy)
Exemplo n.º 2
0
def test_anonymize_database(query_factory, execution, simple_strategy, simple_strategy_update_fake_column, simple_strategy_update, simple_strategy_trunc, simple_strategy_delete):
    manager = Mock()
    manager.attach_mock(execution, "execution")

    provider = PostgreSqlProvider("1.2.3.4", "root", "password", "db_name", seed_rows=200)

    provider.anonymize_database(simple_strategy)

    seed_table_qualifier_map = ["_pynonymizer_seed_fake_data", {"user_name": simple_strategy_update_fake_column}]

    # get_create_seed_table( seed table name, qualifier map)
    query_factory.get_create_seed_table.assert_called_once_with(*seed_table_qualifier_map)

    # seed table should be called with the qual map 150 times
    query_factory.get_insert_seed_row.assert_has_calls( ([call(*seed_table_qualifier_map)] * 200) )

    query_factory.get_truncate_table.assert_called_once_with(simple_strategy_trunc)
    query_factory.get_delete_table.assert_called_once_with(simple_strategy_delete)
    query_factory.get_update_table.assert_called_once_with("_pynonymizer_seed_fake_data", simple_strategy_update)
    query_factory.get_drop_seed_table.assert_called_once_with("_pynonymizer_seed_fake_data")

    # anonymize basic order checks
    ix_create_seed = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_create_seed_table()))
    ix_insert_seed_first = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_insert_seed_row()))
    ix_insert_seed_last =  list_rindex(manager.mock_calls, call.execution.PSqlCmdRunner().db_execute(query_factory.get_insert_seed_row()))
    ix_trunc_table = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_truncate_table()))
    ix_update_table = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_update_table()))
    ix_drop_seed = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_drop_seed_table()))

    assert ix_create_seed < ix_insert_seed_first
    assert ix_insert_seed_last < ix_trunc_table
    assert ix_insert_seed_last < ix_update_table
    assert ix_update_table < ix_drop_seed
Exemplo n.º 3
0
def test_after_script_run(query_factory,execution):
    manager = Mock()
    manager.attach_mock(execution, "execution")

    provider = PostgreSqlProvider("1.2.3.4", "root", "password", "db_name")
    database_strategy = DatabaseStrategy(table_strategies=[
            TruncateTableStrategy("table1")
        ],
        after_scripts=["SELECT `after` FROM `after_table`;"]
    )
    provider.anonymize_database(database_strategy)

    truncate_call_index = manager.mock_calls.index(call.execution.PSqlCmdRunner().db_execute(query_factory.get_truncate_table.return_value))
    after_script_call_index = manager.mock_calls.index( call.execution.PSqlCmdRunner().db_execute("SELECT `after` FROM `after_table`;") )

    # after script should be called after anonymization
    assert truncate_call_index < after_script_call_index
Exemplo n.º 4
0
def test_before_script_run(query_factory, execution):
    manager = Mock()
    manager.attach_mock(execution, "execution")

    provider = PostgreSqlProvider("1.2.3.4", "root", "password", "db_name")
    database_strategy = DatabaseStrategy(
        table_strategies=[TruncateTableStrategy("table1")],
        before_scripts=["SELECT `before` FROM `before_table`;"],
    )

    provider.anonymize_database(database_strategy)

    truncate_call_index = manager.mock_calls.index(
        call.execution.PSqlCmdRunner().db_execute(
            query_factory.get_truncate_table.return_value))
    before_script_call_index = manager.mock_calls.index(
        call.execution.PSqlCmdRunner().db_execute(
            "SELECT `before` FROM `before_table`;"))

    # before script should be before any anonymization happens (truncate)
    assert before_script_call_index < truncate_call_index