Exemple #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
def test_get_auth_headers(_, __, ___, ____, instance):
    check = CloudFoundryApiCheck('cloud_foundry_api', {}, [instance])
    check._oauth_token = "oauth_token"
    assert check.get_auth_header() == {"Authorization": "Bearer oauth_token"}