예제 #1
0
def run_endpoint(monkeypatch: _pytest.monkeypatch.MonkeyPatch,
                 azure_emulator_coords: azure_utils.StorageCoordinates,
                 sample_secret_key: str,
                 endpoint_host: str) -> typing.Generator:

    monkeypatch.setenv('ETL_SECRET_KEY', sample_secret_key)
    monkeypatch.setenv('EXTRACT_ENDPOINT_STORAGE_ACCOUNT',
                       azure_emulator_coords.account)
    monkeypatch.setenv('EXTRACT_ENDPOINT_STORAGE_KEY',
                       azure_emulator_coords.key)
    monkeypatch.setenv('EXTRACT_ENDPOINT_CONTAINER',
                       azure_emulator_coords.container)
    monkeypatch.setenv('EXTRACT_ENDPOINT_STORAGE_DOMAIN',
                       azure_emulator_coords.domain)
    proc = psutil.Popen(['python', 'src/extract_endpoint/endpoint.py'],
                        stderr=subprocess.PIPE,
                        stdout=subprocess.PIPE)
    while True:
        try:
            requests.get(f'http://{endpoint_host}/test_alive')
            break
        except requests.exceptions.ConnectionError:
            pass

    yield None

    proc.kill()
예제 #2
0
def test_client(
    monkeypatch: _pytest.monkeypatch.MonkeyPatch,
    azure_emulator_coords: azure_utils.StorageCoordinates
) -> testing.FlaskClient:

    monkeypatch.setitem(endpoint.App.config, 'AZURE_COORDINATES',
                        azure_emulator_coords)
    return endpoint.App.test_client()
