Пример #1
0
def test_get_refresh_token():
    iam_url = "https://iam.cloud.ibm.com/identity/token"
    access_token_str = get_access_token()
    response = """{
        "access_token": "%s",
        "token_type": "Bearer",
        "expires_in": 3600,
        "expiration": 1524167011,
        "refresh_token": "jy4gl91BQ"
    }""" % (access_token_str)
    responses.add(responses.POST, url=iam_url, body=response, status=200)

    token_manager = IAMTokenManager("iam_apikey")
    token_manager.get_token()

    assert len(responses.calls) == 2
    assert token_manager.refresh_token == "jy4gl91BQ"
def test_get_token():
    iam_url = "https://iam.bluemix.net/identity/token"
    token_manager = IAMTokenManager("iam_apikey", iam_url=iam_url)
    token_manager.user_access_token = 'user_access_token'

    # Case 1:
    token = token_manager.get_token()
    assert token == token_manager.user_access_token

    # Case 2:
    token_manager.user_access_token = ''
    response = """{
        "access_token": "hellohello",
        "token_type": "Bearer",
        "expires_in": 3600,
        "expiration": 1524167011,
        "refresh_token": "jy4gl91BQ"
    }"""
    responses.add(responses.POST, url=iam_url, body=response, status=200)
    token = token_manager.get_token()
    assert token == "hellohello"

    # Case 3:
    token_manager.token_info['expiration'] = int(
        time.time()) - (20 * 24 * 3600)
    token = token_manager.get_token()
    assert "grant_type=urn" in responses.calls[1].request.body
    token_manager.token_info['expiration'] = int(time.time()) - 4000
    token = token_manager.get_token()
    assert "grant_type=refresh_token" in responses.calls[2].request.body

    # Case 4
    token_manager.token_info = {
        "access_token": "dummy",
        "token_type": "Bearer",
        "expires_in": 3600,
        "expiration": int(time.time()) + 3600,
        "refresh_token": "jy4gl91BQ"
    }
    token = token_manager.get_token()
    assert token == 'dummy'