Ejemplo n.º 1
0
def test_get_oauth_token(http_mock, _, __, aggregator, instance, oauth_token):
    check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance])
    oauth_res = mock.MagicMock()
    oauth_res.json.return_value = oauth_token
    http_mock.get.return_value = oauth_res

    # Token gets fetched properly
    check.get_oauth_token()
    assert check._oauth_token == "token"
    assert check._token_expiration == FREEZE_TIME + 1000
    aggregator.assert_service_check(
        name="cloud_foundry_api.uaa.can_authenticate",
        status=CloudFoundryApiCheck.OK,
        tags=[
            "uaa_url:uaa.sys.domain.com", "foo:bar",
            "api_url:api.sys.domain.com"
        ],
        count=1,
    )

    # Token doesn't get refreshed if not needed
    check._oauth_token = "no_refresh"
    check._token_expiration = FREEZE_TIME + 1234
    check.get_oauth_token()
    assert check._oauth_token == "no_refresh"
    assert check._token_expiration == FREEZE_TIME + 1234

    # Token gets refreshed if soon to be expired
    check._oauth_token = "no_refresh"
    check._token_expiration = FREEZE_TIME + 299
    check.get_oauth_token()
    assert check._oauth_token == "token"
    assert check._token_expiration == FREEZE_TIME + 1000
Ejemplo n.º 2
0
def test_get_oauth_token_errors(_, aggregator, instance):
    check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance])
    check._http = None  # initialize the _http attribute for mocking

    with mock.patch.object(
            check, "_http") as http_mock, pytest.raises(RequestException):
        http_mock.get.side_effect = RequestException()
        check.get_oauth_token()
        aggregator.assert_service_check(
            name="cloud_foundry_api.uaa.can_authenticate",
            status=CloudFoundryApiCheck.CRITICAL,
            tags=[
                "uaa_url:uaa.sys.domain.com", "foo:bar",
                "api_url:api.sys.domain.com"
            ],
            count=1,
        )
        aggregator.reset()

    with mock.patch.object(check,
                           "_http") as http_mock, pytest.raises(HTTPError):
        http_mock.get.return_value = mock.MagicMock(
            raise_for_status=mock.MagicMock(side_effect=HTTPError()))
        check.get_oauth_token()
        aggregator.assert_service_check(
            name="cloud_foundry_api.uaa.can_authenticate",
            status=CloudFoundryApiCheck.CRITICAL,
            tags=[
                "uaa_url:uaa.sys.domain.com", "foo:bar",
                "api_url:api.sys.domain.com"
            ],
            count=1,
        )
        aggregator.reset()

    with mock.patch.object(check,
                           "_http") as http_mock, pytest.raises(ValueError):
        http_mock.get.return_value = mock.MagicMock(json=mock.MagicMock(
            side_effect=ValueError()))
        check.get_oauth_token()
        aggregator.assert_service_check(
            name="cloud_foundry_api.uaa.can_authenticate",
            status=CloudFoundryApiCheck.CRITICAL,
            tags=[
                "uaa_url:uaa.sys.domain.com", "foo:bar",
                "api_url:api.sys.domain.com"
            ],
            count=1,
        )
        aggregator.reset()