def test_get_asset_success(patch_fero_get, asset_data): """Test that an asset is returned by get_asset""" patch_fero_get.return_value = asset_data client = Fero(fero_token="fakeToken", hostname="http://test.com") asset = client.get_asset("some-uuid") assert isinstance(asset, Asset) assert asset.name == asset_data["name"] patch_fero_get.assert_called_with("/api/assets/some-uuid/")
def test_get_analysis_success(patch_fero_get, analysis_data): """Test that an analysis is returned by get_analysis""" patch_fero_get.return_value = analysis_data client = Fero(fero_token="fakeToken", hostname="http://test.com") analysis = client.get_analysis("some-uuid") assert isinstance(analysis, Analysis) assert analysis.name == analysis_data["name"] patch_fero_get.assert_called_with("/api/analyses/some-uuid/")
def test_fero_get_raises_error_404(patch_requests_get, mock_response): """Test that a FeroError with the expected message is raised if the url isn't found""" mock_response.status_code = 404 patch_requests_get.return_value = mock_response client = Fero(fero_token="fakeToken", hostname="http://test.com") with pytest.raises(FeroError) as err: client.get("/some/url/") assert err.message == "The requested resource was not found"
def test_get_datasource_success(patch_fero_get, datasource_data): """Test that an data source is returned by get_datasource""" patch_fero_get.return_value = datasource_data client = Fero(fero_token="fakeToken", hostname="http://test.com") ds = client.get_datasource("9f79206e-94fc-4834-8f52-84008b12df86") assert isinstance(ds, DataSource) assert ds.name == datasource_data["name"] patch_fero_get.assert_called_with( "/api/v2/data_source/9f79206e-94fc-4834-8f52-84008b12df86/")
def test_fero_get_raises_error_400(patch_requests_get, mock_response): """Test that a FeroError with the expected message is raised if a 400 is returned""" mock_response.status_code = 400 patch_requests_get.return_value = mock_response client = Fero(fero_token="fakeToken", hostname="http://test.com") with pytest.raises(FeroError) as err: client.get("/some/url/") assert (err.message == "There was an issue connecting to Fero. - Status Code: 400")
def test_search_assets(patch_fero_get, asset_data): """Test that an iterator of assets is returned by search_assets""" patch_fero_get.return_value = {"next": None, "results": [asset_data]} client = Fero(fero_token="fakeToken", hostname="http://test.com") assets = [a for a in client.search_assets(asset_data["name"])] assert len(assets) == 1 assert isinstance(assets[0], Asset) assert assets[0].name == asset_data["name"] patch_fero_get.assert_called_with("/api/assets/", params={"name": asset_data["name"]})
def test_fero_client_post_wrapper(patch_requests_post): """Test that post requests have the correct headers added""" client = Fero(fero_token="fakeToken", hostname="http://test.com") client.post("/some/url/", {"n": "42"}) patch_requests_post.assert_called_with( "http://test.com/some/url/", json={"n": "42"}, headers={"Authorization": "JWT fakeToken"}, verify=True, )
def test_fero_client_env_token(monkeypatch): """Test that a token is found in the env""" monkeypatch.setenv("FERO_TOKEN", "fakeToken") client = Fero() assert isinstance(client, Fero) assert client._fero_token == "fakeToken"
def test_fero_client_token_file(mock_conf_path): """Test that a token is found in the .fero file""" with open(str(mock_conf_path / ".fero"), "w") as fero_file: fero_file.write("FERO_TOKEN=fakeToken\n") client = Fero() assert isinstance(client, Fero) assert client._fero_token == "fakeToken"
def test_fero_client_username_pass_provided(patch_requests_post): """Test that a client is created correctly with a provided username and password""" client = Fero(username="******", password="******") assert isinstance(client, Fero) patch_requests_post.assert_called_with( "https://app.ferolabs.com/api/token/auth/", json={ "username": "******", "password": "******" }, verify=True, ) assert client._fero_token == "fakeToken"
def test_search_assets_paginated(patch_fero_get, asset_data): """Test that an iterator of assets is returned by search_assets""" patch_fero_get.side_effect = [ { "next": "http://test.com/api/assets/?token=1234", "results": [asset_data], }, { "next": None, "results": [asset_data] }, ] client = Fero(fero_token="fakeToken", hostname="http://test.com") assets = [a for a in client.search_assets(asset_data["name"])] assert len(assets) == 2 assert isinstance(assets[0], Asset) assert assets[0].name == asset_data["name"] assert isinstance(assets[1], Asset) assert assets[1].name == asset_data["name"] patch_fero_get.assert_has_calls([ mock.call("/api/assets/", params={"name": asset_data["name"]}), mock.call("/api/assets/?token=1234", params=None), ])
def test_fero_client_user_pass_file(patch_requests_post, mock_conf_path): """Test that username and password are found in the .fero file""" with open(str(mock_conf_path / ".fero"), "w") as fero_file: fero_file.write("FERO_USERNAME=fero\nFERO_PASSWORD=pass\n") client = Fero() assert isinstance(client, Fero) assert client._fero_token == "fakeToken" patch_requests_post.assert_called_with( "https://app.ferolabs.com/api/token/auth/", json={ "username": "******", "password": "******" }, verify=True, )
def test_fero_client_with_jwt(): """Test that a client is created with a jwt provided""" client = Fero(fero_token="fakeToken") assert isinstance(client, Fero) assert client._fero_token == "fakeToken"
def test_fero_client_no_creds(): """Test that an exception is raised if there is no way to get a token""" with pytest.raises(FeroError): Fero()