예제 #3
0
파일: test_core.py 프로젝트: atugushev/wok
def ctx(monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    def _get_current_context() -> object:
        class FakeContext:
            obj: core.Context = core.Context()

        return FakeContext()

    monkeypatch.setattr('click.get_current_context', _get_current_context)
예제 #4
0
def fake_db_coords(monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> typing.Generator:
    def connection_string(self: database.DatabaseCoordinates) -> str:
        prefix = f'{self.username}:{self.password}@' if self.username and self.password else ''
        return f'{prefix}{self.host}:{self.port}'

    connection_string_propery = property(connection_string)

    monkeypatch.setattr(database.DatabaseCoordinates, 'connection_string', connection_string_propery)
    yield
예제 #5
0
def test_update(monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    monkeypatch.setattr(update, 'update', noop)

    runner = CliRunner()
    result = runner.invoke(cli.main,
                           args=[
                               'scan', '--scanner', 'pshtt', '--scanner',
                               'sslyze', '--domains', 'not.a.real.file',
                               '--output', '.'
                           ])
    assert result.exit_code == 0
예제 #6
0
def test_bad_data(local_reader: transform.LocalExtractReader,
                  monkeypatch: _pytest.monkeypatch.MonkeyPatch,
                  capsys: _pytest.capture.CaptureFixture) -> None:
    def raise_error(*args) -> None:  #pylint: disable=unused-argument
        raise InvalidEmbeddedDataTypeError(ceiling.Ceiling)

    monkeypatch.setattr(ceiling.Ceiling, 'from_data', raise_error)

    output = list(transform.transform(local_reader))
    assert not output

    _, err = capsys.readouterr()
    assert all('Ceiling' in line for line in err.split()[1:-1])
예제 #7
0
def mocked_tl_app(monkeypatch: _pytest.monkeypatch.MonkeyPatch,
                  sample_secret_key: str):
    def mock_send_to_tl(data: typing.Dict[str, str]) -> int:
        if 'salt' not in data:
            return HTTPStatus.BAD_REQUEST
        if 'signature' not in data:
            return HTTPStatus.BAD_REQUEST
        hasher = hashlib.new('sha3_256')
        hasher.update((data['salt'] + sample_secret_key).encode())
        actual_signature = hasher.hexdigest()
        if data['signature'] != actual_signature:
            return HTTPStatus.BAD_REQUEST
        return HTTPStatus.OK

    monkeypatch.setattr(endpoint, 'send_to_tl', mock_send_to_tl)
예제 #8
0
def test_process(
        date_result: typing.Tuple[str, int],
        monkeypatch: _pytest.monkeypatch.MonkeyPatch,
    ) -> None:

    date, exit_code = date_result

    monkeypatch.setattr(processing, 'run', noop)

    runner = CliRunner()
    result = runner.invoke(cli.main, args=[
        'process',
        '--date', date,
    ])
    assert result.exit_code == exit_code
예제 #9
0
def betamax_mock_sessions(
        request: _pytest.fixtures.SubRequest,
        monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    """Fixture to patch requests.Session to record with Betamax."""
    # Patch the Session constructor on requests to return a Betamax session
    OldSession = requests.Session

    def create_recorded_session() -> requests.Session:
        session = OldSession()
        recorder = betamax.Betamax(session)
        recorder.use_cassette(_create_cassette_name(request))
        recorder.start()
        request.addfinalizer(recorder.stop)
        return session

    monkeypatch.setattr(requests, 'Session', create_recorded_session)
예제 #10
0
def test_run_all_args(
    date_result: typing.Tuple[str, int],
    monkeypatch: _pytest.monkeypatch.MonkeyPatch,
) -> None:
    monkeypatch.setattr(update, 'update', noop)
    monkeypatch.setattr(processing, 'run', noop)

    date, exit_code = date_result

    runner = CliRunner()
    result = runner.invoke(cli.main,
                           args=[
                               'run', '--date', date, '--scanner', 'pshtt',
                               '--scanner', 'sslyze', '--domains',
                               'not.a.real.file', '--output', '.'
                           ])
    assert result.exit_code == exit_code
예제 #11
0
def test_args_origin_id(monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    """test argument origin-id"""

    origin_id = "00000000-0000-0000-0000-000000000001"

    monkeypatch.setattr(sys, "argv",
                        ["./test-worker.py", "--origin-id", origin_id])

    args = cli.handle_args(worker.parseargs("Test worker"))
    actapi = worker.init_act(args)

    assert actapi.config.origin_id == origin_id

    fact = (actapi.fact("mentions").source("report", "xyz").destination(
        "fqdn", "test.com"))

    assert fact.origin.id == origin_id
예제 #12
0
def test_022_add_converts_to_path(
    cli_runner: click.testing.CliRunner,
    root_repo: pygit2.Repository,
    repo_1_path: pathlib.Path,
    repo_1_url: str,
    monkeypatch: _pytest.monkeypatch.MonkeyPatch,
) -> None:
    @context.with_context
    def _add(ctx: context.Context, path: pathlib.Path, url: str) -> None:
        assert isinstance(path, pathlib.Path)

    monkeypatch.setattr('wok.core.add', _add)

    result = cli_runner.invoke(cli.main, ['init'])
    assert result.exit_code == 0, result.output

    result = cli_runner.invoke(cli.main, ['add', repo_1_url, str(repo_1_path)])
    assert result.exit_code == 0, result.output
예제 #13
0
def test_azure_coordinates_from_env(
        monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    monkeypatch.setenv('EXTRACT_ENDPOINT_STORAGE_ACCOUNT', 'foo')
    monkeypatch.setenv('EXTRACT_ENDPOINT_STORAGE_KEY', 'bar')
    monkeypatch.setenv('EXTRACT_ENDPOINT_STORAGE_DOMAIN', 'baz')
    monkeypatch.setenv('EXTRACT_ENDPOINT_CONTAINER', 'qux')
    coords = transform.AzureCoordinates.from_env()
    assert coords == transform.AzureCoordinates(account='foo',
                                                key='bar',
                                                domain='baz',
                                                container='qux')
예제 #14
0
def test_args_origin_name(
        monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    """ test argument origin-name """

    origin_name = "test-origin"

    monkeypatch.setattr(sys, "argv",
                        ["./test-worker.py", "--origin-name", origin_name])

    args = worker.handle_args(worker.parseargs("Test worker"))
    actapi = worker.init_act(args)

    assert actapi.config.origin_name == origin_name

    fact = actapi.fact("mentions") \
        .source("report", "xyz")\
        .destination("fqdn", "test.com")

    assert fact.origin.name == origin_name
예제 #15
0
def test_042_join_converts_to_path(
    cli_runner: click.testing.CliRunner,
    cooked_repo: pygit2.Repository,
    repo_1_path: pathlib.Path,
    monkeypatch: _pytest.monkeypatch.MonkeyPatch,
) -> None:
    @context.with_context
    def _join(ctx: context.Context,
              repo_paths: typing.Iterable[pathlib.Path]) -> None:
        assert all(
            isinstance(repo_path, pathlib.Path) for repo_path in repo_paths)

    monkeypatch.setattr('wok.core.join', _join)

    result = cli_runner.invoke(cli.main, ['start', 'branch-1'])
    assert result.exit_code == 0, result.output

    result = cli_runner.invoke(cli.main, ['join', str(repo_1_path)])
    assert result.exit_code == 0, result.output
예제 #16
0
def mock_clone(monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    """Fixture to mock the git cloning."""
    class DummyRepo:
        def close(self) -> None:
            return

    def mock(url: str, to_path: Path, progress: Optional[RemoteProgress],
             *args: Any, **kwargs: Any) -> DummyRepo:
        if url.endswith('ROpdebee/no-repo-here.git'):
            raise git.exc.InvalidGitRepositoryError('Test repo does not exist')
        (to_path / '.git').mkdir()
        if progress is not None:
            progress.update(RemoteProgress.COUNTING | RemoteProgress.BEGIN, 0,
                            None)
            progress.update(RemoteProgress.COUNTING, 5, 10)
            progress.update(RemoteProgress.COUNTING | RemoteProgress.END, 10,
                            10)
            progress.update(12345, 10, 10)
        return DummyRepo()

    monkeypatch.setattr(Repo, 'clone_from', mock)
예제 #17
0
def test_debt_view_post(
        monkeypatch: _pytest.monkeypatch.MonkeyPatch,
        client: Client,  # noqa: WPS442
) -> None:
    repo = FakeDebtRepository()
    monkeypatch.setattr(
        DebtView,
        'service',
        DebtCreationService(repo),
        raising=True,
    )
    form_data = {
        'first_name': 'Vitas',
        'last_name': 'Sabenin',
        'debt_amount': 312312,
    }
    response = client.post('/', form_data, follow=True)
    filtered = repo.filter()
    assert response.status_code == HTTPStatus.OK
    assert len(filtered) == 1
    debt = filtered[0]
    assert debt.first_name == form_data['first_name']
    assert debt.last_name == form_data['last_name']
    assert debt.debt_amount == form_data['debt_amount']
예제 #18
0
def test_endpoint_address(
        monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    assert post_to_endpoint._endpoint_address(
    ) == post_to_endpoint.DEFAULT_ENERGUIDE_ENDPOINT_ADDRESS
    monkeypatch.setenv('ENERGUIDE_ENDPOINT_ADDRESS', 'test_address')
    assert post_to_endpoint._endpoint_address() == 'test_address'
예제 #19
0
def test_etl_secret_key(monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    assert post_to_endpoint._etl_secret_key(
    ) == post_to_endpoint.DEFAULT_ETL_SECRET_KEY
    monkeypatch.setenv('ETL_SECRET_KEY', 'test_key')
    assert post_to_endpoint._etl_secret_key() == 'test_key'
예제 #20
0
def sample_secret_key(monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> str:
    monkeypatch.setitem(endpoint.App.config, 'SECRET_KEY', 'sample secret key')
    return endpoint.App.config['SECRET_KEY']
예제 #21
0
def test_run_tl_url(monkeypatch: _pytest.monkeypatch.MonkeyPatch) -> None:
    monkeypatch.setenv('TL_ADDRESS', 'https://www.nrcan.ca:4000')
    assert endpoint._run_tl_url() == 'https://www.nrcan.ca:4000/run_tl'
예제 #22
0
def test_client(monkeypatch: _pytest.monkeypatch.MonkeyPatch, database_name: str) -> testing.FlaskClient:
    monkeypatch.setattr(flask_app, 'DATABASE_NAME', database_name)
    return flask_app.App.test_client()